Module:Authority control

require('strict') local p = {} local title = mw.title.getCurrentTitle local namespace = title.namespace local testcases = (string.sub(title.subpageText,1,9) == 'testcases')

local function addCat(cat,sortkey) if cat and cat ~= '' and (namespace == 0 or testcases) then local redlinkcat = '' if testcases == false and mw.title.new(cat, 14).exists == false then redlinkcat = '' end if sortkey then cat = '' else cat = '' end cat = cat .. redlinkcat return cat else return '' end end

local function redCatCheck( cat ) --cat == 'Blah' (not 'Category:Blah', '', etc.) if cat and cat ~= '' and mw.title.new(cat, 14).exists == false then return '' end return '' end

local function addCat2( cat, id ) if id and id ~= '' then return ''..redCatCheck(cat) else return ''..redCatCheck(cat) end end

local function getCatForId(id,faulty) local cat = 'Articles with ' if faulty then cat = cat .. 'faulty ' end cat = cat .. id .. ' identifiers' return addCat(cat) end

local function getIdsFromWikidata(qid,property) local ids = {} if not mw.wikibase or not qid then return ids end local statements = mw.wikibase.getBestStatements(qid,property) if statements then for _, statement in ipairs( statements ) do			if statement.mainsnak.datavalue then table.insert( ids, statement.mainsnak.datavalue.value ) end end end return ids end

local function makelink(conf,val,nextid,qid) --validate values and create a link local link if nextid==1 then if conf.prefix then link = '*' .. conf.prefix .. '\n**' else link = '*' end else link = '\n**' end if conf.link2 then -- use function to validate and generate link if conf.link2(val) then link = link .. conf.link2(val) else link = false end else local valid_value if conf.pattern then -- use pattern to determine validity if defined valid_value = val:match(conf.pattern) elseif conf.patterns then for i = 1,#conf.patterns do				valid_value = val:match(conf.patterns[i]) if valid_value then break end end elseif conf.valid then -- otherwise use function to determine validity valid_value = conf.valid(val) else -- no validation possible valid_value = val end if valid_value then link = link .. ' '			if not conf.label or nextid>1 then conf.label = tostring(nextid) end if conf.link then valid_value = valid_value:gsub('%%', '%%%%') link = link .. '[' .. mw.ustring.gsub(conf.link,'%$1',valid_value) .. ' ' .. conf.label .. ']'			else link = link .. valid_value end link = link .. ' '		else link = false end end if link then link = link .. getCatForId(conf.category or conf[1]) else local wdlink = qid and 'wikidata:' .. qid ..  or  link = '' if conf.errorcat then link = link .. addCat(conf.errorcat) else link = link .. getCatForId(conf.category or conf[1],true) end link = link .. addCat('All articles with faulty authority control information',conf[1]) end return link end

