モジュール:Authority control/documentation

require('strict')
local p = {}
local configfile = 'Module:Authority control/config' -- default configuation module
local arg = mw.getCurrentFrame().args.config
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 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  .. 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 =  .. sortkey .. title.text .. 
		else
			cat =  .. config.i18n.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)
	local function checkcat(category,label)
		local ret=..label..
		--if mw.title.new(category,14).exists == false then
			--ret = ret..' '..tostring(mw.uri.fullUrl('Category:'..category,'action=edit&preload=Template:Authority_control/preload'))..' 作成'
		--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)
		local category = conf.category or conf1
		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
		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 statement1 and statement1.mainsnak.datavalue.value.id then
				value = statement1.mainsnak.datavalue.value.id
				local qual
				if qualid and statement1.qualifiers'P' .. qualid1.datavalue.value then
					qual = statement1.qualifiers'P' .. qualid1.datavalue.value
				end
				return value, qual
			end
		end
		local _, example = getstatement(conf.property, 1855, conf.property)
		if not example then example =  end
		example = frame:expandTemplate{title = "Hlist", args = {'\n' .. makelink(conf,{id=example},1)}} .. '\n'
		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 = 
		if conf.remark then
			out = out .. 
		else
			out = out .. 
		end
		out = out .. name ..  ..
			 .. config.sectionsconf.section.name ..  ..
			 .. frame:expandTemplate{title='Wikidata property link',args={id='f',conf.property}} ..  ..
			 .. example .. 
		if count then
			out = out .. ..checkcat(articleCat,lang:formatNum(articleCount))..' ('..checkcat(errorCat,errorCount)..')'
		end
		out = out .. 
		if conf.remark then
			out = out .. "Remarks: " .. frame:preprocess{text = conf.remark} .. ""
		end
		return out
	end
	local doc =  ..
	   .. config.i18n.identifier ..  ..
	   .. config.i18n.Section ..  ..
	   .. config.i18n.WDproperty ..  ..
	   .. config.i18n.Appears_as .. 
	if count then
	 	doc = doc ..  .. config.i18n.numofarticles .. ' '
	 		.. '(' .. config.i18n.Faults .. ')'
	end
	doc = doc .. 
	for _, conf in pairs(config.config) do
		doc = doc .. row(conf)
	end
	if count then
		doc = doc ..  .. config.i18n.Totals ..  ..
			 .. P ..  ..
			 .. lang:formatNum(a) .. ' (' .. f .. ')'
	end
	doc = doc .. 
	return doc
end
function p.errorTable(frame)
	local Table = ..
	   .. config.i18n.WDproperty .. ..
	   .. config.i18n.faulty .. config.i18n.identifiers .. ..
	   .. config.i18n.Tracking_category .. 
	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 conf1)
		)
		local count =  mw.site.stats.pagesInCategory(category,config.i18n.pages)
		if count > 0 then
			P = P + 1
			f = f + count
			Table = Table....frame:expandTemplate{ title = config.i18n.linktemplate, args = { id = 'f', conf.property } } .. ..
				..tostring(count)....
				':' .. config.i18n.category .. ':'..category..
		end
	end
	Table = Table.. .. config.i18n.Totals .. ..
		 ..  .. tostring(f) .. ..
		..tostring(P)..
	return Table
end
function p.whitelisttable(frame)
	local Table = ..
	   .. config.i18n.Code .. ..
	   .. config.i18n.Topic .. ..
	   .. config.i18n.identifiers .. 
	for code, wlist in pairs(config.whitelists) do
		Table = Table ..  .. code .. ..
			 .. mw.wikibase.getSitelink('Q' .. wlist.topic) .. 
		local plist = {}
		for _, property in pairs(wlist.properties) do
			table.insert(plist,frame:expandTemplate{title='Wikidata property link', args={'P' .. property}})
		end
		Table = Table ..  .. table.concat(plist,', ') .. 
	end
	Table = Table .. 
	return Table
end
function p.sectiontable(frame)
	local Table = ..
	   .. config.i18n.section_num ..  ..
	   .. config.i18n.Description .. ..
	   .. config.i18n.identifiers .. 
	for number,section in ipairs(config.sections) do
		Table = Table ..  .. number ..  ..	 .. section.name .. 
		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 ..  .. table.concat(plist,', ') .. 
	end
	Table = Table .. 
	return Table
