Content deleted Content added
Remove getNormalValue |
Sync sandbox |
||
Line 79:
-- takes cardinal numer as a numeric and returns the ordinal as a string
-- we need three exceptions in English for 1st, 2nd, 3rd, 21st, .. 31st, etc.
-------------------------------------------------------------------------------
local function makeOrdinal(cardinal)
Line 105 ⟶ 103:
-- failing that it uses the wiki's content language.
-- It returns a language object
-------------------------------------------------------------------------------
local function findLang(langcode)
Line 125 ⟶ 121:
-- roundto takes a positive number (x)
-- and returns it rounded to (sf) significant figures
-------------------------------------------------------------------------------
local function roundto(x, sf)
Line 144 ⟶ 138:
-- decimalToDMS takes a decimal degrees (x) with precision (p)
-- and returns degrees/minutes/seconds according to the precision
-------------------------------------------------------------------------------
local function decimalToDMS(x, p)
Line 178 ⟶ 170:
-- and returns x rounded approximately to the given precision
-- precision should be between 1 and 1e-6, preferably a power of 10.
-------------------------------------------------------------------------------
local function decimalPrecision(x, p)
Line 191 ⟶ 181:
-- if it's integral, cast to an integer:
if x == math.floor(x) then x = math.floor(x) end
-- if it's less than 1e-4, it will be in exponent form, so return a string with 6dp
-- 9e-5 becomes 0.000090
if math.abs(x) < 1e-4 then x = string.format("%f", x) end
return x
end
Line 201 ⟶ 194:
-- df = ["dmy" / "mdy" / "y"] default will be "dmy"
-- bc = ["BC" / "BCE"] default will be "BCE"
-------------------------------------------------------------------------------
local format_Date = function(datetime, dateformat, bc)
Line 255 ⟶ 246:
-- it makes the empty string and nil into the (boolean) value passed as default
-- allowing the parameter to be true or false by default.
-------------------------------------------------------------------------------
local parseParam = function(param, default)
Line 278 ⟶ 267:
-- If it doesn't exist, return the id for the item
-- a second (boolean) value is also returned, value is true when the label exists
-------------------------------------------------------------------------------
local labelOrId = function (id)
Line 295 ⟶ 282:
-- it counts how many references are sourced to something not containing the word "wikipedia"
-- it returns a boolean = true if there are any sourced references.
-------------------------------------------------------------------------------
local sourced = function(claim)
Line 318 ⟶ 303:
-- multiple values are allowed, e.g. "preferred normal" (which is the default)
-- "best" will override the other flags, and set p and n
-------------------------------------------------------------------------------
local function setRanks(rank)
Line 347 ⟶ 330:
-- and (2) a table containing all of the statements for the propertyID and relevant Qid
-- if "best" ranks are requested, it returns those instead of all non-deprecated ranks
-------------------------------------------------------------------------------
local parseInput = function(frame, input_parm, property_id)
Line 415 ⟶ 396:
-- it needs the entityID and propertyID to link from the pen icon
-------------------------------------------------------------------------------
-- Dependencies: parseParam
-------------------------------------------------------------------------------
local function assembleoutput(out, args, entityID, propertyID)
Line 490 ⟶ 471:
-- and qualifiers that match the qualifierID if supplied.
-------------------------------------------------------------------------------
-- Dependencies: parseParam
-- makeOrdinal
-------------------------------------------------------------------------------
local function propertyvalueandquals(objproperty, args, qualID)
Line 543 ⟶ 524:
-- qualifiers don't have a mainsnak, properties do
local datatype = objproperty[1].datatype or objproperty[1].mainsnak.datatype
-- out holds the values for this property
-- mtl holds the language code if the datatype is monolingual text
local out = {}
local mtl = {}
for k, v in pairs(objproperty) do
local snak = v.mainsnak or v
Line 551 ⟶ 535:
-- nothing added to 'out': it isn't sourced when onlysourced=true
------------------------------------
elseif v.rank
-- nothing added to 'out': value
elseif snak.snaktype == "somevalue" then -- value is unknown
out[#out + 1] = i18n["Unknown"]
Line 573 ⟶ 550:
if linked then
if sitelink then
-- strip any namespace or dab from the sitelink
-- and use that as label
local pos = sitelink:find(":") or 0
label = sitelink:sub(pos+1):gsub("%s%(.+%)$", ""):gsub(",.+$", "")
out[#out + 1] = "[[" .. lprefix .. sitelink .. lpostfix .. "|" .. label .. "]]"
elseif islabel then
-- no sitelink, label exists
-- examine what an article with a title the same as the label would be
local
if artitle and artitle.redirectTarget then
-- there's a redirect with the same title as the label
-- let's link to that
out[#out + 1] = "[[".. lprefix .. label .. lpostfix
out[#out] = out[#out] .. "|" .. label .. "]]"
else
-- no sitelink, label exists, not a
--
if wdl then
-- show that there's a Wikidata entry available
wd = wd .. i18n["errors"]["local-article-not-found"]
wd = wd .. "'>[[File:Wikidata-logo.svg|16px|alt=|link=]]</span>"
out[#out
else
-- no
end -- test if article title exists as redirect on current Wiki
else
-- no sitelink and no label
Line 775 ⟶ 741:
else
local ns = "N"
local ew = "
if lat < 0 then
ns = "S"
Line 807 ⟶ 773:
------------------------------------
elseif datatype == "monolingualtext" then -- data type is Monolingual text:
-- has mainsnak.datavalue.value as a table containing
-- collect all the values in 'out' and languages in 'mtl' and process them later
out[#out+1] = prefix .. datavalue.text .. postfix
mtl[#out] = datavalue.language
------------------------------------
else
Line 825 ⟶ 787:
local qsep = (args.qsep or ""):gsub('"', '')
local qargs = {
["osd"] = "false",
["linked"] = tostring(linked),
["prefix"] = args.qprefix,
["postfix"] = args.qpostfix,
["linkprefix"] = args.qlinkprefix,
["linkpostfix"] = args.qlinkpostfix,
["wdl"] = "false",
["unitabbr"] = tostring(uabbr),
["maxvals"] = 0,
["sorted"] = args.qsorted,
["noicon"] = "true",
["list"] = "",
["sep"] = qsep,
["langobj"] = args.langobj,
}
if qualID == "DATES" then qargs.maxvals = 1 end
Line 874 ⟶ 836:
if maxvals > 0 and #out >= maxvals then break end
end -- of for each value loop
-- we need to pick one value to return if the datatype was "monolingualtext"
-- if there's only one value, use that
-- otherwise look through the fallback languages for a match
if datatype == "monolingualtext" and #out >1 then
local langcode = args.langobj.code
langcode = mw.text.split( langcode, '-', true )[1]
local fbtbl = mw.language.getFallbacksFor( langcode )
table.insert( fbtbl, 1, langcode )
bestval = ""
found = false
for idx1, lang1 in ipairs(fbtbl) do
for idx2, lang2 in ipairs(mtl) do
if (lang1 == lang2) and not found then
bestval = out[idx2]
found = true
break
end
end -- loop through values of property
end -- loop through fallback languages
if found then
-- replace output table with a table containing the best value
out = { bestval }
else
-- more than one value and none of them on the list of fallback languages
-- sod it, just give them the first one
out = { out[1] }
end
end
return out
end
Line 882 ⟶ 873:
-------------------------------------------------------------------------------
-- Dependencies: propertyvalueandquals(); assembleoutput();
-- parseParam
-- findLang
-------------------------------------------------------------------------------
local function _getvalue(frame, props, propertyID, entityid)
Line 915 ⟶ 906:
-- The function will now also return qualifiers if parameter qual is supplied
-------------------------------------------------------------------------------
-- Dependencies: setRanks(
-- labelOrId; i18n.latestdatequalifier; format_Date; makeOrdinal; roundto; decimalPrecision; decimalToDMS;
-------------------------------------------------------------------------------
p.getValue = function(frame)
Line 946 ⟶ 935:
-- now redundant to getValue with |rank=best
-------------------------------------------------------------------------------
-- Dependencies: p.getValue
-- parseParam; sourced; labelOrId; i18n.latestdatequalifier; format_Date;
-- makeOrdinal; roundto; decimalPrecision; decimalToDMS;
-------------------------------------------------------------------------------
p.getPreferredValue = function(frame)
Line 961 ⟶ 949:
-- optional 'display' parameter is allowed, defaults to "inline, title"
-------------------------------------------------------------------------------
-- Dependencies: setRanks
-------------------------------------------------------------------------------
p.getCoords = function(frame)
Line 1,011 ⟶ 999:
-- when the property is unsourced (or only sourced to Wikipedia)
-------------------------------------------------------------------------------
-- Dependencies: parseParam
-- i18n.latestdatequalifier; format_Date; findLang; makeOrdinal; roundto; decimalPrecision; decimalToDMS;
-------------------------------------------------------------------------------
p.getQualifierValue = function(frame)
Line 1,085 ⟶ 1,071:
-- The usual whitelisting, blacklisting, onlysourced, etc. are implemented
-------------------------------------------------------------------------------
-- Dependencies: parseParam
-- assembleoutput
-------------------------------------------------------------------------------
p.getValueByQual = function(frame)
Line 1,159 ⟶ 1,145:
-- The usual whitelisting, blacklisting, onlysourced, etc. are implemented
-------------------------------------------------------------------------------
-- Dependencies: parseParam
-------------------------------------------------------------------------------
p.getValueByLang = function(frame)
Line 1,237 ⟶ 1,222:
-- If there is no sitelink, it returns the label as plain text.
-- If there is no label in the local language, it displays the qid instead.
-------------------------------------------------------------------------------
p.getLink = function(frame)
Line 1,257 ⟶ 1,240:
-- It returns the Wikidata label for the local language as plain text.
-- If there is no label in the local language, it displays the qid instead.
-------------------------------------------------------------------------------
p.getLabel = function(frame)
Line 1,272 ⟶ 1,253:
-- If there is a sitelink to an article on the local Wiki, it returns the sitelink as plain text.
-- If there is no sitelink, it returns nothing.
-------------------------------------------------------------------------------
p.getAT = function(frame)
Line 1,289 ⟶ 1,268:
-- It returns the article description for the Wikidata entity if the local parameter is "Wikidata".
-- Nothing is returned if the description doesn't exist or "none" is passed as the local parameter.
-------------------------------------------------------------------------------
p.getDescription = function(frame)
Line 1,309 ⟶ 1,286:
-- pageId returns the page id (entity ID, Qnnn) of the current page
-- returns nothing if the page is not connected to Wikidata
-------------------------------------------------------------------------------
function p.pageId(frame)
Line 1,320 ⟶ 1,295:
-- formatDate is a wrapper to export the private function format_Date
-------------------------------------------------------------------------------
-- Dependencies: format_Date
-------------------------------------------------------------------------------
p.formatDate = function(frame)
Line 1,335 ⟶ 1,310:
-- {{#if:{{#invoke:WikidataIB |checkBlacklist |name=Jim |suppressfields=Dave; Joe; Fred}} | not blacklisted | blacklisted}}
-- displays "not blacklisted"
-------------------------------------------------------------------------------
p.checkBlacklist = function(frame)
Line 1,364 ⟶ 1,337:
-- with a form that uses just a single call to Wikidata:
-- {{#invoke |WikidataIB |emptyor |1= <span class="xxx">{{#invoke:WikidataIB |getvalue |P99 |fwd=ALL}}</span> }}
-------------------------------------------------------------------------------
p.emptyor = function(frame)
Line 1,395 ⟶ 1,366:
-- getLang returns the MediaWiki language code of the current content.
-- If optional parameter |style=full, it returns the language name.
p.getLang = function(frame)
local style = (frame.args.style or ""):lower()
Line 1,413 ⟶ 1,381:
-- failing that, the Wikidata entity ID asociated with the curent page, if it exists
-- otherwise, nothing
p.getQid = function(frame)
local qid = (frame.args.qid or ""):upper()
Line 1,439 ⟶ 1,404:
-- The number is the first unnamed parameter or "|num="
-------------------------------------------------------------------------------
-- Dependencies:
-------------------------------------------------------------------------------
p.formatNumber = function(frame)
Line 1,448 ⟶ 1,413:
end
-------------------------------------------------------------------------------
-- examine dumps the property (the unnamed parameter) from the item given by the parameter 'qid'
-- or from the item corresponding to the current page if qid is not supplied.
-------------------------------------------------------------------------------
p.examine = function( frame )
pid = mw.text.trim( frame.args[1] or frame.args.pid or "" )
if pid == "" then return "No property supplied" end
qid = frame.args.qid or ""
if qid == "" then qid = mw.wikibase.getEntityIdForCurrentPage end
if not qid then return "No item for this page" end
return mw.dumpObject( mw.wikibase.getAllStatements( qid, pid ) )
end
return p
|