Jump to content

Module:Authority control/documentation: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
+ul tag
do not add categories for each identifier unless specified in config, code simplification
Line 1: Line 1:
require('strict')
require('strict')
local p = {}
local p = {}
local frame = mw.getCurrentFrame()
local configfile = 'Module:Authority control/config' -- default configuation module
local arg = mw.getCurrentFrame().args.config
local config_file = frame.args.config and frame.args.config~='' and ('/' .. frame.args.config) or ''
local config = mw.loadData('Module:Authority control/config' .. config_file)
if arg and arg~='' then
configfile = 'Module:Authority control/config/' .. arg
end
local config
if mw.title.new(configfile).exists then
config = mw.loadData(configfile)
else
return error('Invalid configuration file',0)
end
local title = mw.title.getCurrentTitle()
local title = mw.title.getCurrentTitle()
local namespace = title.namespace
local namespace = title.namespace
Line 38: Line 30:
return ''
return ''
end
end
end

local getstatement = function(id, propid, qualid)
local statement = mw.wikibase.getBestStatements('P' .. id, 'P' .. propid)
local value
if statement and statement[1] and statement[1].mainsnak.datavalue.value.id then
value = statement[1].mainsnak.datavalue.value.id
local qual
if qualid and statement[1].qualifiers['P' .. qualid][1].datavalue.value then
qual = statement[1].qualifiers['P' .. qualid][1].datavalue.value
end
return value, qual
end
end
local getname = function(property)
return require('Module:Authority control').getname(property)
end
end


-- Creates a human-readable standalone wikitable version of conf, and tracking categories with page counts, for use in the documentation
-- Creates a human-readable standalone wikitable version of conf, and tracking categories with page counts, for use in the documentation
function p.docConfTable(frame)
function p.docConfTable()
local function checkcat(category,label)
local function checkcat(category,label)
local ret='[[:Category:'..category..'|'..label..']]'
local ret='[[:Category:'..category..'|'..label..']]'
Line 54: Line 62:
local columns = 3 + (count and 1 or 0)
local columns = 3 + (count and 1 or 0)
local row = function(conf)
local row = function(conf)
local category = conf.category or conf[1]
local articleCat = string.format(config.i18n.cat,category)
local articleCount = mw.site.stats.pagesInCategory(articleCat,config.i18n.pages)
local errorCat = conf.errorcat or string.format(
config.i18n.cat,
config.i18n.faulty .. ' ' .. category
)
local errorCount = mw.site.stats.pagesInCategory(errorCat,config.i18n.pages)
P = P + 1 --property count
P = P + 1 --property count
a = a + articleCount
f = f + errorCount
local getstatement = function(id, propid, qualid)
local statement = mw.wikibase.getBestStatements('P' .. id, 'P' .. propid)
local value
if statement and statement[1] and statement[1].mainsnak.datavalue.value.id then
value = statement[1].mainsnak.datavalue.value.id
local qual
if qualid and statement[1].qualifiers['P' .. qualid][1].datavalue.value then
qual = statement[1].qualifiers['P' .. qualid][1].datavalue.value
end
return value, qual
end
end
local example_qid = getstatement(conf.property, 1855)
local example_qid = getstatement(conf.property, 1855)
local example = example_qid and frame:expandTemplate{
local example = example_qid and frame:expandTemplate{
Line 85: Line 71:
)}
)}
} or ''
} or ''
local getname = function(nameprop)
local name = getstatement(conf.property, nameprop)
if name then
return frame:expandTemplate{title='Wikidata fallback link', args={name}}
end
end
local name = getname(1629) or getname(10726) or getname(9073) or getname(2378) or ''
local out = ''
local out = ''
if conf.remark then
if conf.remark then
Line 98: Line 77:
out = out .. '<tr><td>'
out = out .. '<tr><td>'
end
end
out = out .. name .. '</td>' ..
out = out .. getname(conf.property) .. '</td>' ..
'<td style="text-align: center">' .. config.sections[conf.section].name .. '</td>' ..
'<td style="text-align: center">' .. config.sections[conf.section].name .. '</td>' ..
'<td data-sort-value=' .. conf.property .. '>' .. frame:expandTemplate{title='Wikidata property link',args={id='f',conf.property}} .. '</td>' ..
'<td data-sort-value=' .. conf.property .. '>' .. frame:expandTemplate{
title = 'Wikidata property link',
args = {id='f', conf.property}
'<td>' .. example .. '</td>'
} .. '</td><td>' .. example .. '</td></tr>'
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
if conf.remark then
out = out .. "<tr class='expand-child'><td colspan=" .. columns .. ">'''Remarks:''' " .. frame:preprocess{text = conf.remark} .. "</td></tr>"
out = out .. "<tr class='expand-child'><td colspan=" .. columns
.. ">'''Remarks:''' " .. frame:preprocess{text = conf.remark}
.. "</td></tr>"
end
end
return out
return out
Line 115: Line 94:
'<th>' .. config.i18n.Section .. '</th>' ..
'<th>' .. config.i18n.Section .. '</th>' ..
'<th data-sort-type=number>' .. config.i18n.WDproperty .. '</th>' ..
'<th data-sort-type=number>' .. config.i18n.WDproperty .. '</th>' ..
'<th>' .. config.i18n.Appears_as .. '</th>'
'<th>' .. config.i18n.Appears_as .. '</th></tr>'
if count then
doc = doc .. '<th>[[:' .. config.i18n.category .. ':' .. config.i18n.maincat .. '|' .. config.i18n.Articles .. ']] '
.. '([[:' .. config.i18n.category .. ':' .. config.i18n.faultcat .. '|' .. config.i18n.Faults .. ']])</th>'
end
doc = doc .. '</tr>'
for _, conf in pairs(config.config) do
for _, conf in pairs(config.config) do
doc = doc .. row(conf)
doc = doc .. row(conf)
end
if count then
doc = doc .. '<tr><th style="text-align: right;" colspan=' .. columns-1 .. '>' .. config.i18n.Totals .. '</th>' ..
'<th style="text-align: right;">' .. P .. '</th>' ..
'<th style="text-align: right;">' .. lang:formatNum(a) .. ' ([[:' .. config.i18n.category .. ':' .. config.i18n.allfaultycat .. '|' .. f .. ']])</th></tr>'
end
end
doc = doc .. '</table>'
doc = doc .. '</table>'
Line 133: Line 102:
end
end


