Module:Authority control/documentation

Documentation for this module may be created at Module:Authority control/documentation/doc

require('strict')
local p = {}
local configfile = 'Module:Authority control/config'
local title = mw.title.getCurrentTitle()
local namespace = title.namespace
local testcases = (string.sub(title.subpageText,1,9) == 'testcases')
local makelink = require('Module:Authority control').makelink

local function needsAttention(sortkey)
	return '[[Category:Pages with authority control identifiers needing attention|' .. sortkey .. title.text .. ']]'
end

local function addCat(cat,sortkey)
	if cat and cat ~= '' and (namespace == 0 or namespace == 14 or testcases) then
		local redlinkcat = ''
		if testcases == false and mw.title.new(cat, 14).exists == false then
			redlinkcat = needsAttention('N')
		end
		if sortkey then
			cat = '[[Category:'..cat..'|' .. sortkey .. title.text .. ']]'
		else
			cat = '[[Category:'..cat..']]'
		end
		cat = cat .. redlinkcat
		return cat
	else
		return ''
	end
end

-- Creates a human-readable standalone wikitable version of conf, and tracking categories with page counts, for use in the documentation
function p.docConfTable(frame)
	if frame.args.config and frame.args.config~='' then
		configfile = configfile .. '/' .. frame.args.config
	end
	local config = mw.loadData(configfile)
	local getlink = require("Module:Wikidata table")._getLink
	local function checkcat(category,label)
		local ret='[[:Category:'..category..'|'..label..']]'
		if mw.title.new(category,14).exists == false then
			ret = ret..' <span class="plainlinks" style="font-size:85%;">&#91;['..tostring(mw.uri.fullUrl('Category:'..category,'action=edit&preload=Template:Authority_control/preload'))..' create]&#93;</span>'
		end
		return ret
	end
	local doc = ''
	local a, P, f = 0, 0, 0 --cumulative sums
	local count = frame.args.count or false
	local columns = 3 + (count and 1 or 0)
	local lang = mw.getContentLanguage()
	local row = function(conf)
		local category = conf.category or conf[1]
		local articleCat = 'Articles with '..category..' identifiers'
		local articleCount = mw.site.stats.pagesInCategory(articleCat,'pages')
		local errorCat = conf.errorcat or 'Articles with faulty '..(conf.category or conf[1])..' identifiers'
		local errorCount =  mw.site.stats.pagesInCategory(errorCat,'pages')
		P = P + 1 --property count
		a = a + articleCount
		f = f + errorCount
		local example = mw.wikibase.getBestStatements('P'..conf.property,'P1855') or ''
		if example then
			example = example[1].qualifiers['P'..conf.property][1].datavalue.value
			example = mw.getCurrentFrame():expandTemplate{title = "Hlist", args = {'\n' .. makelink(conf,{id=example},1)}} .. '\n'
		end
		local name = mw.wikibase.getBestStatements('P'..conf.property,"P1629")
		if name and name[1] and name[1].mainsnak.datavalue.value.id then
			name = getlink(name[1].mainsnak.datavalue.value.id)
		else
			name = ''
		end
		local out = ''
		if conf.remark then
			out = out .. '<tr><td rowspan=2>'
		else
			out = out .. '<tr><td>'
		end
		out = out .. name .. '</td>' ..
			'<td style="text-align: center">' .. config.sectionNames[conf.section] .. '</td>' ..
			'<td data-sort-value=' .. conf.property .. '>' .. frame:expandTemplate{title='Wikidata property link',args={id='f',conf.property}} .. '</td>' ..
			'<td>' .. example .. '</td>'
		if count then
			out = out .. '<td style="text-align: right;">'..checkcat(articleCat,lang:formatNum(articleCount))..' ('..checkcat(errorCat,errorCount)..')</td>'
		end
		out = out .. '</tr>'
		if conf.remark then
			out = out .. "<tr class='expand-child'><td colspan=" .. columns .. ">'''Remarks:''' "..conf.remark .. "</td></tr>"
		end
		return out
	end
	local doc = '<table class="wikitable sortable">' ..
	  '<tr><th>Identifier</th>' ..
	  '<th>Section</th>' ..
	  '<th data-sort-type=number>Wikidata property</th>' ..
	  '<th>Appears as</th>'
	if count then
	 	doc = doc .. '<th>[[:Category:Articles with authority control information|Articles]] ([[:Category:Articles with faulty authority control information|Faults]])</th>'
	end
	doc = doc .. '</tr>'
	for _, conf in pairs(config.config) do
		doc = doc .. row(conf)
	end
	if count then
		doc = doc .. '<tr><th style="text-align: right;" colspan=' .. columns-1 .. '>Totals</th>' ..
			'<th style="text-align: right;">' .. P .. '</th>' ..
			'<th style="text-align: right;">' .. lang:formatNum(a) .. ' ([[:Category:All articles with faulty authority control information|' .. f .. ']])</th></tr>'
	end
	doc = doc .. '</table>'
	return doc
end

