「モジュール:Authority control」の版間の差分
追加された内容 削除された内容
(モジュール:Authority_control/sandboxの2023-01-06T12:15:01Z版より複製 (スクリプトによる) : WP:AN/PEでの追加依頼) |
|||
(同じ利用者による、間の1版が非表示) | |||
1行目: | 1行目: | ||
require('strict') |
require('strict') |
||
local p = {} |
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 title = mw.title.getCurrentTitle() |
||
local namespace = title.namespace |
local namespace = title.namespace |
||
local testcases = |
local testcases = title.subpageText == config.i18n.testcases |
||
local function |
local function needsAttention(sortkey) |
||
return '[[' .. config.i18n.category .. ':' .. config.i18n.attentioncat .. '|' .. sortkey .. title.text .. ']]' |
|||
if cat and cat ~= '' then |
|||
end |
|||
local function addCat(cat,sortkey) |
|||
if cat and cat ~= '' and (namespace == 0 or namespace == 14 or testcases) then |
|||
local redlinkcat = '' |
local redlinkcat = '' |
||
if testcases == false and mw.title.new(cat, 14).exists == false then |
if testcases == false and mw.title.new(cat, 14).exists == false then |
||
redlinkcat = needsAttention('N') |
|||
redlinkcat = '[[Category:赤リンクの典拠情報カテゴリがあるページ]]' |
|||
end |
end |
||
if sortkey then |
|||
return '[[Category:'..cat..']]'..redlinkcat |
|||
cat = '[[' .. config.i18n.category .. ':'..cat..'|' .. sortkey .. title.text .. ']]' |
|||
else |
|||
cat = '[[' .. config.i18n.category .. ':'..cat..']]' |
|||
end |
|||
cat = cat .. redlinkcat |
|||
return cat |
|||
else |
else |
||
return '' |
return '' |
||
17行目: | 38行目: | ||
end |
end |
||
local function getCatForId(id) |
local function getCatForId(id,faulty) |
||
local cat = |
local cat = string.format( |
||
config.i18n.cat, |
|||
if namespace == 0 then |
|||
faulty and config.i18n.faulty..id or id |
|||
cat = id..'識別子が指定されている記事' |
|||
) |
|||
return addCat(cat) |
return addCat(cat) |
||
end |
end |
||
local function getIdsFromWikidata(qid,property) |
local function getIdsFromWikidata(qid,property) |
||
local function getquals(statement,qualid) |
|||
local ids = {} |
|||
if statement.qualifiers and statement.qualifiers['P'..qualid] then |
|||
if not mw.wikibase or not qid then |
|||
return mw.wikibase.renderSnak(statement.qualifiers['P'..qualid][1]) |
|||
return ids |
|||
else |
|||
return false |
|||
end |
|||
end |
end |
||
local ids = {} |
|||
local statements = mw.wikibase.getBestStatements(qid,property) |
|||
if |
if qid then |
||
for _, statement in ipairs( |
for _, statement in ipairs(mw.wikibase.getBestStatements(qid,property)) do |
||
if statement.mainsnak.datavalue then |
if statement.mainsnak.datavalue then |
||
local val = statement.mainsnak.datavalue.value |
|||
if val then |
|||
local namedas = getquals(statement,1810) or getquals(statement,742) or '' |
|||
table.insert(ids,{id=val,name=namedas}) |
|||
end |
|||
end |
end |
||
end |
end |
||
41行目: | 69行目: | ||
end |
end |
||
local |
local _makelink = function(conf,val,nextid,qid) --validate values and create a link |
||
local function tooltip(text,label) |
|||
if label and label~='' then |
|||
return mw.getCurrentFrame():expandTemplate{title = "Tooltip", args = {text,label}} |
|||
else |
|||
return text |
|||
end |
|||
end |
|||
local link |
local link |
||
if nextid==1 then |
|||
if conf.link2 then -- use function to validate and generate link |
|||
if conf.prefix then |
|||
link = '*' .. conf.prefix .. '\n**' |
|||
else |
|||
link = '*' |
|||
end |
|||
else |
|||
link = '\n**' |
|||
end |
|||
local valid_value = false |
|||
if conf.customlink then -- use function to validate and generate link |
|||
local label = nextid>1 and nextid |
|||
local newlink= require(config.auxiliary)[conf.customlink](val.id,label) |
|||
if newlink then |
|||
link = link .. newlink |
|||
valid_value = true |
|||
end |
|||
else |
else |
||
local valid_value |
|||
if conf.pattern then -- use pattern to determine validity if defined |
if conf.pattern then -- use pattern to determine validity if defined |
||
valid_value = |
valid_value = string.match(val.id,'^'..conf.pattern..'$') |
||
elseif conf.patterns then |
elseif conf.patterns then |
||
for |
for _,pattern in ipairs(conf.patterns) do |
||
valid_value = val:match( |
valid_value = val.id:match('^'..pattern..'$') |
||
if valid_value then break end |
if valid_value then break end |
||
end |
end |
||
elseif conf.valid then -- otherwise use function to determine validity |
elseif conf.valid then -- otherwise use function to determine validity |
||
valid_value = conf.valid(val) |
valid_value = require(config.auxiliary)[conf.valid](val.id) |
||
else -- no validation possible |
else -- no validation possible |
||
valid_value = val |
valid_value = val.id |
||
end |
end |
||
if valid_value then |
if valid_value then |
||
local newlink |
|||
local label = conf.label |
local label = conf.label |
||
if not label or nextid>1 then |
if not label or nextid>1 then |
||
66行目: | 116行目: | ||
if conf.link then |
if conf.link then |
||
valid_value = valid_value:gsub('%%', '%%%%') |
valid_value = valid_value:gsub('%%', '%%%%') |
||
newlink = '[' .. mw.ustring.gsub(conf.link,'%$1',valid_value) .. ' ' .. label .. ']' |
|||
else |
else |
||
newlink = valid_value |
|||
end |
end |
||
link = link .. '<span class="uid">'..tooltip(newlink,val.name)..'</span>' |
|||
else |
|||
link = false |
|||
end |
end |
||
end |
end |
||
if |
if valid_value then |
||
link = link .. getCatForId(conf.category or conf[1]) |
|||
link = '<span class="uid">'..link..'</span>' |
|||
else |
else |
||
--local preview = require("Module:If preview") |
|||
local faultyCat = '誤った'..(conf.errorcat or conf.category or conf[1])..'識別子が指定されている記事' |
|||
local wdlink = qid and '[[:wikidata:' .. qid .. '#P' .. conf.property .. ']]' or '' |
|||
link = '<span class="error">'..conf[1]..'識別子の値'..val..'は正しくありません。</span>'..addCat(faultyCat) |
|||
local tooltip = string.format( |
|||
config.i18n.idnotvalid, |
|||
conf[1], |
|||
val.id |
|||
) |
|||
link = link .. '[[File:' .. config.i18n.warningicon .. '|20px|frameless|link=' .. wdlink .. '|' .. tooltip .. ']]' |
|||
if conf.errorcat then |
|||
link = link .. addCat(conf.errorcat) |
|||
else |
|||
link = link .. getCatForId(conf.category or conf[1],true) |
|||
end |
|||
link = link .. addCat(config.i18n.allfaultycat,conf[1])-- .. preview._warning({'The '..conf[1]..' id '..val..' is not valid.'}) |
|||
end |
end |
||
return link |
return link |
||
87行目: | 148行目: | ||
--[[==========================================================================]] |
--[[==========================================================================]] |
||
function p.authorityControl(frame) |
function p.authorityControl(frame) |
||
local function resolveQID(qid) |
|||
local config = require("Module:Authority control/config") |
|||
if qid then |
|||
qid = 'Q'..mw.ustring.gsub(qid, '^[Qq]', '') |
|||
if mw.wikibase.isValidEntityId(qid) and mw.wikibase.entityExists(qid) then |
|||
local sitelink = mw.wikibase.getSitelink(qid) |
|||
if sitelink then |
|||
return mw.wikibase.getEntityIdForTitle(sitelink) or mw.wikibase.getEntity(qid).id |
|||
end |
|||
return mw.wikibase.getEntity(qid).id |
|||
end |
|||
end |
|||
end |
|||
local conf = config.config |
local conf = config.config |
||
local |
local parentArgs = frame:getParent().args |
||
local aliases = config.aliases |
|||
local deprecated = config.deprecated |
|||
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 auxCats = '' |
||
local rct = |
local rct = false -- boolean to track if there are any links to be returned |
||
local qid,topic |
|||
local numqids = 4 -- support for 4 additional qids |
|||
local |
local wikilink = function(qid,hideifequal) |
||
local label,sitelink = mw.wikibase.getLabel(qid),mw.wikibase.getSitelink(qid) |
|||
local sections = {} |
|||
if label then |
|||
for _ = 1,totsect do table.insert(sections,{}) end |
|||
if sitelink then |
|||
local target = mw.title.new(sitelink) |
|||
-- check for suppressed identifiers |
|||
if target==title or (target.isRedirect and target.redirectTarget==title) then -- do not link |
|||
local suppress = {} |
|||
return label |
|||
if parentArgs.suppress then |
|||
else -- make wikilink to article |
|||
local suppresslist = mw.text.split(parentArgs.suppress,"%s*,%s*") -- split parameter by comma |
|||
return '[[' .. sitelink .. '|' .. label .. ']]' |
|||
for _,v in ipairs(suppresslist) do |
|||
end |
|||
if v:match("^%d+$") then |
|||
v = "P"..tostring(v) |
|||
else |
else |
||
return label |
|||
v = string.upper(v) |
|||
end |
end |
||
else |
|||
suppress[v] = true -- index table by identifier name |
|||
auxCats = auxCats .. needsAttention('L') |
|||
return qid |
|||
end |
end |
||
end |
end |
||
if namespace == 0 then |
|||
qid = mw.wikibase.getEntityIdForCurrentPage() |
|||
--redirect aliases to proper parameter names |
|||
end |
|||
for _, a in pairs(aliases) do |
|||
if qid then -- article is connected to Wikidata item |
|||
local alias, param = a[1], a[2] |
|||
if |
if parentArgs.qid and (resolveQID(parentArgs.qid) ~= qid) then -- non-matching qid parameter |
||
auxCats = auxCats .. needsAttention('D') |
|||
parentArgs[param] = parentArgs[alias] |
|||
end |
|||
else -- page is not connected to any Wikidata item |
|||
qid = resolveQID(parentArgs.qid) -- check qid parameter if no wikidata item is connected |
|||
if qid then -- qid parameter is valid, set topic to display |
|||
topic = mw.wikibase.getLabel(qid) |
|||
if topic then |
|||
if mw.ustring.lower(title.subpageText) == mw.ustring.lower(topic) then -- suppress topic display if subpagename equals topic up to case change |
|||
topic = nil |
|||
end |
|||
if topic and mw.wikibase.getSitelink(qid) then -- make wikilink to article |
|||
topic = '[[' .. mw.wikibase.getSitelink(qid) .. '|' .. topic .. ']]' |
|||
end |
|||
else |
|||
auxCats = auxCats .. needsAttention('L') |
|||
end |
|||
elseif parentArgs.qid and parentArgs.qid~='' then -- invalid qid has been supplied, add to tracking cat |
|||
auxCats = auxCats .. needsAttention('Q') |
|||
end |
end |
||
end |
end |
||
local qids = {} -- setup any additional QIDs |
|||
if parentArgs.additional=='auto' and qid then -- check P527 for parts to add additional qids |
|||
--redirect deprecated parameters to proper parameter names, and assign tracking cat |
|||
local checkparts = function(property) |
|||
for _, d in pairs( deprecated ) do |
|||
local parts = mw.wikibase.getBestStatements(qid,property) |
|||
local dep, param = d[1], d[2] |
|||
if parts then |
|||
if (parentArgs[param] == nil or parentArgs[param] == '') and parentArgs[dep] then |
|||
for _,part in ipairs(parts) do |
|||
parentArgs[param] = parentArgs[dep] |
|||
if part.mainsnak.datavalue and part.mainsnak.datavalue.value.id then |
|||
if namespace == 0 then |
|||
local resolvedqid = resolveQID(part.mainsnak.datavalue.value.id) |
|||
auxCats = auxCats .. '[[Category:非推奨の典拠管理識別子が指定されている記事|'..dep..']]' |
|||
if resolvedqid then |
|||
table.insert(qids,resolvedqid) |
|||
end end end end end |
|||
for _,part in ipairs(config.auto_additional) do |
|||
checkparts('P'..tostring(part)) |
|||
end |
|||
elseif parentArgs.additional and parentArgs.additional ~= '' then |
|||
for _,v in ipairs(mw.text.split(parentArgs.additional,"%s*,%s*")) do |
|||
v = resolveQID(v) |
|||
if v then |
|||
if v == qid then -- duplicate of qid parameter |
|||
auxCats = auxCats .. needsAttention('R') |
|||
end |
|||
table.insert(qids,v) |
|||
else -- invalid QID specified |
|||
auxCats = auxCats .. needsAttention('A') |
|||
end |
end |
||
end |
end |
||
end |
end |
||
local sections = {} |
|||
local function makeSections(qid,additional) |
|||
local localparams = false |
|||
local numsections = 0 |
|||
for _,_ in ipairs(config.sections) do numsections = numsections + 1 end |
|||
for _ = 1,#qids+numsections do table.insert(sections,{}) end |
|||
local qslink = '' -- setup link to add using QuickStatements |
|||
-- check which identifiers to show/suppress in template |
|||
local show = {} -- setup list |
|||
local showall = true |
|||
local function stripP(pid) |
|||
if pid:match("^[Pp]%d+$") then |
|||
pid = mw.ustring.gsub(pid,'[Pp]','') --strip P from property number |
|||
end |
|||
if pid:match("^%d+$") then |
|||
return tonumber(pid) |
|||
end |
|||
end |
|||
local function addshowlist(list) |
|||
if list and list ~= '' then |
|||
for _,v in ipairs(mw.text.split(string.lower(list),"%s*,%s*")) do |
|||
local vprop = stripP(v) |
|||
if vprop then -- e.g. show=P214 to show one particular property |
|||
show[vprop] = true |
|||
else -- e.g. show=arts to use whitelist |
|||
if config.whitelists[v] then |
|||
for _,w in ipairs(config.whitelists[v].properties) do |
|||
show[w] = true |
|||
end |
|||
end |
|||
end |
|||
end |
|||
showall = false |
|||
end |
|||
end |
|||
addshowlist(frame.args.show) -- check show= parameter on wrapper template |
|||
addshowlist(parentArgs.show or parentArgs.country) -- check show parameter on article template |
|||
if parentArgs.suppress then |
|||
local suppresslist = mw.text.split(parentArgs.suppress,"%s*,%s*") -- split parameter by comma |
|||
for _,v in ipairs(suppresslist) do |
|||
v = stripP(string.upper(v)) |
|||
if v then |
|||
show[v] = false |
|||
auxCats = auxCats .. '[[' .. config.i18n.category .. ':' .. config.i18n.suppressedcat .. ']]' |
|||
else |
|||
auxCats = auxCats .. needsAttention('P') |
|||
end |
|||
end |
|||
end |
|||
local function makeSections(qid,addit) |
|||
local tval = {} |
local tval = {} |
||
local function parameter_is_used(property) |
local function parameter_is_used(property) |
||
145行目: | 295行目: | ||
used = true |
used = true |
||
end |
end |
||
elseif tval[property] == false then -- property has been manually suppressed |
|||
used = true |
|||
end |
end |
||
end |
end |
||
151行目: | 303行目: | ||
for _, params in ipairs(conf) do |
for _, params in ipairs(conf) do |
||
tval[params.property] = getIdsFromWikidata(qid, 'P' .. params.property) -- setup table for values with property number as key |
tval[params.property] = getIdsFromWikidata(qid, 'P' .. params.property) -- setup table for values with property number as key |
||
local showb = true |
|||
if additional then |
|||
if (show[params.property] == nil) and (show[string.upper(params[1])] == nil ) then |
|||
showb = showall -- if not specified then depends on showall |
|||
tval[params.property] = false -- indicates the identifier is suppressed |
|||
elseif (show[params.property] == false) or (show[string.upper(params[1])] == false) then -- if either are false then id will be suppressed |
|||
end |
|||
showb = false |
|||
else |
|||
end |
|||
if not showb then |
|||
tval[params.property] = false -- indicates the identifier is suppressed |
|||
elseif not addit then |
|||
local val = parentArgs[mw.ustring.lower(params[1])] or parentArgs[params[1]] |
local val = parentArgs[mw.ustring.lower(params[1])] or parentArgs[params[1]] |
||
if val and val~='' then -- add local parameter to list if not already in |
|||
if suppress["P"..tostring(params.property)] or suppress[string.upper(params[1])] or val == '' then |
|||
localparams = true |
|||
if tval[params.property][1] and (namespace == 0 or testcases) then |
|||
suppressedIdCount = suppressedIdCount + 1 |
|||
if parentArgs['arts'] ~= 'arts' then |
|||
auxCats = auxCats .. '[[Category:抑制された典拠管理識別子がある記事|'..params[1]..']]' |
|||
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 |
local bnew = true |
||
for _, w in pairs(tval[params.property]) do |
for _, w in pairs(tval[params.property]) do |
||
if val == w then |
if val == w.id then |
||
bnew = false |
bnew = false |
||
end |
end |
||
end |
end |
||
if bnew then -- add new value to table |
if bnew then -- add new value to table |
||
if |
if qid then |
||
-- 末尾はQ328(英語版ウィキペディア)ではなくQ177837(日本語版ウィキペディア)とする |
|||
auxCats = auxCats .. '[[Category:ウィキデータと異なる典拠管理識別子をパラメータで指定しているページ|'..params[1]..']]' |
|||
qslink = qslink .. '%7C%7C' .. qid .. '%7CP' .. params.property .. '%7C%22' .. mw.uri.encode(val,"PATH") .. '%22%7CS143%7CQ177837' |
|||
end |
end |
||
table.insert(tval[params.property],val) |
table.insert(tval[params.property],{id=val,name=''}) |
||
else |
|||
iMatches = iMatches+1 |
|||
end |
end |
||
end |
end |
||
191行目: | 338行目: | ||
end |
end |
||
end |
end |
||
if |
if tval[params.property] ~= false and not suppress then |
||
local tlinks = {} -- setup table for links |
local tlinks = {} -- setup table for links |
||
local nextIdVal = 1 |
local nextIdVal = 1 |
||
local row = '' |
|||
for _,val in ipairs(tval[params.property]) do |
for _,val in ipairs(tval[params.property]) do |
||
local link = |
local link = _makelink(params,val,nextIdVal,qid) |
||
row = row .. link |
|||
if link then -- add category unless link==false |
|||
link = link .. getCatForId(cat) |
|||
end |
|||
table.insert(tlinks,link) |
table.insert(tlinks,link) |
||
nextIdVal = nextIdVal + 1 |
nextIdVal = nextIdVal + 1 |
||
end |
end |
||
if |
if nextIdVal>=2 then |
||
row = row .. '\n' |
|||
table.insert(sections[addit or params.section],row) |
|||
rct = true |
|||
end |
|||
for i, l in ipairs(tlinks) do |
|||
if i==1 and not params.prefix then |
|||
row = row..'*' |
|||
else |
|||
row = row..'\n**' |
|||
end |
|||
row = row .. l |
|||
end |
|||
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 |
|||
if tval[params.property][2] then |
|||
-- auxCats = auxCats .. getCatForId('multiple') |
|||
end |
|||
end |
end |
||
end |
end |
||
236行目: | 365行目: | ||
end |
end |
||
makeSections(qid,false) |
|||
--use QID= parameter for testing/example purposes only |
|||
for c = 1,#qids do |
|||
local function resolveQID(qid) |
|||
makeSections(qids[c],numsections+c) |
|||
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 |
|||
local qid = parentArgs['qid'] or parentArgs['QID'] |
|||
qids[1] = resolveQID(qid) |
|||
elseif mw.wikibase then |
|||
qids[1] = mw.wikibase.getEntityIdForCurrentPage() |
|||
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 .. '[[Category:ウィキデータと同じ典拠管理識別子をパラメータで指定しているページ]]' |
|||
end |
|||
if parentArgs['arts'] == 'arts' and suppressedIdCount > 0 then |
|||
if namespace == 0 or testcases then |
|||
auxCats = auxCats .. addCat('ACArtで'..suppressedIdCount..'種類の典拠管理識別子が抑制された記事') |
|||
end |
|||
end |
end |
||
--configure Navbox |
--configure Navbox |
||
local outString = '' |
local outString = '' |
||
if rct |
if rct or localparams then -- there is at least one link to display |
||
local Navbox = require('Module:Navbox') |
local Navbox = require('Module:Navbox') |
||
local sect,lastsect = 0,0 |
local sect,lastsect = 0,0 |
||
274行目: | 379行目: | ||
navboxclass = 'authority-control', |
navboxclass = 'authority-control', |
||
bodyclass = 'hlist', |
bodyclass = 'hlist', |
||
state = parentArgs.state or |
state = parentArgs.state or config.i18n.autocollapse, |
||
navbar = 'off' |
navbar = 'off' |
||
} |
} |
||
for c=1, |
for c=1,numsections+#qids do |
||
if #sections[c] ~= 0 then -- section is non-empty |
if #sections[c] ~= 0 then -- section is non-empty |
||
sect = sect + 1 |
sect = sect + 1 |
||
lastsect = c |
lastsect = c |
||
local sectname |
local sectname |
||
if c <= |
if c <= numsections then -- regular section |
||
sectname = |
sectname = config.sections[c].name |
||
else -- section from additional qid |
else -- section from additional qid |
||
local qid = qids[c-numsections] |
|||
sectname = mw.wikibase.getLabel(qids[c-totsect+numqids+1]) .. pencil(qids[c-totsect+numqids +1]) |
|||
sectname = wikilink(qid) .. pencil(qid) |
|||
end |
end |
||
navboxArgs['group' .. c] = sectname |
navboxArgs['group' .. c] = sectname |
||
291行目: | 397行目: | ||
end |
end |
||
end |
end |
||
if localparams then |
|||
local aclink = '[[Help:典拠管理|典拠管理]]' |
|||
lastsect = lastsect + 1 |
|||
if sect == 1 then -- special display when only one section |
|||
sect = sect + 1 |
|||
if lastsect == 1 or lastsect == 8 then -- no special label when only general or other IDs are present |
|||
navboxArgs['group' .. lastsect] = config.i18n.warning |
|||
local warning = frame:expandTemplate{title = config.i18n.errortemplate, args = {config.i18n.localparams}} |
|||
elseif lastsect <= totsect - numqids then -- other regular section |
|||
if qslink ~= '' then |
|||
navboxArgs['group' .. lastsect] = aclink .. ': ' .. sectionNames[lastsect] .. pencil(qids[1]) |
|||
warning = warning .. ' ' .. config.i18n.movetowd .. '<span class="qs autoconfirmed-show"> [[File:Commons to Wikidata QuickStatements.svg|20px|link=https://quickstatements.toolforge.org/#/v1=' .. qslink .. '|' .. config.i18n.addtowd .. ']]</span>' |
|||
elseif not qid then |
|||
if namespace == 0 then |
|||
warning = warning .. ' ' .. config.i18n.connecttowd |
|||
elseif namespace==14 or namespace==2 or namespace==118 then |
|||
warning = warning .. ' ' .. config.i18n.qidcode |
|||
end |
|||
end |
|||
navboxArgs['list' .. lastsect] = warning |
|||
end |
|||
if topic then -- display in expanded form with topic |
|||
navboxArgs.title = config.i18n.aclink .. ' – ' .. topic .. pencil(qid) |
|||
elseif sect == 1 then -- special display when only one section |
|||
if lastsect <= numsections then |
|||
if config.sections[lastsect].hidelabelwhenalone then -- no special label when only general or other IDs are present |
|||
navboxArgs['group' .. lastsect] = config.i18n.aclink .. pencil(qid) |
|||
else -- other regular section |
|||
navboxArgs['group' .. lastsect] = config.i18n.aclink .. ': ' .. navboxArgs['group' .. lastsect] .. pencil(qid) |
|||
end |
|||
else -- section from additional qid |
else -- section from additional qid |
||
navboxArgs['group' .. lastsect] = aclink .. ': ' .. navboxArgs['group' .. lastsect] |
navboxArgs['group' .. lastsect] = config.i18n.aclink .. ': ' .. navboxArgs['group' .. lastsect] |
||
end |
end |
||
else -- add title to navbox |
else -- add title to navbox |
||
navboxArgs.title = aclink .. pencil( |
navboxArgs.title = config.i18n.aclink .. pencil(qid) |
||
end |
end |
||
outString = Navbox._navbox(navboxArgs) |
outString = Navbox._navbox(navboxArgs) |
||
else -- 典拠管理識別子が1つもない |
|||
end |
|||
--auxCats |
|||
if rct == 0 then |
|||
if namespace == 0 or testcases then |
if namespace == 0 or testcases then |
||
auxCats = auxCats .. addCat('典拠管理識別子が1つもない記事') |
auxCats = auxCats .. addCat('典拠管理識別子が1つもない記事') |
||
end |
|||
elseif rct >= 25 then |
|||
if namespace == 0 or testcases then |
|||
local eCat = rct..'種類の典拠管理識別子がある記事' |
|||
-- auxCats = auxCats .. addCat(eCat) --25種類以上の識別子が指定された場合、追跡カテゴリをつけるという機能は日本語版では導入されていない |
|||
end |
end |
||
end |
end |
||
if parentArgs.state then |
|||
if parentArgs.state |
|||
if namespace == 0 or testcases then |
|||
and parentArgs.state~='' |
|||
local sCat |
|||
and parentArgs.state~=config.i18n.collapsed |
|||
and parentArgs.state~=config.i18n.expanded |
|||
and parentArgs.state~=config.i18n.autocollapse then --invalid state parameter |
|||
auxCats = auxCats .. needsAttention('S') |
|||
else sCat = 'Normdatenでstateに不明な値を指定している記事' |
|||
end |
|||
-- auxCats = auxCats .. addCat(sCat) -- 日本語版でのカテゴリ導入は様子を見てから |
|||
end |
|||
end |
end |
||
if testcases then |
if testcases then |
||
auxCats = mw.ustring.gsub(auxCats, '(%[%[)( |
auxCats = mw.ustring.gsub(auxCats, '(%[%[)(' .. config.i18n.category .. ')', '%1:%2') --for easier checking |
||
end |
end |
||
335行目: | 449行目: | ||
outString = outString..auxCats |
outString = outString..auxCats |
||
if namespace ~= 0 then |
if namespace ~= 0 then |
||
-- 日本語版独自の変更あり |
|||
outString = mw.ustring.gsub(outString, '(%[%[)(Category:[^%]]*記事)', '%1:%2') --by definition |
|||
outString = mw.ustring.gsub(outString,'(%[%[)(' .. config.i18n.category .. ':)([^%|%]]+' .. config.i18n.Articles .. ')%|?[^%|%]]*(%]%])','%1:%2%3%4') |
|||
end |
end |
||
local check = require('Module:Check for unknown parameters')._check |
|||
local sortkey |
|||
if namespace == 0 then |
|||
sortkey = '*' .. title.text |
|||
else |
|||
sortkey = title.fullText |
|||
end |
|||
outString = outString .. check({ |
|||
['unknown'] = '[[' .. config.i18n.category .. ':' .. config.i18n.pageswithparams .. '|' .. sortkey .. ']]', |
|||
['preview'] = config.i18n.previewwarning, 'show', 'country', 'suppress', 'additional', 'qid', 'state' |
|||
}, parentArgs) |
|||
return outString |
return outString |
||
end |
end |
||
p.makelink = function(conf,val,nextid,qid) |
|||
-- Creates a human-readable standalone wikitable version of conf, and tracking categories with page counts, for use in the documentation |
|||
return _makelink(conf,val,nextid,qid) |
|||
function p.docConfTable(frame) |
|||
local wikiTable = '<table class="wikitable sortable">'.. |
|||
'<tr><th rowspan=2>引数名</th>'.. |
|||
'<th rowspan=2>グループ</th>'.. |
|||
'<th rowspan=2>表示名</th>'.. |
|||
'<th rowspan=2 data-sort-type=number>ウィキデータのプロパティ</th>'.. |
|||
'<th colspan=2>追跡カテゴリおよび該当ページ数</th></tr>'.. |
|||
'<tr><th>[[:Category:典拠管理情報がある記事|記事]]</th>'.. |
|||
'<th>[[:Category:誤った典拠管理情報が指定されている記事|誤った識別子]]</th></tr>' |
|||
local lang = mw.getContentLanguage() |
|||
local a, f, P = 0, 0, 0 --cumulative sums |
|||
local config = require("Module:Authority control/config") |
|||
local conf = config.config |
|||
local sectionNames = config.sectionNames |
|||
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%;">[['..tostring(mw.uri.fullUrl('Category:'..category,'action=edit&preload=Template:Authority_control/preload'))..' 作成]]</span>' |
|||
end |
|||
return ret |
|||
end |
|||
for _, conf in pairs(conf) do |
|||
local param, pid, section = conf[1], conf.property, sectionNames[conf.section] |
|||
local appearsAs = makelink(conf,conf.example,1) |
|||
local link = conf.idlink or param..' (識別子)' |
|||
local category = conf.category or param |
|||
local args = { id = 'f', pid } |
|||
local wpl = frame:expandTemplate{ title = 'Wikidata property link', args = args } |
|||
local articleCat = category..'識別子が指定されている記事' |
|||
local faultyCat = '誤った'.. (conf.errorcat or category) ..'識別子が指定されている記事' |
|||
local articleCount = lang:formatNum( mw.site.stats.pagesInCategory(articleCat, 'pages') ) |
|||
local faultyCount = lang:formatNum( mw.site.stats.pagesInCategory(faultyCat, 'pages') ) |
|||
P = P + 1 --property count |
|||
a = a + lang:parseFormattedNumber(articleCount) |
|||
f = f + lang:parseFormattedNumber(faultyCount) |
|||
wikiTable = wikiTable..'<tr><td>[['..link..'|'..param..']]</td>'.. |
|||
'<td>'..section..'</td>'.. |
|||
'<td>'..appearsAs..'</td>'.. |
|||
'<td data-sort-value='..pid..'>'..wpl..'</td>'.. |
|||
'<td style="text-align: right;">'..checkcat(articleCat,articleCount)..'</td>'.. |
|||
'<td style="text-align: right;">'..checkcat(faultyCat,faultyCount)..'</td></tr>' |
|||
end |
|||
wikiTable = wikiTable..'<tr><th style="text-align: right;" colspan=3>合計</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) |
|||
end |
end |
||
2023年8月31日 (木) 12:46時点における最新版
このモジュールについての説明文ページを モジュール:Authority control/doc に作成できます
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 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 function getCatForId(id,faulty)
local cat = string.format(
config.i18n.cat,
faulty and config.i18n.faulty..id or id
)
return addCat(cat)
end
local function getIdsFromWikidata(qid,property)
local function getquals(statement,qualid)
if statement.qualifiers and statement.qualifiers['P'..qualid] then
return mw.wikibase.renderSnak(statement.qualifiers['P'..qualid][1])
else
return false
end
end
local ids = {}
if qid then
for _, statement in ipairs(mw.wikibase.getBestStatements(qid,property)) do
if statement.mainsnak.datavalue then
local val = statement.mainsnak.datavalue.value
if val then
local namedas = getquals(statement,1810) or getquals(statement,742) or ''
table.insert(ids,{id=val,name=namedas})
end
end
end
end
return ids
end
local _makelink = function(conf,val,nextid,qid) --validate values and create a link
local function tooltip(text,label)
if label and label~='' then
return mw.getCurrentFrame():expandTemplate{title = "Tooltip", args = {text,label}}
else
return text
end
end
local link
if nextid==1 then
if conf.prefix then
link = '*' .. conf.prefix .. '\n**'
else
link = '*'
end
else
link = '\n**'
end
local valid_value = false
if conf.customlink then -- use function to validate and generate link
local label = nextid>1 and nextid
local newlink= require(config.auxiliary)[conf.customlink](val.id,label)
if newlink then
link = link .. newlink
valid_value = true
end
else
if conf.pattern then -- use pattern to determine validity if defined
valid_value = string.match(val.id,'^'..conf.pattern..'$')
elseif conf.patterns then
for _,pattern in ipairs(conf.patterns) do
valid_value = val.id:match('^'..pattern..'$')
if valid_value then break end
end
elseif conf.valid then -- otherwise use function to determine validity
valid_value = require(config.auxiliary)[conf.valid](val.id)
else -- no validation possible
valid_value = val.id
end
if valid_value then
local newlink
local label = conf.label
if not label or nextid>1 then
label = tostring(nextid)
end
if conf.link then
valid_value = valid_value:gsub('%%', '%%%%')
newlink = '[' .. mw.ustring.gsub(conf.link,'%$1',valid_value) .. ' ' .. label .. ']'
else
newlink = valid_value
end
link = link .. '<span class="uid">'..tooltip(newlink,val.name)..'</span>'
end
end
if valid_value then
link = link .. getCatForId(conf.category or conf[1])
else
--local preview = require("Module:If preview")
local wdlink = qid and '[[:wikidata:' .. qid .. '#P' .. conf.property .. ']]' or ''
local tooltip = string.format(
config.i18n.idnotvalid,
conf[1],
val.id
)
link = link .. '[[File:' .. config.i18n.warningicon .. '|20px|frameless|link=' .. wdlink .. '|' .. tooltip .. ']]'
if conf.errorcat then
link = link .. addCat(conf.errorcat)
else
link = link .. getCatForId(conf.category or conf[1],true)
end
link = link .. addCat(config.i18n.allfaultycat,conf[1])-- .. preview._warning({'The '..conf[1]..' id '..val..' is not valid.'})
end
return link
end
--[[==========================================================================]]
--[[ Main ]]
--[[==========================================================================]]
function p.authorityControl(frame)
local function resolveQID(qid)
if qid then
qid = 'Q'..mw.ustring.gsub(qid, '^[Qq]', '')
if mw.wikibase.isValidEntityId(qid) and mw.wikibase.entityExists(qid) then
local sitelink = mw.wikibase.getSitelink(qid)
if sitelink then
return mw.wikibase.getEntityIdForTitle(sitelink) or mw.wikibase.getEntity(qid).id
end
return mw.wikibase.getEntity(qid).id
end
end
end
local conf = config.config
local parentArgs = frame:getParent().args
local auxCats = ''
local rct = false -- boolean to track if there are any links to be returned
local qid,topic
local wikilink = function(qid,hideifequal)
local label,sitelink = mw.wikibase.getLabel(qid),mw.wikibase.getSitelink(qid)
if label then
if sitelink then
local target = mw.title.new(sitelink)
if target==title or (target.isRedirect and target.redirectTarget==title) then -- do not link
return label
else -- make wikilink to article
return '[[' .. sitelink .. '|' .. label .. ']]'
end
else
return label
end
else
auxCats = auxCats .. needsAttention('L')
return qid
end
end
if namespace == 0 then
qid = mw.wikibase.getEntityIdForCurrentPage()
end
if qid then -- article is connected to Wikidata item
if parentArgs.qid and (resolveQID(parentArgs.qid) ~= qid) then -- non-matching qid parameter
auxCats = auxCats .. needsAttention('D')
end
else -- page is not connected to any Wikidata item
qid = resolveQID(parentArgs.qid) -- check qid parameter if no wikidata item is connected
if qid then -- qid parameter is valid, set topic to display
topic = mw.wikibase.getLabel(qid)
if topic then
if mw.ustring.lower(title.subpageText) == mw.ustring.lower(topic) then -- suppress topic display if subpagename equals topic up to case change
topic = nil
end
if topic and mw.wikibase.getSitelink(qid) then -- make wikilink to article
topic = '[[' .. mw.wikibase.getSitelink(qid) .. '|' .. topic .. ']]'
end
else
auxCats = auxCats .. needsAttention('L')
end
elseif parentArgs.qid and parentArgs.qid~='' then -- invalid qid has been supplied, add to tracking cat
auxCats = auxCats .. needsAttention('Q')
end
end
local qids = {} -- setup any additional QIDs
if parentArgs.additional=='auto' and qid then -- check P527 for parts to add additional qids
local checkparts = function(property)
local parts = mw.wikibase.getBestStatements(qid,property)
if parts then
for _,part in ipairs(parts) do
if part.mainsnak.datavalue and part.mainsnak.datavalue.value.id then
local resolvedqid = resolveQID(part.mainsnak.datavalue.value.id)
if resolvedqid then
table.insert(qids,resolvedqid)
end end end end end
for _,part in ipairs(config.auto_additional) do
checkparts('P'..tostring(part))
end
elseif parentArgs.additional and parentArgs.additional ~= '' then
for _,v in ipairs(mw.text.split(parentArgs.additional,"%s*,%s*")) do
v = resolveQID(v)
if v then
if v == qid then -- duplicate of qid parameter
auxCats = auxCats .. needsAttention('R')
end
table.insert(qids,v)
else -- invalid QID specified
auxCats = auxCats .. needsAttention('A')
end
end
end
local sections = {}
local localparams = false
local numsections = 0
for _,_ in ipairs(config.sections) do numsections = numsections + 1 end
for _ = 1,#qids+numsections do table.insert(sections,{}) end
local qslink = '' -- setup link to add using QuickStatements
-- check which identifiers to show/suppress in template
local show = {} -- setup list
local showall = true
local function stripP(pid)
if pid:match("^[Pp]%d+$") then
pid = mw.ustring.gsub(pid,'[Pp]','') --strip P from property number
end
if pid:match("^%d+$") then
return tonumber(pid)
end
end
local function addshowlist(list)
if list and list ~= '' then
for _,v in ipairs(mw.text.split(string.lower(list),"%s*,%s*")) do
local vprop = stripP(v)
if vprop then -- e.g. show=P214 to show one particular property
show[vprop] = true
else -- e.g. show=arts to use whitelist
if config.whitelists[v] then
for _,w in ipairs(config.whitelists[v].properties) do
show[w] = true
end
end
end
end
showall = false
end
end
addshowlist(frame.args.show) -- check show= parameter on wrapper template
addshowlist(parentArgs.show or parentArgs.country) -- check show parameter on article template
if parentArgs.suppress then
local suppresslist = mw.text.split(parentArgs.suppress,"%s*,%s*") -- split parameter by comma
for _,v in ipairs(suppresslist) do
v = stripP(string.upper(v))
if v then
show[v] = false
auxCats = auxCats .. '[[' .. config.i18n.category .. ':' .. config.i18n.suppressedcat .. ']]'
else
auxCats = auxCats .. needsAttention('P')
end
end
end
local function makeSections(qid,addit)
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
local showb = true
if (show[params.property] == nil) and (show[string.upper(params[1])] == nil ) then
showb = showall -- if not specified then depends on showall
elseif (show[params.property] == false) or (show[string.upper(params[1])] == false) then -- if either are false then id will be suppressed
showb = false
end
if not showb then
tval[params.property] = false -- indicates the identifier is suppressed
elseif not addit then
local val = parentArgs[mw.ustring.lower(params[1])] or parentArgs[params[1]]
if val and val~='' then -- add local parameter to list if not already in
localparams = true
local bnew = true
for _, w in pairs(tval[params.property]) do
if val == w.id then
bnew = false
end
end
if bnew then -- add new value to table
if qid then
-- 末尾はQ328(英語版ウィキペディア)ではなくQ177837(日本語版ウィキペディア)とする
qslink = qslink .. '%7C%7C' .. qid .. '%7CP' .. params.property .. '%7C%22' .. mw.uri.encode(val,"PATH") .. '%22%7CS143%7CQ177837'
end
table.insert(tval[params.property],{id=val,name=''})
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 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'
table.insert(sections[addit or params.section],row)
rct = true
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
makeSections(qid,false)
for c = 1,#qids do
makeSections(qids[c],numsections+c)
end
--configure Navbox
local outString = ''
if rct or localparams 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 config.i18n.autocollapse,
navbar = 'off'
}
for c=1,numsections+#qids do
if #sections[c] ~= 0 then -- section is non-empty
sect = sect + 1
lastsect = c
local sectname
if c <= numsections then -- regular section
sectname = config.sections[c].name
else -- section from additional qid
local qid = qids[c-numsections]
sectname = wikilink(qid) .. pencil(qid)
end
navboxArgs['group' .. c] = sectname
navboxArgs['list' .. c] = table.concat(sections[c])
end
end
if localparams then
lastsect = lastsect + 1
sect = sect + 1
navboxArgs['group' .. lastsect] = config.i18n.warning
local warning = frame:expandTemplate{title = config.i18n.errortemplate, args = {config.i18n.localparams}}
if qslink ~= '' then
warning = warning .. ' ' .. config.i18n.movetowd .. '<span class="qs autoconfirmed-show"> [[File:Commons to Wikidata QuickStatements.svg|20px|link=https://quickstatements.toolforge.org/#/v1=' .. qslink .. '|' .. config.i18n.addtowd .. ']]</span>'
elseif not qid then
if namespace == 0 then
warning = warning .. ' ' .. config.i18n.connecttowd
elseif namespace==14 or namespace==2 or namespace==118 then
warning = warning .. ' ' .. config.i18n.qidcode
end
end
navboxArgs['list' .. lastsect] = warning
end
if topic then -- display in expanded form with topic
navboxArgs.title = config.i18n.aclink .. ' – ' .. topic .. pencil(qid)
elseif sect == 1 then -- special display when only one section
if lastsect <= numsections then
if config.sections[lastsect].hidelabelwhenalone then -- no special label when only general or other IDs are present
navboxArgs['group' .. lastsect] = config.i18n.aclink .. pencil(qid)
else -- other regular section
navboxArgs['group' .. lastsect] = config.i18n.aclink .. ': ' .. navboxArgs['group' .. lastsect] .. pencil(qid)
end
else -- section from additional qid
navboxArgs['group' .. lastsect] = config.i18n.aclink .. ': ' .. navboxArgs['group' .. lastsect]
end
else -- add title to navbox
navboxArgs.title = config.i18n.aclink .. pencil(qid)
end
outString = Navbox._navbox(navboxArgs)
else -- 典拠管理識別子が1つもない
if namespace == 0 or testcases then
auxCats = auxCats .. addCat('典拠管理識別子が1つもない記事')
end
end
if parentArgs.state
and parentArgs.state~=''
and parentArgs.state~=config.i18n.collapsed
and parentArgs.state~=config.i18n.expanded
and parentArgs.state~=config.i18n.autocollapse then --invalid state parameter
auxCats = auxCats .. needsAttention('S')
end
if testcases then
auxCats = mw.ustring.gsub(auxCats, '(%[%[)(' .. config.i18n.category .. ')', '%1:%2') --for easier checking
end
--out
outString = outString..auxCats
if namespace ~= 0 then
-- 日本語版独自の変更あり
outString = mw.ustring.gsub(outString,'(%[%[)(' .. config.i18n.category .. ':)([^%|%]]+' .. config.i18n.Articles .. ')%|?[^%|%]]*(%]%])','%1:%2%3%4')
end
local check = require('Module:Check for unknown parameters')._check
local sortkey
if namespace == 0 then
sortkey = '*' .. title.text
else
sortkey = title.fullText
end
outString = outString .. check({
['unknown'] = '[[' .. config.i18n.category .. ':' .. config.i18n.pageswithparams .. '|' .. sortkey .. ']]',
['preview'] = config.i18n.previewwarning, 'show', 'country', 'suppress', 'additional', 'qid', 'state'
}, parentArgs)
return outString
end
p.makelink = function(conf,val,nextid,qid)
return _makelink(conf,val,nextid,qid)
end
return p