function p.errorTable(frame)
function p.whitelisttable()
local Table = '<table class="wikitable sortable">'..
'<tr><th>' .. config.i18n.WDproperty .. '</th>'..
'<th>' .. lang:ucfirst(config.i18n.faulty) .. ' ' .. config.i18n.identifiers .. '</th>'..
'<th>[[:' .. config.i18n.category .. ':' .. config.i18n.faultcat .. '|' .. config.i18n.Tracking_category .. ']]</th></tr>'
local f, P = 0, 0, 0 --cumulative sums
for _, conf in pairs(config.config) do
local category = conf.errorcat or string.format(
config.i18n.cat,
config.i18n.faulty .. ' ' .. (conf.category or conf[1])
)
local count = mw.site.stats.pagesInCategory(category,'pages')
if count > 0 then
P = P + 1
f = f + count
Table = Table..'<tr><td>'..frame:expandTemplate{ title = config.i18n.linktemplate, args = { id = 'f', conf.property } } .. '</td>'..
'<td style="text-align:center;">'..tostring(count)..'</td>'..
'<td>[[:' .. config.i18n.category .. ':'..category..']]</td></tr>'
end
end
Table = Table..'<tr><th>' .. config.i18n.Totals .. '</th>'..
'<th style="text-align:center;">' .. '[[:' .. config.i18n.category .. ':' .. config.i18n.allfaultycat .. '|' .. tostring(f) .. ']]</th>'..
'<th style="text-align:center;">'..tostring(P)..'</th></tr></table>'
return Table
end

