मोड्युल:Authority control

require('strict')local p = {}local title = mw.title.getCurrentTitle()local namespace = title.namespacelocal testcases = (string.sub(title.subpageText,1,9) == 'testcases')local function addCat(cat)if cat and cat ~= '' thenlocal redlinkcat = ''if testcases == false and mw.title.new(cat, 14).exists == false thenredlinkcat = '[[Category:Pages with red-linked authority control categories]]'endreturn '[[Category:'..cat..']]'..redlinkcatelsereturn ''endendlocal function getCatForId(id)local cat = ''if namespace == 0 thencat = 'Articles with '..id..' identifiers'endreturn addCat(cat)endlocal function getIdsFromWikidata(qid,property)local ids = {}if not mw.wikibase or not qid thenreturn idsendlocal statements = mw.wikibase.getBestStatements(qid,property)if statements thenfor _, statement in ipairs( statements ) doif statement.mainsnak.datavalue thentable.insert( ids, statement.mainsnak.datavalue.value )endendendreturn idsendlocal function makelink(conf,val,nextid) --validate values and create a linklocal linkif conf.link2 then -- use function to validate and generate linklink = conf.link2(val)elselocal valid_valueif conf.pattern then -- use pattern to determine validity if definedvalid_value = val:match(conf.pattern)elseif conf.patterns thenfor i = 1,#conf.patterns dovalid_value = val:match(conf.patterns[i])if valid_value then break endendelseif conf.valid then -- otherwise use function to determine validityvalid_value = conf.valid(val)else -- no validation possiblevalid_value = valendif valid_value thenlocal label = conf.labelif not label or nextid>1 thenlabel = tostring(nextid)endif conf.link thenvalid_value = valid_value:gsub('%%', '%%%%')link = '[' .. mw.ustring.gsub(conf.link,'%$1',valid_value) .. ' ' .. label .. ']'elselink = valid_valueendelselink = falseendendif link thenlink = '<span class="uid">'..link..'</span>'elselocal faultyCat = 'Articles with faulty '..(conf.errorcat or conf.category or conf[1])..' identifiers'link = '<span class="error">The '..conf[1]..' id '..val..' is not valid.</span>'..addCat(faultyCat)endreturn linkend--[[==========================================================================]]--[[                                   Main                                   ]]--[[==========================================================================]]function p.authorityControl(frame)local config = require("Module:Authority control/config")local conf = config.configlocal sectionNames = config.sectionNameslocal resolveEntity = require( 'Module:ResolveEntityId' )local parentArgs = frame:getParent().args --WD IDs added here laterlocal iParentArgs = 0 --count original/manual parent args only laterlocal iMatches,suppressedIdCount = 0,0local auxCats = ''local rct = 0 -- total number of links returnedlocal numqids = 4 -- support for 4 additional qidslocal totsect = #sectionNames + numqidslocal sections = {}for _ = 1,totsect do table.insert(sections,{}) end-- check for suppressed identifierslocal suppress = {}if parentArgs.suppress thenlocal suppresslist = mw.text.split(parentArgs.suppress,"%s*,%s*") -- split parameter by commafor _,v in ipairs(suppresslist) doif v:match("^%d+$") thenv = "P"..tostring(v)elsev = string.upper(v)endsuppress[v] = true -- index table by identifier nameendendlocal function makeSections(qid,additional)local tval = {}local function parameter_is_used(property)local used = falseif property thenif tval[property] thenif tval[property][1] thenused = trueendelseif tval[property] == false then -- property has been manually suppressedused = trueendendreturn usedendfor _, params in ipairs(conf) dotval[params.property] = getIdsFromWikidata(qid, 'P' .. params.property) -- setup table for values with property number as keyif additional thenif suppress["P"..tostring(params.property)] or suppress[string.upper(params[1])] thentval[params.property] = false -- indicates the identifier is suppressedendelselocal 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 == '' thenif tval[params.property][1] and (namespace == 0 or testcases) thensuppressedIdCount = suppressedIdCount + 1if parentArgs['arts'] ~= 'arts' thenauxCats = auxCats .. '[[Category:Articles with suppressed authority control identifiers|'..params[1]..']]'endendtval[params.property] = false -- indicates the identifier is suppressedelseif val then -- add local parameter to wikidata iParentArgs = iParentArgs + 1local bnew = truefor _, w in pairs(tval[params.property]) doif val == w thenbnew = falseendendif bnew then -- add new value to tableif tval[params.property][1] thenauxCats = auxCats .. '[[Category:Pages using authority control with parameters different on Wikidata|'..params[1]..']]'endtable.insert(tval[params.property],val)elseiMatches = iMatches+1endendendlocal suppress = falseif params.suppressedbyproperty thenfor _,sc in ipairs(params.suppressedbyproperty) doif parameter_is_used(sc) thensuppress = trueendendendif not tval[params.property] == false and not suppress thenlocal tlinks = {} -- setup table for linkslocal nextIdVal = 1for _,val in ipairs(tval[params.property]) dolocal link = makelink(params,val,nextIdVal)local cat = params.category or params[1]if link then -- add category unless link==falselink = link .. getCatForId(cat)endtable.insert(tlinks,link)nextIdVal = nextIdVal + 1endif tval[params.property][1] then -- assemblelocal row = ''if params.prefix thenrow = row..'*'..params.prefixendfor i, l in ipairs(tlinks) doif i==1 and not params.prefix thenrow = row..'*'elserow = row..'\n**'endrow = row .. lendrow = 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 + 1endendendendlocal function pencil(qid)if not qid thenreturn ''endlocal args = { pid = 'identifiers' } -- #target the list of identifiersargs.qid = qidreturn require('Module:EditAtWikidata')._showMessage(args)endlocal function resolveQID(qid)if qid thenqid = 'Q'..mw.ustring.gsub(qid, '^[Qq]', '')qid = resolveEntity._id(qid) --nil if unresolvableendreturn qidendlocal qids = {}if namespace == 0 thenqids[1] = mw.wikibase.getEntityIdForCurrentPage()endif not qids[1] thenqids[1] = resolveQID(parentArgs['qid']) --use qid parameter if no wikidata item is connectedendmakeSections(qids[1],false)for c = 2,5 doqids[c] = resolveQID(parentArgs['qid' .. c])if qids[c] then makeSections(qids[c],totsect-numqids+c-1) endendif iMatches > 0 and iMatches == iParentArgs thenauxCats = auxCats .. '[[Category:Pages using authority control with parameters all matching Wikidata]]'endif parentArgs['arts'] == 'arts' and suppressedIdCount > 0 thenif namespace == 0 or testcases thenlocal s = 's'if suppressedIdCount == 1 then s = '' endauxCats = auxCats .. addCat('ACArt with '..suppressedIdCount..' suppressed element'..s)endend--configure Navboxlocal outString = ''if rct > 0 then -- there is at least one link to displaylocal Navbox = require('Module:Navbox')local sect,lastsect = 0,0local navboxArgs = {name  = 'Authority control',navboxclass = 'authority-control',bodyclass = 'hlist',state = parentArgs.state or 'autocollapse',navbar = 'off'}for c=1,totsect doif #sections[c] ~= 0 then -- section is non-emptysect = sect + 1lastsect = clocal sectnameif c <= totsect - numqids then -- regular sectionsectname = sectionNames[c]else -- section from additional qidsectname = mw.wikibase.getLabel(qids[c-totsect+numqids+1]) .. pencil(qids[c-totsect+numqids +1])endnavboxArgs['group' .. c] = sectnamenavboxArgs['list' .. c] = table.concat(sections[c])endendlocal aclink = '[[Help:प्राधिकरण नियन्त्रण|प्राधिकरण नियन्त्रण]]'if sect == 1 then -- special display when only one sectionif lastsect == 1 or lastsect == 8 then -- no special label when only general or other IDs are presentnavboxArgs['group' .. lastsect] = aclink .. pencil(qids[1])elseif lastsect <= totsect - numqids  then -- other regular sectionnavboxArgs['group' .. lastsect] = aclink .. ': ' .. sectionNames[lastsect] .. pencil(qids[1])else -- section from additional qidnavboxArgs['group' .. lastsect] = aclink .. ': ' .. navboxArgs['group' .. lastsect]endelse -- add title to navboxnavboxArgs.title = aclink .. pencil(qids[1])endoutString = Navbox._navbox(navboxArgs)end--auxCatsif rct == 0 or rct >= 25 thenif namespace == 0 or testcases thenauxCats = auxCats .. addCat('AC with '..rct..' elements')endendif parentArgs.state thenif namespace == 0 or testcases thenlocal sCatif 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'endauxCats = auxCats .. addCat(sCat)endendif testcases thenauxCats = mw.ustring.gsub(auxCats, '(%[%[)(Category)', '%1:%2') --for easier checkingend--outoutString = outString..auxCatsif namespace ~= 0 thenoutString = mw.ustring.gsub(outString, '(%[%[)(Category:Articles)', '%1:%2') --by definitionendreturn outStringend-- Creates a human-readable standalone wikitable version of conf, and tracking categories with page counts, for use in the documentationfunction p.docConfTable(frame)local wikiTable = '<table class="wikitable sortable">'..  '<tr><th rowspan=2>Code</th>'..  '<th rowspan=2>Identifier</th>'..  '<th rowspan=2 data-sort-type=number>Wikidata property</th>'..  '<th rowspan=2>Section</th>'..  '<th rowspan=2>Appears as</th>'..  '<th rowspan=2>Remarks</th>'..  '<th colspan=2>Tracking categories and page counts</th></tr>'..  '<tr><th>[[:Category:Articles with authority control information|Articles]]</th>'..  '<th>[[:Category:Articles with faulty authority control information|Faulty IDs]]</th></tr>'local lang = mw.getContentLanguage()local a, f, P = 0, 0, 0 --cumulative sumslocal config = require("Module:Authority control/config")local conf = config.configlocal sectionNames = config.sectionNameslocal getlink = require("Module:Wikidata table")._getLinklocal function checkcat(category,label)local ret='[[:Category:'..category..'|'..label..']]'if mw.title.new(category, 14).exists == false thenret = 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>'endreturn retendfor _, conf in pairs(conf) dolocal category = conf.category or conf[1]local articleCat = 'Articles with '..category..' identifiers'local wpl = frame:expandTemplate{ title = 'Wikidata property link', args = { id = 'f', conf.property } }local faultyCat =  'Articles with faulty '.. (conf.errorcat or category) ..' identifiers'local articleCount = lang:formatNum( mw.site.stats.pagesInCategory(articleCat, 'pages') )local faultyCount =  lang:formatNum( mw.site.stats.pagesInCategory(faultyCat, 'pages') )P = P + 1 --property counta = a + lang:parseFormattedNumber(articleCount)f = f + lang:parseFormattedNumber(faultyCount)local name = mw.wikibase.getBestStatements('P'..conf.property,"P9073")if name thenif name[1] thenname = name[1].mainsnak.datavalue.value.idif name thenname = getlink(name)endelsename = falseendendwikiTable = wikiTable..'<tr><td>[['..(conf.idlink or conf[1]..' (identifier)')..'|'..conf[1]..']]</td>'..'<td>'..(name or '')..'</td>'..'<td data-sort-value='..conf.property..'>'..wpl..'</td>'..'<td>'..sectionNames[conf.section]..'</td>'..'<td>'..makelink(conf,conf.example,1)..'</td>'..'<td>'..(conf.remark or '')..'</td>'..'<td style="text-align: right;">'..checkcat(articleCat,articleCount)..'</td>'..'<td style="text-align: right;">'..checkcat(faultyCat,faultyCount)..'</td></tr>'endwikiTable = wikiTable..'<tr><th style="text-align: right;" colspan=5>Totals</th>'..'<th style="text-align: right;">'..lang:formatNum(P)..'</th>'..'<th style="text-align: right;">'..lang:formatNum(a)..'</th>'..'<th style="text-align: right;">'..lang:formatNum(f)..'</th></tr></table>'return require('Module:Suppress categories').main(wikiTable)endreturn p
🔥 Top keywords: मुख्य पृष्ठलक्ष्मीप्रसाद देवकोटाविशेष:Searchपृथ्वीनारायण शाहनेपालको सर्वोच्च अदालतभानुभक्त आचार्यसन्दुक रुइतनेपालको भूगोलSpecial:Searchगौतम बुद्धनेपालका प्रदेशहरूनेपालनेपालको इतिहासलुम्बिनी प्रदेशनिर्जला एकादशीपारिजात (साहित्यकार)नेपाली शब्दकोशमोतीराम भट्टविश्वेश्वरप्रसाद कोइरालाबागमती प्रदेशनेपालको संविधान २०७२नेपाल स्काउटनेपाली राष्ट्रिय क्रिकेट टोलीउखान-टुक्कापञ्चैबाजानेपालमा भएका सडकहरूको सूचीपशुपतिनाथ मन्दिरचौबिसी राज्यहरूकोशी प्रदेशदोस्रो विश्व युद्धनेपालका स्थानीय तहहरूजलवायु परिवर्तनपृथ्वीसयौँ थुङ्गा फूलका हामीबकरी ईदसगरमाथाझमक घिमिरेविशेष:RecentChangesईद