Module:Wd: Difference between revisions

Content deleted Content added
mNo edit summary
Replaced flag 'unit' with flag 'raw', added flag 'mdy', fixed aliases for function 'label'
Line 32:
stt.period = 0
stt.linked = false
stt.propertyWithQualifier = false
stt.withUnitlinked = false
stt.rawValue = false
stt.shortName = false
stt.singleValue = false
stt.mdyDate = false
stt.langCode = mw.language.getContentLanguage().code
Line 133 ⟶ 134:
end
 
function State:convertUnit(unit, addLinklink)
addLinklink = addLinklink or false
local itemID, label, target
Line 150 ⟶ 151:
target = nil
if addLinklink or label == nil then
target = mw.wikibase.sitelink(itemID)
end
if addLinklink then
if target then
return " " .. "[[" .. target .. "|" .. (label or target) .. "]]"
Line 160 ⟶ 161:
if not label then
return " " .. "[[:d:" .. itemID .. "|" .. itemID .. "]]"
end
end
Line 193 ⟶ 194:
end
 
function State:getValue(snak, addUnitraw, addLinklink)
addUnitraw = addUnitraw or false
addLinklink = addLinklink or false
if snak.snaktype == 'value' then
Line 210 ⟶ 211:
local value = mw.ustring.gsub(snak.datavalue.value['amount'], "^\+(.+)$", "%1")
if addUnitnot raw then
local unit = self:convertUnit(snak.datavalue.value['unit'], addLinklink)
if unit then
value = value .. unit
Line 219 ⟶ 220:
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
Line 255 ⟶ 256:
yRound = math.ceil(yDiv)
if addUnitnot raw then
if precision == 6 then
suffix = " millennium"
Line 264 ⟶ 265:
suffix = self:getOrdinalSuffix(yRound) .. suffix
else
-- if nonot unit addedverbose, take the first year of the century/millennium
-- (e.g. 1901 for 20th century or 2001 for 3rd millennium)
yRound = (yRound - 1) * yFactor + 1
Line 273 ⟶ 274:
yRound = math.floor(yDiv) * yFactor
if addUnitnot raw then
suffix = "s"
end
end
if not addUnitraw 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)
Line 338 ⟶ 339:
end
if addUnitnot raw then
if precision == 3 then
suffix = " million years"
Line 364 ⟶ 365:
if calendarID and calendarID == aliasesQ.prolepticJulianCalendar then
if addUnitnot raw then
if addLinklink then
calendar = " ([[Julian calendar|Julian]])"
else
Line 376 ⟶ 377:
end
if addUnitnot raw then
local ce = nil
Line 386 ⟶ 387:
if ce then
if addLinklink then
ce = "[[Common Era|" .. ce .. "]]"
end
Line 395 ⟶ 396:
if m then
valuedateStr = self.langObj:formatDate("F", "1-"..m.."-1") .. " " .. value
if d then
valueif = d .. " " .self.mdyDate valuethen
dateStr = dateStr .. " " .. d .. ","
else
dateStr = d .. " " .. dateStr
end
end
value = dateStr .. " " .. value
end
Line 430 ⟶ 437:
local partsGlue = ", "
if not addUnitraw then
degSymbol = "/"
minSymbol = "/"
Line 492 ⟶ 499:
value = latValue .. partsGlue .. lonValue
if addLinklink then
globe = self:parseWikidataURL(snak.datavalue.value['globe'])
Line 509 ⟶ 516:
local target = 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 self.shortName then
Line 518 ⟶ 533:
end
if addLinklink or value == nil then
target = mw.wikibase.sitelink(itemID)
end
if addLinklink then
if target then
value = "[[" .. target .. "|" .. (value or target) .. "]]"
elseif not value then
value = "[[:d:" .. itemID .. "|" .. itemID .. "]]"
end
elseif not value then
Line 537 ⟶ 552:
end
elseif snak.snaktype == 'somevalue' then
if raw then
return "unknown"
return " " -- single space represents 'somevalue'
else
return "unknown"
end
elseif snak.snaktype == 'novalue' then
if raw then
return "none"
return "" -- empty value represents 'novalue'
else
return "none"
end
else
return nil
end
end
 