end
-- Main/External Call for Pages with authority control identifiers
function p.autoDetect(frame)
	local function whichTOC(frame)
		local pageCount = mw.site.stats.pagesInCategory(title.text, config.i18n.pages)
		if pageCount >= 5000 then
			return frame:expandTemplate{ title = 'CategoryTOC6' }
			     ..frame:expandTemplate{ title = 'LargeCategoryTOC' }
		elseif pageCount > 400 then
			return frame:expandTemplate{ title = 'CategoryTOC5' }
		end
		return 
	end
	local function wpa(frame,id,faulty)
		-- 日本語版独自: カテゴリに対応する識別子が存在しない場合の動作
		local outString = frame:expandTemplate{ title = 'Wikipedia category', args = { hidden = 'yes', tracking = 'yes' } }
			.. frame:expandTemplate{ title = 'Possibly empty category' }
		local name = nil
		for _, conf in pairs(config.config) do
			if conf.category == id or conf1:lower() == id:lower() then
				local getname = function(nameprop)
					local name = mw.wikibase.getBestStatements('P'..conf.property,nameprop)
					if name and name1 and name1.mainsnak.datavalue and name1.mainsnak.datavalue.value.id then
						return frame:expandTemplate{title='Wikidata fallback link',args={name1.mainsnak.datavalue.value.id}}
					end
				end
				-- Unlike enwiki, 'name' is defined outside of the loop
				name = getname('P1629') or getname('P10726') or getname('P9073') or getname('P2378') or 
				if mw.ustring.find(name, '識別子', 1, true) or mw.ustring.find(name, 'ID', 1, true) then
				else
					name = name .. '識別子'
				end
				outString = outString
				    .. frame:expandTemplate{ title = 'Category explanation', args = {name..'がある記事を収集しています。手動でサブカテゴリを追加しないでください。'} }
					.. frame:expandTemplate{ title = 'Cat more', args = {'Wikipedia:典拠管理', ':d:Property:P'..conf.property} }
					.. '\n' .. whichTOC( frame )
				break
			end
		end
		if not name then
			outString = outString .. needsAttention('W')
		end
		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
	if namespace == 14 then --cat space
		local wpfaultyID = mw.ustring.match(title.text, "誤った(%w%.%-'・ +)識別子が指定されている記事")
		local wpID = mw.ustring.match(title.text, "(%w%.%-'・ +)識別子が指定されている記事")
		if wpfaultyID then
			return wpa(frame,wpfaultyID,true)-- must be before wpID check, in case they both match
		elseif wpID then
			return wpa(frame,wpID,false)
		else
			return needsAttention('U')
		end
	end
end
function p.idproposal(frame)
	local property = frame.args.property or 
	local out = 'Wikidata property: ' .. frame:expandTemplate{title='Wikidata property link', args={'P' .. property}} .. 
	out = out .. 'Section: '
	local section = frame.args.section or 
	if section~= then
		out = out .. config.sectionssection.name .. ' (' .. section .. ')'
	else
		out = out .. 'Not specified'
	end
	out = out .. 'Link: '
	local link = frame.args.link or 
	if link== then
		link = mw.wikibase.getBestStatements('P'..property,'P1630') or 
		link = link1.mainsnak.datavalue.value
	end	
	if link~= then
		out = out ..  .. link .. 
	else
		out = out .. 'Not specified'
	end
	out = out .. 'Label: '
	local label = frame.args.label or 
	if label~= then
		out = out .. label
	else
		out = out .. 'Not specified'
	end
	out = out .. 'Example: '
	local example = mw.wikibase.getBestStatements('P'..property,'P1855') or 
	if example then
		example = example1.qualifiers'P'..property1.datavalue.value
		example = frame:expandTemplate{title = "Hlist", args = {'\n' .. makelink({1=,link=link,label=label},{id=example},1)}} .. '\n'
		out = out .. example
	else
		out = out .. 'Not available'
	end
	out = out .. 
	return out
end
return p