function p.errorTable(frame)
	local config = mw.loadData(configfile)
	local Table = '<table class="wikitable sortable">'..
	  '<tr><th>Code</th>'..
	  '<th>Wikidata property</th>'..
	  '<th>Faulty IDs</th>'..
	  '<th>[[:Category:Articles with faulty authority control information|Tracking category]]</th></tr>'
	local f, P = 0, 0, 0 --cumulative sums
	for _, conf in pairs(config.config) do
		local category = conf.errorcat or 'Articles with faulty '..(conf.category or conf[1])..' identifiers'
		local count =  mw.site.stats.pagesInCategory(category,'pages')
		if count > 0 then
			P = P + 1
			f = f + count
			Table = Table..'<tr><th>[['..(conf.idlink or conf[1]..' (identifier)')..'|'..conf[1]..']]</th>'..
				'<td>'..frame:expandTemplate{ title = 'Wikidata property link', args = { id = 'f', conf.property } } .. '</td>'..
				'<td style="text-align:center;">'..tostring(count)..'</td>'..
				'<td>[[:Category:'..category..']]</td></tr>'
		end
	end
	Table = Table..'<tr><th colspan=2 style="text-align: right;">Totals</th>'..
		'<th style="text-align:center;">' .. '[[:Category:All articles with faulty authority control information|' .. tostring(f) .. ']]</th>'..
		'<th style="text-align:center;">'..tostring(P)..'</th></tr></table>'
	return Table
end

function p.whitelisttable(frame)
	local config = mw.loadData(configfile)
	local Table = '<table class="wikitable sortable">'..
	  '<tr><th>Code</th>'..
	  '<th>Topic</th>'..
	  '<th>Identifiers</th></tr>'
	for code, wlist in pairs(config.whitelists) do
		Table = Table .. '<tr><th>' .. code .. '</th>'..
			'<td>[[' .. mw.wikibase.getSitelink('Q' .. wlist.topic) .. ']]</td>'
		local plist = {}
		for _, property in pairs(wlist.properties) do
			table.insert(plist,frame:expandTemplate{title='Wikidata property link', args={'P' .. property}})
		end
		Table = Table .. '<td>' .. table.concat(plist,', ') .. '</td></tr>'
	end
	Table = Table .. '</table>'
	return Table
end

function p.sectiontable(frame)
	local config = mw.loadData(configfile)
	local Table = '<table class="wikitable sortable">'..
	  '<tr><th>Section</th>'..
	  '<th>Description</th>'..
	  '<th>Identifiers</th></tr>'
	for number,section in ipairs(config.sectionNames) do
		Table = Table .. '<tr><th>' .. number .. '</th>' ..	'<td>' .. section .. '</td>'
		local plist = {}
		for _,id in pairs(config.config) do
			if id.section == number then
				table.insert(plist,frame:expandTemplate{title='Wikidata property link', args={'P' .. id.property}})
			end
		end
		Table = Table .. '<td>' .. table.concat(plist,', ') .. '</td></tr>'
	end
	Table = Table .. '</table>'
	return Table
end

-- Main/External Call for Pages with authority control identifiers
function p.autoDetect(frame)
	local config = mw.loadData(configfile)
	local function whichTOC(frame) -- standardize TOC behavior via {{CatAutoTOC}}
		return frame:expandTemplate{ title = 'CatAutoTOC', args = { align = 'center' } }
	end
	local ac_conf = require('Module:Authority control/config').config
	local rmCats = require('Module:Suppress categories').main
	--For use in [[Category:Articles with faulty authority control information]], i.e. on [[Category:Articles with faulty VIAF identifiers]]
	local function wpfaulty( frame, id )
		for _, conf in pairs(ac_conf) do
			if conf.category == id or conf[1] == id then
				local outString = frame:expandTemplate{ title = 'Cat more', args = {'Wikipedia:Authority control', conf.idlink or conf[1]..' (identifier)', ':d:Property:P'..conf.property} }
					.. frame:expandTemplate{ title = 'Possibly empty category' }
					.. frame:expandTemplate{ title = 'Wikipedia category', args = { hidden = 'yes', tracking = 'yes' } }
					.. frame:expandTemplate{ title = 'Polluted category' }
					.. whichTOC( frame )
					.. '\nPages in this category should only be added by [[Module:Authority control]].'
					.. addCat('Articles with '..id..' identifiers')
					.. addCat('Articles with faulty authority control information',id)
				return outString
			end
		end
		return ''
	end
	--For use in [[Category:Articles with authority control information]], i.e. on [[Category:Articles with VIAF identifiers]]
	local function wp(frame,id )
		for _, conf in pairs( ac_conf ) do
			if conf.category == id or conf[1] == id then
				local link = '[[' .. (conf.idlink or conf[1] .. ' (identifier)') .. '|' .. conf[1] .. ']]'
				local outString = frame:expandTemplate{ title = 'Category explanation', args = {'articles with '..link..' identifiers.'..' Please do not add [[Wikipedia:Categorization#Subcategorization|subcategories]].'} }
					.. frame:expandTemplate{ title = 'Cat more', args = {'Wikipedia:Authority control', ':d:Property:P'..conf.property} }
					.. frame:expandTemplate{ title = 'Possibly empty category' }
					.. frame:expandTemplate{ title = 'Wikipedia category', args = { hidden = 'yes', tracking = 'yes' } }
					.. whichTOC( frame )
					.. '\nPages in this category should only be added by [[Module:Authority control]].'
					.. addCat('Articles with authority control information',id)
				return outString
			end
		end
		return ''
	end
	if namespace == 14 then --cat space
		local wpfaultyID = mw.ustring.match(title.text, 'Articles with faulty ([%w%.%- ]+) identifiers')
		local wpID       = mw.ustring.match(title.text, 'Articles with ([%w%.%- ]+) identifiers')
		if wpfaultyID then
			return wpfaulty(frame,wpfaultyID)-- must be before wpID check, in case they both match
		elseif wpID then
			return wp(frame, wpID)-- to keep the regex simple
		else
			return needsAttention('U')
		end
	end
	return ''
end

return p