Module:Wikidata: Difference between revisions

Content deleted Content added
Bad syntax for calling isType
m update comment too
 
(16 intermediate revisions by 7 users not shown)
Line 1:
-- vim: set noexpandtab ft=lua ts=4 sw=4:
require('Module:No globalsstrict')
 
local p = {}
Line 80:
}
 
if wiki.langcode ~= "en" then
--require("Module:i18n").loadI18n("Module:Wikidata/i18n", i18n)
--require("Module:i18n").loadI18n("Module:Wikidata/i18n", i18n)
-- got idea from [[:w:Module:Wd]]
-- got idea from [[:w:Module:Wd]]
local module_title; if ... == nil then
local module_title; =if mw.getCurrentFrame():getTitle().. == nil then
module_title = mw.getCurrentFrame():getTitle()
else
else
module_title = ...
module_title = ...
end
require('Module:i18n').loadI18n(module_title..'/i18n', i18n)
end
require('Module:i18n').loadI18n(module_title..'/i18n', i18n)
 
-- this function needs to be internationalised along with the above:
Line 112 ⟶ 114:
return '<span class="error">' .. (i18n.errors[code] or code) .. '</span>'
end
local function parseDateFormat(f, timestamp, addon, prefix_addon, addon_sep)
 
local year_suffix
local tstr = ""
local lang_obj = mw.language.new(wiki.langcode)
local f_parts = mw.text.split(f, 'Y', true)
for idx, f_part in pairs(f_parts) do
year_suffix = ''
if string.match(f_part, "x[mijkot]$") then
-- for non-Gregorian year
f_part = f_part .. 'Y'
elseif idx < #f_parts then
-- supress leading zeros in year
year_suffix = lang_obj:formatDate('Y', timestamp)
year_suffix = string.gsub(year_suffix, '^0+', '', 1)
end
tstr = tstr .. lang_obj:formatDate(f_part, timestamp) .. year_suffix
end
if addon ~= "" and prefix_addon then
return addon .. addon_sep .. tstr
elseif addon ~= "" then
return tstr .. addon_sep .. addon
else
return tstr
end
end
local function parseDateValue(timestamp, date_format, date_addon)
local prefix_addon = i18n["datetime"]["prefix-addon"]
Line 122 ⟶ 148:
timestamp = '+' .. string.sub(timestamp, 2)
addon = date_addon
end
local function d(f)
local year_suffix
local tstr = ""
local lang_obj = mw.language.new(wiki.langcode)
local f_parts = mw.text.split(f, 'Y', true)
for idx, f_part in pairs(f_parts) do
year_suffix = ''
if string.match(f_part, "x[mijkot]$") then
-- for non-Gregorian year
f_part = f_part .. 'Y'
elseif idx < #f_parts then
-- supress leading zeros in year
year_suffix = lang_obj:formatDate('Y', timestamp)
year_suffix = string.gsub(year_suffix, '^0+', '', 1)
end
tstr = tstr .. lang_obj:formatDate(f_part, timestamp) .. year_suffix
end
if addon ~= "" and prefix_addon then
return addon .. addon_sep .. tstr
elseif addon ~= "" then
return tstr .. addon_sep .. addon
else
return tstr
end
end
local _date_format = i18n["datetime"]["format"][date_format]
if _date_format ~= nil then
return dparseDateFormat(_date_format, timestamp, addon, prefix_addon, addon_sep)
else
return printError("unknown-datetime-format")
Line 218 ⟶ 219:
_date_format = i18n["datetime"][9]
end
return parseDateFormat(_date_format, timestamp, addon, prefix_addon, addon_sep)
local year_suffix
local tstr = ""
local lang_obj = mw.language.new(wiki.langcode)
local f_parts = mw.text.split(_date_format, 'Y', true)
for idx, f_part in pairs(f_parts) do
year_suffix = ''
if string.match(f_part, "x[mijkot]$") then
-- for non-Gregorian year
f_part = f_part .. 'Y'
elseif idx < #f_parts then
-- supress leading zeros in year
year_suffix = lang_obj:formatDate('Y', timestamp)
year_suffix = string.gsub(year_suffix, '^0+', '', 1)
end
tstr = tstr .. lang_obj:formatDate(f_part, timestamp) .. year_suffix
end
local fdate
if addon ~= "" and prefix_addon then
fdate = addon .. addon_sep .. tstr
elseif addon ~= "" then
fdate = tstr .. addon_sep .. addon
else
fdate = tstr
end
 
return fdate
else
return printError("unknown-datetime-format")
Line 345 ⟶ 321:
if parameter then
if parameter == "link" then
local linkTarget = mw.wikibase.sitelinkgetSitelink(id)
local linkName = mw.wikibase.labelgetLabel(id)
if linkTarget then
-- if there is a local Wikipedia article link to it using the label or the article title
Line 358 ⟶ 334:
end
else
return mw.wikibase.labelgetLabel(id) or id
end
end
Line 392 ⟶ 368:
return entity.claims[property]
else
property = mw.wikibase.resolvePropertyId(property)
-- otherwise, iterate over all properties, fetch their labels and compare this to the given property name
if not property then return end
for k, v in pairs(entity.claims) do
 
