「モジュール:Wd」の版間の差分

提供:関南・東山地方Wiki
ナビゲーションに移動 検索に移動
template>K-iczn
(en:Module:Wd 2016年12月11日 (日) 14:14‎(UTC)より)
template>K-iczn
(en:Module:Wd 2017年1月12日 (木) 22:42(UTC)より)
2行目: 2行目:


local aliasesP = {
local aliasesP = {
coord          = "P625",
author          = "P50",
author          = "P50",
publisher      = "P123",
publisher      = "P123",
importedFrom    = "P143",
importedFrom    = "P143",
statedIn        = "P248",
statedIn        = "P248",
coord          = "P625",
publicationDate = "P577",
publicationDate = "P577",
startTime      = "P580",
startTime      = "P580",
24行目: 24行目:
prolepticJulianCalendar = "Q1985786"
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 = {}
local State = {}
State.__index = State
State.__index = State


-- allows for recursive calls
function State.new(cfg)
function State.new()
local stt = {}
local stt = {}
setmetatable(stt, State)
setmetatable(stt, State)
stt.conf = cfg
stt.outPreferred = {}
stt.outPreferred = {}
37行目: 102行目:
stt.outDeprecated = {}
stt.outDeprecated = {}
stt.bestRank = true
stt.parsedFormat = {}
stt.foundRank = 3
stt.separator = {}
stt.maxRank = nil
stt.movSeparator = {}
stt.minRank = nil
stt.puncMark = {}
stt.period = 0
stt.propertyWithQualifier = false
stt.pageTitle = false
stt.linked = false
stt.linked = false
51行目: 111行目:
stt.shortName = false
stt.shortName = false
stt.singleValue = false
stt.singleValue = false
stt.mdyDate = false
stt.withRefs = false
stt.langCode = mw.language.getContentLanguage().code
stt.langName = mw.language.fetchLanguageName(stt.langCode, stt.langCode)
stt.langObj = mw.language.new(stt.langCode)
stt:setRankBoundaries("best")
return stt
return stt
end
end


function State:unknownDatatypeError(type)
function unknownDataTypeError(dataType)
return "<strong class=\"error\">'" .. type .. "'は不明もしくは未対応のデータタイプです。</strong>"
return "<strong class=\"error\">'" .. type .. "'は不明もしくは未対応のデータタイプです。</strong>"
end
end


function State:parseWikidataURL(url)
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 parseWikidataURL(url)
local i, j
local i, j
81行目: 141行目:
end
end


function State:parseDate(dateStr, precision)
function parseDate(dateStr, precision)
precision = precision or "d"
precision = precision or "d"
local i, j, index, ptr
local i, j, index, ptr
149行目: 209行目:
end
end


function State:convertUnit(unit, link)
function convertUnit(unit, link)
link = link or false
link = link or false
local itemID, label, title
local itemID, label, title
157行目: 217行目:
end
end
itemID = self:parseWikidataURL(unit)
itemID = parseWikidataURL(unit)
if itemID then
if itemID then
187行目: 247行目:
end
end


function State:getShortName(itemID)
function getOrdinalSuffix(num)
return p._property({"single", itemID, aliasesP.shortName})
end
 
function State:getLabel(ID)
return p._label({ID})
end
 
function State:getOrdinalSuffix(num)
if tostring(num):sub(-2,-2) == '1' then
if tostring(num):sub(-2,-2) == '1' then
return "th"  -- 10th, 11th, 12th, 13th, ... 19th
return "th"  -- 10th, 11th, 12th, 13th, ... 19th
213行目: 265行目:
end
end


function State:addDecimalMarks(n)
function addDecimalMarks(n)
local left,num,right = string.match(n,'^([^%d]*%d)(%d*)(.-)$')
local left,num,right = string.match(n,'^([^%d]*%d)(%d*)(.-)$')
return left..(num:reverse():gsub('(%d%d%d)','%1,'):reverse())..right
return left..(num:reverse():gsub('(%d%d%d)','%1,'):reverse())..right
end
end


function State:getValue(snak, raw, link, anyLang)
function convertRank(rank)
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
raw = raw or false
link = link or false
link = link or false
short = short or false
anyLang = anyLang or false
anyLang = anyLang or false
239行目: 420行目:
if not raw then
if not raw then
value = self:addDecimalMarks(value)
value = addDecimalMarks(value)
local unit = self:convertUnit(snak.datavalue.value['unit'], link)
local unit = convertUnit(snak.datavalue.value['unit'], link)
if unit then
if unit then
value = value .. unit
value = value .. unit
265行目: 446行目:
end
end
y, m, d = self:parseDate(snak.datavalue.value['time'], p)
y, m, d = parseDate(snak.datavalue.value['time'], p)
if y < 0 then
if y < 0 then
291行目: 472行目:
end
end
suffix = self:getOrdinalSuffix(yRound) .. suffix
suffix = getOrdinalSuffix(yRound) .. suffix
else
else
-- if not verbose, take the first year of the century/millennium
-- if not verbose, take the first year of the century/millennium
367行目: 548行目:
end
end
if addUnit then
if not raw then
if precision == 3 then
if precision == 3 then
suffix = "百万年"
suffix = "百万年"
390行目: 571行目:
if mayAddCalendar then
if mayAddCalendar then
calendarID = self:parseWikidataURL(snak.datavalue.value['calendarmodel'])
calendarID = parseWikidataURL(snak.datavalue.value['calendarmodel'])
if calendarID and calendarID == aliasesQ.prolepticJulianCalendar then
if calendarID and calendarID == aliasesQ.prolepticJulianCalendar then
if addUnit then
if not raw then
if addLink then
if link then
calendar = " ([[ユリウス暦]])"
calendar = " ([[ユリウス暦]])"
else
else
528行目: 709行目:
if link then
if link then
globe = self:parseWikidataURL(snak.datavalue.value['globe'])
globe = parseWikidataURL(snak.datavalue.value['globe'])
if globe then
if globe then
553行目: 734行目:
end
end
if self.shortName then
if short then
value = self:getShortName(itemID)
value = getShortName(itemID)
end
end
577行目: 758行目:
return value
return value
else
else
return self:unknownDatatypeError(snak.datavalue.type)
return unknownDataTypeError(snak.datavalue.type)
end
end
elseif snak.snaktype == 'somevalue' then
elseif snak.snaktype == 'somevalue' then
587行目: 768行目:
elseif snak.snaktype == 'novalue' then
elseif snak.snaktype == 'novalue' then
if raw then
if raw then
return ""  -- empty value represents 'novalue'
return ""  -- empty string represents 'novalue'
else
else
return "none"
return "none"
596行目: 777行目:
end
end


function State:getSingleRawQualifier(claim, qualifierID)
function Config:getSingleRawQualifier(claim, qualifierID)
local qualifiers
local qualifiers
608行目: 789行目:
end
end


function State:snakEqualsValue(snak, value)
function Config:snakEqualsValue(snak, value)
local snakValue = self:getValue(snak, true)  -- raw = true
local snakValue = self:getValue(snak, true)  -- raw = true
616行目: 797行目:
end
end


function State:setRankBoundaries(rank)
function Config:setRankBoundaries(rank)
local rankPos
local rankPos
self.foundRank = 3  -- must equal the lowest possible rank
if (rank == "best") then
if (rank == "best") then
self.bestRank = true
self.bestRank = true
self.foundRank = 3
return
return
else
else
647行目: 829行目:
end
end


function State:convertRank(rank)
function Config:processFlag(flag)
if (rank == "preferred") then
if flag == "linked" then
return 1
self.curState.linked = true
elseif (rank == "normal") then
return true
return 2
elseif flag == "raw" then
elseif (rank == "deprecated") then
self.curState.rawValue = true
return 3
else
if self.curState == self.states[parameters.reference] then
return 4  -- default (in its literal sense)
-- raw reference values end with periods and require a separator different from ""
end
self.separators["sep%r"][1] = " "
end
 
function State:rankMatches(rankPos)
if self.bestRank then
if self.foundRank > rankPos then
self.foundRank = rankPos
-- found a better rank, reset worse rank outputs
if self.foundRank == 1 then
self.outNormal = {}
self.outDeprecated = {}
elseif self.foundRank == 2 then
self.outDeprecated = {}
end
end
end
return self.foundRank >= rankPos  -- == would also work here
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
else
return (self.maxRank <= rankPos and rankPos <= self.minRank)
return false
end
end
end
end


function State:datePrecedesDate(aY, aM, aD, bY, bM, bD)
function Config:processFlagOrCommand(flag)
if aY == nil or bY == nil then
local param = ""
return nil
end
aM = aM or 1
aD = aD or 1
bM = bM or 1
bD = bD or 1
if aY < bY then
if flag == "property" or flag == "properties" then
return true
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
end
if aY > bY then
if self.states[param] then
return false
return false
end
end
if aM < bM then
-- create a new State for each command
return true
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
end
if aM > bM then
self.curState = self.states[param]
return false
end
if aD < bD then
return true
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
return false
end
end


function State:timeMatches(claim)
function Config:timeMatches(claim)
local startTime = nil
local startTime = nil
local startTimeY = nil
local startTimeY = nil
730行目: 930行目:
startTime = self:getSingleRawQualifier(claim, aliasesP.startTime)
startTime = self:getSingleRawQualifier(claim, aliasesP.startTime)
if startTime and startTime ~= "" and startTime ~= " " then
if startTime and startTime ~= "" and startTime ~= " " then
startTimeY, startTimeM, startTimeD = self:parseDate(startTime)
startTimeY, startTimeM, startTimeD = parseDate(startTime)
end
end
endTime = self:getSingleRawQualifier(claim, aliasesP.endTime)
endTime = self:getSingleRawQualifier(claim, aliasesP.endTime)
if endTime and endTime ~= "" and endTime ~= " " then
if endTime and endTime ~= "" and endTime ~= " " then
endTimeY, endTimeM, endTimeD = self:parseDate(endTime)
endTimeY, endTimeM, endTimeD = parseDate(endTime)
elseif endTime == " " then
elseif endTime == " " then
-- end time is 'unknown', assume it is somewhere in the past;
-- end time is 'unknown', assume it is somewhere in the past;
744行目: 944行目:
end
end
if startTimeY ~= nil and endTimeY ~= nil and self:datePrecedesDate(endTimeY, endTimeM, endTimeD, startTimeY, startTimeM, startTimeD) then
if startTimeY ~= nil and endTimeY ~= nil and datePrecedesDate(endTimeY, endTimeM, endTimeD, startTimeY, startTimeM, startTimeD) then
-- invalidate end time if it precedes start time
-- invalidate end time if it precedes start time
endTimeY = nil
endTimeY = nil
753行目: 953行目:
if self.period == 1 then
if self.period == 1 then
-- future
-- future
if startTimeY == nil or not self:datePrecedesDate(now['year'], now['month'], now['day'], startTimeY, startTimeM, startTimeD) then
if startTimeY == nil or not datePrecedesDate(now['year'], now['month'], now['day'], startTimeY, startTimeM, startTimeD) then
return false
return false
else
else
760行目: 960行目:
elseif self.period == 2 then
elseif self.period == 2 then
-- current
-- current
if (startTimeY ~= nil and self:datePrecedesDate(now['year'], now['month'], now['day'], startTimeY, startTimeM, startTimeD)) or
if (startTimeY ~= nil and datePrecedesDate(now['year'], now['month'], now['day'], startTimeY, startTimeM, startTimeD)) or
  (endTimeY ~= nil and not self:datePrecedesDate(now['year'], now['month'], now['day'], endTimeY, endTimeM, endTimeD)) then
  (endTimeY ~= nil and not datePrecedesDate(now['year'], now['month'], now['day'], endTimeY, endTimeM, endTimeD)) then
    return false
    return false
else
else
768行目: 968行目:
elseif self.period == 3 then
elseif self.period == 3 then
-- former
-- former
if endTimeY == nil or self:datePrecedesDate(now['year'], now['month'], now['day'], endTimeY, endTimeM, endTimeD) then
if endTimeY == nil or datePrecedesDate(now['year'], now['month'], now['day'], endTimeY, endTimeM, endTimeD) then
return false
return false
else
else
776行目: 976行目:
end
end


function State:appendOutput(value, rankPos)
function State:claimMatches(claim)
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
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
if rankPos == 1 then
self.outPreferred[#self.outPreferred + 1] = value
self.outPreferred[#self.outPreferred + 1] = result
if self.singleValue then
if self.singleValue then
786行目: 1,021行目:
end
end
elseif rankPos == 2 then
elseif rankPos == 2 then
self.outNormal[#self.outNormal + 1] = value
self.outNormal[#self.outNormal + 1] = result
if self.singleValue and not self.bestRank and self.maxRank == 2 then
if self.singleValue and not self.conf.bestRank and self.conf.maxRank == 2 then
done = true
done = true
end
end
elseif rankPos == 3 then
elseif rankPos == 3 then
self.outDeprecated[#self.outDeprecated + 1] = value
self.outDeprecated[#self.outDeprecated + 1] = result
if self.singleValue and not self.bestRank and self.maxRank == 3 then
if self.singleValue and not self.conf.bestRank and self.conf.maxRank == 3 then
done = true
done = true
end
end
805行目: 1,040行目:
local out = ""
local out = ""
if self.outDeprecated[1] then
local function walk(formatTable, result)
if self.singleValue then
local str = ""
out = self.outDeprecated[1]
else
for i, v in pairs(formatTable.req) do
out = table.concat(self.outDeprecated, ", ")
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
end
end
for i, v in ipairs(formatTable) do
if self.outNormal[1] then
if v.param then
if self.singleValue then
str = str .. result[v.str]
out = self.outNormal[1]
else
else
str = str .. v.str
if out ~= "" then
out = "; " .. out
end
end
out = table.concat(self.outNormal, ", ") .. out
if v.child then
str = str .. walk(v.child, result)
end
end
end
return str
end
end
if self.outPreferred[1] then
local function prepend(results)
if self.singleValue then
local sep = ""
out = self.outPreferred[1]
local result, value
else
-- 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
if out ~= "" then
out = "; " .. out
sep = self.separator[1]  -- fixed separator
result[parameters.separator] = self.movSeparator[1]  -- movable separator
else
sep = ""
result[parameters.separator] = self.puncMark[1]  -- optional punctuation mark
end
end
out = table.concat(self.outPreferred, ", ") .. out
value = walk(self.parsedFormat, result)
if value ~= "" then
out = value .. sep .. out
end
end
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
return out
end
end


-- logic based on https://www.wikidata.org/wiki/Help:Sources
-- 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)
function State:getReferences(claim)
local snaks, snakValue, lang, params, leadParams, property, ref
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 value = ""
local snaks = {}
local params = {}
local leadParams = {}
if not claim.references then
if statement.snaks then
return ""
for i, v in pairs(statement.snaks) do
end
if v[1] then
snaks[i] = v[1]
for i, v in ipairs(claim.references) do
end
if v.snaks then
end
ref = ""
snaks = {}
if snaks[aliasesP.importedFrom] then
params = {}
snaks[aliasesP.importedFrom] = nil
leadParams = {}
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
for i2, v2 in pairs(v.snaks) do
if snaks[aliasesP.language] then
if v2[1] then
snakValue = self.conf:getValue(snaks[aliasesP.language])
snaks[i2] = v2[1]
if self.conf.langName ~= snakValue then
params["language"] = snakValue
end
end
end
end
if snaks[aliasesP.importedFrom] then
value = mw.getCurrentFrame():expandTemplate{title="cite_web", args=params}
snaks[aliasesP.importedFrom] = nil
else
end
for i, v in pairs(snaks) do
property = getLabel(i)
if snaks[aliasesP.referenceURL] and snaks[aliasesP.title] then
params["url"] = self:getValue(snaks[aliasesP.referenceURL])
params["title"] = self:getValue(snaks[aliasesP.title], false, false, true)
if snaks[aliasesP.publicationDate] then params["date"]        = self:getValue(snaks[aliasesP.publicationDate])           end
if property ~= "" then
if snaks[aliasesP.retrieved]      then params["access-date"]  = self:getValue(snaks[aliasesP.retrieved])                end
snakValue, lang = self.conf:getValue(v, false, (i == aliasesP.statedIn), false, true) -- link = true/false, anyLang = true
if snaks[aliasesP.archiveURL]      then params["archive-url"]  = self:getValue(snaks[aliasesP.archiveURL])                end
if snaks[aliasesP.archiveDate]    then params["archive-date"] = self:getValue(snaks[aliasesP.archiveDate])              end
if lang and lang ~= self.conf.langCode then
if snaks[aliasesP.author]          then params["author"]      = self:getValue(snaks[aliasesP.author])                    end
snakValue = "''" .. snakValue .. "'' (" .. mw.language.fetchLanguageName(lang, self.conf.langCode) .. ")"
if snaks[aliasesP.publisher]      then params["publisher"]    = self:getValue(snaks[aliasesP.publisher])                 end
end
if snaks[aliasesP.quote]          then params["quote"]        = self:getValue(snaks[aliasesP.quote], false, false, true) end
if snaks[aliasesP.language] then
snakValue = self:getValue(snaks[aliasesP.language])
if self.langName ~= snakValue then
if i == aliasesP.referenceURL or i == aliasesP.statedIn then
params["language"] = snakValue
leadParams[#leadParams + 1] = snakValue
elseif i ~= aliasesP.language or self.conf.langName ~= snakValue then
params[#params + 1] = property .. ": " .. snakValue
end
end
end
end
value = table.concat(leadParams, "; ")
params = table.concat(params, "; ")
if params ~= "" then
if value ~= "" then
value = value .. "; "
end
value = value .. params
end
if value ~= "" then
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
return value
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 matches = false
local rankPos = nil
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
end
ref = mw.getCurrentFrame():expandTemplate{title="cite_web", args=params}
if numValues > 0 then
else
doAppend = false
for i2, v2 in pairs(snaks) do
end
property = self:getLabel(i2)
end
if doAppend then
local function walk(formatTable)
local value
if property ~= "" then
for i2, v2 in pairs(formatTable.req) do
snakValue, lang = self:getValue(v2, false, (i2 == aliasesP.statedIn), true)
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 lang and lang ~= self.langCode then
if result.count == hooks.count then
snakValue = "''" .. snakValue .. "'' (" .. mw.language.fetchLanguageName(lang, self.langCode) .. ")"
-- 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
end
if i2 == aliasesP.referenceURL or i2 == aliasesP.statedIn then
if v2.child then
leadParams[#leadParams + 1] = snakValue
walk(v2.child)
elseif i2 ~= aliasesP.language or self.langName ~= snakValue then
params[#params + 1] = property .. ": " .. snakValue
end
end
end
end
return true
end
end
gotRequired = walk(self.parsedFormat)
ref = table.concat(leadParams, "; ")
-- only append the result if we got values for all required parameters on the root level
params = table.concat(params, "; ")
if gotRequired then
done = self:appendOutput(result, rankPos)
if params ~= "" then
if done then
if ref ~= "" then
break
ref = ref .. "; "
end
end
ref = ref .. params
end
end
if ref ~= "" then
ref = ref .. "."
end
end
if ref ~= "" then
value = value .. mw.getCurrentFrame():extensionTag("ref", ref)
end
end
end
end
end
end
return value
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
 
function p.qualifier(frame)
return p._qualifier(frame.args)
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
end


function State:processFlag(flag)
function p.references(frame)
if flag == "linked" then
return p._references(frame.args)
self.linked = true
return true
elseif flag == "raw" then
self.rawValue = true
return true
elseif flag == "short" then
self.shortName = true
return true
elseif flag == "single" then
self.singleValue = true
return true
elseif flag == "mdy" then
self.mdyDate = true
return true
elseif flag == "refs" then
self.withRefs = 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
end


function p.property(frame)
function p._references(args)
return p._property(frame.args)
return execCommand(args, "references")
end
end


function p._property(args, _)
function execCommand(args, funcName)
_ = _ or State.new()
_ = Config.new()
_:processFlagOrCommand(funcName)  -- process first command (== function name)
local parsedFormat, formatParams, claims
local hooks = {count = 0}
local entity, propertyID, claims, rankPos, value, done
local nextArg = mw.text.trim(args[1] or "")
local nextArg = mw.text.trim(args[1] or "")
local nextIndex = 2
local nextIndex = 2
while _:processFlag(nextArg) do
-- process flags and commands
while _:processFlagOrCommand(nextArg) do
nextArg = mw.text.trim(args[nextIndex] or "")
nextArg = mw.text.trim(args[nextIndex] or "")
nextIndex = nextIndex + 1
nextIndex = nextIndex + 1
end
end
-- check for optional item ID
if nextArg:sub(1,1):upper() == "Q" then
if nextArg:sub(1,1):upper() == "Q" then
entity = mw.wikibase.getEntity(nextArg)
_.entity = mw.wikibase.getEntity(nextArg) -- item ID given
propertyID = mw.text.trim(args[nextIndex] or "")
_.propertyID = mw.text.trim(args[nextIndex] or "") -- property ID
nextIndex = nextIndex + 1
else
else
entity = mw.wikibase.getEntity()
_.entity = mw.wikibase.getEntity() -- no item ID given, use item connected to current page
propertyID = nextArg
_.propertyID = nextArg -- property ID
end
end
if aliasesP[propertyID] then
-- check if given property ID is an alias
propertyID = aliasesP[propertyID]
if aliasesP[_.propertyID] then
_.propertyID = aliasesP[_.propertyID]
end
end
propertyID = propertyID:upper()
_.propertyID = _.propertyID:upper()
if entity and entity.claims then claims = entity.claims[propertyID] end
if _.states[parameters.qualifier] then
if claims then
-- do further processing if "qualifier(s)" command was given
for i, v in ipairs(claims) do
rankPos = _:convertRank(v.rank)
nextArg = args[nextIndex]
if _:rankMatches(rankPos) and _:timeMatches(v) then
nextIndex = nextIndex + 1
value = _:getValue(v.mainsnak, _.rawValue, _.linked)
if value then
_.qualifierID = nextArg
if _.withRefs then
value = value .. _:getReferences(v)
nextArg = mw.text.trim(args[nextIndex] or "")
end
nextIndex = nextIndex + 1
done = _:appendOutput(value, rankPos)
if done then
if nextArg == "" then
break
-- claim ID or literal value has NOT been given
end
_.propertyValue = nil
end
_.qualifierID = mw.text.trim(_.qualifierID or "")
end
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
end
return _:out()
else
_.qualifierID = _.qualifierID:upper()
return ""
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
end
end
function p.qualifier(frame)
return p._qualifier(frame.args)
end
function p._qualifier(args, _)
_ = _ or State.new()
local entity, propertyID, propertyValue, qualifierID, claims, qualifiers, rankPos, outValue, outInter, outQualifiers
-- check for special property value 'somevalue' or 'novalue'
local done = false
if _.propertyValue then
if _.propertyValue ~= "" and mw.text.trim(_.propertyValue) == "" then
local nextArg = mw.text.trim(args[1] or "")
_.propertyValue = " "  -- single space represents 'somevalue', whereas empty string represents 'novalue'
local nextIndex = 2
else
_.propertyValue = mw.text.trim(_.propertyValue)
while _:processFlag(nextArg) do
end
nextArg = mw.text.trim(args[nextIndex] or "")
nextIndex = nextIndex + 1
end
end
if nextArg:sub(1,1):upper() == "Q" then
-- parse the desired format, or choose an appropriate format
entity = mw.wikibase.getEntity(nextArg)
if args["format"] then
propertyID = mw.text.trim(args[nextIndex] or "")
parsedFormat, formatParams = parseFormat(mw.text.trim(args["format"]))
nextIndex = nextIndex + 1
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
else
entity = mw.wikibase.getEntity()
parsedFormat, formatParams = parseFormat(formats.reference)
propertyID = nextArg
-- 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
end
if aliasesP[propertyID] then
-- process overridden separator values;
propertyID = aliasesP[propertyID]
-- must come AFTER parsing the formats
for i, v in pairs(_.separators) do
if args[i] then
_.separators[i][1] = args[i]
end
end
end
propertyID = propertyID:upper()
-- make sure that at least one required parameter has been defined
if not next(parsedFormat.req) then
return missingRequiredParameterError()
end
nextArg = args[nextIndex]
-- make sure that the separator parameter "%s" is not amongst the required parameters
nextIndex = nextIndex + 1
if parsedFormat.req[parameters.separator] then
return extraRequiredParameterError(parameters.separator)
qualifierID = nextArg
end
nextArg = mw.text.trim(args[nextIndex] or "")
nextIndex = nextIndex + 1
if nextArg == "" then
-- define the hooks that should be called (getProperty, getQualifiers, getReferences);
-- claim ID or literal value has NOT been given
-- only define a hook if both its command ("propert(y|ies)", "qualifier(s)", "reference(s)") and its parameter ("%p", "%q", "%r") have been given
propertyValue = nil
for i, v in pairs(_.states) do
qualifierID = mw.text.trim(qualifierID or "")
if formatParams[i] then
else
hooks[i] = hookNames[i][1]
-- claim ID or literal value has been given
hooks.count = hooks.count + 1
propertyValue = qualifierID  -- cannot be nil when reached; empty value represents 'novalue'
if propertyValue ~= "" and mw.text.trim(propertyValue) == "" then
propertyValue = " " -- single space represents 'somevalue'
else
propertyValue = mw.text.trim(propertyValue)
end
end
qualifierID = nextArg
end
end
if aliasesP[qualifierID] then
-- create a state for "properties" if it doesn't exist yet, which will be used as a base configuration for each claim iteration;
qualifierID = aliasesP[qualifierID]
-- must come AFTER defining the hooks
if not _.states[parameters.property] then
_.states[parameters.property] = State.new(_)
end
end
qualifierID = qualifierID:upper()
-- 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 _.entity and _.entity.claims then claims = _.entity.claims[_.propertyID] end
if claims then
if claims then
for i, v in ipairs(claims) do
-- iterate through the claims to collect values
rankPos = _:convertRank(v.rank)
return _.states[parameters.property]:iterate(claims, hooks, State.claimMatches) -- pass property State with level 1 hooks and matchHook
if propertyValue == nil or _:snakEqualsValue(v.mainsnak, propertyValue) then
if _:rankMatches(rankPos) and _:timeMatches(v) then
outValue = nil
outInter = nil
outQualifiers = {}
if _.propertyWithQualifier then
-- get the property value first
outValue = _:getValue(v.mainsnak, _.rawValue, _.linked)
if outValue and _.withRefs then
outValue = outValue .. _:getReferences(v)
end
end
if v.qualifiers then qualifiers = v.qualifiers[qualifierID] end
if (not _.propertyWithQualifier or outValue) and qualifiers then
-- get a bare qualifier, or the qualifiers connected to the property if it had a value
for i2, v2 in ipairs(qualifiers) do
outInter = _:getValue(v2, _.rawValue, _.linked)
if outInter then
if not _.propertyWithQualifier then
done = _:appendOutput(outInter, rankPos)
if done then
break
end
else
outQualifiers[#outQualifiers + 1] = outInter
end
end
end
end
if _.propertyWithQualifier and outValue then
outQualifiers = table.concat(outQualifiers, ", ")
if outQualifiers ~= "" then
outQualifiers = " <span style=\"font-size:smaller\">(" .. outQualifiers .. ")</span>"
outValue = outValue .. outQualifiers
end
done = _:appendOutput(outValue, rankPos)
end
if done then
break
end
end
end
end
return _:out()
else
else
return ""
return ""
end
end
end
function p.propertyWithQualifier(frame)
return p._propertyWithQualifier(frame.args)
end
function p._propertyWithQualifier(args, _)
_ = _ or State.new()
_.propertyWithQualifier = true
return p._qualifier(args, _)
end
end


1,159行目: 1,510行目:


function p._label(args, _)
function p._label(args, _)
_ = _ or State.new()
_ = _ or Config.new()
_.curState = State.new(_)
local ID
local ID
1,187行目: 1,539行目:
end
end
if _.rawValue then
if _.curState.rawValue then
if mw.wikibase.getEntity(ID) or mw.wikibase.resolvePropertyId(ID) then
if mw.wikibase.getEntity(ID) or mw.wikibase.resolvePropertyId(ID) then
if _.linked then
if _.curState.linked then
if ID:sub(1,1) == "P" then
if ID:sub(1,1) == "P" then
label = "[[d:Property:" .. ID .. "|" .. ID .. "]]"
label = "[[d:Property:" .. ID .. "|" .. ID .. "]]"
1,204行目: 1,556行目:
label = mw.wikibase.label(ID) or ""
label = mw.wikibase.label(ID) or ""
if _.linked and label ~= "" then
if _.curState.linked and label ~= "" then
label = "[[d:Property:" .. ID .. "|" .. label .. "]]"
label = "[[d:Property:" .. ID .. "|" .. label .. "]]"
end
end
1,210行目: 1,562行目:
else
else
if not _.pageTitle then
if not _.pageTitle then
if _.shortName then
if _.curState.shortName then
label = _:getShortName(ID)
label = getShortName(ID)
end
end
1,226行目: 1,578行目:
-- at this point, 'label' will be nil or a non-empty string
-- at this point, 'label' will be nil or a non-empty string
if _.linked or label == nil then
if _.curState.linked or label == nil then
title = mw.wikibase.sitelink(ID)
title = mw.wikibase.sitelink(ID)
end
end
if _.linked and title then
if _.curState.linked and title then
label = "[[" .. title .. "|" .. (label or title) .. "]]"
label = "[[" .. title .. "|" .. (label or title) .. "]]"
else
else
1,238行目: 1,590行目:
end
end
else
else
if _.rawValue then
if _.curState.rawValue then
label = mw.wikibase.getEntityIdForCurrentPage() or ""
label = mw.wikibase.getEntityIdForCurrentPage() or ""
if _.linked and label ~= "" then
if _.curState.linked and label ~= "" then
label = "[[d:" .. label .. "|" .. label .. "]]"
label = "[[d:" .. label .. "|" .. label .. "]]"
end
end
1,252行目: 1,604行目:
end
end
if _.linked or label == nil then
if _.curState.linked or label == nil then
title = mw.title.getCurrentTitle().prefixedText
title = mw.title.getCurrentTitle().prefixedText
end
end
if _.linked then
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
label = "[[" .. title .. "|" .. (label or title) .. "]]"  -- not much use since it links to the current page, but does add wiki mark-up
else
else
1,272行目: 1,624行目:


function p._title(args, _)
function p._title(args, _)
_ = _ or State.new()
_ = _ or Config.new()
_.pageTitle = true
_.pageTitle = true
return p._label(args, _)
return p._label(args, _)
1,285行目: 1,637行目:
assert(p[f], 'The function "' .. f .. '" does not exist')
assert(p[f], 'The function "' .. f .. '" does not exist')
    args = {}
args = {}
    for i, v in ipairs(frame.args) do
    if i > 1 then
-- copy arguments from immutable to mutable table
        args[i-1] = v
for i, v in pairs(frame.args) do
        end
args[i] = v
    end
end
frame.args = args
-- remove the function name from the list
table.remove(args, 1)
return p[f](frame)
return p["_"..f](args)
end
end


return p
return p

2017年1月13日 (金) 14:42時点における版

このモジュールについての説明文ページを モジュール:Wd/doc に作成できます

local p = {}

local aliasesP = {
	coord           = "P625",
	author          = "P50",
	publisher       = "P123",
	importedFrom    = "P143",
	statedIn        = "P248",
	publicationDate = "P577",
	startTime       = "P580",
	endTime         = "P582",
	retrieved       = "P813",
	referenceURL    = "P854",
	archiveURL      = "P1065",
	title           = "P1476",
	quote           = "P1683",
	shortName       = "P1813",
	language        = "P2439",
	archiveDate     = "P2960"
}

local aliasesQ = {
	percentage              = "Q11229",
	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 = {}
	stt.outNormal = {}
	stt.outDeprecated = {}
	
	stt.parsedFormat = {}
	stt.separator = {}
	stt.movSeparator = {}
	stt.puncMark = {}
	
	stt.linked = false
	stt.rawValue = false
	stt.shortName = false
	stt.singleValue = false
	
	return stt
end

function unknownDataTypeError(dataType)
	return "<strong class=\"error\">'" .. type .. "'は不明もしくは未対応のデータタイプです。</strong>"
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 parseWikidataURL(url)
	local i, j
	
	if url:match('^http[s]?://') then
		i, j = url:find("Q")
		
		if i then
			return url:sub(i)
		end
	end
	
	return nil
end

function parseDate(dateStr, precision)
	precision = precision or "d"
	local i, j, index, ptr
	local parts = {nil, nil, nil}
	
	if dateStr == nil then
		return parts[1], parts[2], parts[3]  -- year, month, day
	end
	
	-- 'T' for snak values, '/' for outputs with '/Julian' attached
	i, j = dateStr:find("[T/]")
	
	if i then
		dateStr = dateStr:sub(1, i-1)
	end
	
	local from = 1
	
	if dateStr:sub(1,1) == "-" then
		-- this is a negative number, look further ahead
		from = 2
	end
	
	index = 1
	ptr = 1
	
	i, j = dateStr:find("-", from)
	
	if i then
		-- year
		parts[index] = tonumber(mw.ustring.gsub(dateStr:sub(ptr, i-1), "^\+(.+)$", "%1"), 10)  -- remove + sign
		
		if parts[index] == -0 then
			parts[index] = 0
		end
		
		if precision == "y" then
			-- we're done
			return parts[1], parts[2], parts[3]  -- year, month, day
		end
		
		index = index + 1
		ptr = i + 1
		
		i, j = dateStr:find("-", ptr)
		
		if i then
			-- month
			parts[index] = tonumber(dateStr:sub(ptr, i-1), 10)
			
			if precision == "m" then
				-- we're done
				return parts[1], parts[2], parts[3]  -- year, month, day
			end
			
			index = index + 1
			ptr = i + 1
		end
	end
	
	if dateStr:sub(ptr) ~= "" then
		-- day if we have month, month if we have year, or year
		parts[index] = tonumber(dateStr:sub(ptr), 10)
	end
	
	return parts[1], parts[2], parts[3]  -- year, month, day
end

function convertUnit(unit, link)
	link = link or false
	local itemID, label, title
	
	if unit == "" or unit == "1" then
		return nil
	end
	
	itemID = parseWikidataURL(unit)
	
	if itemID then
		if itemID == aliasesQ.percentage then
			return "%"
		else
			label = mw.wikibase.label(itemID)
			title = nil
			
			if link or label == nil then
				title = mw.wikibase.sitelink(itemID)
			end
			
			if link then
				if title then
					return " " .. "[[" .. title .. "|" .. (label or title) .. "]]"
				end
				
				if not label then
					return " " .. "[[d:" .. itemID .. "|" .. itemID .. "]]"
				end
			end
			
			return " " .. (label or title or itemID)
		end
	end
	
	return " " .. unit
end

function getOrdinalSuffix(num)
	if tostring(num):sub(-2,-2) == '1' then
		return "th"  -- 10th, 11th, 12th, 13th, ... 19th
	end
	
	num = tostring(num):sub(-1)
	
	if num == '1' then
		return ""
	elseif num == '2' then
		return ""
	elseif num == '3' then
		return ""
	else
		return ""
	end
end

function addDecimalMarks(n)
	local left,num,right = string.match(n,'^([^%d]*%d)(%d*)(.-)$')
	return left..(num:reverse():gsub('(%d%d%d)','%1,'):reverse())..right
end

function convertRank(rank)
	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
	
	if snak.snaktype == 'value' then
		if snak.datavalue.type == 'string' then
			return snak.datavalue.value
		elseif snak.datavalue.type == 'monolingualtext' then
			if anyLang then
				return snak.datavalue.value['text'], snak.datavalue.value['language']
			elseif snak.datavalue.value['language'] == self.langCode then
				return snak.datavalue.value['text']
			else
				return nil
			end
		elseif snak.datavalue.type == 'quantity' then
			-- strip + signs from front
			local value = mw.ustring.gsub(snak.datavalue.value['amount'], "^\+(.+)$", "%1")
			
			if not raw then
				value = addDecimalMarks(value)
				local unit = convertUnit(snak.datavalue.value['unit'], link)
				if unit then
					value = value .. unit
				end
			end
			
			return value
		elseif snak.datavalue.type == 'time' then
			local y, m, d, p, yDiv, yRound, yFull, value, calendarID, dateStr
			local yFactor = 1
			local sign = 1
			local suffix = ""
			local mayAddCalendar = false
			local calendar = ""
			local precision = snak.datavalue.value['precision']
			
			if precision == 11 then
				p = "d"
			elseif precision == 10 then
				p = "m"
			else
				p = "y"
				yFactor = 10^(9-precision)
			end
			
			y, m, d = parseDate(snak.datavalue.value['time'], p)
			
			if y < 0 then
				sign = -1
				y = y * sign
			end
			
			-- if precision is tens/hundreds/thousands/millions/billions of years
			if precision <= 8 then
				yDiv = y / yFactor
				
				-- if precision is tens/hundreds/thousands of years
				if precision >= 6 then
					mayAddCalendar = true
					
					if precision <= 7 then
						-- round centuries/millenniums up (e.g. 20th century or 3rd millennium)
						yRound = math.ceil(yDiv)
						
						if not raw then
							if precision == 6 then
								suffix = " millennium"
							else
								suffix = " century"
							end
							
							suffix = getOrdinalSuffix(yRound) .. suffix
						else
							-- if not verbose, take the first year of the century/millennium
							-- (e.g. 1901 for 20th century or 2001 for 3rd millennium)
							yRound = (yRound - 1) * yFactor + 1
						end
					else
						-- precision == 8
						-- round decades down (e.g. 2010s)
						yRound = math.floor(yDiv) * yFactor
						
						if not raw then
							suffix = "s"
						end
					end
					
					if raw and sign < 0 then
						-- if BCE then compensate for "counting backwards"
						-- (e.g. -2019 for 2010s BCE, -2000 for 20th century BCE or -3000 for 3rd millennium BCE)
						yRound = yRound + yFactor - 1
					end
				else
					local yReFactor, yReDiv, yReRound
					
					-- round to nearest for tens of thousands of years or more
					yRound = math.floor(yDiv + 0.5)
					
					if yRound == 0 then
						if precision <= 2 and y ~= 0 then
							yReFactor = 1e6
							yReDiv = y / yReFactor
							yReRound = math.floor(yReDiv + 0.5)
							
							if yReDiv == yReRound then
								-- change precision to millions of years only if we have a whole number of them
								precision = 3
								yFactor = yReFactor
								yRound = yReRound
							end
						end
						
						if yRound == 0 then
							-- otherwise, take the unrounded (original) number of years
							precision = 5
							yFactor = 1
							yRound = y
							mayAddCalendar = true
						end
					end
					
					if precision >= 1 and y ~= 0 then
						yFull = yRound * yFactor
						
						yReFactor = 1e9
						yReDiv = yFull / yReFactor
						yReRound = math.floor(yReDiv + 0.5)
						
						if yReDiv == yReRound then
							-- change precision to billions of years if we're in that range
							precision = 0
							yFactor = yReFactor
							yRound = yReRound
						else
							yReFactor = 1e6
							yReDiv = yFull / yReFactor
							yReRound = math.floor(yReDiv + 0.5)
							
							if yReDiv == yReRound then
								-- change precision to millions of years if we're in that range
								precision = 3
								yFactor = yReFactor
								yRound = yReRound
							end
						end
					end
					
					if not raw then
						if precision == 3 then
							suffix = "百万年"
						elseif precision == 0 then
							suffix = "億年"
						else
							yRound = yRound * yFactor
							if yRound == 1 then
								suffix = "年"
							else
								suffix = "年"
							end
						end
					else
						yRound = yRound * yFactor
					end
				end
			else
				yRound = y
				mayAddCalendar = true
			end
			
			if mayAddCalendar then
				calendarID = parseWikidataURL(snak.datavalue.value['calendarmodel'])
				
				if calendarID and calendarID == aliasesQ.prolepticJulianCalendar then
					if not raw then
						if link then
							calendar = " ([[ユリウス暦]])"
						else
							calendar = " (ユリウス暦)"
						end
					else
						calendar = "/ユリウス暦"
					end
				end
			end
			
			if not raw then
				local ce = nil
				
				if sign < 0 then
					ce = "BCE"
				elseif precision <= 5 then
					ce = "CE"
				end
				
				if ce then
					if link then
						ce = "[[:en:Common Era|" .. ce .. "]]"
					end
					suffix = suffix .. " " .. ce
				end
				
				value = tostring(yRound)
				
				if m then
					dateStr = self.langObj:formatDate("F", "1-"..m.."-1")
					
					if d then
						if self.mdyDate then
							dateStr = dateStr .. " " .. d .. ","
						else
							dateStr = d .. " " .. dateStr
						end
					end
					
					value = dateStr .. " " .. value
				end
				
				value = value .. suffix .. calendar
			else
				value = tostring(yRound * sign)
				
				if m then
					value = value .. "-" .. m
					
					if d then
						value = value .. "-" .. d
					end
				end
				
				value = value .. calendar
			end
			
			return value
		elseif snak.datavalue.type == 'globecoordinate' then
			local precision, numDigits, strFormat, value, globe
			local latValue, latitude, latDegrees, latMinutes, latSeconds
			local latDirection = "N"
			local lonValue, longitude, lonDegrees, lonMinutes, lonSeconds
			local lonDirection = "E"
			
			local degSymbol = "°"
			local minSymbol = "'"
			local secSymbol = '"'
			local partsGlue = ", "
			
			if raw then
				degSymbol = "/"
				minSymbol = "/"
				secSymbol = "/"
				partsGlue = "/"
			end
			
			latitude = snak.datavalue.value['latitude']
			longitude = snak.datavalue.value['longitude']
			
			if latitude < 0 then
				latDirection = "S"
				latitude = math.abs(latitude)
			end
			if longitude < 0 then
				lonDirection = "W"
				longitude = math.abs(longitude)
			end
			
			precision = snak.datavalue.value['precision']
			
			latitude = math.floor(latitude / precision + 0.5) * precision
			longitude = math.floor(longitude / precision + 0.5) * precision
			
			latDegrees = math.floor(latitude)
			lonDegrees = math.floor(longitude)
			
			latMinutes = math.floor((latitude - latDegrees) * 60)
			lonMinutes = math.floor((longitude - lonDegrees) * 60)
			
			latSeconds = (latitude - (latDegrees + latMinutes / 60)) * 3600
			lonSeconds = (longitude - (lonDegrees + lonMinutes / 60)) * 3600
			
			latValue = latDegrees .. degSymbol
			lonValue = lonDegrees .. degSymbol
			
			if precision < 1 then
				latValue = latValue .. latMinutes .. minSymbol
				lonValue = lonValue .. lonMinutes .. minSymbol
			end
			
			if precision < (1 / 60) then
				numDigits = math.ceil(-math.log10(3600 * precision))
				
				if numDigits < 0 or numDigits == -0 then
					numDigits = 0
				end
				
				strFormat = "%." .. numDigits .. "f"
				
				latSeconds = string.format(strFormat, latSeconds)
				lonSeconds = string.format(strFormat, lonSeconds)
				
				latValue = latValue .. latSeconds .. secSymbol
				lonValue = lonValue .. lonSeconds .. secSymbol
			end
			
			latValue = latValue .. latDirection
			lonValue = lonValue .. lonDirection
			
			value = latValue .. partsGlue .. lonValue
			
			if link then
				globe = parseWikidataURL(snak.datavalue.value['globe'])
				
				if globe then
					globe = mw.wikibase.label(globe):lower()
				else
					globe = "earth"
				end
				
				value = "[https://tools.wmflabs.org/geohack/geohack.php?language="..self.langCode.."&params="..latitude.."_"..latDirection.."_"..longitude.."_"..lonDirection.."_globe:"..globe.." "..value.."]"
			end
			
			return value
		elseif snak.datavalue.type == 'wikibase-entityid' then
			local value = ""
			local title = nil
			local itemID = "Q" .. snak.datavalue.value['numeric-id']
			
			if raw then
				if link then
					return "[[d:" .. itemID .. "|" .. itemID .. "]]"
				else
					return itemID
				end
			end
			
			if short then
				value = getShortName(itemID)
			end
			
			if value == "" then
				value = mw.wikibase.label(itemID)
			end
			
			if link or value == nil then
				title = mw.wikibase.sitelink(itemID)
			end
			
			if link then
				if title then
					value = "[[" .. title .. "|" .. (value or title) .. "]]"
				elseif not value then
					value = "[[d:" .. itemID .. "|" .. itemID .. "]]"
				end
			elseif not value then
				value = (title or itemID)
			end
			
			return value
		else
			return unknownDataTypeError(snak.datavalue.type)
		end
	elseif snak.snaktype == 'somevalue' then
		if raw then
			return " "  -- single space represents 'somevalue'
		else
			return "unknown"
		end
	elseif snak.snaktype == 'novalue' then
		if raw then
			return ""  -- empty string represents 'novalue'
		else
			return "none"
		end
	else
		return nil
	end
end

function Config:getSingleRawQualifier(claim, qualifierID)
	local qualifiers
	
	if claim.qualifiers then qualifiers = claim.qualifiers[qualifierID] end
	
	if qualifiers and qualifiers[1] then
		return self:getValue(qualifiers[1], true)  -- raw = true
	else
		return nil
	end
end

function Config:snakEqualsValue(snak, value)
	local snakValue = self:getValue(snak, true)  -- raw = true
	
	if snakValue and snak.snaktype == 'value' and snak.datavalue.type == 'wikibase-entityid' then value = value:upper() end
	
	return snakValue == value
end

function Config:setRankBoundaries(rank)
	local rankPos
	
	self.foundRank = 3  -- must equal the lowest possible rank
	
	if (rank == "best") then
		self.bestRank = true
		return
	else
		self.bestRank = false
	end
	
	if (rank:sub(1,9) == "preferred") then
		rankPos = 1
	elseif (rank:sub(1,6) == "normal") then
		rankPos = 2
	elseif (rank:sub(1,10) == "deprecated") then
		rankPos = 3
	end
	
	if (rank:sub(-1) == "+") then
		self.maxRank = 1
		self.minRank = rankPos
	elseif (rank:sub(-1) == "-") then
		self.maxRank = rankPos
		self.minRank = 3
	else
		self.maxRank = rankPos
		self.minRank = rankPos
	end
end

function Config:processFlag(flag)
	if flag == "linked" then
		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 flag == "property" or flag == "properties" then
		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 self.states[param] then
		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 Config:timeMatches(claim)
	local startTime = nil
	local startTimeY = nil
	local startTimeM = nil
	local startTimeD = nil
	local endTime = nil
	local endTimeY = nil
	local endTimeM = nil
	local endTimeD = nil
	
	if self.period == 0 then
		-- any time
		return true
	end
	
	local now = os.date('!*t')
	
	startTime = self:getSingleRawQualifier(claim, aliasesP.startTime)
	if startTime and startTime ~= "" and startTime ~= " " then
		startTimeY, startTimeM, startTimeD = parseDate(startTime)
	end
	
	endTime = self:getSingleRawQualifier(claim, aliasesP.endTime)
	if endTime and endTime ~= "" and endTime ~= " " then
		endTimeY, endTimeM, endTimeD = parseDate(endTime)
	elseif endTime == " " then
		-- end time is 'unknown', assume it is somewhere in the past;
		-- we can do this by taking the current date as a placeholder for the end time
		endTimeY = now['year']
		endTimeM = now['month']
		endTimeD = now['day']
	end
	
	if startTimeY ~= nil and endTimeY ~= nil and datePrecedesDate(endTimeY, endTimeM, endTimeD, startTimeY, startTimeM, startTimeD) then
		-- invalidate end time if it precedes start time
		endTimeY = nil
		endTimeM = nil
		endTimeD = nil
	end
	
	if self.period == 1 then
		-- future
		if startTimeY == nil or not datePrecedesDate(now['year'], now['month'], now['day'], startTimeY, startTimeM, startTimeD) then
			return false
		else
			return true
		end
	elseif self.period == 2 then
		-- current
		if (startTimeY ~= nil and datePrecedesDate(now['year'], now['month'], now['day'], startTimeY, startTimeM, startTimeD)) or
		   (endTimeY ~= nil and not datePrecedesDate(now['year'], now['month'], now['day'], endTimeY, endTimeM, endTimeD)) then
		    return false
		else
		   	return true
		end
	elseif self.period == 3 then
		-- former
		if endTimeY == nil or datePrecedesDate(now['year'], now['month'], now['day'], endTimeY, endTimeM, endTimeD) then
			return false
		else
			return true
		end
	end
end

function State:claimMatches(claim)
	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] = result
		
		if self.singleValue then
			done = true
		end
	elseif rankPos == 2 then
		self.outNormal[#self.outNormal + 1] = result
		
		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] = result
		
		if self.singleValue and not self.conf.bestRank and self.conf.maxRank == 3 then
			done = true
		end
	end
	
	return done
end

function State:out()
	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
			if v.param then
				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
				sep = self.separator[1]  -- fixed separator
				result[parameters.separator] = self.movSeparator[1]  -- movable separator
			else
				sep = ""
				result[parameters.separator] = self.puncMark[1]  -- optional punctuation mark
			end
			
			value = walk(self.parsedFormat, result)
			
			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 statement.snaks then
		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.language] then
				snakValue = self.conf:getValue(snaks[aliasesP.language])
				
				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 property ~= "" then
					snakValue, lang = self.conf:getValue(v, false, (i == aliasesP.statedIn), false, true)  -- link = true/false, anyLang = true
					
					if lang and lang ~= self.conf.langCode then
						snakValue = "''" .. snakValue .. "'' (" .. mw.language.fetchLanguageName(lang, self.conf.langCode) .. ")"
					end
					
					if i == aliasesP.referenceURL or i == aliasesP.statedIn then
						leadParams[#leadParams + 1] = snakValue
					elseif i ~= aliasesP.language or self.conf.langName ~= snakValue then
						params[#params + 1] = property .. ": " .. snakValue
					end
				end
			end
			
			value = table.concat(leadParams, "; ")
			params = table.concat(params, "; ")
			
			if params ~= "" then
				if value ~= "" then
					value = value .. "; "
				end
				value = value .. params
			end
			
			if value ~= "" then
				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
	
	return value
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 matches = false
	local rankPos = nil
	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)
					local value
					
					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
					end
				end
			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

function p.qualifier(frame)
	return p._qualifier(frame.args)
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 hooks = {count = 0}
	
	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
	if aliasesP[_.propertyID] then
		_.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

function p.label(frame)
	return p._label(frame.args)
end

function p._label(args, _)
	_ = _ or Config.new()
	_.curState = State.new(_)
	
	local ID
	local label = ""
	local title = nil
	
	local nextArg = mw.text.trim(args[1] or "")
	local nextIndex = 2
	
	while _:processFlag(nextArg) do
		nextArg = mw.text.trim(args[nextIndex] or "")
		nextIndex = nextIndex + 1
	end
	
	ID = nextArg
	
	if ID ~= "" then
		if aliasesP[ID] then
			ID = aliasesP[ID]
		end
		
		ID = ID:upper()
		
		-- check if this is a valid ID, and if the number is not larger than max int (to prevent error)
		if not string.match(ID, '^[QP]%d+$') or tonumber(string.match(ID, '%d+')) > 2147483647 then
			return ""
		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 .. "]]"
					else
						label = "[[d:" .. ID .. "|" .. ID .. "]]"
					end
				else
					label = ID
				end
			end
		else
			if ID:sub(1,1) == "P" then
				if not _.pageTitle then
					label = mw.wikibase.label(ID) or ""
					
					if _.curState.linked and label ~= "" then
						label = "[[d:Property:" .. ID .. "|" .. label .. "]]"
					end
				end
			else
				if not _.pageTitle then
					if _.curState.shortName then
						label = getShortName(ID)
					end
					
					-- at this point, 'label' will be a string and not nil
					
					if label == "" then
						label = mw.wikibase.label(ID)
					end
				else
					-- set 'label' to nil so 'title' will always prevail
					label = nil
				end
				
				-- 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
					label = label or title or ""
				end
			end
		end
	else
		if _.curState.rawValue then
			label = mw.wikibase.getEntityIdForCurrentPage() or ""
			
			if _.curState.linked and label ~= "" then
				label = "[[d:" .. label .. "|" .. label .. "]]"
			end
		else
			if not _.pageTitle then
				label = mw.wikibase.label()
			else
				-- set 'label' to nil so 'title' will always prevail
				label = nil
			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
				label = label or title
			end
		end
	end
	
	return label
end

function p.title(frame)
	return p._title(frame.args)
end

function p._title(args, _)
	_ = _ or Config.new()
	_.pageTitle = true
	return p._label(args, _)
end

-- main function that may be used by wrapper templates
function p.main(frame)
	local f, args, i, v
	
	frame = frame:getParent() or frame
	f = mw.text.trim(frame.args[1] or "")
	assert(p[f], 'The function "' .. f .. '" does not exist')
	
	args = {}
	
	-- copy arguments from immutable to mutable table
	for i, v in pairs(frame.args) do
		args[i] = v
	end
	
	-- remove the function name from the list
	table.remove(args, 1)
	
	return p["_"..f](args)
end

return p