Modul:Reference score: Forskjell mellom sideversjoner

Fra Wikisida.no
Hopp til navigering Hopp til søk
(Ny side: local i18n = mw.loadData( 'Module:References/i18n' ) local langPriority = { -- primary language nb=1, -- important languages nn=2, da=3, sv=4, fo=5, is=6, -- main languages en=7,…)
 
Ingen redigeringsforklaring
Linje 1: Linje 1:
local i18n = mw.loadData( 'Module:References/i18n' )
local i18n = mw.loadData( 'Module:References/i18n' )
local conf = mw.loadData( 'Module:References/conf' )


local langPriority = {
local highScore = 5
local midScore = 15
-- primary language
local lowScore = 25
nb=1,
local maxRefs = 4
-- important languages
nn=2, da=3, sv=4, fo=5, is=6,
-- main languages
en=7, de=8, fr=9, it=10, es=11, pt=12, pl=13, ru=14, ja=15, zh=16
}

local domainPriority = {
-- primary domain
no=1,
-- important domains
dk=2, se=3, fo=4, is=5,
-- main domains
eu=6, de=7, fr=8, it=9, es=10, pt=11, pl=12, ru=13, ja=14, zh=15,
com=16, org=17, net=18
}
local priorityHigh = 5
local priorityLow = 25


local h = {}
local h = {}


-- Can be tested in the console with
-- Can be tested in the console with
-- `=p.renderCategories()`
-- `=p.categories()`
-- `=p.renderCategories(0)`
-- `=p.categories(0)`
-- `=p.renderCategories(1)`
-- `=p.categories(1)`
-- `=p.renderCategories(2)`
-- `=p.categories(2)`
function h.renderCategories( num )
function h.categories( num )
local wiki = ''
local t = {}
if num then
if num then
table.insert( mw.message.newRawMessage( i18n['category-pages-using-references-from-statement'] ):plain() )
wiki = wiki .. mw.ustring.format('[[Category:%s]]',
mw.message.newRawMessage( i18n['category-pages-using-references-from-statement'] ):plain() )
if num == 1 then
if num == 1 then
table.insert( mw.message.newRawMessage( i18n['category-pages-using-single-reference-from-statement'] ):plain() )
wiki = wiki .. mw.ustring.format('[[Category:%s]]',
mw.message.newRawMessage( i18n['category-pages-using-single-reference-from-statement'] ):plain() )
elseif num > 1 then
elseif num > 1 then
table.insert( mw.message.newRawMessage( i18n['category-pages-using-multiple-references-from-statement'], num ):plain() )
wiki = wiki .. mw.ustring.format('[[Category:%s]]',
mw.message.newRawMessage( i18n['category-pages-using-multiple-references-from-statement'], num ):plain() )
end
end
end
end
return wiki
return t
end
end


Linje 63: Linje 45:
-- `=p.prioritize(mw.wikibase.getAllStatements('Q20', 'P31')[2].references)`
-- `=p.prioritize(mw.wikibase.getAllStatements('Q20', 'P31')[2].references)`
-- `=p.prioritize(mw.wikibase.getAllStatements('Q66363708', 'P31')[2].references)`
-- `=p.prioritize(mw.wikibase.getAllStatements('Q66363708', 'P31')[2].references)`
function h.prioritize( list )
function h.score( list )
local t = {}
local t = {}
for i=1,lowScore do
t[i] = false
end
for _,v in ipairs( list ) do
for _,v in ipairs( list ) do
-- do not include any "imported from"
-- do not include any "imported from"
if not v.snacks.P143 or not v.snacks.P3452 then
if v.snaks and not( v.snaks.P143 or v.snaks.P3452 ) then
--mw.logObject(v.snaks)
local keep = priorityLow
--do return end
local keep = lowScore
-- try language of "title"
-- try language of "title"
if v.snacks.P1476 then
if v.snaks.P1476 then
for _,w in ipairs(v.snacks.P1476) do
for _,w in ipairs(v.snaks.P1476) do
if w.snaktype == 'value' and w.datatype == 'monolingualtext' then
if w.snaktype == 'value'
if w.datavalue.type == 'monolingualtext' then
and w.datatype == 'monolingualtext'
local priority = langPriority[w.datavalue.value.language]
and w.datavalue.type == 'monolingualtext' then
local score = conf.languageScore[w.datavalue.value.language]
if priority then
if score then
-- note that higher number means lower prority
keep = (keep < priority) and keep or priority
-- note that higher number means lower prority
keep = (keep < score) and keep or score
end
end
end
end
end
Linje 84: Linje 71:
end
end
-- try root domain of "reference url"
-- try root domain of "reference url"
if v.snacks.P854 then
if v.snaks.P854 then
for _,w in ipairs(v.snacks.P854) do
for _,w in ipairs(v.snaks.P854) do
if w.snaktype == 'value' and w.datatype == 'url' then
if w.snaktype == 'value'
if w.datavalue.type == 'string' then
and w.datatype == 'url'
local uri = mw.uri.new( w.datavalue.value )
and w.datavalue.type == 'string' then
local root = string.match( uri.path, '%.[^.]+$' )
local uri = mw.uri.new( w.datavalue.value )
local priority = domainPriority[root]
local root = string.match( uri.path, '%.[^.]+$' )
local score = conf.domainScore[root]
if priority then
if score then
-- note that higher number means lower prority
keep = (keep < priority) and keep or priority
-- note that higher number means lower prority
keep = (keep < score) and keep or score
end
end
end
end
end
end
end
end
end
-- try reference to entity
-- some entries that usually imply high quality
for _,u in pairs(v.snaks) do
if v.snacks.P92 or v.snacks.P248 then
for _,w in ipairs(u) do
if w.snaktype == 'value'
and w.datatype == 'wikibase-item'
and w.datavalue.type == 'wikibase-entityid' then
local score = conf.entityScore[w.datavalue.value.id]
if score then
-- note that higher number means lower prority
keep = (keep < score) and keep or score
end
end
end
end
-- some properties that usually imply somewhat quality
if v.snaks.P92 or v.snaks.P248 then
-- note that higher number means lower prority
-- note that higher number means lower prority
keep = (keep < priorityHigh) and priorityHigh or priority
keep = (keep < midScore) and keep or midScore
end
end
table.insert( t, keep, v )
table.insert( t, keep, v )
end
end
end
end
return t
local compacted = {}
end
for _,v in ipairs(t) do

table.insert( compacted, v )
-- Can be somewhat tested by
-- =mw.dumpObject(p.compact(p.score(mw.wikibase.getAllStatements('Q40826', 'P569')[1].references)))
function h.compact( list, limit )
limit = limit or maxRefs
local t = {}
local counter = 0
for _,v in ipairs( list ) do
if v then
counter = counter + 1
if limit and counter <= limit then
table.insert( t, v )
elseif not limit then
table.insert( t, v )
end
end
end
end
return compacted
return t
end
end


function h.renderReferences( frame )
function h.renderReferences( frame )
local wiki = ''
--return h. ..h.renderCategories( )
for _,v in ipairs( h.categories( num ) ) do
wiki = wiki .. mw.ustring.format('[[Category:%s]]', v )
end
return wiki
end
end



Sideversjonen fra 27. aug. 2019 kl. 23:57

Dokumentasjon for denne modulen kan opprettes på Modul:Reference score/dok

local i18n = mw.loadData( 'Module:References/i18n' )
local conf = mw.loadData( 'Module:References/conf' )

local highScore = 5
local midScore = 15
local lowScore = 25
local maxRefs = 4

local h = {}

-- Can be tested in the console with
-- `=p.categories()`
-- `=p.categories(0)`
-- `=p.categories(1)`
-- `=p.categories(2)`
function h.categories( num )
	local t = {}
	if num then
		table.insert( mw.message.newRawMessage( i18n['category-pages-using-references-from-statement'] ):plain() )
		if num == 1 then
			table.insert( mw.message.newRawMessage( i18n['category-pages-using-single-reference-from-statement'] ):plain() )
		elseif num > 1 then
			table.insert( mw.message.newRawMessage( i18n['category-pages-using-multiple-references-from-statement'], num ):plain() )
		end
	end
	return t
end

function h.standardFormat(frame, prop)
	local found = false
	local hits = 0
	local refs = ''
	for i,v in ipairs( prop.references or {} ) do
		if v.snaks and not v.snaks.P143 then
			found = found or true
			hits = hits + 1
			-- hash will merge similar entries
			refs = refs .. frame:extensionTag( 'ref', mw.wikibase.formatValues( v.snaks ), { name = v.hash } )
		end
	end
	return mw.wikibase.formatValue( prop.mainsnak ) .. refs .. cats
end

-- Can be tested in the console with
-- `=p.prioritize(mw.wikibase.getAllStatements('Q20', 'P31')[2].references)`
-- `=p.prioritize(mw.wikibase.getAllStatements('Q66363708', 'P31')[2].references)`
function h.score( list )
	local t = {}
	for i=1,lowScore do
		t[i] = false
	end
	for _,v in ipairs( list ) do
		-- do not include any "imported from"
		if v.snaks and not( v.snaks.P143 or v.snaks.P3452 ) then
			--mw.logObject(v.snaks)
			--do return end
			local keep = lowScore
			-- try language of "title"
			if v.snaks.P1476 then
				for _,w in ipairs(v.snaks.P1476) do
					if w.snaktype == 'value'
							and w.datatype == 'monolingualtext'
							and w.datavalue.type == 'monolingualtext' then
						local score = conf.languageScore[w.datavalue.value.language]
						if score then
							-- note that higher number means lower prority
							keep = (keep < score) and keep or score
						end
					end
				end
			end
			-- try root domain of "reference url"
			if v.snaks.P854 then
				for _,w in ipairs(v.snaks.P854) do
					if w.snaktype == 'value'
							and w.datatype == 'url'
							and w.datavalue.type == 'string' then
						local uri = mw.uri.new( w.datavalue.value )
						local root = string.match( uri.path, '%.[^.]+$' )
						local score = conf.domainScore[root]
						if score then
							-- note that higher number means lower prority
							keep = (keep < score) and keep or score
						end
					end
				end
			end
			-- try reference to entity
			for _,u in pairs(v.snaks) do
				for _,w in ipairs(u) do
					if w.snaktype == 'value'
							and w.datatype == 'wikibase-item'
							and w.datavalue.type == 'wikibase-entityid' then
						local score = conf.entityScore[w.datavalue.value.id]
						if score then
							-- note that higher number means lower prority
							keep = (keep < score) and keep or score
						end
					end
				end
			end
			-- some properties that usually imply somewhat quality
			if v.snaks.P92 or v.snaks.P248 then
				-- note that higher number means lower prority
				keep = (keep < midScore) and keep or midScore
			end
			table.insert( t, keep, v )
		end
	end
	return t
end

-- Can be somewhat tested by
-- =mw.dumpObject(p.compact(p.score(mw.wikibase.getAllStatements('Q40826', 'P569')[1].references)))
function h.compact( list, limit )
	limit = limit or maxRefs
	local t = {}
	local counter = 0
	for _,v in ipairs( list ) do
		if v then
			counter = counter + 1
			if limit and counter <= limit then
				table.insert( t, v )
			elseif not limit then
				table.insert( t, v )
			end
		end
	end
	return t
end

function h.renderReferences( frame )
	local wiki = ''
	for _,v in ipairs( h.categories( num ) ) do
		wiki = wiki .. mw.ustring.format('[[Category:%s]]', v )
	end
	return wiki
end

return h