求真百科歡迎當事人提供第一手真實資料,洗刷冤屈,終結網路霸凌。

變更

前往: 導覽搜尋

模块:Wikidata

移除 5,045 位元組, 5 年前
merged from en oldid=823805865
}
-- Credit to http://stackoverflow.com/a/1283608/2644759-- cc-by-sa 3.0local function tableMerge(t1, t2) for k,v in pairsrequire(t2) do if type(v) == "tableModule:i18n" then if type).loadI18n(t1[k] or false) == "tableModule:Wikidata/i18n" then tableMerge(t1[k] or {}, t2[k] or {}i18n) else t1-- got idea from [[k:w:Module:Wd] = v end else t1[k] = v end end return t1end local function loadI18n()module_title; if ... == nil then local exist, res module_title = pcallmw.getCurrentFrame(require, "Module):Wikidata-i18n"getTitle()else if exist and next(res) ~module_title = nil then tableMerge(i18n, res.i18n) end..
end
 require('Module:i18n').loadI18n(module_title..'-i18n', i18n)
-- this function needs to be internationalised along with the above:
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 endend
local function parseDateValue(timestamp, date_format, date_addon)
local prefix_addon = i18n["datetime"]["prefix-addon"]
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")
_date_format = i18n["datetime"][9]
end
local year_suffix local tstr = "" local lang_obj = mw.language.new(wiki.langcode) local f_parts = mw.text.splitreturn parseDateFormat(_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+'addon, ''prefix_addon, 1) end tstr = tstr .. lang_obj:formatDate(f_part, timestampaddon_sep) .. 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")
end
local function parseInput(frame)
local qid = frame.args.qid
if qid and (#qid == 0) then qid = nil end
local propertyID = mw.text.trim(frame.args[1] or "")
local input_parm = mw.text.trim(frame.args[2] or "")
if input_parm ~= "FETCH_WIKIDATA" then
return false, input_parm, nil, nil
end
local entity = mw.wikibase.getEntityObject(qid)
local claims
if entity and entity.claims then
claims = entity.claims[propertyID]
if not claims then
return false, "", nil, nil
end
else
return false, "", nil, nil
end
return true, entity, claims, propertyID
end
local function isType(claims, type)
return claims[1] and claims[1].mainsnak.snaktype == "value" and claims[1].mainsnak.datavalue.type == type
end
local function getValue(entity, claims, propertyID, delim, labelHook)
if labelHook == nil then
labelHook = function (qnumber)
return nil;
end
end
if isType(claims, "wikibase-entityid") then
local out = {}
for k, v in pairs(claims) do
local qnumber = "Q" .. v.mainsnak.datavalue.value["numeric-id"]
local sitelink = mw.wikibase.sitelink(qnumber)
local label = labelHook(qnumber) or mw.wikibase.label(qnumber) or qnumber
if sitelink then
out[#out + 1] = "[[" .. sitelink .. "|" .. label .. "]]"
else
out[#out + 1] = "[[:d:" .. qnumber .. "|" .. label .. "]]<abbr title='" .. i18n["errors"]["local-article-not-found"] .. "'>[*]</abbr>"
end
end
return table.concat(out, delim)
else
-- just return best values
return entity:formatPropertyValues(propertyID).value
end
end
------------------------------------------------------------------------------
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.getEntityObject(id).descriptions[langcode or wiki.langcode].value
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.getEntityObject(id).labels[langcode or wiki.langcode].value
-- This is used to get a value, or a comma separated list of them if multiple values exist
p.getValue = function(frame)
local propertyID delimdefault = mw.text.trim(frame.args[1] or ", ")-- **internationalise later** local input_parm delim = mw.text.trim(frame.args[2] .delimiter or "") if input_parm =delim = "FETCH_WIKIDATA" then local entity = mw.wikibasestring.getEntityObjectgsub(delim, '"', '') local claims if entity and entity.claims #delim == 0 then claims delim = entity.claims[propertyID]delimdefault end if claims then -- if wiki-linked value output as link if possible if (claims[1] and claims[1].mainsnak.snaktype == "value" and claims[1].mainsnak.datavalue.type == "wikibase-entityid") then local out = {} for kgo, errorOrentity, v in pairs(claims) do local sitelink , propertyID = mw.wikibase.sitelinkparseInput("Q" .. v.mainsnak.datavalue.value["numeric-id"]frame) local label = mw.wikibase.label("Q" .. v.mainsnak.datavalue.value["numeric-id"]) if label == nil not go then label = "Q" .. v.mainsnak.datavalue.value["numeric-id"] end  if sitelink then out[#out + 1] = "[[" .. sitelink .. "|" .. label .. "]]" else out[#out + 1] = "[[:d:Q" .. v.mainsnak.datavalue.value["numeric-id"] .. "|" .. label .. "]]<abbr title='" .. i18n["errors"]["local-article-not-found"] .. "'>[*]</abbr>" end end return table.concat(out, "、") else -- just return best values return entity:formatPropertyValues(propertyID).value end else return "" end else return input_parmerrorOrentity
end
return getValue(errorOrentity, claims, propertyID, delim)
end
-- Same as above, but uses the short name property for label if available.
p.getValueShortName = function(frame)
local go, errorOrentity, claims, propertyID = mw.text.trim(frame.args[1] or "") local input_parm = mw.text.trimparseInput(frame.args[2] or "") if input_parm == "FETCH_WIKIDATA" not go then local entity = mw.wikibase.getEntityObject()return errorOrentity end local claims if entity and entity.claims then claims = entity.claims[propertyID]errorOrentity end if claims then -- if wiki-linked value output as link if possible if (claims[1] and claims[1].mainsnak.snaktype == "value" and claims[1].mainsnak.datavalue.type == "wikibase-entityid") then local out = {} for k, v in pairsfunction labelHook (claimsqnumber) do local sitelink = mw.wikibase.sitelink("Q" .. v.mainsnak.datavalue.value["numeric-id"]) local label local claimEntity = mw.wikibase.getEntity("Q" .. v.mainsnak.datavalue.value["numeric-id"]qnumber) if claimEntity ~= nil then if claimEntity.claims.P1813 then for k2, v2 in pairs(claimEntity.claims.P1813) do for _, variant in ipairs{"zh", "zh-hans", "zh-hant", "zh-cn", "zh-hk", "zh-mo", "zh-sg", "zh-tw"} do if v2.mainsnak.datavalue.value.language == variant then label = v2.mainsnak.datavalue.value.text end end end
end
end
if label == nil or label == "" then label = mw.wikibase.label("Q" .. v.mainsnak.datavalue.value["numeric-id"]) end
if label == nil then label = "Q" .. v.mainsnak.datavalue.value["numeric-id"] end
 
if sitelink then
out[#out + 1] = "[[" .. sitelink .. "|" .. label .. "]]"
else
out[#out + 1] = "[[:d:Q" .. v.mainsnak.datavalue.value["numeric-id"] .. "|" .. label .. "]]<abbr title='" .. i18n["errors"]["local-article-not-found"] .. "'>[*]</abbr>"
end
end
return table.concat(out, "、")
else
-- just return best vakues
return entity:formatPropertyValues(propertyID).value
end
else
return ""
end
else if label == nil or label == "" then return nil end return input_parmlabel
end
return getValue(errorOrentity, claims, propertyID, ", ", labelHook);
end
end
if claims then
-- if wiki-linked value output as link if possible if return getValue(entity, claims[1] and claims[1].mainsnak.snaktype == "value" and claims[1].mainsnak.datavalue.type == "wikibase-entityid") then local out = {} for k, v in pairs(claims) do local sitelink = mw.wikibase.sitelink("Q" .. v.mainsnak.datavalue.value["numeric-id"]) local label = mw.wikibase.label("Q" .. v.mainsnak.datavalue.value["numeric-id"]) if label == nil then label = "Q" .. v.mainsnak.datavalue.value["numeric-id"] end  if sitelink then out[#out + 1] = "[[" .. sitelink .. "|" .. label .. "]]propertyID, " else out[#out + 1] = "[[:d:Q" .. v.mainsnak.datavalue.value["numeric-id"] .. "|" .. label .. "]]<abbr title='" .. i18n["errors"]["local-article-not-found"] .. "'>[*]</abbr>" end end return table.concat(out, " 、") else return entity:formatPropertyValues(propertyID).value end
else
return ""
end
end
 p.getQualifierValue = local functiongetQualifier(frame, outputHook)
local propertyID = mw.text.trim(frame.args[1] or "")
local qualifierID = mw.text.trim(frame.args[2] or "")
for k2, v2 in pairs(v.qualifiers[qualifierID]) do
if v2.snaktype == 'value' then
if (mw.wikibase.sitelink("Q" .. v2.datavalue.value["numeric-id"])) then out[#out + 1] = "[[" .. mw.wikibase.sitelink("Q" .. v2.datavalue.value["numeric-id"]) .. "]]" else out[#out + 1] = "[[:d:Q" .. v2.datavalue.value["numeric-id"] .. "|" .. mw.wikibase.labeloutputHook("Q" .. v2.datavalue.value["numeric-id"]) .. "]]<abbr title='" .. i18n["errors"]["local-article-not-found"] .. "'>[*]</abbr>" end;
end
end
end
return table.concat(out, "、"), true
else
return "", false
end
else
return input_parm, false endendp.getQualifierValue = function(frame) local function outputValue(value) local qnumber = "Q" .. value.datavalue.value["numeric-id"] if (mw.wikibase.sitelink(qnumber)) then return "[[" .. mw.wikibase.sitelink(qnumber) .. "]]" else return "[[:d:" .. qnumber .. "|" ..qnumber .. "]]<abbr title='" .. i18n["errors"]["local-article-not-found"] .. "'>[*]</abbr>" end
end
return (getQualifier(frame, outputValue))
end
-- This is used to get a value like 'male' (for property p21) which won't be linked and numbers without the thousand separators
p.getRawValue = function(frame)
local go, errorOrentity, claims, propertyID = mw.text.trim(frame.args[1] or "") local input_parm = mw.text.trimparseInput(frame.args[2] or "") if input_parm == "FETCH_WIKIDATA" not go then local entity = mw.wikibase.getEntityObject()return errorOrentity end local claims if entity and entity.claims then claims = entity.claims[propertyID] enderrorOrentity if claims then local result = entity:formatPropertyValues(propertyID, mw.wikibase.entity.claimRanks).value  -- if number type: remove thousand separators, bounds and units if isType(claims[1] and claims[1].mainsnak.snaktype == "value" and claims[1].mainsnak.datavalue.type == , "quantity") then result = mw.ustring.gsub(result, "(%d),(%d)", "%1%2") result = mw.ustring.gsub(result, "(%d)±.*", "%1") end return result else return "" end else return input_parm
end
return result
end
-- This is used to get the unit name for the numeric value returned by getRawValue
p.getUnits = function(frame)
local go, errorOrentity, claims, propertyID = mw.text.trim(frame.args[1] or "") local input_parm = mw.text.trimparseInput(frame.args[2] or "") if input_parm == "FETCH_WIKIDATA" not go then local entity = mw.wikibase.getEntityObject()return errorOrentity end local claims if entity and entity.claims then claims = entity.claims[propertyID] enderrorOrentity if claims then local result = entity:formatPropertyValues(propertyID, mw.wikibase.entity.claimRanks).value if isType(claims[1] and claims[1].mainsnak.snaktype == "value" and claims[1].mainsnak.datavalue.type == , "quantity") then result = mw.ustring.sub(result, mw.ustring.find(result, " ")+1, -1) end return result else return "" end else return input_parm
end
return result
end
-- This is used to get the unit's QID to use with the numeric value returned by getRawValue
p.getUnitID = function(frame)
local propertyID = mw.text.trim(frame.args[1] or "") local input_parm go, errorOrentity, claims = mw.text.trimparseInput(frame.args[2] or "") if input_parm == "FETCH_WIKIDATA" not go then local entity = mw.wikibase.getEntityObject()return errorOrentity end local claims if entity and entity.claims then claims = entity.claims[propertyID] enderrorOrentity if claims then local result if isType(claims[1] and claims[1].mainsnak.snaktype == "value" and claims[1].mainsnak.datavalue.type == , "quantity") then -- get the url for the unit entry on Wikidata: result = claims[1].mainsnak.datavalue.value.unit -- and just reurn the last bit from "Q" to the end (which is the QID): result = mw.ustring.sub(result, mw.ustring.find(result, "Q"), -1) end return result else return "" end else return input_parm
end
return result
end
p.getRawQualifierValue = function(frame)
local propertyID = mw.text.trimfunction outputHook(frame.args[1] or "") local qualifierID = mw.text.trim(frame.args[2] or "") local input_parm = mw.text.trim(frame.args[3] or ""value) if input_parm == "FETCH_WIKIDATA" then local entity = mw.wikibase.getEntityObject() if entity.claims[propertyID] ~= nil then local out = {} for k, v in pairs(entity.claims[propertyID]) do for k2, v2 in pairs(v.qualifiers[qualifierID]) do if v2.snaktype == 'value' then if v2.datavalue.value["numeric-id"] then out[#out + 1] = return mw.wikibase.label("Q" .. v2value.datavalue.value["numeric-id"]) else out[#out + 1] = v2.datavalue.value end end end end local ret = table.concat(out, "、") return string.upper(string.sub(ret, 1, 1)) .. string.sub(ret, 2)
else
return ""value.datavalue.value
end
else
return input_parm
end
local ret, gotData = getQualifier(frame, outputHook)
if gotData then
ret = string.upper(string.sub(ret, 1, 1)) .. string.sub(ret, 2)
end
return ret
end
-- So I'll just supply "Z" in the call to formatDate below:
p.getDateValue = function(frame)
local propertyID = mw.text.trim(frame.args[1] or "")
local input_parm = mw.text.trim(frame.args[2] or "")
local date_format = mw.text.trim(frame.args[3] or i18n["datetime"]["default-format"])
local date_addon = mw.text.trim(frame.args[4] or i18n["datetime"]["default-addon"])
if input_parm == "FETCH_WIKIDATA" then local entity go, errorOrentity, claims = mw.wikibase.getEntityObjectparseInput(frame) if not go then if return errorOrentity end local entity.claims[propertyID] ~= nil thenerrorOrentity local out = {} for k, v in pairs(entity.claims[propertyID]) do if v.mainsnak.datavalue.type == 'time' then local timestamp = v.mainsnak.datavalue.value.time 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) end end return table.concat(out, "、") else return ""
end
else
return input_parm
end
return table.concat(out, "、")
end
 
p.getQualifierDateValue = function(frame)
local propertyID = mw.text.trim(frame.args[1] or "")
local qualifierID = mw.text.trim(frame.args[2] or "")
local input_parm = mw.text.trim(frame.args[3] or "")
local date_format = mw.text.trim(frame.args[4] or i18n["datetime"]["default-format"])
local date_addon = mw.text.trim(frame.args[5] or i18n["datetime"]["default-addon"])
if input_parm == "FETCH_WIKIDATA" then local entity = mw.wikibase.getEntityObjectfunction outputHook(value) if entity.claims[propertyID] ~= nil then local out = {} for k, v in pairs(entity.claims[propertyID]) do for k2, v2 in pairs(v.qualifiers[qualifierID]) do if v2.snaktype =timestamp = 'value' then local timestamp = v2.datavalue.value.time out[#out + 1] = return parseDateValue(timestamp, date_format, date_addon) end end end return table.concat(out, "、") else return "" end else return input_parm
end
return (getQualifier(frame, outputHook))
end
-- If a property is chosen that is not of type "commonsMedia", it will return empty text.
p.getImages = function(frame)
local propertyID = mw.text.trim(frame.args[1] or "")
local input_parm = mw.text.trim(frame.args[2] or "")
local sep = mw.text.trim(frame.args[3] or " ")
local imgsize = mw.text.trim(frame.args[4] or "frameless")
if input_parm == "FETCH_WIKIDATA" then local entity go, errorOrentity, claims = mw.wikibase.getEntityObjectparseInput(frame) local claims if entity and entity.claims not go then claims = entity.claims[propertyID] return errorOrentity end if claims then local entity = errorOrentity if (claims[1] and claims[1].mainsnak.datatype == "commonsMedia") then local out = {} for k, v in pairs(claims) do local filename = v.mainsnak.datavalue.value out[#out + 1] = "[[File:" .. filename .. "|" .. imgsize .. "]]" end return table.concat(out, sep) else return "" end else return ""
end
return table.concat(out, sep)
else
return input_parm""
end
end
p.getPropertyIDs = function(frame)
local propertyID go, errorOrentity, propclaims = mw.text.trimparseInput(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 if not normally be used.go then local qid = frame.args.qid return errorOrentity if qid and (#qid == 0) then qid = nil end if input_parm == "FETCH_WIKIDATA" then local entity = mw.wikibase.getEntityObject(qid)errorOrentity 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
return table.concat(out, "、")
else
-- not a wikibase-entityid, so return input_parmempty return ""
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"]
-- getting sitelink of a given wiki
-- get sitelink of current item if qid not supplied
function p.getSiteLink(frame)
local qid = frame.args.qid if qid == "" then qid = nil end local f = mw.text.trim( frame.args[1]or "") local entity = mw.wikibase.getEntity(qid)
if not entity then
return