--========================================================================== --                                  Main --========================================================================== function p.authorityControl(frame) local config = require("Module:Authority control/config") local conf = config.config local sectionNames = config.sectionNames local resolveEntity = require( 'Module:ResolveEntityId' ) local parentArgs = frame:getParent.args --WD IDs added here later local iParentArgs = 0 --count original/manual parent args only later local iMatches,suppressedIdCount = 0,0 local auxCats = '' local rct = 0 -- total number of links returned local numqids = 4 -- support for 4 additional qids local totsect = #sectionNames + numqids local sections = {} for _ = 1,totsect do table.insert(sections,{}) end -- check for suppressed identifiers local suppress = {} if parentArgs.suppress then local suppresslist = mw.text.split(parentArgs.suppress,"%s*,%s*") -- split parameter by comma for _,v in ipairs(suppresslist) do			if v:match("^%d+$") then v = "P"..tostring(v) else v = string.upper(v) end suppress[v] = true -- index table by identifier name end end local function makeSections(qid,additional) local tval = {} local function parameter_is_used(property) local used = false if property then if tval[property] then if tval[property][1] then used = true end elseif tval[property] == false then -- property has been manually suppressed used = true end end return used end for _, params in ipairs(conf) do tval[params.property] = getIdsFromWikidata(qid, 'P' .. params.property) -- setup table for values with property number as key if additional then if suppress["P"..tostring(params.property)] or suppress[string.upper(params[1])] then tval[params.property] = false -- indicates the identifier is suppressed end else local val = parentArgs[mw.ustring.lower(params[1])] or parentArgs[params[1]] if suppress["P"..tostring(params.property)] or suppress[string.upper(params[1])] or val == '' then if tval[params.property][1] and (namespace == 0 or testcases) then suppressedIdCount = suppressedIdCount + 1 if parentArgs['arts'] ~= 'arts' then auxCats = auxCats .. ''						end end tval[params.property] = false -- indicates the identifier is suppressed elseif val then -- add local parameter to wikidata iParentArgs = iParentArgs + 1 local bnew = true for _, w in pairs(tval[params.property]) do						if val == w then bnew = false end end if bnew then -- add new value to table if tval[params.property][1] then auxCats = auxCats .. ''						end table.insert(tval[params.property],val) else iMatches = iMatches+1 end end end local suppress = false if params.suppressedbyproperty then for _,sc in ipairs(params.suppressedbyproperty) do					if parameter_is_used(sc) then suppress = true end end end if not tval[params.property] == false and not suppress then local tlinks = {} -- setup table for links local nextIdVal = 1 local row = '' for _,val in ipairs(tval[params.property]) do					local link = makelink(params,val,nextIdVal,qid) row = row .. link table.insert(tlinks,link) nextIdVal = nextIdVal + 1 end if nextIdVal>=2 then row = row..'\n' local sec = additional or params.section -- uses section 9 for qid2, section 10 for qid3, etc.					table.insert(sections[sec],row) rct = rct + 1 end end end end local function pencil(qid) if not qid then return '' end local args = { pid = 'identifiers' } -- #target the list of identifiers args.qid = qid return require('Module:EditAtWikidata')._showMessage(args) end

local function resolveQID(qid) if qid then qid = 'Q'..mw.ustring.gsub(qid, '^[Qq]', '') qid = resolveEntity._id(qid) --nil if unresolvable end return qid end local qids = {} if namespace == 0 then qids[1] = mw.wikibase.getEntityIdForCurrentPage end if not qids[1] then qids[1] = resolveQID(parentArgs['qid']) --use qid parameter if no wikidata item is connected end makeSections(qids[1],false) for c = 2,5 do qids[c] = resolveQID(parentArgs['qid' .. c]) if qids[c] then makeSections(qids[c],totsect-numqids+c-1) end end if iMatches > 0 and iMatches == iParentArgs then auxCats = auxCats .. ''	end if parentArgs['arts'] == 'arts' and suppressedIdCount > 0 then if namespace == 0 or testcases then local s = 's'			if suppressedIdCount == 1 then s = '' end auxCats = auxCats .. addCat('ACArt with '..suppressedIdCount..' suppressed element'..s)		end end

--configure Navbox local outString = '' if rct > 0 then -- there is at least one link to display local Navbox = require('Module:Navbox') local sect,lastsect = 0,0 local navboxArgs = { name = 'Authority control', navboxclass = 'authority-control', bodyclass = 'hlist', state = parentArgs.state or 'autocollapse', navbar = 'off' }		for c=1,totsect do			if #sections[c] ~= 0 then -- section is non-empty sect = sect + 1 lastsect = c				local sectname if c <= totsect - numqids then -- regular section sectname = sectionNames[c] else -- section from additional qid sectname = mw.wikibase.getLabel(qids[c-totsect+numqids+1]) .. pencil(qids[c-totsect+numqids +1]) end navboxArgs['group' .. c] = sectname navboxArgs['list' .. c] = table.concat(sections[c]) end end local aclink = 'Authority control' if sect == 1 then -- special display when only one section if lastsect == 1 or lastsect == 8 then -- no special label when only general or other IDs are present navboxArgs['group' .. lastsect] = aclink .. pencil(qids[1]) elseif lastsect <= totsect - numqids then -- other regular section navboxArgs['group' .. lastsect] = aclink .. ': ' .. sectionNames[lastsect] .. pencil(qids[1]) else -- section from additional qid navboxArgs['group' .. lastsect] = aclink .. ': ' .. navboxArgs['group' .. lastsect] end else -- add title to navbox navboxArgs.title = aclink .. pencil(qids[1]) end outString = Navbox._navbox(navboxArgs) end