function State:getRawValue(snak)
if snak.snaktype == 'value' and snak.datavalue.type == 'wikibase-entityid' then
return "Q" .. snak.datavalue.value['numeric-id']
elseif snak.snaktype == 'somevalue' then
return " " -- single space represents 'somevalue'
elseif snak.snaktype == 'novalue' then
return "" -- empty value represents 'novalue'
else
return self:getValue(snak, false, false)
end
end
Line 563 ⟶ 574:
if qualifiers and qualifiers[1] then
return self:getRawValuegetValue(qualifiers[1], true) -- raw = true
else
return nil
Line 570 ⟶ 581:
 
function State:snakEqualsValue(snak, value)
local snakValue = self:getRawValuegetValue(snak, true) -- raw = true
if snakValue and snak.snaktype == 'value' and snak.datavalue.type == 'wikibase-entityid' then value = value:upper() end
Line 805 ⟶ 816:
self.linked = true
return true
elseif flag == "unitraw" then
self.withUnitrawValue = true
return true
elseif flag == "short" then
Line 813 ⟶ 824:
elseif flag == "single" then
self.singleValue = 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
Line 866 ⟶ 880:
rankPos = _:convertRank(v.rank)
if _:rankMatches(rankPos) and _:timeMatches(v) then
value = _:getValue(v.mainsnak, _.withUnitrawValue, _.linked)
if value then
done = _:appendOutput(value, rankPos)
Line 955 ⟶ 969:
if _.propertyWithQualifier then
-- get the property value first
outValue = _:getValue(v.mainsnak, _.withUnitrawValue, _.linked)
end
Line 962 ⟶ 976:
-- 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, _.withUnitrawValue, _.linked)
if outInter then
if not _.propertyWithQualifier then
Line 1,006 ⟶ 1,020:
local _ = State.new()
_.propertyWithQualifier = true
_.withUnit = true
return p._qualifier(args, _)
end
Line 1,017 ⟶ 1,030:
local _ = State.new()
local label = ""ID
local targetvalue = ""
local target = nil
local nextArg = mw.text.trim(args[1] or "")
local nextIndex = 2
Line 1,027 ⟶ 1,042:
end
ifID = nextArg

if ID ~= "" then
if nextArg:sub(1,1):upper() == "Q"aliasesP[ID] then
ID = aliasesP[ID]
if _.shortName then
end
label = _:getShortName(nextArg)
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 _.rawValue then
if mw.wikibase.getEntity(ID) or mw.wikibase.resolvePropertyId(ID) then
if _.linked then
if ID:sub(1,1) == "P" then
value = "[[d:Property:" .. ID .. "|" .. ID .. "]]"
else
value = "[[d:" .. ID .. "|" .. ID .. "]]"
end
else
value = ID
end
end
else
if ID:sub(1,1) == "P" then
value = mw.wikibase.label(ID) or ""
if _.linked and value ~= "" then
value = "[[d:Property:" .. ID .. "|" .. value .. "]]"
end
else
if _.shortName then
value = _:getShortName(ID)
end
-- at this point, 'value' will be a string and not nil
if value == "" then
value = mw.wikibase.label(ID)
end
-- at this point, 'value' will be nil or a non-empty string
if _.linked or value == nil then
target = mw.wikibase.sitelink(ID)
end
if _.linked and target then
value = "[[" .. target .. "|" .. (value or target) .. "]]"
else
value = value or target or ""
end
end
end
else
if _.rawValue then
value = mw.wikibase.getEntityIdForCurrentPage() or ""
if label_.linked =and value ~= "" then
value = "[[d:" .. value .. "|" .. value .. "]]"
label = mw.wikibase.label(nextArg)
end
else
value = mw.wikibase.label()
if _.linked or labelvalue == nil then
target = mw.wikibasetitle.sitelinkgetCurrentTitle(nextArg).prefixedText
end
if _.linked and target then
labelvalue = "[[" .. target .. "|" .. (labelvalue or target) .. "]]" -- not much use since it links to the current page, but does add wiki mark-up
else
value = value or target
end
else
label = mw.wikibase.label(nextArg)
end
return (label or target or "")
else
return (mw.wikibase.label() or "")
end
return value
end