function p.whitelisttable(frame)
local Table = '<table class="wikitable sortable">'..
local Table = '<table class="wikitable sortable">'..
'<tr><th>' .. config.i18n.Code .. '</th>'..
'<tr><th>' .. config.i18n.Code .. '</th>'..
Line 169: Line 112:
local plist = {}
local plist = {}
for _, property in pairs(wlist.properties) do
for _, property in pairs(wlist.properties) do
table.insert(plist,frame:expandTemplate{title='Wikidata property link', args={'P' .. property}})
table.insert(plist, frame:expandTemplate{title='Wikidata property link', args={'P' .. property}})
end
end
Table = Table .. '<td>' .. table.concat(plist,', ') .. '</td></tr>'
Table = Table .. '<td>' .. table.concat(plist,', ') .. '</td></tr>'
Line 177: Line 120:
end
end


function p.sectiontable(frame)
function p.sectiontable()
local Table = '<table class="wikitable sortable">'..
local Table = '<table class="wikitable sortable">'..
'<tr><th>' .. config.i18n.Section .. '</th>' ..
'<tr><th>' .. config.i18n.Section .. '</th>' ..
Line 187: Line 130:
for _,id in pairs(config.config) do
for _,id in pairs(config.config) do
if id.section == number then
if id.section == number then
table.insert(plist,frame:expandTemplate{title=config.i18n.linktemplate, args={'P' .. id.property}})
table.insert(plist, frame:expandTemplate{
title=config.i18n.linktemplate,
args={'P' .. id.property}
})
end
end
end
end
Line 197: Line 143:


-- Main/External Call for Pages with authority control identifiers
-- Main/External Call for Pages with authority control identifiers
function p.autoDetect(frame)
function p.autoDetect()
local function whichTOC(frame) -- standardize TOC behavior via {{CatAutoTOC}}
local function whichTOC() -- standardize TOC behavior via {{CatAutoTOC}}
return frame:expandTemplate{ title = 'CatAutoTOC', args = { align = 'center' } }
return frame:expandTemplate{ title = 'CatAutoTOC', args = { align = 'center' } }
end
end
local function wpa(frame,id,faulty)
local function wpa(id, faulty)
for _,conf in pairs(config.config) do
for _, conf in pairs(config.config) do
if conf.category == id or conf[1] == id then
if conf.category==id then
local getname = function(nameprop)
local outString = frame:expandTemplate{
title = 'Category explanation',
local name = mw.wikibase.getBestStatements('P'..conf.property,nameprop)
args = {'articles with ' .. getname(conf.property) .. ' identifiers.'..' Please do not add [[Wikipedia:Categorization#Subcategorization|subcategories]].\nPages in this category should only be added by [[Module:Authority control]].'}
if name and name[1] and name[1].mainsnak.datavalue.value.id then
return frame:expandTemplate{title='Wikidata fallback link',args={name[1].mainsnak.datavalue.value.id}}
} .. frame:expandTemplate{
end
title = 'Cat more',
args = {'Wikipedia:Authority control', ':d:Property:P'..conf.property}
end
} .. frame:expandTemplate{
local name = getname('P1629') or getname('P10726') or getname('P9073') or getname('P2378') or ''
title = 'Possibly empty category'
local outString = frame:expandTemplate{ title = 'Category explanation', args = {'articles with '..name..' identifiers.'..' Please do not add [[Wikipedia:Categorization#Subcategorization|subcategories]].\nPages in this category should only be added by [[Module:Authority control]].'}}
.. frame:expandTemplate{ title = 'Cat more', args = {'Wikipedia:Authority control', ':d:Property:P'..conf.property} }
} .. frame:expandTemplate{
.. frame:expandTemplate{ title = 'Possibly empty category' }
title = 'Wikipedia category',
.. frame:expandTemplate{ title = 'Wikipedia category', args = { hidden = 'yes', tracking = 'yes' } }
args = {hidden = 'yes', tracking = 'yes' }
.. frame:expandTemplate{ title = 'Polluted category' }
} .. frame:expandTemplate{
title = 'Polluted category'
.. whichTOC( frame )
} .. whichTOC()
if faulty then
if faulty then
outString = outString .. addCat(config.i18n.faultcat,id) .. addCat(string.format(config.i18n.cat, id))
outString = outString .. addCat(config.i18n.faultcat,id) .. addCat(string.format(config.i18n.cat, id))
Line 230: Line 177:
local wpID = mw.ustring.match(title.text, 'Articles with (.+) identifiers')
local wpID = mw.ustring.match(title.text, 'Articles with (.+) identifiers')
if wpfaultyID then
if wpfaultyID then
return wpa(frame,wpfaultyID,true)-- must be before wpID check, in case they both match
return wpa(wpfaultyID, true)-- must be before wpID check, in case they both match
elseif wpID then
elseif wpID then
return wpa(frame,wpID,false)
return wpa(wpID, false)
else
else
return needsAttention('U')
return needsAttention('U')
Line 239: Line 186:
end
end