--auxCats if rct == 0 or rct >= 25 then if namespace == 0 or testcases then auxCats = auxCats .. addCat('AC with '..rct..' elements') end end if parentArgs.state then if namespace == 0 or testcases then local sCat if parentArgs.state == 'collapsed' then sCat = 'AC using state parameter: collapsed' elseif parentArgs.state == 'expanded' then sCat = 'AC using state parameter: expanded' elseif parentArgs.state == 'autocollapse' then sCat = 'AC using state parameter: autocollapse' else sCat = 'AC using state parameter: other' end auxCats = auxCats .. addCat(sCat) end end if testcases then auxCats = mw.ustring.gsub(auxCats, '(%[%[)(Category)', '%1:%2') --for easier checking end

--out outString = outString..auxCats if namespace ~= 0 then outString = mw.ustring.gsub(outString,'(%[%[)(Category:Articles)([^%|%]]+)%|?[^%|%]]*(%]%])','%1:%2%3%4') outString = mw.ustring.gsub(outString,'(%[%[)(Category:All articles)([^%|%]]+)%|?[^%|%]]*(%]%])','%1:%2%3%4') end

return outString 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 wikiTable = ' ' return require('Module:Suppress categories').main(wikiTable) end

-- Local Utility Functions local function whichTOC( frame ) -- standardize TOC behavior via return frame:expandTemplate{ title = 'CatAutoTOC', args = { align = 'center' } } end

-- Main/External Call for Pages with authority control identifiers function p.autoDetect( frame ) local ac_conf = require('Module:Authority control/config').config local rmCats = require('Module:Suppress categories').main local currentTitle = mw.title.getCurrentTitle local title = currentTitle.text --For use in, i.e. on 	local function wpfaulty( frame, id ) for _, conf in pairs( ac_conf ) do			if conf.category == id or conf[1] == id then local linktarget = conf.idlink or conf[1]..' (identifier)' local wdpl      = ':d:Property:P'..conf.property local txCatMore = frame:expandTemplate{ title = 'Cat more', args = {'Wikipedia:Authority control', linktarget, wdpl} } local txEmptyCat = frame:expandTemplate{ title = 'Possibly empty category' } local txWPCat   = frame:expandTemplate{ title = 'Wikipedia category', args = { hidden = 'yes', tracking = 'yes' } } local txDirtyCat = frame:expandTemplate{ title = 'Polluted category' } local txTOC = whichTOC( frame ) local idCat = 'Articles with '..id..' identifiers' local wpfCat = 'Articles with faulty authority control information' local outString = txCatMore..txEmptyCat..txWPCat..txDirtyCat..txTOC..'\n'.. 'Pages in this category should only be added by Module:Authority control.'.. addCat2(idCat)..addCat2(wpfCat, id) return outString end end return '' end --For use in, i.e. on 	local function wp( frame, id ) for _, conf in pairs( ac_conf ) do			if conf.category == id or conf[1] == id then local linktarget = conf.idlink or conf[1]..' (identifier)' local link   = ..conf[1].. local wdpl   = ':d:Property:P'..conf.property --local example = 'The '..conf[1]..' identifier appears as '.. rmCats(conf[3](conf[5]))..' in the '..conf[4]..' section.' local txCatExplain = frame:expandTemplate{ title = 'Category explanation', args = {'articles with '..link..' identifiers.'..' Please do not add subcategories.'} } local txCatMore   = frame:expandTemplate{ title = 'Cat more', args = {'Wikipedia:Authority control', wdpl} } local txEmptyCat  = frame:expandTemplate{ title = 'Possibly empty category' } local txWPCat     = frame:expandTemplate{ title = 'Wikipedia category', args = { hidden = 'yes', tracking = 'yes' } } local txTOC = whichTOC( frame ) local wpCat = 'Articles with authority control information' local outString = txCatExplain..txCatMore..txEmptyCat..txWPCat..txTOC..'\n'..'Pages in this category should only be added by Module:Authority control.'..addCat2(wpCat, id) return outString end end return '' end if currentTitle.namespace == 14 then --cat space local wpfaultyID = mw.ustring.match(title, 'Articles with faulty ([%w%.%- ]+) identifiers') local wpID      = mw.ustring.match(title, '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 '' end end return '' end

return p