Module:Wikidata: Difference between revisions

From Chalo Chatu, Zambia online encyclopedia
no edit summary
(Created page with "-- vim: set noexpandtab ft=lua ts=4 sw=4: require('Module:No globals') local p = {} local debug = false --------------------------------------------------------------------...")
 
No edit summary
Line 513: Line 513:
local propertyID = mw.text.trim(frame.args[1] or "")
local propertyID = mw.text.trim(frame.args[1] or "")
local input_parm = mw.text.trim(frame.args[2] or "")
local input_parm = mw.text.trim(frame.args[2] or "")
local delimdefault = ", " -- **internationalise later**
local delim = frame.args.delimiter or ""
delim = string.gsub(delim, '"', '')
if #delim == 0 then
delim = delimdefault
end
local qid = frame.args.qid
if qid and (#qid == 0) then qid = nil end
if input_parm == "FETCH_WIKIDATA" then
if input_parm == "FETCH_WIKIDATA" then
local entity = mw.wikibase.getEntityObject()
local entity = mw.wikibase.getEntityObject(qid)
local claims
local claims
if entity and entity.claims then
if entity and entity.claims then
Line 534: Line 542:
end
end
end
end
return table.concat(out, ", ")
return table.concat(out, delim)
else
else
-- just return best values
-- just return best values
Line 607: Line 615:
if input_parm == "FETCH_WIKIDATA" then
if input_parm == "FETCH_WIKIDATA" then
local entity = mw.wikibase.getEntity(itemID)
local entity = mw.wikibase.getEntity(itemID)
local claims = entity.claims[propertyID]
local claims
if entity and entity.claims then
claims = entity.claims[propertyID]
end
if claims then
if claims then
-- if wiki-linked value output as link if possible
-- if wiki-linked value output as link if possible
Line 692: Line 703:
local propertyID = mw.text.trim(frame.args[1] or "")
local propertyID = mw.text.trim(frame.args[1] or "")
local input_parm = mw.text.trim(frame.args[2] or "")
local input_parm = mw.text.trim(frame.args[2] or "")
local qid = frame.args.qid
if qid and (#qid == 0) then qid = nil end
if input_parm == "FETCH_WIKIDATA" then
if input_parm == "FETCH_WIKIDATA" then
local entity = mw.wikibase.getEntityObject()
local entity = mw.wikibase.getEntityObject(qid)
local claims
local claims
if entity and entity.claims then claims = entity.claims[propertyID] end
if entity and entity.claims then claims = entity.claims[propertyID] end
Line 781: Line 794:
local timestamp = v.mainsnak.datavalue.value.time
local timestamp = v.mainsnak.datavalue.value.time
local dateprecision = v.mainsnak.datavalue.value.precision
local dateprecision = v.mainsnak.datavalue.value.precision
-- A year can be stored like this: "+1872-00-00T00:00:00Z",
-- which is processed here as if it were the day before "+1872-01-01T00:00:00Z",
-- and that's the last day of 1871, so the year is wrong.
-- So fix the month 0, day 0 timestamp to become 1 January instead:
timestamp = timestamp:gsub("%-00%-00T", "-01-01T")
out[#out + 1] = parseDateFull(timestamp, dateprecision, date_format, date_addon)
out[#out + 1] = parseDateFull(timestamp, dateprecision, date_format, date_addon)
end
end
Line 950: Line 968:
end
end
return imglbl
return imglbl
else
return input_parm
end
end
-- This is used to get the QIDs of all of the values of a property, as a comma separated list if multiple values exist
-- Usage: {{#invoke:Wikidata |getPropertyIDs |<PropertyID> |FETCH_WIKIDATA}}
-- Usage: {{#invoke:Wikidata |getPropertyIDs |<PropertyID> |<InputParameter> |qid=<QID>}}
p.getPropertyIDs = function(frame)
local propertyID = mw.text.trim(frame.args[1] or "")
local input_parm = mw.text.trim(frame.args[2] or "")
-- can take a named parameter |qid which is the Wikidata ID for the article. This will not normally be used.
local qid = frame.args.qid
if qid and (#qid == 0) then qid = nil end
if input_parm == "FETCH_WIKIDATA" then
local entity = mw.wikibase.getEntityObject(qid)
local propclaims
if entity and entity.claims then
propclaims = entity.claims[propertyID]
end
if propclaims then
-- if wiki-linked value collect the QID in a table
if (propclaims[1] and propclaims[1].mainsnak.snaktype == "value" and propclaims[1].mainsnak.datavalue.type == "wikibase-entityid") then
local out = {}
for k, v in pairs(propclaims) do
out[#out + 1] = "Q" .. v.mainsnak.datavalue.value["numeric-id"]
end
return table.concat(out, ", ")
else
-- not a wikibase-entityid, so return empty
return ""
end
else
-- no claim, so return empty
return ""
end
else
else
return input_parm
return input_parm
Administrators, upwizcampeditors
0

edits