Modul:Reference score

Fra Wikipedia, den frie encyklopedi
Hopp til navigering Hopp til søk
---Module for chosing the best reference.

--@table for localization
local i18n = mw.loadData( 'Module:Reference score/i18n' )

-- @table for configuration
local conf = mw.loadData( 'Module:Reference score/conf' )

-- @table for export
local h = {}

--- Make a list of categories.
-- Note this probably should be moved out of this module.
-- @tparam number num of references
-- @treturn table of strings
function h.makeCategories( num )
	local t = {}
	if num and num > 0 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[string.format('category-pages-using-%d-references-from-statement', num )] ):plain() )
		end
	end
	return t
end

--- Generate a language score for the claims.
-- @tparam table claims to process
-- @treturn number best score
function h.scoreLanguage( claims )
	local keep = conf.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

--- Generate a root domain score for the claims.
-- @tparam table claims to process
-- @treturn number best score
function h.scoreDomain( claims )
	local keep = conf.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.host, '%.([^.]+)$' )
			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

--- Generate an entity score for the claims.
-- @tparam table claims to process
-- @treturn number best score
function h.scoreEntity( claims )
	local keep = conf.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[v.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

--- Generate a property score for the claims.
-- @tparam table claims to process
-- @treturn number best score
function h.scoreProperty( claims )
	local keep = conf.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

--- Generate scores for the references.
-- Note that the generated list is sparse, and is using false as marker.
-- @tparam table list to process
-- @treturn table of references
function h.score( list )
	-- make sure we have a continuous target list
	local t = {}
	for i=1,conf.lowScore do
		t[i] = false
	end
	-- loop over the source list
	for _,v in ipairs( list ) do
		-- is the reference excluded?
		local exclude = false
		for _,w in ipairs( conf.exclude ) do
			if v.snaks and v.snaks[w] then
				exclude = true
				break
			end
		end
		-- only process included (ie not excluded) references
		if not exclude then
			local keep = conf.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.scoreEntity( 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 < score) and keep or score
			end
			table.insert( t, keep, v )
		end
	end
	return t
end

--- Compact the sparse list.
-- Note that the input list is sparse, and using false as marker.
-- @tparam table list of any
-- @tparam[limit=conf.maxRefs] nil|number limit for truncation of list
-- @treturn table
function h.compact( list, limit )
	limit = limit or conf.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

--- Render references.
-- @tparam table frame
-- @tparam table list of references
-- @treturn string
function h.render( frame, list )
	local scored = h.score( list or {} )
	local compacted = h.compact( scored )
	local wiki = ''
	local hits = 0
	for i,v in ipairs( compacted ) do
		hits = hits + 1
		local keys = {}
		for k,_ in pairs( v.snaks ) do
			table.insert( keys, k )
		end
		local snaks = {}
		for _,k in pairs( mw.wikibase.orderProperties( keys ) ) do
			-- lua keeps injection order
			snaks[k] = v.snaks[k]
		end
		-- hash will merge similar entries
		local content = mw.wikibase.formatValues( snaks )
		local attrs = { name = string.format( 'hash-%s', v.hash ) }
		wiki = wiki .. frame:extensionTag( 'ref', content, attrs )
	end
	for _,v in ipairs( h.makeCategories( hits ) ) do
		wiki = wiki .. mw.ustring.format('[[Category:%s]]', v )
	end
	return wiki
end

return h