Module:Wd: Difference between revisions

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)
-- used for cleaner output when subst:ituting this module
if chr == '_' then
function replaceHTMLSpaces(str)
-- replace underscores with spaces
return mw.ustring.gsub(str, " ", " ")
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 string.param:len(param) > 2 then
return hookNames[string.param:sub(param, 1, 2).."\\d"][index]
else
return nil
Line 498 ⟶ 534:
param = param - 1
elseif param == 1 then
if not string.chr:match(chr, '%d') then
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(string.strFormat:format(strFormat, math.floor(latitude * 3600 * 10^numDigits + 0.5) / 10^numDigits))
lonSeconds = tonumber(string.strFormat:format(strFormat, math.floor(longitude * 3600 * 10^numDigits + 0.5) / 10^numDigits))
latMinutes = math.floor(latSeconds / 60)
Line 978 ⟶ 1,014:
if precision < (1 / 60) then
latSeconds = string.strFormat:format(strFormat, latSeconds)
lonSeconds = string.strFormat:format(strFormat, 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 string.flag:sub(flag, -1) ~= 's' then
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)))
if aliasesP[nextArg] then
nextArg = aliasesP[nextArg]
end
_.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 aliasesP[_.propertyID] then= replaceAlias(_.propertyID):upper()
_.propertyID = aliasesP[_.propertyID]
end
_.propertyID = _.propertyID:upper()
if _.states.qualifiersCount > 0 then
Line 1,822 ⟶ 1,872:
-- claim ID or literal value has been given
_.propertyValue = mw.text.trim(args[nextIndex])
nextArg = args[nextIndex] -- don't trim because might be single space representing 'somevalue'
nextIndex = nextIndex + 1
_.propertyValue = nextArg
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()
if aliasesP[nextArg] then
nextArg = aliasesP[nextArg]
end
_.qualifierIDs[parameters.qualifier..i] = 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 = nextArg -- claim ID or literal value (possibly nil)
_.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(replaceHTMLSpaces(mw.text.trim(args["format"])))
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 = replaceHTMLSpaces(mw.text.trimreplaceSpecialChars(args[i]))
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[string.i:sub(i, 1, 2)] then
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()
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.ID:match(ID, '^[QP]%d+$') or tonumber(string.ID:match(ID, '%d+')) > 2147483647 then
return ""
end