Content deleted Content added
Add new method getValueShortName, per talk message. |
m Consistent formatting and no spaces and tabs at end of lines |
||
Line 9:
-- module local variables and functions
local wiki =
{
langcode = mw.language.getContentLanguage().code
Line 17:
local i18n =
{
{▼
["unknown-datetime-format"] = "Unknown datetime format.",
["local-article-not-found"] = "Article is not yet available in this wiki."
{
-- $1 is a placeholder for the actual number
Line 37:
[4] = "$100,000 years", -- precision: hundred thousand years
[5] = "$10,000 years", -- precision: ten thousand years
[6] = "$1 millennium",
[7] = "$1 century", -- precision: century
[8] = "$1s", -- precision: decade
-- the following use the format of #time parser function
[9] = "Y", -- precision: year,
[10] = "F Y", -- precision: month
[11] = "F j, Y", -- precision: day
Line 53:
-- the following are for function getDateValue() and getQualifierDateValue()
["default-format"] = "dmy", -- default value of the #3 (getDateValue) or
["default-addon"] = "BC",
-- #5 (getQualifierDateValue) argument
["prefix-addon"] = false,
-- datetime string; or the addon will be suffixed
["addon-sep"] = " ", -- separator between datetime string and addon (or inverse)
Line 71:
["monolingualtext"] = '<span lang="%language">%text</span>',
["warnDump"] = "[[Category:Called function 'Dump' from module Wikidata]]",
["ordinal"] =
{
[1] = "st",
Line 92:
else
t1[k] = v
end
return t1
Line 185:
addon = date_addon
end
-- get the next four characters after the + (should be the year now in all cases)
-- ok, so this is dirty, but let's get it working first
Line 192:
return ""
end
-- precision is 10000 years or more
if precision <= 5 then
Line 203:
else
relative = mw.ustring.gsub(i18n.datetime.afternow, "$1", relative)
end
return relative
end
Line 228:
return era
end
local _date_format = i18n["datetime"]["format"][date_format]
if _date_format ~= nil then
local year_suffix
local tstr = ""
Line 259:
fdate = tstr
end
return fdate
else
Line 271:
local function orderedpairs(array, order)
if not order then return pairs(array) end
-- return iterator function
end
Line 296:
local date, year = normalizeDate(date)
if year == 0 and precision <= 9 then return "" end
if precision <= 5 then
local factor = 10 ^ ((5 - precision) + 4)
Line 306:
else
relative = mw.ustring.gsub(i18n.datetime.afternow, "$1", relative)
end
return relative
end
local era
if precision == 6 then era = mw.ustring.gsub(i18n.datetime[6], "$1", tostring(math.floor((math.abs(year) - 1) / 1000) + 1)) end
Line 320:
return era
end
-- precision is less than years
if precision > 9 then
Line 337:
end
]]--
local formatstr = i18n.datetime[precision]
if year == 0 then formatstr = mw.ustring.gsub(formatstr, i18n.datetime[9], "")
Line 354:
-- data fields: entity-type [string], numeric-id [int, Wikidata id]
local id
if data["entity-type"] == "item" then id = "Q" .. data["numeric-id"]
elseif data["entity-type"] == "property" then id = "P" .. data["numeric-id"]
else return printError("unknown-entity-type")
end
if parameter then
if parameter == "link" then
Line 366:
if linkTarget then
-- if there is a local Wikipedia article link to it using the label or the article title
return "[[" .. linkTarget
else
-- if there is no local Wikipedia article output the label or link to the Wikidata object to let the user input a proper label
Line 404:
local function findClaims(entity, property)
if not property or not entity or not entity.claims then return end
if mw.ustring.match(property, "^P%d+$") then
-- if the property is given by an id (P..) access the claim list by this id
Line 430:
return mw.wikibase.renderSnak(snak)
end
local function getQualifierSnak(claim, qualifierId)
-- a "snak" is Wikidata terminology for a typed key/value pair
Line 447:
end
end
local function getValueOfClaim(claim, qualifierId, parameter)
local error
Line 468:
if refparts then refparts = refparts .. ", " else refparts = "" end
-- output the label of the property of the reference part, e.g. "imported from" for P143
refparts = refparts .. tostring(mw.wikibase.label(snakkey)) .. ": "
-- output all values of this reference part, e.g. "German Wikipedia" and "English Wikipedia" if the referenced claim was imported from both sites
for snakidx = 1, #snakval do
Line 527:
local label = mw.wikibase.label("Q" .. v.mainsnak.datavalue.value["numeric-id"])
if label == nil then label = "Q" .. v.mainsnak.datavalue.value["numeric-id"] end
if sitelink then
out[#out + 1] = "[[" .. sitelink .. "|" .. label .. "]]"
Line 568:
if claimEntity.claims.P1813 then
for k2, v2 in pairs(claimEntity.claims.P1813) do
label = v2.mainsnak.datavalue.value.text
end
Line 574:
end
end
if label == nil then label = "Q" .. v.mainsnak.datavalue.value["numeric-id"] end
if sitelink then
out[#out + 1] = "[[" .. sitelink .. "|" .. label .. "]]"
Line 616:
local label = mw.wikibase.label("Q" .. v.mainsnak.datavalue.value["numeric-id"])
if label == nil then label = "Q" .. v.mainsnak.datavalue.value["numeric-id"] end
if sitelink then
out[#out + 1] = "[[" .. sitelink .. "|" .. label .. "]]"
Line 673:
if claims then
local result = entity:formatPropertyValues(propertyID, mw.wikibase.entity.claimRanks).value
-- if number type: remove thousand separators, bounds and units
if (claims[1] and claims[1].mainsnak.snaktype == "value" and claims[1].mainsnak.datavalue.type == "quantity") then
Line 903:
id = nil
end
-- look for named parameter lang
-- it should contain a two-character ISO-639 language code
Line 911:
lang = mw.language.getContentLanguage().code
end
-- first unnamed parameter is the local parameter, if supplied
local input_parm = mw.text.trim(frame.args[1] or "")
Line 971:
local default = frame.args["default"]
if default then showerrors = nil end
-- get wikidata entity
local entity = mw.wikibase.getEntityObject(id)
Line 982:
if showerrors then return printError("property-not-found") else return default end
end
-- get initial sort indices
local sortindices = {}
Line 991:
local comparator = function(a, b)
local rankmap = { deprecated = 2, normal = 1, preferred = 0 }
local ranka = rankmap[claims[a].rank or "normal"] ..
local rankb = rankmap[claims[b].rank or "normal"] ..
return ranka < rankb
table.sort(sortindices, comparator)
local result
local error
Line 1,005:
for idx in pairs(claims) do
local claim = claims[sortindices[idx]]
value, error =
if not value and showerrors then value = error end
if value and references then value = value .. getReferences(frame, claim) end
Line 1,012:
result = table.concat(result, list)
else
-- return first element
local claim = claims[sortindices[1]]
result, error = getValueOfClaim(claim, qualifierId, parameter)
if result and references then result = result .. getReferences(frame, claim) end
end
if result then return result else
if showerrors then return error else return default end
Line 1,045:
end
end
data = data[index] or data[tonumber(index)]
if not data then
return
end
i = i + 1
end
|