Modul:WikidataIB: Perbedaan antara revisi

Konten dihapus Konten ditambahkan
Dari WP:EN.
 
Perbarui sintaks LUA per versi terbaru dari modul yang bersangkutan.
Baris 12:
local i18n =
{
["errors"] =
{
{
["property-not-found"] = "PropertyProperti nottidak foundditemukan.",
["entity-not-found"] = "Butir Wikidata entitytidak not foundditemukan.",
["unknown-claim-type"] = "UnknownJenis claimklaim typeyang tidak diketahui.",
["unknown-entity-type"] = "UnknownJenis entitybutir typeyang tidak diketahui.",
["qualifier-not-found"] = "QualifierPenanda nottidak foundditemukan.",
["site-not-found"] = "Proyek Wikimedia projecttidak not foundditemukan.",
["unknown-datetime-format"] = "UnknownFormat datetime formatyang tidak diketahui.",
["local-article-not-found"] = "ArticleArtikel isini available ontersedia Wikidata, buttetapi nottidak ondi Wikipedia"
},
["months"] =
{
{
"JanuaryJanuari", "FebruaryFebruari", "MarchMaret", "April", "MayMei", "JuneJuni", "JulyJuli", "AugustAgustus", "September", "OctoberOktober", "November", "DecemberDesember"
},
["century"] = "centuryabad",
["BC"] = "BCM",
["BCE"] = "BCESM",
["ordinal"] =
{
Baris 38:
},
["filespace"] = "File",
["editonwikidata"] = "EditSunting thisini ondi Wikidata",
["latestdatequalifier"] = function (date) return "before " .. date end,
-- some languages, e.g. Bosnian use a period as a suffix after each number in a date
["datenumbersuffix"] = "",
["abbr"] =
{
["Q828224"] = "km",
["Q11573"] = "m",
["Q174728"] = "cm",
["Q174789"] = "mm",
["Q712226"] = "sq km",
["Q11570"] = "kg",
["Q41803"] = "g",
["Q3241121"] = "mg",
["Q2332346"] = "ml",
},
}
 
--require("Module:i18n").loadI18n("Module:WikidataIB/i18n", i18n)
-- got idea from [[:w:Module:Wd]]
local module_title; if ... == nil then
module_title = mw.getCurrentFrame():getTitle()
else
module_title = ...
end
require('Module:i18n').loadI18n(module_title..'/i18n', i18n)
 
 
Baris 55 ⟶ 62:
-- Private functions
-------------------------------------------------------------------------------
--
 
-------------------------------------------------------------------------------
-- makeOrdinal needs to be internationalised along with the above:
Baris 76 ⟶ 83:
return tostring(cardinal) .. ordsuffix
end
 
 
-------------------------------------------------------------------------------
-- roundto takes a positive number (x)
-- and returns it rounded to (sf) significant figures
local function roundto(x, sf)
x = math.abs(x)
if sf < 1 then sf = 1 end
local e = math.floor(math.log10(x)) - sf + 1
local m = math.floor(x / 10^e + 0.5)
x = m * 10^e
-- if it's integral, cast to an integer:
if x == math.floor(x) then x = math.floor(x) end
return x
end
 
 
-------------------------------------------------------------------------------
Baris 88 ⟶ 111:
-- keep anything before punctuation - we just want a single date:
local dateval = string.match( datetime, "[%w ]+")
 
local dateformat = string.lower(dateformat or "dmy") -- default to dmy
 
local bc = string.upper(bc or "") -- can't use nil for bc
-- we only want to accept two possibilities: BC or default to BCE
Baris 98 ⟶ 121:
bc = "&nbsp;" .. i18n["BCE"]
end
 
local postchrist = true -- start by assuming no BCE
local dateparts = {}
Baris 106 ⟶ 129:
else
-- we'll keep the parts that are not 'BCE' in a table
dateparts[#dateparts + 1] = word
end
end
if postchrist then bc = "" end -- set AD dates to no suffix **internationalise later**
 
local sep = "&nbsp;" -- separator is nbsp
local fdate = table.concat(dateparts, "&nbsp;") -- formatted date defaults to same order as input
 
-- if we have day month year, check dateformat
if #dateparts == 3 then
Baris 124 ⟶ 147:
fdate = dateparts[2]
end
 
return fdate .. bc
end
 
 
-------------------------------------------------------------------------------
Baris 134 ⟶ 158:
-- allowing the parameter to be true or false by default.
local parseParam = function(param, default)
if param and (#param >~= 0)"" then
param = param:lower()
if (param == "false") or (param == "no") or (param == "0") then
Baris 145 ⟶ 169:
end
end
 
 
-------------------------------------------------------------------------------
-- The label in a Wikidata item is subject to vulnerabilities
-- that an attacker might try to exploit.
-- It needs to be 'sanitised' by removing any wikitext before use.
Baris 159 ⟶ 184:
end
end
 
 
-------------------------------------------------------------------------------
Baris 175 ⟶ 201:
return refs > 0
end
 
 
-- parseInput processes the Q-id , the blacklist and the whitelist
Baris 182 ⟶ 209:
local parseInput = function(frame, input_parm, property_id)
-- There may be a local parameter supplied, if it's blank, set it to nil
local input_parm = mw.text.trim(input_parm or "")
if input_parm and (#input_parm == 0)"" then input_parm = nil end
 
-- can take a named parameter |qid which is the Wikidata ID for the article.
-- This will not normally be used because it's an expensive call.
local qidargs = frame.args.qid
iflocal qid and (#qid == 0) then args.qid = nil end
if qid == "" then qid = nil end
 
-- The blacklist is passed in named parameter |suppressfields
local blacklist = frame.args.suppressfields or args.spf
 
-- The whitelist is passed in named parameter |fetchwikidata
local whitelist = frame.args.fetchwikidata or args.fwd
 
-- The name of the field that this function is called from is passed in named parameter |name
local fieldname = frame.args.name
if blacklist then
-- The name is compulsory when blacklist is used, so return nil if it is not supplied
if not fieldname or (#fieldname == 0)"" then return false, nil, nil end
-- If this field is on the blacklist, then return nil
if blacklist:find(fieldname) then return false, nil, nil end
end
 
-- If we got this far then we're not on the blacklist
-- The blacklist overrides any locally supplied parameter as well
-- If a non-blank input parameter was supplied return it
if input_parm then return false, input_parm, nil end
 
-- Otherwise see if this field is on the whitelist:
if not (whitelist and (whitelist == 'ALL' or whitelist:find(fieldname))) then
Baris 215 ⟶ 243:
return false, input_parm, nil
end
 
-- See what's on Wikidata:
local entity = mw.wikibase.getEntityObject(qid)
if entity and entity.claims then
local props = entity.claims[property_id]
if props and props[1] then
return true, entity, props
end
Baris 227 ⟶ 255:
return false, input_parm, nil
end
 
 
local function _getvalue(frame, entity, props, filter, propertyID)
Baris 232 ⟶ 261:
-- if nothing or an empty string is passed set it true
-- if "false" or "no" or "0" is passed set it false
local onlysrcargs = parseParam(frame.args.onlysourced, true)
local onlysrc = parseParam(args.onlysourced or args.osd, true)
 
-- noicon is a boolean passed to suppress the trailing "edit at Wikidata" icon
-- for use when the value is processed further by the infobox
-- if nothing or an empty string is passed set it false
-- if "false" or "no" or "0" is passed set it false
local noic = parseParam(frame.args.noicon, false)
 
-- wdlinks is a boolean passed to enable links to Wikidata when no article exists
-- if nothing or an empty string is passed set it false
-- if "false" or "no" or "0" is passed set it false
local wdl = parseParam(frame.args.wdlinks or args.wdl, false)
 
-- sorted is a boolean passed to enable sorting of the values returned
-- if nothing or an empty string is passed set it false
-- if "false" or "no" or "0" is passed set it false
local sorted = parseParam(frame.args.sorted, false)
 
-- separatorsep is a string that is used to separate multiple returned values
-- if nothing or an empty string is passed set it to the default
-- any double-quotes " are stripped out, so that spaces may be passed
-- e.g. |sep=" - "
local sepdefault = ", " -- **internationalise later**
local separator = frame.args.sep or ""
separator = string.gsub(separator, '"', '')
if #separator == 0 then
separator = sepdefault
end
 
-- list is the name of a stringtemplate that maya list of bemultiple "",values "hlist"is orpassed "ubl"through
-- examples include "hlist" and "ubl"
-- this controls whether multiple values are output as comma-separated
local list = args.list or ""
-- as a horizontal list (hlist) or unbulleted list (ubl)
 
local list = frame.args.list or ""
if list~="hlist" and list~="ubl" then list = "" end
-- prefix is a string that may be nil, empty (""), or a string of characters
-- this is prefixed to each value
-- useful when when multiple values are returned
-- any double-quotes " are stripped out, so that spaces may be passed
local prefix = frame.args.prefix or ""
prefix = string.gsub(prefix, '"', '')
 
-- postfix is a string that may be nil, empty (""), or a string of characters
-- this is postfixed to each value
-- useful when when multiple values are returned
-- any double-quotes " are stripped out, so that spaces may be passed
local postfix = frame.args.postfix or ""
postfix = string.gsub(postfix, '"', '')
 
-- linkprefix is a string that may be nil, empty (""), or a string of characters
-- this creates a link and is then prefixed to each value
-- useful when when multiple values are returned and indirect links are needed
-- any double-quotes " are stripped out, so that spaces may be passed
local lprefix = frame.args.linkprefix or ""
lprefix = string.gsub(lprefix, '"', '')
 
-- linkpostfix is a string that may be nil, empty (""), or a string of characters
-- this is postfixed to each value when linking is enabled with lprefix
-- useful when when multiple values are returned
-- any double-quotes " are stripped out, so that spaces may be passed
local lpostfix = frame.args.linkpostfix or ""
lpostfix = string.gsub(lpostfix, '"', '')
 
-- maxvals is a string that may be nil, empty (""), or a number
-- this determines how many items may be returned when multiple values are available
-- setting it = 1 is useful where the returned string is used within another call, e.g. image
local maxvals = tonumber(args.maxvals) or 0
 
-- unitabbr is a boolean passed to enable unit abbreviations for common units
-- if nothing or an empty string is passed set it false
-- if "false" or "no" or "0" is passed set it false
local uabbr = parseParam(args.unitabbr, false)
 
-- So now we have something to return: deal with:
-- (1) ["datatype"] = "wikibase-item";
Baris 303 ⟶ 341:
-- ["datatype"] = "url"
-- (4) ["datatype"] = "globe-coordinate", or anything else
 
local lang = mw.language.getContentLanguage().code
local thisQid = entity.id
Baris 313 ⟶ 351:
local datavalue = v.mainsnak.datavalue
datavalue = datavalue and datavalue.value
if (not filter(v)) or (onlysrc and not sourced(v)) then
-- do nothing; either isn't preferred when prefered values are requested, or isn't sourced when onlysourced=true
elseif v.mainsnak.snaktype == "somevalue" then -- check for value is unknown
Baris 326 ⟶ 364:
local label = labelOrId(qnumber)
if sitelink then
out[#out + 1] = "[[" .. lprefix .. sitelink .. lpostfix .. "|" .. label .. "]]"
else
-- no sitelink, so check first for a redirect with that label
Baris 334 ⟶ 372:
-- no sitelink, but there's a redirect with the same title as the label
-- let's link to that
out[#out + 1] = "[[".. lprefix .. label .. lpostfix .. "|" .. label .. "]]"
else
-- no sitelink and not a redirect but an article exists with the same title as the label
Baris 374 ⟶ 412:
if dateprecision >= 10 then -- prepend month
fpvdate = i18n.months[tonumber(timestamp:sub(7, 8))] .. " " .. fpvdate
dateformat = frame.args.df
if dateprecision >= 11 then -- prepend day
fpvdate = tonumber(timestamp:sub(10, 11)) .. " " .. fpvdate
Baris 382 ⟶ 420:
end
end
fdate = format_Date(fpvdate, dateformat, frame.args.bc)
elseif dateprecision == 7 then -- century
local century = math.floor((fpvdate - 1) / 100) + 1
fdate = makeOrdinal(century) .. "&nbsp;" .. i18n["century"]
if timestamp:sub(1, 1) == "-" then -- date is BC
local bc = string.upper(frame.args.bc or "") -- can't use nil for bc
-- we only want to accept two possibilities: BC or default to BCE
if bc == "BC" then
Baris 404 ⟶ 442:
-- commonsMedia or external-id or string or url
-- all have mainsnak.datavalue.value as string
if lprefix >~= "" then
out[#out+1] = "[[" .. lprefix .. datavalue .. lpostfix .. "|" .. prefix .. datavalue .. postfix .. "]]"
else
Baris 414 ⟶ 452:
-- the unit is of the form http://www.wikidata.org/entity/Q829073
-- convert amount to a number
local amt
local amount = tonumber(datavalue.amount) or "Not a number"
-- check if upper and/or lower bounds are given and construct a string if sosignificant
local upb = tonumber(datavalue.upperBound)
if upb then
amt = amount .. " +" .. (upb - amount)
end
local lowb = tonumber(datavalue.lowerBound)
if upb and lowb then
-- differences rounded to 2 sig fig:
amt = (amt or amount) .. " -" .. (amount - lowb)
local posdif = roundto(upb - amount, 2)
end
local negdif = roundto(amount - lowb, 2)
-- check if bounds are symmetrical and re-write string if so
if upb and lowb and ((upb - amount)posdif ~== (amount - lowb))negdif then
-- non-symmetrical
amt = amount .. " ±" .. (amount - lowb)
amount = amount .. " +" .. posdif .. " -" .. negdif
elseif posdif ~= 0 then
-- symmetrical and significant
amount = amount .. " ±" .. posdif
end
-- otherwise range is zero, so leave it off: amount = amount
end
-- extract the qid in the form 'Qnnn' from the value.unit url and then fetch the label from that
Baris 433 ⟶ 473:
local unitqid = string.match( datavalue.unit, "(Q%d+)" )
if unitqid then
unitif =uabbr mwand i18n.wikibase.label( abbr[unitqid] )then
-- it's on the list of common abbreviations, so use that
if unit then unit = " " .. unit end
unit = "&nbsp;" .. i18n.abbr[unitqid]
else
-- otherwise use the label (i.e. unit name) if it exists
local uname = mw.wikibase.label( unitqid )
if uname then unit = " " .. uname end
end
end
out[#out+1] = (amt or amount) .. unit
else
-- some other data type
Baris 445 ⟶ 491:
break
end -- of datatype/unknown value/sourced check
if maxvals > 0 and #out >= maxvals then break end
end -- of for each value loop
 
-- if there's anything to return, then return a list
-- comma-separated by default, but may be specified by the sep parameter
Baris 452 ⟶ 499:
if #out > 0 then
if sorted then table.sort(out) end
-- if there's a pen icon add it the end of the last value
if list == "hlist" then
if not noic then
return frame:expandTemplate{title = 'Hlist', args = out}
out[#out] = out[#out] .. icon
elseif list == "ubl" then
end
return frame:expandTemplate{title = 'Unbulleted list', args = out}
if list == "" then
return table.concat(out, separator)
else
return frame:expandTemplate{title = list, args = out}
if noic then
return table.concat(out, separator)
else
return table.concat(out, separator) .. icon
end
end
else
Baris 467 ⟶ 512:
end
end
 
 
 
Baris 472 ⟶ 518:
-- Public functions
-------------------------------------------------------------------------------
--
-- getValue is used to get a value, or a comma separated list of them if multiple values exist
--
Baris 477 ⟶ 524:
p.getValue = function(frame)
local propertyID = mw.text.trim(frame.args[1] or "")
 
local success, errorOrEntity, props = parseInput(frame, frame.args[2], propertyID)
if not success then
return errorOrEntity
else
entity = errorOrEntity
end
local function filter(claim)
return true
end
return _getvalue(frame, entityerrorOrEntity, props, filter, propertyID)
end
 
 
-------------------------------------------------------------------------------
-- getPreferredValue is used to get a value, or a comma separated list of them if multiple values exist
Baris 495 ⟶ 542:
p.getPreferredValue = function(frame)
local propertyID = mw.text.trim(frame.args[1] or "")
 
local success, errorOrEntity, props = parseInput(frame, frame.args[2], propertyID)
if not success then
return errorOrEntity
else
entity = errorOrEntity
end
local prefflag = false
for k, v in pairs(props) do
if v.rank == "preferred" then
prefflag = true
break
end
Baris 512 ⟶ 557:
return claim.rank == "preferred" or prefflag == false
end
return _getvalue(frame, entityerrorOrEntity, props, filter, propertyID)
end
 
 
-------------------------------------------------------------------------------
-- getSourcedValuegetNormalValue is used to get a value, or a comma separated list of them if multiple values exist
-- If normal ranks are set, it will return those values, otherwise all values
-- but only values that are sourced are returned
-- redundant to getValue with onlysourced=true but kept for backwards compatibility
-- now defined via getValue
--
p.getSourcedValuegetNormalValue = function(frame)
local propertyID = mw.text.trim(frame.args.onlysourced[1] =or "yes")
return p.getValue(frame)
end
 
local success, errorOrEntity, props = parseInput(frame, frame.args[2], propertyID)
if not success then
return errorOrEntity
end
local normflag = false
for k, v in pairs(props) do
if v.rank == "normal" then
normflag = true
break
end
end
local function filter(claim)
return claim.rank == "normal" or normflag == false
end
return _getvalue(frame, errorOrEntity, props, filter, propertyID)
end
 
-------------------------------------------------------------------------------
Baris 534 ⟶ 592:
p.getCoords = function(frame)
local propertyID = "P625"
 
-- if there is a 'display' parameter supplied, use it
-- otherwise default to "inline, title"
local disp = frame.args.display
if (not disp) or (#disp == 0)"" then
disp = "inline, title"
end
 
local success, errorOrEntity = parseInput(frame, frame.args[1], property_idpropertyID)
if not success then
return errorOrEntity
Baris 557 ⟶ 615:
-- read the latitude numbers into a table
for num in mw.ustring.gmatch(lat, "%d+%.?%d*") do
lat_long[#lat_long + 1] = num
end
-- add the N/S
Baris 577 ⟶ 635:
-------------------------------------------------------------------------------
-- getQualifierValue is used to get a formatted value of a qualifier
--
-- The call needs: a property (the unnamed parameter or 1=)
-- a target value for that property (pval=)
Baris 584 ⟶ 642:
-- The boolean onlysourced= parameter can be set to return nothing
-- when the property is unsourced (or only sourced to Wikipedia)
--
p.getQualifierValue = function(frame)
local args = frame.args
local propertyID = mw.text.trim(frame.args[1] or "")
 
-- The PropertyID of the target value of the property
-- whose qualifier is to be returned is passed in named parameter |pval=
local propvalue = frame.args.pval
 
-- The PropertyID of the qualifier
-- whose value is to be returned is passed in named parameter |qual=
local qualifierID = frame.args.qual
 
-- onlysourced is a boolean passed to return qualifiers
Baris 600 ⟶ 659:
-- if nothing or an empty string is passed set it false
-- if "false" or "no" or 0 is passed set it false
local onlysrc = parseParam(frame.args.onlysourced, false)
 
 
local success, errorOrEntity, props = parseInput(frame, frame.args[2], propertyID)
if not success then
return errorOrEntity
Baris 647 ⟶ 706:
return nil
end
 
 
-------------------------------------------------------------------------------
Baris 684 ⟶ 744:
if itemID == "" then return end
return mw.wikibase.sitelink(itemID)
end
 
 
-------------------------------------------------------------------------------
-- getDescription returns the article description for a Qid
-- Qid defaults to the current article if omitted
-- Any local parameter supplied (other than "Wikidata") becomes the return value
-- Nothing is returned if the description doesn't exist or 'none' is passed locally
--
p.getDescription = function(frame)
local desc = mw.text.trim(frame.args[1] or "")
local itemID = mw.text.trim(frame.args.qid or "")
if itemID == "" then itemID = nil end
if desc:lower() == 'wikidata' then
return mw.wikibase.description(itemID)
elseif desc:lower() == 'none' then
return nil
else
return desc
end
end
 
Baris 693 ⟶ 773:
return format_Date(frame.args[1], frame.args.df, frame.args.bc)
end
 
 
-------------------------------------------------------------------------------
Baris 710 ⟶ 791:
end
 
 
-------------------------------------------------------------------------------
-- emptyor returns nil if its first unnamed argument is just punctuation, whitespace or html tags
-- otherwise it returns the argument unchanged (including leading/trailing space)
-- unless the argument may contain "=" when it must be called explicitly:
-- |1=arg
-- (when leading/trailing spaces are trimmed)
p.emptyor = function(frame)
local s = frame.args[1]
if not s or s == "" then return nil end
local sx = s:gsub("%s", ""):gsub("<[^>]*>", ""):gsub("%p", "")
if sx == "" then
return nil
else
return s
end
end
 
return p