Modul:Reference score
Utseende
Moduldokumentasjon
[opprett]
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( t, mw.message.newRawMessage( i18n['category-pages-using-references-from-statement'] ):plain() )
if num == 1 then
table.insert( t, mw.message.newRawMessage( i18n['category-pages-using-single-reference-from-statement'] ):plain() )
elseif num > 1 then
table.insert( t, 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
function h.scoreLanguage( claims )
local keep = lowScore
for _,v in ipairs( claims ) do
if v.snaktype == 'value'
and v.datatype == 'monolingualtext'
and v.datavalue.type == 'monolingualtext' then
local score = conf.languageScore[v.datavalue.value.language]
if score then
-- note that higher number means lower prority
keep = (keep < score) and keep or score
end
end
end
return keep
end
function h.scoreDomain( claims )
local keep = lowScore
for _,v in ipairs( claims ) do
if v.snaktype == 'value'
and v.datatype == 'url'
and v.datavalue.type == 'string' then
local uri = mw.uri.new( v.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
return keep
end
function h.scoreEntity( claims )
local keep = lowScore
for _,v in ipairs(claims) do
if v.snaktype == 'value'
and v.datatype == 'wikibase-item'
and v.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
return keep
end
function h.scoreProperty( claims )
local keep = lowScore
for _,v in ipairs(claims) do
-- strictly speaking this could be dropped as all should be equal
local score = conf.propertyScore[v.property]
if score then
-- note that higher number means lower prority
keep = (keep < score) and keep or score
end
end
return keep
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"
local exclude = false
for _,v in ipairs( conf.exclude ) do
if v.snaks and not v.snaks[v] then
exclude = true
break
end
end
if not exclude then
local keep = lowScore
-- try language of "title"
if v.snaks.P1476 then
local score = h.scoreLanguage(v.snaks.P1476)
if score then
-- note that higher number means lower prority
keep = (keep < score) and keep or score
end
end
-- try root domain of "reference url"
if v.snaks.P854 then
local score = h.scoreDomain(v.snaks.P854)
if score then
-- note that higher number means lower prority
keep = (keep < score) and keep or score
end
end
-- try reference to entity
for _,w in pairs(v.snaks) do
local score = h.scoreDomain( w )
if score then
-- note that higher number means lower prority
keep = (keep < score) and keep or score
end
end
-- some properties that usually imply somewhat quality
for _,w in pairs(v.snaks) do
local score = h.scoreProperty( w )
-- 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.render( frame, list )
local scored = h.score( list or {} )
local compacted = h.compact( list )
local wiki = ''
local hits = 0
for i,v in ipairs( compacted ) do
hits = hits + 1
-- hash will merge similar entries
wiki = wiki .. frame:extensionTag( 'ref', mw.wikibase.formatValues( v.snaks ), { name = v.hash } )
end
for _,v in ipairs( h.categories( hits ) ) do
wiki = wiki .. mw.ustring.format('[[Category:%s]]', v )
end
return wiki
end
return h