en:Module:Wd 2017年9月20日 (水) 19:46(UTC)より
template>K-iczn (en:Module:Wd 2017年9月10日 (日) 18:05(UTC)より) |
template>K-iczn (en:Module:Wd 2017年9月20日 (水) 19:46(UTC)より) |
||
26行目: | 26行目: | ||
references = "references", | references = "references", | ||
label = "label", | label = "label", | ||
title = "title" | title = "title", | ||
alias = "alias", | |||
aliases = "aliases" | |||
} | } | ||
87行目: | 89行目: | ||
qualifier = "%q", | qualifier = "%q", | ||
reference = "%r", | reference = "%r", | ||
alias = "%a", | |||
separator = "%s", | separator = "%s", | ||
general = "%x" | general = "%x" | ||
95行目: | 98行目: | ||
qualifier = "%q[%s][%r]", | qualifier = "%q[%s][%r]", | ||
reference = "%r", | reference = "%r", | ||
propertyWithQualifier = "%p[ <span style=\"font-size:smaller\">(%q)</span>][%s][%r]" | propertyWithQualifier = "%p[ <span style=\"font-size:smaller\">(%q)</span>][%s][%r]", | ||
alias = "%a[%s]" | |||
} | } | ||
102行目: | 106行目: | ||
[parameters.reference] = {"getReferences", "getReference"}, | [parameters.reference] = {"getReferences", "getReference"}, | ||
[parameters.qualifier] = {"getAllQualifiers"}, | [parameters.qualifier] = {"getAllQualifiers"}, | ||
[parameters.qualifier.."\\d"] = {"getQualifiers", "getQualifier"} | [parameters.qualifier.."\\d"] = {"getQualifiers", "getQualifier"}, | ||
[parameters.alias] = {"getAlias"} | |||
} | } | ||
577行目: | 582行目: | ||
endParam() | endParam() | ||
-- make sure that at least one required parameter has been defined | |||
if not next(root.req) then | |||
error(missingRequiredParameterError()) | |||
end | |||
-- make sure that the separator parameter "%s" is not amongst the required parameters | |||
if root.req[parameters.separator] then | |||
error(extraRequiredParameterError(parameters.separator)) | |||
end | |||
return root, params | return root, params | ||
755行目: | 770行目: | ||
outString = outString .. valuesArray[i][1] | outString = outString .. valuesArray[i][1] | ||
end | end | ||
end | end | ||
1,935行目: | 1,945行目: | ||
return values | return values | ||
end | |||
-- level 1 hook | |||
function State:getAlias(object) | |||
local value = object.value | |||
local title = nil | |||
if value and self.linked then | |||
if self.conf.entityID:sub(1,1) == "Q" then | |||
title = mw.wikibase.sitelink(self.conf.entityID) | |||
elseif self.conf.entityID:sub(1,1) == "P" then | |||
title = "d:Property:" .. self.conf.entityID | |||
end | |||
if title then | |||
value = buildWikilink(title, value) | |||
end | |||
end | |||
value = {value} -- create one value object | |||
if #value > 0 then | |||
return {value} -- wrap the value object in an array and return it | |||
else | |||
return {} -- return empty array if there was no value | |||
end | |||
end | end | ||
2,035行目: | 2,071行目: | ||
function p._property(args) | function p._property(args) | ||
loadSubmodules() | loadSubmodules() | ||
return | return valueCommand(args, p.commands.property) | ||
end | end | ||
2,045行目: | 2,081行目: | ||
function p._properties(args) | function p._properties(args) | ||
loadSubmodules() | loadSubmodules() | ||
return | return valueCommand(args, p.commands.properties) | ||
end | end | ||
2,055行目: | 2,091行目: | ||
function p._qualifier(args) | function p._qualifier(args) | ||
loadSubmodules() | loadSubmodules() | ||
return | return valueCommand(args, p.commands.qualifier) | ||
end | end | ||
2,065行目: | 2,101行目: | ||
function p._qualifiers(args) | function p._qualifiers(args) | ||
loadSubmodules() | loadSubmodules() | ||
return | return valueCommand(args, p.commands.qualifiers) | ||
end | end | ||
2,075行目: | 2,111行目: | ||
function p._reference(args) | function p._reference(args) | ||
loadSubmodules() | loadSubmodules() | ||
return | return valueCommand(args, p.commands.reference) | ||
end | end | ||
2,085行目: | 2,121行目: | ||
function p._references(args) | function p._references(args) | ||
loadSubmodules() | loadSubmodules() | ||
return | return valueCommand(args, p.commands.references) | ||
end | end | ||
function | function valueCommand(args, funcName) | ||
_ = Config.new() | local _ = Config.new() | ||
_:processFlagOrCommand(funcName) -- process first command (== function name) | _:processFlagOrCommand(funcName) -- process first command (== function name) | ||
local parsedFormat, formatParams, claims, sep | local parsedFormat, formatParams, claims, sep, value | ||
local hooks = {count = 0} | local hooks = {count = 0} | ||
2,220行目: | 2,256行目: | ||
-- process overridden separator values; | -- process overridden separator values; | ||
-- must come AFTER parsing the | -- must come AFTER parsing the format | ||
for i, v in pairs(_.separators) do | for i, v in pairs(_.separators) do | ||
if args[i] then | if args[i] then | ||
2,233行目: | 2,269行目: | ||
end | end | ||
-- define the hooks that should be called (getProperty, getQualifiers, getReferences); | |||
-- only define a hook if both its command ("propert(y|ies)", "reference(s)", "qualifier(s)") and its parameter ("%p", "%r", "%q1", "%q2", "%q3") have been given | |||
for i, v in pairs(_.states) do | |||
-- define the hooks that should be called (getProperty, getQualifiers, getReferences); | |||
-- only define a hook if both its command ("propert(y|ies)", "reference(s)", "qualifier(s)") and its parameter ("%p", "%r", "%q1", "%q2", "%q3") have been given | |||
for i, v in pairs(_.states) do | |||
-- e.g. 'formatParams["%q1"] or formatParams["%q"]' to define hook even if "%q1" was not defined to be able to build a complete value for "%q" | -- e.g. 'formatParams["%q1"] or formatParams["%q"]' to define hook even if "%q1" was not defined to be able to build a complete value for "%q" | ||
if formatParams[i] or formatParams[i:sub(1, 2)] then | if formatParams[i] or formatParams[i:sub(1, 2)] then | ||
2,306行目: | 2,332行目: | ||
-- then iterate through the claims to collect values | -- then iterate through the claims to collect values | ||
value = _:concatValues(_.states[parameters.property]:iterate(claims, hooks, State.claimMatches)) -- pass property state with level 1 hooks and matchHook | |||
-- if desired, add a clickable icon that may be used to edit the returned values on Wikidata | |||
if _.editable and value ~= "" then | |||
value = value .. _:getEditIcon() | |||
end | |||
return value | |||
else | else | ||
return "" | return "" | ||
2,317行目: | 2,350行目: | ||
end | end | ||
function p._label(args | function p._label(args) | ||
loadSubmodules() | |||
return nameCommand(args, p.commands.label) | |||
end | |||
function p.title(frame) | |||
loadSubmodules(frame) | |||
return p._title(copyTable(frame.args)) | |||
end | |||
function p._title(args) | |||
loadSubmodules() | loadSubmodules() | ||
return nameCommand(args, p.commands.title) | |||
local ID = nil | end | ||
local | |||
function p.alias(frame) | |||
local nextArg = args[1] | loadSubmodules(frame) | ||
local nextIndex = 2 | return p._alias(copyTable(frame.args)) | ||
end | |||
while _:processFlag(nextArg) do | |||
nextArg = args[nextIndex] | function p._alias(args) | ||
nextIndex = nextIndex + 1 | loadSubmodules() | ||
return nameCommand(args, p.commands.alias) | |||
end | |||
function p.aliases(frame) | |||
loadSubmodules(frame) | |||
return p._aliases(copyTable(frame.args)) | |||
end | |||
function p._aliases(args) | |||
loadSubmodules() | |||
return nameCommand(args, p.commands.aliases) | |||
end | |||
function nameCommand(args, funcName) | |||
local _ = Config.new() | |||
_.curState = State.new(_) | |||
local ID = nil | |||
local value = nil | |||
local nextArg = args[1] | |||
local nextIndex = 2 | |||
while _:processFlag(nextArg) do | |||
nextArg = args[nextIndex] | |||
nextIndex = nextIndex + 1 | |||
end | end | ||
2,379行目: | 2,445行目: | ||
end | end | ||
if _.pageTitle | -- serve according to the given command | ||
if funcName == p.commands.label then | |||
value = _:getLabel(ID, _.curState.rawValue, _.curState.linked, _.curState.shortName) | |||
elseif funcName == p.commands.title then | |||
_.pageTitle = true | |||
if not ID then | if not ID then | ||
value = mw.title.getCurrentTitle().prefixedText | |||
elseif ID:sub(1,1) == "Q" then | elseif ID:sub(1,1) == "Q" then | ||
value = mw.wikibase.sitelink(ID) | |||
end | |||
if _.curState.linked and value then | |||
value = buildWikilink(value) | |||
end | |||
elseif funcName == p.commands.alias or funcName == p.commands.aliases then | |||
local aliases, parsedFormat, formatParams, sep | |||
local hooks = {count = 0} | |||
if funcName == p.commands.alias then | |||
_.curState.singleValue = true | |||
end | end | ||
if | -- parse the desired format, or parse the default aliases format | ||
if args["format"] then | |||
parsedFormat, formatParams = parseFormat(args["format"]) | |||
else | |||
parsedFormat, formatParams = parseFormat(formats.alias) | |||
end | end | ||
-- process overridden separator values; | |||
-- must come AFTER parsing the format | |||
-- " | for i, v in pairs(_.separators) do | ||
if args[i] then | |||
sep = replaceSpecialChars(args[i]) | |||
if sep ~= "" then | |||
_.separators[i][1] = {sep} | |||
else | |||
_.separators[i][1] = nil | |||
end | |||
end | |||
end | |||
-- define the hook that should be called (getAlias); | |||
-- only define the hook if the parameter ("%a") has been given | |||
if formatParams[parameters.alias] then | |||
hooks[parameters.alias] = getHookName(parameters.alias, 1) | |||
hooks.count = hooks.count + 1 | |||
end | |||
-- set the parsed format and the separators (and optional punctuation mark) | |||
_.curState.parsedFormat = parsedFormat | |||
_.curState.separator = _.separators["sep"] | |||
_.curState.movSeparator = _.separators["sep"..parameters.separator] | |||
_.curState.puncMark = _.separators["punc"] | |||
_.entity = mw.wikibase.getEntity(ID) | |||
if _.entity and _.entity.aliases then aliases = _.entity.aliases[_.langCode] end | |||
if aliases then | |||
value = _:concatValues(_.curState:iterate(aliases, hooks)) | |||
end | |||
end | end | ||
if _.editable and | value = value or "" | ||
if _.editable and value ~= "" then | |||
-- if desired, add a clickable icon that may be used to edit the returned value on Wikidata | -- if desired, add a clickable icon that may be used to edit the returned value on Wikidata | ||
value = value .. _:getEditIcon() | |||
end | end | ||
return | return value | ||
end | end | ||