Content deleted Content added
Allow for empty flags |
Overhauled module structure; replaced 'single' flag with separate commands (functions), added separate 'reference(s)' command |
||
Line 2:
local aliasesP = {
coord = "P625",
author = "P50",
publisher = "P123",
importedFrom = "P143",
statedIn = "P248",
publicationDate = "P577",
startTime = "P580",
Line 24 ⟶ 25:
prolepticJulianCalendar = "Q1985786"
}
local parameters = {
property = "%p",
qualifier = "%q",
reference = "%r",
separator = "%s"
}
local formats = {
property = "%p[%s][%r]",
qualifier = "%q[%s][%r]",
reference = "%r",
propertyWithQualifier = "%p[ <span style=\"font-size:smaller\">(%q)</span>][%s][%r]"
}
local hookNames = {
-- {level_1, level_2}
[parameters.property] = {"getProperty"},
[parameters.qualifier] = {"getQualifiers", "getQualifier"},
[parameters.reference] = {"getReferences", "getReference"}
}
local Config = {}
Config.__index = Config
-- allows for recursive calls
function Config.new()
local cfg = {}
setmetatable(cfg, Config)
cfg.separators = {
-- use tables so that we can pass by reference
["sep"] = {" "},
["sep%s"] = {","},
["sep%q"] = {", "},
["sep%r"] = {""},
["punc"] = {""}
}
cfg.entity = nil
cfg.propertyID = nil
cfg.propertyValue = nil
cfg.qualifierID = nil
cfg.bestRank = true
cfg.foundRank = 3
cfg.maxRank = nil
cfg.minRank = nil
cfg:setRankBoundaries("best")
cfg.period = 0
cfg.mdyDate = false
cfg.pageTitle = false
cfg.langCode = mw.language.getContentLanguage().code
cfg.langName = mw.language.fetchLanguageName(cfg.langCode, cfg.langCode)
cfg.langObj = mw.language.new(cfg.langCode)
cfg.states = {}
cfg.curState = nil
return cfg
end
local State = {}
State.__index = State
function State.new(cfg)
local stt = {}
setmetatable(stt, State)
stt.conf = cfg
stt.outPreferred = {}
Line 37 ⟶ 103:
stt.outDeprecated = {}
stt.
stt.
stt.
stt.
stt.linked = false
Line 51 ⟶ 112:
stt.shortName = false
stt.singleValue = false
return stt
end
function
return "<strong class=\"error\">Unknown or unsupported datatype '" ..
end
function missingRequiredParameterError()
return "<strong class=\"error\">No required parameters defined, needing at least one.</strong>"
end
function extraRequiredParameterError(param)
return "<strong class=\"error\">Parameter '" .. param .. "' must be defined as optional.</strong>"
end
function
local i, j
Line 81 ⟶ 142:
end
function
precision = precision or "d"
local i, j, index, ptr
Line 149 ⟶ 210:
end
function
link = link or false
local itemID, label, title
Line 157 ⟶ 218:
end
itemID =
if itemID then
Line 187 ⟶ 248:
end
function
if tostring(num):sub(-2,-2) == '1' then
return "th" -- 10th, 11th, 12th, 13th, ... 19th
Line 213 ⟶ 266:
end
function
local left,num,right = string.match(n,'^([^%d]*%d)(%d*)(.-)$')
return left..(num:reverse():gsub('(%d%d%d)','%1,'):reverse())..right
end
function
if (rank == "preferred") then
return 1
elseif (rank == "normal") then
return 2
elseif (rank == "deprecated") then
return 3
else
return 4 -- default (in its literal sense)
end
end
function datePrecedesDate(aY, aM, aD, bY, bM, bD)
if aY == nil or bY == nil then
return nil
end
aM = aM or 1
aD = aD or 1
bM = bM or 1
bD = bD or 1
if aY < bY then
return true
end
if aY > bY then
return false
end
if aM < bM then
return true
end
if aM > bM then
return false
end
if aD < bD then
return true
end
return false
end
function alwaysTrue()
return true
end
function parseFormat(str)
local chr, esc, param, root, cur
local params = {}
local function newObject(array)
local obj = {} -- new object
obj.str = ""
array[#array + 1] = obj -- array{object}
obj.parent = array
return obj
end
root = {} -- array
root.req = {}
cur = newObject(root)
esc = false
param = false
for i = 1, #str do
chr = str:sub(i,i)
if not esc then
if chr == '\\' then
esc = true
elseif chr == '%' then
cur = newObject(cur.parent)
param = true
else
if chr == '[' then
cur.child = {} -- new array
cur.child.req = {}
cur.child.parent = cur
cur = newObject(cur.child)
elseif chr == ']' then
if cur.parent.parent then
cur = newObject(cur.parent.parent.parent)
end
else
cur.str = cur.str .. chr
if param then
cur.str = "%"..cur.str
cur.param = true
params[cur.str] = true
cur.parent.req[cur.str] = true
cur = newObject(cur.parent)
end
end
param = false
end
else
cur.str = cur.str .. chr
esc = false
if param then
cur.str = "%"..cur.str
cur.param = true
params[cur.str] = true
cur.parent.req[cur.str] = true
cur = newObject(cur.parent)
param = false
end
end
end
return root, params
end
function getShortName(itemID)
return p._property({itemID, aliasesP.shortName}) -- "property" is single
end
function getLabel(ID)
return p._label({ID})
end
function Config:getValue(snak, raw, link, short, anyLang)
raw = raw or false
link = link or false
short = short or false
anyLang = anyLang or false
Line 239 ⟶ 421:
if not raw then
value =
local unit =
if unit then
value = value .. unit
Line 265 ⟶ 447:
end
y, m, d =
if y < 0 then
Line 291 ⟶ 473:
end
suffix =
else
-- if not verbose, take the first year of the century/millennium
Line 390 ⟶ 572:
if mayAddCalendar then
calendarID =
if calendarID and calendarID == aliasesQ.prolepticJulianCalendar then
Line 528 ⟶ 710:
if link then
globe =
if globe then
Line 553 ⟶ 735:
end
if
value =
end
Line 577 ⟶ 759:
return value
else
return
end
elseif snak.snaktype == 'somevalue' then
Line 587 ⟶ 769:
elseif snak.snaktype == 'novalue' then
if raw then
return "" -- empty
else
return "none"
Line 596 ⟶ 778:
end
function
local qualifiers
Line 608 ⟶ 790:
end
function
local snakValue = self:getValue(snak, true) -- raw = true
Line 616 ⟶ 798:
end
function
local rankPos
self.foundRank = 3 -- must equal the lowest possible rank
if (rank == "best") then
self.bestRank = true
return
else
Line 647 ⟶ 830:
end
function
if
self.curState.linked = true
return true
elseif flag == "raw" then
self.curState.rawValue = true
if self.curState == self.states[parameters.reference] then
-- raw reference values end with periods and require a separator different from ""
self.separators["sep%r"][1] = " "
end
return true
elseif flag == "short" then
self.curState.shortName = true
return true
elseif flag == "mdy" then
self.mdyDate = true
return true
elseif flag == "best" or flag:match('^preferred[+-]?$') or flag:match('^normal[+-]?$') or flag:match('^deprecated[+-]?$') then
self:setRankBoundaries(flag)
return true
elseif flag == "future" then
self.period = 1
return true
elseif flag == "current" then
self.period = 2
return true
elseif flag == "former" then
self.period = 3
return true
elseif flag == "" then
-- ignore empty flags and carry on
return true
else
return false
end
end
function Config:processFlagOrCommand(flag)
local param = ""
if
param = parameters.property
elseif flag:match('^qualifier[s]?$') then
param = parameters.qualifier
elseif flag:match('^reference[s]?$') then
param = parameters.reference
else
return self:processFlag(flag)
end
if
return false
end
-- create a new State for each command
self.states[param] = State.new(self)
-- use "%x" as the general parameter name
self.states[param].parsedFormat = parseFormat("%x") -- will be overwritten for param=="%p"
-- set the separator
self.states[param].separator = self.separators["sep"..param] -- will be nil for param=="%p", which will be set separately
if string.sub(flag, -1) ~= 's' then
self.states[param].singleValue = true
end
self.curState = self.states[param]
return true
end
function Config:rankMatches(rankPos)
if self.bestRank then
return self.foundRank >= rankPos
else
return (self.maxRank <= rankPos and rankPos <= self.minRank)
end
end
function
local startTime = nil
local startTimeY = nil
Line 730 ⟶ 931:
startTime = self:getSingleRawQualifier(claim, aliasesP.startTime)
if startTime and startTime ~= "" and startTime ~= " " then
startTimeY, startTimeM, startTimeD =
end
endTime = self:getSingleRawQualifier(claim, aliasesP.endTime)
if endTime and endTime ~= "" and endTime ~= " " then
endTimeY, endTimeM, endTimeD =
elseif endTime == " " then
-- end time is 'unknown', assume it is somewhere in the past;
Line 744 ⟶ 945:
end
if startTimeY ~= nil and endTimeY ~= nil and
-- invalidate end time if it precedes start time
endTimeY = nil
Line 753 ⟶ 954:
if self.period == 1 then
-- future
if startTimeY == nil or not
return false
else
Line 760 ⟶ 961:
elseif self.period == 2 then
-- current
if (startTimeY ~= nil and
(endTimeY ~= nil and not
return false
else
Line 768 ⟶ 969:
elseif self.period == 3 then
-- former
if endTimeY == nil or
return false
else
Line 776 ⟶ 977:
end
function State:
local matches, rankPos
-- if a property value was given, check if it matches the claim's property value
if self.conf.propertyValue then
matches = self.conf:snakEqualsValue(claim.mainsnak, self.conf.propertyValue)
else
matches = true
end
-- check if the claim's rank and time period match
rankPos = convertRank(claim.rank)
matches = (matches and self.conf:rankMatches(rankPos) and self.conf:timeMatches(claim))
return matches, rankPos
end
function State:appendOutput(result, rankPos)
local done = false
-- a rankPos should only apply to complete claims, not to its individual qualifiers or references;
-- for the latter two, no rankPos should be given and their default rankPos must be the highest possible (i.e. 1)
if rankPos then
if (self.conf.bestRank or self.singleValue) and self.conf.foundRank > rankPos then
self.conf.foundRank = rankPos
-- found a better rank, reset worse rank outputs
if self.conf.foundRank == 1 then
self.outNormal = {}
self.outDeprecated = {}
elseif self.conf.foundRank == 2 then
self.outDeprecated = {}
end
end
else
rankPos = 1
end
if rankPos == 1 then
self.outPreferred[#self.outPreferred + 1] =
if self.singleValue then
Line 786 ⟶ 1,022:
end
elseif rankPos == 2 then
self.outNormal[#self.outNormal + 1] =
if self.singleValue and not self.conf.bestRank and self.conf.maxRank == 2 then
done = true
end
elseif rankPos == 3 then
self.outDeprecated[#self.outDeprecated + 1] =
if self.singleValue and not self.conf.bestRank and self.conf.maxRank == 3 then
done = true
end
Line 805 ⟶ 1,041:
local out = ""
local function walk(formatTable, result)
local str = ""
for i, v in pairs(formatTable.req) do
if not result[i] then
-- we've got no result for a parameter that is required on this level,
-- so skip this level (and its children) by returning an empty string
return ""
end
end
for i, v in ipairs(formatTable) do
str = str .. result[v.str]
else
str = str .. v.str
end
if v.child then
str = str .. walk(v.child, result)
end
end
return str
end
local function prepend(results)
local sep = ""
local result, value
-- iterate from back to front, so that we know when to add separators
for i = #results, 1, -1 do
result = results[i]
-- if there is already some output, then add the separators
if out ~= "" then
result[parameters.separator] = self.movSeparator[1] -- movable separator
else
sep = ""
result[parameters.separator] = self.puncMark[1] -- optional punctuation mark
end
if value ~= "" then
out = value .. sep .. out
end
end
end
prepend(self.outDeprecated)
prepend(self.outNormal)
prepend(self.outPreferred)
-- reset state before next iteration
self.outDeprecated = {}
self.outNormal = {}
self.outPreferred = {}
return out
end
-- level 1 hook
function State:getProperty(claim)
return self.conf:getValue(claim.mainsnak, self.rawValue, self.linked, self.shortName)
end
-- level 1 hook
function State:getQualifiers(claim)
local qualifiers
if claim.qualifiers then qualifiers = claim.qualifiers[self.conf.qualifierID] end
if qualifiers then
-- iterate through claim's qualifier statements to collect their values
return self.conf.states[parameters.qualifier]:iterate(qualifiers, {["%x"] = hookNames[parameters.qualifier][2], count = 1}) -- pass qualifier State with level 2 hook
else
return nil
end
end
-- level 2 hook
function State:getQualifier(snak)
return self.conf:getValue(snak, self.rawValue, self.linked, self.shortName)
end
-- level 1 hook
function State:getReferences(claim)
if claim.references then
-- iterate through claim's reference statements to collect their values
return self.conf.states[parameters.reference]:iterate(claim.references, {["%x"] = hookNames[parameters.reference][2], count = 1}) -- pass reference State with level 2 hook
else
return nil
end
end
-- level 2 hook
-- logic determined based on https://www.wikidata.org/wiki/Help:Sources
function State:getReference(statement)
local snakValue, lang, property
local value = ""
local snaks = {}
local params = {}
local leadParams = {}
if
for i, v in pairs(statement.snaks) do
if v[1] then
snaks[i] = v[1]
end
end
if snaks[aliasesP.importedFrom] then
snaks[aliasesP.importedFrom] = nil
end
if snaks[aliasesP.referenceURL] and snaks[aliasesP.title] then
params["url"] = self.conf:getValue(snaks[aliasesP.referenceURL])
params["title"] = self.conf:getValue(snaks[aliasesP.title], false, false, false, true) -- anyLang = true
if snaks[aliasesP.publicationDate] then params["date"] = self.conf:getValue(snaks[aliasesP.publicationDate]) end
if snaks[aliasesP.retrieved] then params["access-date"] = self.conf:getValue(snaks[aliasesP.retrieved]) end
if snaks[aliasesP.archiveURL] then params["archive-url"] = self.conf:getValue(snaks[aliasesP.archiveURL]) end
if snaks[aliasesP.archiveDate] then params["archive-date"] = self.conf:getValue(snaks[aliasesP.archiveDate]) end
if snaks[aliasesP.author] then params["author"] = self.conf:getValue(snaks[aliasesP.author]) end
if snaks[aliasesP.publisher] then params["publisher"] = self.conf:getValue(snaks[aliasesP.publisher]) end
if snaks[aliasesP.quote] then params["quote"] = self.conf:getValue(snaks[aliasesP.quote], false, false, false, true) end -- anyLang = true
if snaks[aliasesP.
if self.conf.langName ~= snakValue then
params["language"] = snakValue
end
end
value = mw.getCurrentFrame():expandTemplate{title="cite_web", args=params}
else
for i, v in pairs(snaks) do
property = getLabel(i)
if
snakValue, lang = self.conf:getValue(
if
snakValue = "''" .. snakValue .. "'' (" .. mw.language.fetchLanguageName(lang, self.conf.langCode) .. ")"
end
if
leadParams[#leadParams + 1] = snakValue
elseif i ~= aliasesP.language or self.conf.langName ~= snakValue then
params[#params + 1] = property .. ": " .. snakValue
end
end
params = table.concat(params, "; ")
if params ~= "" then end
value = value .. params
end
if
value = value ..
end
end
if value ~= "" then
if not self.rawValue then
-- add <ref> tags with the reference's hash as its name (to deduplicate references)
value = mw.getCurrentFrame():extensionTag("ref", value, {name = statement.hash})
end
else
value = nil
end
end
Line 930 ⟶ 1,225:
end
-- iterate through claims, claim's qualifiers or claim's references to collect values
function State:iterate(statements, hooks, matchHook)
matchHook = matchHook or alwaysTrue
local done = false
local
local
local result, numValues, doAppend, gotRequired
for i, v in ipairs(statements) do
-- rankPos will be nil for non-claim statements (e.g. qualifiers, references, etc.),
-- but let appendOutput handle that
matches, rankPos = matchHook(self, v)
if matches then
result = {count = 0}
doAppend = true
-- if we need to return a single value, check if we don't have one already
if self.singleValue then
if not rankPos or rankPos == 1 then
numValues = #self.outPreferred
elseif rankPos == 2 then
numValues = #self.outNormal
elseif rankPos == 3 then
numValues = #self.outDeprecated
end
if numValues > 0 then
doAppend = false
end
end
if doAppend then
local function walk(formatTable)
for i2, v2 in pairs(formatTable.req) do
if not result[i2] and hooks[i2] then
-- call a hook and add its return value to the result
value = self[hooks[i2]](self, v)
if value then
result[i2] = value
result.count = result.count + 1
else
return false -- we miss a required value for this level
end
end
if result.count == hooks.count then
-- we're done if all hooks have been called;
-- returning at this point breaks the loop
return true
end
end
for i2, v2 in ipairs(formatTable) do
if result.count == hooks.count then
-- we're done if all hooks have been called;
-- returning at this point prevents further childs from being processed
return true
end
if v2.child then
walk(v2.child)
end
end
return true
end
gotRequired = walk(self.parsedFormat)
-- only append the result if we got values for all required parameters on the root level
if gotRequired then
done = self:appendOutput(result, rankPos)
if done then
break
Line 1,016 ⟶ 1,307:
end
end
end
return self:out()
end
function p.property(frame)
return p._property(frame.args)
end
function p._property(args)
return execCommand(args, "property")
end
function p.properties(frame)
return p._properties(frame.args)
end
function p._properties(args)
return execCommand(args, "properties")
end
Line 1,026 ⟶ 1,332:
end
function p._qualifier(args
return execCommand(args, "qualifier")
end
function p.qualifiers(frame)
return p._qualifiers(frame.args)
end
function p._qualifiers(args)
return execCommand(args, "qualifiers")
end
function p.reference(frame)
return p._reference(frame.args)
end
function p._reference(args)
return execCommand(args, "reference")
end
function p.references(frame)
return p._references(frame.args)
end
function p._references(args)
return execCommand(args, "references")
end
function execCommand(args, funcName)
_ = Config.new()
_:processFlagOrCommand(funcName) -- process first command (== function name)
local parsedFormat, formatParams, claims
local
local nextArg = mw.text.trim(args[1] or "")
local nextIndex = 2
-- process flags and commands
while _:processFlagOrCommand(nextArg) do
nextArg = mw.text.trim(args[nextIndex] or "")
nextIndex = nextIndex + 1
end
-- check for optional item ID
if nextArg:sub(1,1):upper() == "Q" then
_.entity = mw.wikibase.getEntity(nextArg) -- item ID given
_.propertyID = mw.text.trim(args[nextIndex] or "") -- property ID
nextIndex = nextIndex + 1
else
_.entity = mw.wikibase.getEntity() -- no item ID given, use item connected to current page
_.propertyID = nextArg -- property ID
end
-- check if given property ID is an alias
_.propertyID = aliasesP[_.propertyID]
end
_.propertyID = _.propertyID:upper()
if _.states[parameters.qualifier] then
-- do further processing if "qualifier(s)" command was given
nextArg = args[nextIndex]
nextIndex = nextIndex + 1
_.qualifierID = nextArg
nextArg = mw.text.trim(args[nextIndex] or "")
nextIndex = nextIndex + 1
if nextArg == "" then
-- claim ID or literal value has NOT been given
_.propertyValue = nil
_.qualifierID = mw.text.trim(_.qualifierID or "")
else
-- claim ID or literal value has been given
_.propertyValue = _.qualifierID -- cannot be nil when reached
_.qualifierID = nextArg
end
-- check if given qualifier ID is an alias
if aliasesP[_.qualifierID] then
_.qualifierID = aliasesP[_.qualifierID]
end
_.qualifierID = _.qualifierID:upper()
elseif _.states[parameters.reference] then
-- do further processing if "reference(s)" command was given
nextArg = args[nextIndex]
nextIndex = nextIndex + 1
_.propertyValue = nextArg -- claim ID or literal value (possibly nil)
end
-- check for special property value 'somevalue' or 'novalue'
if _.propertyValue then
if _.propertyValue ~= "" and mw.text.trim(_.propertyValue) == "" then
_.propertyValue = " " -- single space represents 'somevalue', whereas empty string represents 'novalue'
else
_.propertyValue = mw.text.trim(_.propertyValue)
end
end
-- parse the desired format, or choose an appropriate format
if args["format"] then
parsedFormat, formatParams = parseFormat(mw.text.trim(args["format"]))
elseif _.states[parameters.qualifier] then
if _.states[parameters.property] then
parsedFormat, formatParams = parseFormat(formats.propertyWithQualifier)
else
parsedFormat, formatParams = parseFormat(formats.qualifier)
end
elseif _.states[parameters.property] then
parsedFormat, formatParams = parseFormat(formats.property)
else
parsedFormat, formatParams = parseFormat(formats.reference)
-- if only "reference(s)" has been given, make the emtpy string the default separator (except when raw)
if not _.states[parameters.reference].rawValue then
_.separators["sep"][1] = ""
end
end
-- process overridden separator values;
-- must come AFTER parsing the formats
for i, v in pairs(_.separators) do
if args[i] then
_.separators[i][1] = args[i]
end
end
-- make sure that at least one required parameter has been defined
if not next(parsedFormat.req) then
return missingRequiredParameterError()
end
-- make sure that the separator parameter "%s" is not amongst the required parameters
if parsedFormat.req[parameters.separator] then
return extraRequiredParameterError(parameters.separator)
end
-- define the hooks that should be called (getProperty, getQualifiers, getReferences);
-- only define a hook if both its command ("propert(y|ies)", "qualifier(s)", "reference(s)") and its parameter ("%p", "%q", "%r") have been given
for i, v in pairs(_.states) do
if formatParams[i] then
hooks[i] = hookNames[i][1]
hooks.count = hooks.count + 1
end
end
-- create a state for "properties" if it doesn't exist yet, which will be used as a base configuration for each claim iteration;
-- must come AFTER defining the hooks
if not _.states[parameters.property] then
_.states[parameters.property] = State.new(_)
end
-- set the parsed format and the separators (and optional punctuation mark)
_.states[parameters.property].parsedFormat = parsedFormat
_.states[parameters.property].separator = _.separators["sep"]
_.states[parameters.property].movSeparator = _.separators["sep"..parameters.separator]
_.states[parameters.property].puncMark = _.separators["punc"]
if _.entity and _.entity.claims then claims = _.entity.claims[_.propertyID] end
if claims then
-- iterate through the claims to collect values
return _.states[parameters.property]:iterate(claims, hooks, State.claimMatches) -- pass property State with level 1 hooks and matchHook
else
return ""
end
end
Line 1,159 ⟶ 1,511:
function p._label(args, _)
_ = _ or
_.curState = State.new(_)
local ID
Line 1,187 ⟶ 1,540:
end
if _.curState.rawValue then
if mw.wikibase.getEntity(ID) or mw.wikibase.resolvePropertyId(ID) then
if _.curState.linked then
if ID:sub(1,1) == "P" then
label = "[[d:Property:" .. ID .. "|" .. ID .. "]]"
Line 1,204 ⟶ 1,557:
label = mw.wikibase.label(ID) or ""
if _.curState.linked and label ~= "" then
label = "[[d:Property:" .. ID .. "|" .. label .. "]]"
end
Line 1,210 ⟶ 1,563:
else
if not _.pageTitle then
if _.curState.shortName then
label =
end
Line 1,226 ⟶ 1,579:
-- at this point, 'label' will be nil or a non-empty string
if _.curState.linked or label == nil then
title = mw.wikibase.sitelink(ID)
end
if _.curState.linked and title then
label = "[[" .. title .. "|" .. (label or title) .. "]]"
else
Line 1,238 ⟶ 1,591:
end
else
if _.curState.rawValue then
label = mw.wikibase.getEntityIdForCurrentPage() or ""
if _.curState.linked and label ~= "" then
label = "[[d:" .. label .. "|" .. label .. "]]"
end
Line 1,252 ⟶ 1,605:
end
if _.curState.linked or label == nil then
title = mw.title.getCurrentTitle().prefixedText
end
if _.curState.linked then
label = "[[" .. title .. "|" .. (label or title) .. "]]" -- not much use since it links to the current page, but does add wiki mark-up
else
Line 1,272 ⟶ 1,625:
function p._title(args, _)
_ = _ or
_.pageTitle = true
return p._label(args, _)
|