function p.idproposal(frame)
function p.idproposal()
local property = frame.args.property or ''
local property = frame.args.property or ''
local out = '<ul><li>Wikidata property: ' .. frame:expandTemplate{title='Wikidata property link', args={'P' .. property}} .. '</li>'
local out = '<ul><li>Wikidata property: ' .. frame:expandTemplate{title='Wikidata property link', args={'P' .. property}} .. '</li>'
Line 252: Line 199:
local link = frame.args.link or ''
local link = frame.args.link or ''
if link=='' then
if link=='' then
link = mw.wikibase.getBestStatements('P'..property,'P1630') or ''
link = mw.wikibase.getBestStatements('P' .. property, 'P1630') or ''
link = link[1].mainsnak.datavalue.value
link = link[1].mainsnak.datavalue.value
end
end

Revision as of 06:17, 31 August 2024

require('strict')
local p = {}
local frame = mw.getCurrentFrame()
local config_file = frame.args.config and frame.args.config~='' and ('/' .. frame.args.config) or ''
local config = mw.loadData('Module:Authority control/config' .. config_file)
local title = mw.title.getCurrentTitle()
local namespace = title.namespace
local testcases = title.subpageText == config.i18n.testcases
local makelink = require(config.i18n.ACmodule).makelink
local lang = mw.getContentLanguage()

local function needsAttention(sortkey)
	return '[[' .. config.i18n.category .. ':' .. config.i18n.attentioncat .. '|' .. 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 = '[[' .. config.i18n.category .. ':'..cat..'|' .. sortkey .. title.text .. ']]'
		else
			cat = '[[' .. config.i18n.category .. ':'..cat..']]'
		end
		cat = cat .. redlinkcat
		return cat
	else
		return ''
	end
end

local getstatement = function(id, propid, qualid)
	local statement = mw.wikibase.getBestStatements('P' .. id, 'P' .. propid)
	local value
	if statement and statement[1] and statement[1].mainsnak.datavalue.value.id then
		value = statement[1].mainsnak.datavalue.value.id
		local qual
		if qualid and statement[1].qualifiers['P' .. qualid][1].datavalue.value then
			qual = statement[1].qualifiers['P' .. qualid][1].datavalue.value
		end
		return value, qual
	end
end
local getname = function(property)
	return require('Module:Authority control').getname(property)
end

-- Creates a human-readable standalone wikitable version of conf, and tracking categories with page counts, for use in the documentation
function p.docConfTable()
	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 row = function(conf)
		P = P + 1 --property count
		local example_qid = getstatement(conf.property, 1855)
		local example = example_qid and frame:expandTemplate{
			title = 'Hlist',
			args = {tostring(
				mw.html.create('ul')
				:node(makelink(conf, example_qid))
			)}
		} or ''
		local out = ''
		if conf.remark then
			out = out .. '<tr><td rowspan=2>'
		else
			out = out .. '<tr><td>'
		end
		out = out .. getname(conf.property) .. '</td>' ..
			'<td style="text-align: center">' .. config.sections[conf.section].name .. '</td>' ..
			'<td data-sort-value=' .. conf.property .. '>' .. frame:expandTemplate{
				title = 'Wikidata property link',
				args = {id='f', conf.property}
			} .. '</td><td>' .. example .. '</td></tr>'
		if conf.remark then
			out = out .. "<tr class='expand-child'><td colspan=" .. columns
				.. ">'''Remarks:''' " .. frame:preprocess{text = conf.remark}
				.. "</td></tr>"
		end
		return out
	end
	local doc = '<table class="wikitable sortable">' ..
	  '<tr><th>' .. lang:ucfirst(config.i18n.identifier) .. '</th>' ..
	  '<th>' .. config.i18n.Section .. '</th>' ..
	  '<th data-sort-type=number>' .. config.i18n.WDproperty .. '</th>' ..
	  '<th>' .. config.i18n.Appears_as .. '</th></tr>'
	for _, conf in pairs(config.config) do
		doc = doc .. row(conf)
	end
	doc = doc .. '</table>'
	return doc