if mw.wikibase.label(k) == property then return v end
return entity.claims[property]
end
return
end
end
Line 451 ⟶ 426:
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.labelgetLabel(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 471 ⟶ 446:
return false, input_parm, nil, nil
end
local entity = mw.wikibase.getEntityObjectgetEntity(qid)
local claims
if entity and entity.claims then
Line 496 ⟶ 471:
for k, v in pairs(claims) do
local qnumber = "Q" .. v.mainsnak.datavalue.value["numeric-id"]
local sitelink = mw.wikibase.sitelinkgetSitelink(qnumber)
local label = labelHook(qnumber) or mw.wikibase.labelgetLabel(qnumber) or qnumber
if sitelink then
out[#out + 1] = "[[" .. sitelink .. "|" .. label .. "]]"
Line 527 ⟶ 502:
function p.descriptionIn(frame)
local langcode = frame.args[1]
local id = frame.args[2] -- "id" must be nil, as access to other Wikidata objects is disabled in Mediawiki configuration
-- return description of a Wikidata entity in the given language or the default language of this Wikipedia site
return mw.wikibase.getEntityObjectgetEntity(id).descriptions[:getDescription(langcode or wiki.langcode].value)
end
 
function p.labelIn(frame)
local langcode = frame.args[1]
local id = frame.args[2] -- "id" must be nil, as access to other Wikidata objects is disabled in Mediawiki configuration
-- return label of a Wikidata entity in the given language or the default language of this Wikipedia site
return mw.wikibase.getEntityObjectgetEntity(id).labels[:getLabel(langcode or wiki.langcode].value)
end
 
Line 609 ⟶ 584:
local input_parm = mw.text.trim(frame.args[3] or "")
if input_parm == "FETCH_WIKIDATA" then
local entity = mw.wikibase.getEntityObjectgetEntity()
if entity.claims[propertyID] ~= nil then
local out = {}
Line 630 ⟶ 605:
local function outputValue(value)
local qnumber = "Q" .. value.datavalue.value["numeric-id"]
if (mw.wikibase.sitelinkgetSitelink(qnumber)) then
return "[[" .. mw.wikibase.sitelinkgetSitelink(qnumber) .. "]]"
else
return "[[:d:" .. qnumber .. "|" ..qnumber .. "]]<abbr title='" .. i18n["errors"]["local-article-not-found"] .. "'>[*]</abbr>"
Line 689 ⟶ 664:
local function outputHook(value)
if value.datavalue.value["numeric-id"] then
return mw.wikibase.labelgetLabel("Q" .. value.datavalue.value["numeric-id"])
else
return value.datavalue.value
Line 765 ⟶ 740:
 
-- This is used to get the TA98 (Terminologia Anatomica first edition 1998) values like 'A01.1.00.005' (property P1323)
-- which are then linked to httphttps://wwwifaa.unifr.ch/ifaa/Public/EntryPage/TA98%20Tree/Entity%20TA98%20EN/01.1.00.005%20Entity%20TA98%20EN.htm
-- uses the newer mw.wikibase calls instead of directly using the snaks
-- formatPropertyValues returns a table with the P1323 values concatenated with ", " so we have to split them out into a table in order to construct the return string
p.getTAValue = function(frame)
local ent = mw.wikibase.getEntityObjectgetEntity()
local props = ent:formatPropertyValues('P1323')
local out = {}
Line 777 ⟶ 752:
t = mw.text.split( v, ", ")
for k2, v2 in pairs(t) do
out[#out + 1] = "[httphttps://wwwifaa.unifr.ch/ifaa/Public/EntryPage/TA98%20Tree/Entity%20TA98%20EN/" .. string.sub(v2, 2) .. "%20Entity%20TA98%20EN.htm " .. v2 .. "]"
end
end
Line 823 ⟶ 798:
local input_parm = mw.text.trim(frame.args[1] or "")
if input_parm == "FETCH_WIKIDATA" then
local ent = mw.wikibase.getEntityObjectgetEntity(id)
local imgs
if ent and ent.claims then
Line 888 ⟶ 863:
-- returns the page id (Q...) of the current page or nothing of the page is not connected to Wikidata
function p.pageId(frame)
local entity =return mw.wikibase.getEntityObjectgetEntityIdForCurrentPage()
if not entity then return nil else return entity.id end
end
 
function p.claim(frame)
local property = frame.args[1] or ""
local id = frame.args["id"] -- "id" must be nil, as access to other Wikidata objects is disabled in Mediawiki configuration
local qualifierId = frame.args["qualifier"]
local parameter = frame.args["parameter"]
Line 904 ⟶ 878:
 
-- get wikidata entity
local entity = mw.wikibase.getEntityObjectgetEntity(id)
if not entity then
if showerrors then return printError("entity-not-found") else return default end
Line 961 ⟶ 935:
id = nil
end
local data = mw.wikibase.getEntityObjectgetEntity(id)
if not data then
return nil
Line 987 ⟶ 961:
 
-- getting sitelink of a given wiki
-- get sitelink of current item if qid not supplied
function p.getSiteLink(frame)
local fqid = frame.args[1].qid
if qid == "" then qid = nil end
local entity = mw.wikibase.getEntity()
local f = mw.text.trim( frame.args[1] or "")
local entity = mw.wikibase.getEntity(qid)
if not entity then
return
Line 1,002 ⟶ 979:
function p.Dump(frame)
local f = (frame.args[1] or frame.args.id) and frame or frame:getParent()
local data = mw.wikibase.getEntityObjectgetEntity(f.args.id)
if not data then
return i18n.warnDump