Content deleted Content added
Added 'sourced' flag |
Added option to match claims based on qualifier value(s) |
||
Line 96:
cfg.propertyValue = nil
cfg.qualifierIDs = {}
cfg.qualifierIDsAndValues = {}
cfg.bestRank = true
Line 147 ⟶ 148:
return stt
end
function replaceAlias(ID)
if aliasesP[ID] then
ID = aliasesP[ID]
end
return ID
end
Line 181 ⟶ 190:
end
function replaceSpecialChar(chr)
if chr == '_' then
-- replace underscores with spaces
return ' '
else
return chr
end
end
function replaceSpecialChars(str)
local chr
local esc = false
local strOut = ""
for i = 1, #str do
chr = str:sub(i,i)
if not esc then
if chr == '\\' then
esc = true
else
strOut = strOut .. replaceSpecialChar(chr)
end
else
strOut = strOut .. chr
esc = false
end
end
return strOut
end
Line 370 ⟶ 406:
if hookNames[param] then
return hookNames[param][index]
elseif
return hookNames[
else
return nil
Line 498 ⟶ 534:
param = param - 1
elseif param == 1 then
if not
endParam()
end
end
cur.str = cur.str .. replaceSpecialChar(chr)
end
else
Line 954 ⟶ 990:
-- use string.format() to strip decimal point followed by a zero (.0) for whole numbers
latSeconds = tonumber(
lonSeconds = tonumber(
latMinutes = math.floor(latSeconds / 60)
Line 978 ⟶ 1,014:
if precision < (1 / 60) then
latSeconds =
lonSeconds =
if not raw then
Line 1,231 ⟶ 1,267:
self.states[param].separator = self.separators["sep"..param] -- will be nil for param=="%p", which will be set separately
if
self.states[param].singleValue = true
end
Line 1,238 ⟶ 1,274:
return true
end
function Config:qualifierMatches(claim, ID, value)
local qualifiers
if claim.qualifiers then qualifiers = claim.qualifiers[ID] end
if qualifiers then
for i, v in pairs(qualifiers) do
if self:snakEqualsValue(v, value) then
return true
end
end
elseif value == "" then
-- if the qualifier is not present then treat it the same as the special value 'novalue'
return true
end
return false
end
Line 1,337 ⟶ 1,391:
else
matches = true
end
-- if any qualifier values were given, check if each matches one of the claim's qualifier values
for i, v in pairs(self.conf.qualifierIDsAndValues) do
matches = (matches and self.conf:qualifierMatches(claim, i, v))
end
Line 1,795 ⟶ 1,854:
nextIndex = nextIndex + 1
elseif nextArg:sub(1,9):lower() == "property:" then
nextArg = replaceAlias(mw.text.trim(nextArg:sub(10)))
_.entity = mw.wikibase.getEntity(nextArg) -- entity ID of a property given
_.propertyID = mw.text.trim(args[nextIndex] or "") -- property ID
Line 1,810 ⟶ 1,864:
-- check if given property ID is an alias
if _.states.qualifiersCount > 0 then
Line 1,822 ⟶ 1,872:
-- claim ID or literal value has been given
_.propertyValue = mw.text.trim(args[nextIndex])
nextIndex = nextIndex + 1
end
Line 1,832 ⟶ 1,880:
nextIndex = nextIndex + 1
-- check if given qualifier ID is an alias and add it
_.qualifierIDs[parameters.qualifier..i] = replaceAlias(nextArg):upper()
end
elseif _.states[parameters.reference] then
-- do further processing if "reference(s)" command was given
nextArg = args[nextIndex] -- claim ID or literal value (possibly nil)
nextIndex = nextIndex + 1
if nextArg then
_.propertyValue = mw.text.trim(nextArg)
end
end
-- check for special property value 'somevalue' or 'novalue'
if _.propertyValue then
_.propertyValue = replaceSpecialChars(_.propertyValue)
if _.propertyValue ~= "" and mw.text.trim(_.propertyValue) == "" then
_.propertyValue = " " -- single space represents 'somevalue', whereas empty string represents 'novalue'
Line 1,859 ⟶ 1,907:
-- parse the desired format, or choose an appropriate format
if args["format"] then
parsedFormat, formatParams = parseFormat
elseif _.states.qualifiersCount > 0 then -- "qualifier(s)" command given
if _.states[parameters.property] then -- "propert(y|ies)" command given
Line 1,893 ⟶ 1,941:
for i, v in pairs(_.separators) do
if args[i] then
sep =
if sep ~= "" then
Line 1,917 ⟶ 1,965:
for i, v in pairs(_.states) do
-- e.g. 'formatParams["%q1"] or formatParams["%q"]' to define hook even if "%q1" was not defined to be able to build a complete value for "%q"
if formatParams[i] or formatParams[
hooks[i] = getHookName(i, 1)
hooks.count = hooks.count + 1
Line 1,953 ⟶ 2,001:
_.states[parameters.property].movSeparator = _.separators["sep"..parameters.separator]
_.states[parameters.property].puncMark = _.separators["punc"]
-- process qualifier matching values, analogous to _.propertyValue
for i, v in pairs(args) do
i = tostring(i)
if i:match('^[Pp]%d+$') or aliasesP[i] then
v = replaceSpecialChars(v)
-- check for special qualifier value 'somevalue'
if v ~= "" and mw.text.trim(v) == "" then
v = " " -- single space represents 'somevalue'
end
_.qualifierIDsAndValues[replaceAlias(i):upper()] = v
end
end
if _.entity and _.entity.claims then claims = _.entity.claims[_.propertyID] end
Line 2,002 ⟶ 2,066:
if ID then
ID = replaceAlias(ID):upper()
-- check if this is a valid ID, and if the number is not larger than max int (to prevent error)
if not
return ""
end
|