end

function p.whitelisttable()
	local Table = '<table class="wikitable sortable">'..
	  '<tr><th>' .. config.i18n.Code .. '</th>'..
	  '<th>' .. config.i18n.Topic .. '</th>'..
	  '<th>' .. lang:ucfirst(config.i18n.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()
	local Table = '<table class="wikitable sortable">'..
	  '<tr><th>' .. config.i18n.Section .. '</th>' ..
	  '<th>' .. config.i18n.Description .. '</th>'..
	  '<th>' .. lang:ucfirst(config.i18n.identifiers) .. '</th></tr>'
	for number,section in ipairs(config.sections) do
		Table = Table .. '<tr><th>' .. number .. '</th>' ..	'<td>' .. section.name .. '</td>'
		local plist = {}
		for _,id in pairs(config.config) do
			if id.section == number then
				table.insert(plist, frame:expandTemplate{
					title=config.i18n.linktemplate,
					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()
	local function whichTOC() -- standardize TOC behavior via {{CatAutoTOC}}
		return frame:expandTemplate{ title = 'CatAutoTOC', args = { align = 'center' } }
	end
	local function wpa(id, faulty)
		for _, conf in pairs(config.config) do
			if conf.category==id then
				local outString = frame:expandTemplate{
					title = 'Category explanation',
					args = {'articles with ' .. getname(conf.property) .. ' identifiers.'..' Please do not add [[Wikipedia:Categorization#Subcategorization|subcategories]].\nPages in this category should only be added by [[Module:Authority control]].'}
				} .. 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' }
				} .. frame:expandTemplate{
					title = 'Polluted category'
				} .. whichTOC()
				if faulty then
					outString = outString .. addCat(config.i18n.faultcat,id) .. addCat(string.format(config.i18n.cat, id))
				else
					outString = outString .. addCat(config.i18n.maincat,id)
				end
				return outString
			end
		end
	end
	if namespace == 14 then --cat space
		local wpfaultyID = mw.ustring.match(title.text, 'Articles with faulty (.+) identifiers')
		local wpID = mw.ustring.match(title.text, 'Articles with (.+) identifiers')
		if wpfaultyID then
			return wpa(wpfaultyID, true)-- must be before wpID check, in case they both match
		elseif wpID then
			return wpa(wpID, false)
		else
			return needsAttention('U')
		end
	end
end

function p.idproposal()
	local property = frame.args.property or ''
	local out = '<ul><li>Wikidata property: ' .. frame:expandTemplate{title='Wikidata property link', args={'P' .. property}} .. '</li>'
	out = out .. '<li>Section: '
	local section = frame.args.section or ''
	if section~='' then
		out = out .. config.sections[section].name .. ' (' .. section .. ')'
	else
		out = out .. 'Not specified'
	end
	out = out .. '</li><li>Link: '
	local link = frame.args.link or ''
	if link=='' then
		link = mw.wikibase.getBestStatements('P' .. property, 'P1630') or ''
		link = link[1].mainsnak.datavalue.value
	end	
	if link~='' then
		out = out .. '<code>' .. link .. '</code>'
	else
		out = out .. 'Not specified'
	end
	out = out .. '</li><li>Label: '
	local label = frame.args.label or ''
	if label~='' then
		out = out .. label
	else
		out = out .. 'Not specified'
	end
	out = out .. '</li><li>Example: '
	local example = mw.wikibase.getBestStatements('P'..property,'P1855') or ''
	if example then
		example = example[1].qualifiers['P'..property][1].datavalue.value
		example = frame:expandTemplate{title = 'Hlist', args = {makelink({[1]='', link=link, label=label}, example)}}
		out = out .. example
	else
		out = out .. 'Not available'
	end
	out = out .. '</li></ul>'
	return out
end

return p