Modulo:WikidataIB: differenze tra le versioni
Contenuto cancellato Contenuto aggiunto
m modulo inesistente su it.wiki |
sync con en.wiki (oldid 1164721334) |
||
(Una versione intermedia di un altro utente non mostrate) | |||
Riga 1:
-- Version:
-- Module to implement use of a blacklist and whitelist for infobox fields
-- Can take a named parameter |qid which is the Wikidata ID for the article
Riga 11:
-- whitelist is passed in named parameter |fetchwikidata (or |fwd)
require("strict")
local p = {}
local cdate -- initialise as nil and only load _complex_date function if needed
--
-- Module:Calendar
-- Module:ISOdate
-- Module:DateI18n
-- Module:I18n/complex date
-- Module:Ordinal
-- Module:I18n/ordinal
-- Module:Yesno
-- Module:Formatnum
-- Module:Linguistic
--
-- The following, taken from https://www.mediawiki.org/wiki/Wikibase/DataModel#Dates_and_times,
-- is needed to use Module:Complex date which seemingly requires date precision as a string.
Line 151 ⟶ 160:
langobj = mw.language.new( langcode )
else
langcode = mw.getCurrentFrame():
if mw.language.isKnownLanguageTag(langcode) then
langobj = mw.language.new( langcode )
Line 405 ⟶ 414:
end
if not cdate then
cdate = require("Module:Complex date")._complex_date
end
local fdate = cdate(calendarmodel, adj, tostring(iso), dp[dprec], bc, "", "", "", "", lang, 1)
Line 566 ⟶ 574:
-- shortname is boolean switch to use P1813 (short name) instead of label if true.
-- lang is the current language code.
-- uselbl is boolean switch to force display of the label instead of the sitelink (default: false)
-- linkredir is boolean switch to allow linking to a redirect (default: false)
-- formatvalue is boolean switch to allow formatting as italics or quoted (default: false)
-------------------------------------------------------------------------------
-- Dependencies: labelOrId(); donotlink[]
-------------------------------------------------------------------------------
local linkedItem = function(id,
local lprefix = (args.lp or args.lprefix or args.linkprefix or ""):gsub('"', '') -- toughen against nil values passed
local lpostfix = (args.lpostfix or ""):gsub('"', '')
local prefix = (args.prefix or ""):gsub('"', '')
local postfix = (args.postfix or ""):gsub('"', '')
local dtxt = args.dtxt
local shortname = args.shortname or args.sn
local lang = args.lang or "en" -- fallback to default if missing
local uselbl = args.uselabel or args.uselbl
uselbl = parseParam(uselbl, false)
local linkredir = args.linkredir
linkredir = parseParam(linkredir, false)
local formatvalue = args.formatvalue or args.fv
formatvalue = parseParam(formatvalue, false)
-- see if item might need italics or quotes
local fmt = ""
if next(formats) and formatvalue then
for k, v in ipairs( mw.wikibase.getBestStatements(id, "P31") ) do
fmt = formats[v.mainsnak.datavalue.value.id]
break -- pick the first match
end
end
end
Line 607 ⟶ 628:
if sitelink then
if not (dtxt or shortname) then
-- if sitelink and label are the same except for case, no need to process further
local
if pos > 0 then
if mw.site.namespaces[pfx] then -- that prefix is a valid namespace, so remove it
slink = sitelink:sub(pos+1)
end
end
-- remove stuff after commas or inside parentheses - ie. dabs
slink = slink:gsub("%s%(.+%)$", ""):gsub(",.+$", "")
-- if uselbl is false, use sitelink instead of label
if not uselbl then
-- use slink as display, preserving label case - find("^%u") is true for 1st char uppercase
if label:find("^%u") then
label = slink:gsub("^(%l)", string.upper)
else
label = slink:gsub("^(%u)", string.lower)
end
end
end
end
Line 631 ⟶ 658:
end
elseif islabel then
-- no sitelink, label exists, so check if a redirect with that title exists, if linkredir is true
-- display plain label by default
disp = prefix .. fmt .. label .. fmt .. postfix
if linkredir then
if not donotlink[label] and artitle and artitle.redirectTarget then
-- there's a redirect with the same title as the label, so let's link to that
disp = "[[".. lprefix .. label .. lpostfix .. "|" .. prefix .. fmt .. label .. fmt .. postfix .. "]]"
end
end -- test if article title exists as redirect on current Wiki
else
Line 796 ⟶ 823:
-- createicon assembles the "Edit at Wikidata" pen icon.
-- It returns a wikitext string inside a span class="penicon"
-- if entityID is nil or empty, the ID associated with current page is used
-- langcode and propertyID may be nil or empty
-------------------------------------------------------------------------------
-- Dependencies: i18n[];
-------------------------------------------------------------------------------
local createicon = function(langcode, entityID, propertyID)
langcode = langcode or ""
if not entityID or entityID == "" then entityID= mw.wikibase.getEntityIdForCurrentPage() end
propertyID = propertyID or ""
local icon = " <span class='penicon autoconfirmed-show'>[["
-- " <span data-bridge-edit-flow='overwrite' class='penicon'>[[" -> enable Wikidata Bridge
.. i18n["filespace"]
Line 806 ⟶ 838:
.. i18n["editonwikidata"]
.. "|link=https://www.wikidata.org/wiki/" .. entityID
if langcode ~= "" then icon = icon .. "?uselang=" .. langcode end
if propertyID ~= "" then icon = icon .. "#" .. propertyID end
icon = icon .. "|" .. i18n["editonwikidata"] .. "]]</span>"
return icon
Line 816 ⟶ 848:
-- assembleoutput takes the sequence table containing the property values
-- and formats it according to switches given. It returns a string or nil.
-- It
-------------------------------------------------------------------------------
-- Dependencies: parseParam();
Line 940 ⟶ 972:
local qnumber = dv.id
if linked then
val = linkedItem(qnumber
else -- no link wanted so check for display-text, otherwise test for lang code
local label, islabel
Line 1 248 ⟶ 1 280:
args.pd = pd
-- allow qualifiers to have a different date format; default to year unless qualsonly is set
args.qdf = args.qdf or args.qualifierdateformat or args.df or (not qualsonly and "y")
local lang = args.lang or
-- qualID is a string list of wanted qualifiers or "ALL"
Line 1 376 ⟶ 1 408:
elseif t ~= "" then
if qualsonly then
out[#out+1] = timeend
elseif timeend == "" then
out[#out+1] = timestart
else
out[#out+1] = timestart .. dsep .. timeend
end
else
out[#out] = out[#out] .. " (" .. timestart .. dsep .. timeend .. ")"
Line 1 456 ⟶ 1 494:
local uabbr = parseParam(args.unitabbr or args.uabbr, false)
local filter = (args.unit or ""):upper()
local maxvals = tonumber(args.maxvals) or 0
if filter == "" then filter = nil end
Line 1 487 ⟶ 1 526:
return nil
end -- of check for string
if maxvals > 0 and #out >= maxvals then break end
end -- of loop through values of propertyID
return assembleoutput(out, frame.args, qid, propertyID)
Line 1 529 ⟶ 1 569:
end
for i2, v2 in ipairs(proptbl) do
local parttbl = v2.qualifiers and v2.qualifiers.P518
if parttbl then
-- this higher ___location has qualifier 'applies to part' (P518)
Line 1 561 ⟶ 1 601:
if #inst > 0 then
for k, v in ipairs(inst) do
local instid = v.mainsnak.datavalue and v.mainsnak.datavalue.value.id
-- stop if it's a country (or a country within the United Kingdom if skip is true)
if instid == "Q6256" or instid == "Q3624078" or (skip and instid == "Q3336843") then
Line 1 573 ⟶ 1 613:
if prop and prop.mainsnak.datavalue then
if not skip or count == 0 then
local args = { lprefix = ":" }
out[#out+1] = linkedItem(qid, args) -- get a linked value if we can
end
qid, prevqid = prop.mainsnak.datavalue.value.id, qid
Line 1 805 ⟶ 1 846:
-- getCoords is used to get coordinates for display in an infobox
-- whitelist and blacklist are implemented
-- optional 'display' parameter is allowed, defaults to nil - was "inline, title"
-------------------------------------------------------------------------------
-- Dependencies: setRanks(); parseInput(); decimalPrecision();
Line 1 813 ⟶ 1 854:
-- if there is a 'display' parameter supplied, use it
-- otherwise default to
local disp = frame.args.display or ""
if disp == "" then
disp = nil -- default to not supplying display parameter, was "inline, title"
end
Line 1 874 ⟶ 1 915:
-- whose value is to be returned is passed in named parameter |qual=
local qualifierID = frame.args.qual
-- A filter can be set like this: filter=P642==Q22674854
local filter, fprop, fval
local ftable = mw.text.split(frame.args.filter or "", "==")
if ftable[2] then
fprop = mw.text.trim(ftable[1])
fval = mw.text.trim(ftable[2])
filter = true
end
-- onlysourced is a boolean passed to return qualifiers
Line 1 899 ⟶ 1 949:
for k1, v1 in pairs(props) do
if v1.mainsnak.snaktype == "value" and v1.mainsnak.datavalue.type == "wikibase-entityid" then
-- It's a wiki-linked value, so check if it's the target (in propvalue) and if it has qualifiers
if v1.mainsnak.datavalue.value.id == propvalue and v1.qualifiers then
if onlysrc == false or sourced(v1) then
-- if we've got this far, we have a (sourced) claim with qualifiers
-- which matches the target, so apply the filter and find the value(s) of the qualifier we want
if quals then
-- can't reference qualifer, so set onlysourced = "no" (args are strings, not boolean) local qargs = frame.args
qargs.onlysourced = "no"
local vals = propertyvalueandquals(quals, qargs, qid)
for k, v in ipairs(vals) do
out[#out + 1] = v
end
end
end
Line 2 095 ⟶ 2 146:
-- Dependencies: parseParam; setRanks; parseInput; sourced; propertyvalueandquals assembleoutput;
-------------------------------------------------------------------------------
p.
args.reqranks = setRanks(args.rank)
args.langobj = findLang(args.lang)
Line 2 113 ⟶ 2 163:
local maxvals = tonumber(args.maxvals) or 0
local out = {}
for i, v in ipairs(props) do
local snak = v.mainsnak
Line 2 127 ⟶ 2 177:
return assembleoutput(out, args, qid, pid)
end
p.getPropertyIDs = function(frame)
local args = frame.args
return p._getPropertyIDs(args)
end
Line 2 162 ⟶ 2 217:
qlist = qlist:gsub("[%p%s]+", " ") .. " "
local out = {}
for i, v in ipairs(props) do
local snak = v.mainsnak
Line 2 196 ⟶ 2 251:
-- Dependencies: parseParam; setRanks; parseInput; sourced; propertyvalueandquals assembleoutput;
-------------------------------------------------------------------------------
p.
-- parameter sets for commonly used groups of parameters
local paraset = tonumber(args.ps or args.parameterset or 0)
if paraset == 1 then
-- a common setting
args.rank = "best"
args.fetchwikidata = "ALL"
args.onlysourced = "no"
args.noicon = "true"
elseif paraset == 2 then
-- equivalent to raw
args.rank = "best"
args.fetchwikidata = "ALL"
args.onlysourced = "no"
args.noicon = "true"
args.linked = "no"
args.pd = "true"
elseif paraset == 3 then
-- third set goes here
end
args.reqranks = setRanks(args.rank)
args.langobj = findLang(args.lang)
args.lang = args.langobj.code
local pid1 = args.prop1 or args.pid1 or ""
local pid2 = args.prop2 or args.pid2 or ""
if pid1 == "" or pid2 == "" then return nil end
local f = {}
f.args = args
local qid1, statements1 = parseInput(f, args[1], pid1)
-- parseInput nulls empty args[1] and returns args[1] if nothing on Wikidata
if not qid1 then return statements1 end
-- otherwise it returns the qid and a table for the statement
local onlysrc = parseParam(args.onlysourced or args.osd, true)
local maxvals = tonumber(args.maxvals) or 0
Line 2 232 ⟶ 2 310:
end -- of loop through values of property1
return assembleoutput(out, args, qid1, pid1)
end
p.getPropOfProp = function(frame)
local args= frame.args
if not args.prop1 and not args.pid1 then
args = frame:getParent().args
if not args.prop1 and not args.pid1 then return i18n.errors["No property supplied"] end
end
return p._getPropOfProp(args)
end
Line 2 412 ⟶ 2 500:
if maxvals > 0 and #cat2 >= maxvals then break end
end
local out = {}
for k1, v1 in ipairs(cat1) do
for k2, v2 in ipairs(cat2) do
Line 2 696 ⟶ 2 784:
if not (whitelist == 'ALL' or whitelist:find(fieldname)) then return nil end
local qid =
if qid == "" then qid =
if not qid or not mw.wikibase.entityExists(qid) then return nil end
local
if not aliases then return nil end
args.langobj = findLang(args.lang)
Line 2 906 ⟶ 2 992:
-------------------------------------------------------------------------------
-- followQid takes
-- If qid is not given, it uses the qid for the connected page
-- or returns nil if there isn't one.
Line 2 912 ⟶ 2 998:
-- If props is given, the Wikidata item for the qid is examined for each property in turn.
-- If that property contains a value that is another Wikibase-item, that item's qid is returned,
-- and the search terminates, unless |all=y when all of the qids are returned,
-- If |list= is set to a template, the qids are passed as arguments to the template.
-- If props is not given, the qid is returned.
-------------------------------------------------------------------------------
-- Dependencies: parseParam()
-------------------------------------------------------------------------------
p.
local qid = (
local all = parseParam(
local list = args.list or ""
if list == "" then list = nil end
if qid == "" then
qid = mw.wikibase.getEntityIdForCurrentPage()
Line 2 925 ⟶ 3 014:
if not qid then return nil end
local out = {}
local props = (
if props ~= "" then
for p in mw.text.gsplit(props, "%p") do -- split at punctuation and iterate
Line 2 942 ⟶ 3 031:
end
if #out > 0 then
local ret = ""
if list then
ret = mw.getCurrentFrame():expandTemplate{title = list, args = out}
else
ret = table.concat(out, " ")
end
return ret
else
return qid
end
end
p.followQid = function(frame)
return p._followQid(frame.args)
end
Line 2 968 ⟶ 3 067:
-------------------------------------------------------------------------------
p.siteID = function(frame)
local txtlang = frame:
-- This deals with specific exceptions: be-tarask -> be-x-old
if txtlang == "be-tarask" then
Line 2 992 ⟶ 3 091:
-------------------------------------------------------------------------------
p.projID = function(frame)
local txtlang = frame:
-- This deals with specific exceptions: be-tarask -> be-x-old
if txtlang == "be-tarask" then
Line 3 085 ⟶ 3 184:
if qid:sub(1,1) ~= "Q" then qid = mw.wikibase.getEntityIdForCurrentPage() end
if not qid then return nil end
local ranks = setRanks(args.rank)
local stats = {}
if ranks.b then
Line 3 095 ⟶ 3 194:
if stats[1].mainsnak.datatype == "wikibase-item" then
for k, v in pairs( stats ) do
local ms = v.mainsnak
if ranks[v.rank:sub(1,1)] and ms.snaktype == "value" and ms.datavalue.value.id == val then
return val
Line 3 111 ⟶ 3 210:
-- but it keeps the "edit at Wikidata" pen icon out of the microformat.
-- Usually it will take its url parameter directly from a Wikidata call:
-- e.g. {{#invoke:WikidataIB |url2 |url={{wdib |P856 |qid=Q23317 |fwd=ALL |osd=no}} }}
-------------------------------------------------------------------------------
-- Dependencies: none
Line 3 118 ⟶ 3 217:
local txt = frame.args.url or ""
if txt == "" then return nil end
-- extract any icon
local url, icon = txt:match("(.+) (.+)")
-- make sure there's at least a space at the end
url = (url or txt) .. " "
icon = icon or ""
-- extract any protocol like https://
local prot = url:match("(https*://).+[ \"\']")
-- extract address
local
if prot then
addr = url:match("https*://(.+)[ \"\']") or " "
else
prot = "//"
addr = url:match("[^%p%s]+%.(.+)[ \"\']") or " "
end
-- strip trailing / from end of ___domain-only url and add <wbr/> before . and /
local disp, n = addr:gsub( "^([^/]+)/$", "%1" ):gsub("%/", "<wbr/>/"):gsub("%.", "<wbr/>.")
return '<span class="url">[' .. prot .. addr .. " " .. disp .. "]</span> " .. icon
end
Line 3 141 ⟶ 3 250:
local url = frame.args.url or ""
if url:upper() == "NONE" then return nil end
local urls = {}
local quals = {}
local qid = frame.args.qid or ""
if url and url ~= "" then
urls[1] = url
else
if qid == "" then qid = mw.wikibase.getEntityIdForCurrentPage() end
if not qid then return nil end
local prop856 = mw.wikibase.getBestStatements(qid, "P856")
for k, v in pairs(prop856) do
Line 3 162 ⟶ 3 272:
end -- test for website having a value
end -- loop through website(s)
end
if #urls == 0 then return nil end
Line 3 202 ⟶ 3 310:
local args = frame.args or frame:getParent().args or {}
local qid = args.qid or ""
if qid == "" then qid =
if not qid or not mw.wikibase.entityExists(qid) then return i18n["entity-not-found"] end
local labels =
if not labels then return i18n["labels-not-found"] end
Line 3 229 ⟶ 3 335:
local args = frame.args or frame:getParent().args or {}
local qid = args.qid or ""
if qid == "" then qid =
if not qid or not mw.wikibase.entityExists(qid) then return i18n["entity-not-found"] end
local descriptions =
if not descriptions then return i18n["descriptions-not-found"] end
Line 3 256 ⟶ 3 360:
local args = frame.args or frame:getParent().args or {}
local qid = args.qid or ""
if qid == "" then qid =
if not qid or not mw.wikibase.entityExists(qid) then return i18n["entity-not-found"] end
local aliases =
if not aliases then return i18n["aliases-not-found"] end
Line 3 329 ⟶ 3 431:
local site = args.site or ""
local showdab = parseParam(args.showdab, true)
local qid = mw.wikibase.getEntityIdForTitle(title, site)
if qid then
local prop31 = mw.wikibase.getBestStatements(qid, "P31")[1]
|