Mòdul:Wikidades/proves: diferència entre les revisions
Aparença
Contingut suprimit Contingut afegit
corr. |
tree_instance |
||
Línia 1: | Línia 1: | ||
local p = {} |
local p = {} |
||
local labels = { |
|||
-- module local variables |
|||
["capital"] = "ciutat", |
|||
local wiki = |
|||
["departament francès"] = "departament", |
|||
{ |
|||
["districte francès"] = "districte", |
|||
langcode = mw.language.getContentLanguage().code |
|||
["estat sobirà"] = "estat", |
|||
["reialme de la Commonwealth"] = "estat", |
|||
["zona de govern local d'Austràlia"] = "govern local" |
|||
} |
} |
||
local |
local function uc_first(word) |
||
return mw.ustring.upper(mw.ustring.sub(word, 1, 1)) .. mw.ustring.sub(word, 2) |
|||
["errors"] = { |
|||
["property-not-found"] = "Propietat no trobada.", |
|||
["entity-not-found"] = "Entitat no trobada.", |
|||
["unknown-claim-type"] = "Tipus d'afirmació (claim) desconegut.", |
|||
["unknown-entity-type"] = "Tipus d'entitat desconegut.", |
|||
["qualifier-not-found"] = "Qualificador no trobat.", |
|||
["site-not-found"] = "Projecte Wikimedia no trobat.", |
|||
["unknown-datetime-format"] = "Format data/horari desconegut.", |
|||
["local-article-not-found"] = "L'article encara no està disponible en aquest wiki" |
|||
}, |
|||
["datetime"] = |
|||
{ |
|||
-- $1 is a placeholder for the actual number |
|||
[0] = "$1 mil milions d'anys", -- precision: billion years |
|||
[1] = "$100 milions d'anys", -- precision: hundred million years |
|||
[2] = "$10 milions d'anys", -- precision: ten million years |
|||
[3] = "$1 milions d'anys", -- precision: million years |
|||
[4] = "$100.000 anys", -- precision: hundred thousand years |
|||
[5] = "$10.000 anys", -- precision: ten thousand years |
|||
[8] = "dècada del $1", -- precision: decade |
|||
-- the following use the format of #time parser function |
|||
[6] = "xrY mil·lenni", -- precision: millennium |
|||
[7] = "segle xrY", -- precision: century |
|||
[9] = "Y", -- precision: year, |
|||
[10] = "F Y", -- precision: month |
|||
[11] = "j F Y", -- precision: day |
|||
[12] = "j F Y ga", -- precision: hour |
|||
[13] = "j F Y g:ia", -- precision: minute |
|||
[14] = "j F Y g:i:sa", -- precision: second |
|||
["beforenow"] = "fa $1", -- how to format negative numbers for precisions 0 to 5 |
|||
["afternow"] = "d'aquí $1", -- how to format positive numbers for precisions 0 to 5 |
|||
["bc"] = '$1 aC', -- how print negative years |
|||
["ad"] = "$1" -- how print positive years |
|||
}, |
|||
["monolingualtext"] = '<span lang="%language">%text</span>', |
|||
["warnDump"] = "[[Categoria:Funció Dump del mòdul Wikidata]]" |
|||
} |
|||
-- Local grammatical cases |
|||
local function case(word, case) |
|||
-- de moment no res, possibles usos: femení, plural |
|||
return word |
|||
end |
end |
||
local function |
local function fixInfoboxLabel(label, link) |
||
if |
if labels[label] then |
||
label = labels[label] |
|||
return '[' .. data .. ' ' .. mw.text.split(data, '//' )[2] .. ']' |
|||
elseif mw.ustring.find((parameter or ''), '$1', 1, true) then -- formatting = a pattern |
|||
return mw.ustring.gsub(parameter, '$1', data) .. '' -- hack to get only the first result of the function |
|||
else |
else |
||
label = string.gsub(label, " del? .+", "") |
|||
return data |
|||
label = string.gsub(label, " d'.+", "") |
|||
end |
|||
label = string.gsub(label, " %(.+", "") |
|||
end |
|||
if label == 'estat' then |
|||
label = 'estat federat' |
|||
local function printDatavalueCoordinate(data, parameter) |
|||
local globetable = {Q2 = 'earth', Q111 = 'mars', Q405 = 'moon'} |
|||
if parameter == 'latitude' then |
|||
return data.latitude |
|||
elseif parameter == 'longitude' then |
|||
return data.longitude |
|||
elseif parameter == 'dimension' then |
|||
return data.dimension |
|||
else |
|||
if data.globe == '' or data.globe == nil then |
|||
return 'earth' |
|||
else |
|||
local globenum = mw.text.split(data.globe, 'entity/')[2] -- http://www.wikidata.org/wiki/Q2 |
|||
if globetable[globenum] then |
|||
return globetable[globenum] |
|||
else |
|||
return globenum |
|||
end |
|||
end |
end |
||
end |
end |
||
end |
|||
local function printDatavalueQuantity(data, parameter) |
|||
-- exemples: 277±1 Centímetre, 1,94 metre |
|||
local amount = data.amount |
|||
amount = mw.ustring.gsub(amount, "%+", "") |
|||
local sortkey = string.format("%09d", amount) |
|||
local lang = mw.language.new(wiki.langcode) |
|||
amount = lang:formatNum(tonumber(amount)) |
|||
return amount, sortkey |
|||
end |
|||
local function printDatavalueTime(data, parameter) |
|||
-- Dates and times are stored in ISO 8601 format |
|||
-- check for negative date, ex. "-0027-01-16T00:00:00Z" |
|||
local suffix = "" |
|||
local timestamp = data.time |
|||
local sortkey = timestamp |
|||
if string.sub(timestamp, 1, 1) == '-' then |
|||
timestamp = '+' .. string.sub(timestamp, 2) |
|||
suffix = " aC" |
|||
elseif string.sub(timestamp, 2, 3) == '00' then |
|||
suffix = " dC" |
|||
end |
|||
local function d(f, t) |
|||
return mw.language.new(wiki.langcode):formatDate(f, t or timestamp) .. suffix |
|||
end |
|||
local precision = data.precision or 11 |
|||
local any = tonumber(mw.ustring.match(timestamp, "^\+?%d+")) |
|||
local ret = "" |
|||
return mw.text.trim(uc_first(label)) |
|||
-- precision is 10000 years or more |
|||
if precision <= 5 then |
|||
local factor = 10 ^ ((5 - precision) + 4) |
|||
local y2 = math.ceil(math.abs(any) / factor) |
|||
local relative = mw.ustring.gsub(i18n.datetime[precision], "$1", tostring(y2)) |
|||
if suffix == " aC" then |
|||
ret = mw.ustring.gsub(i18n.datetime.beforenow, "$1", relative) |
|||
else |
|||
ret = mw.ustring.gsub(i18n.datetime.afternow, "$1", relative) |
|||
end |
|||
-- precision is millennia, centuries or decades |
|||
elseif precision == 6 then |
|||
ret = d('xrY "mil·lenni"', string.format("%04d", tostring(math.floor((math.abs(any) - 1) / 1000) + 1))) |
|||
elseif precision == 7 then -- segles |
|||
ret = d('"segle" xrY', string.format("%04d", tostring(math.floor((math.abs(any) - 1) / 100) + 1))) |
|||
elseif precision == 8 then |
|||
ret = mw.ustring.gsub(i18n.datetime[8], "$1", tostring(math.floor(math.abs(any) / 10) * 10)) .. suffix |
|||
-- precision is year |
|||
elseif parameter == 'year' or precision == 9 then |
|||
ret = tostring(any) .. suffix |
|||
-- precision is month |
|||
elseif precision == 10 then |
|||
timestamp = timestamp .. " + 1 day" -- formatDate yyyy-mm-00 returns the previous month |
|||
ret, _ = string.gsub(d("F Y"), " 0+", " ") -- supress leading zeros in year |
|||
elseif parameter and parameter ~= "table" then |
|||
ret, _ = string.gsub(d(parameter), " 0+", " ") |
|||
else |
|||
ret, _ = string.gsub(d("j F Y"), " 0+", " ") |
|||
end |
|||
return ret, sortkey |
|||
end |
end |
||
local function |
local function getEntityValue(id, property, parameter) |
||
local entityId = "Q" .. tostring(data['numeric-id']) |
|||
local label = mw.wikibase.label(entityId) |
|||
local sitelink = mw.wikibase.sitelink(entityId) |
|||
local parameter = parameters.formatting |
|||
local labelcase = label or sitelink |
|||
if parameters.case then |
|||
labelcase = case(labelcase, parameters.case) |
|||
end |
|||
if parameter == 'raw' then |
|||
return entityId, entityId |
|||
elseif parameter == 'label' then |
|||
return (label or entityId), label or entityId |
|||
elseif parameter == 'sitelink' then |
|||
return (sitelink or 'wikidata:' .. entityId), sitelink or entityId |
|||
elseif mw.ustring.find((parameter or ''), '$1', 1, true) then -- formatting = a pattern |
|||
return mw.ustring.gsub(parameter, '$1', labelcase or entityId) .. '', labelcase or entityId |
|||
else |
|||
if sitelink then |
|||
return '[[' .. sitelink .. '|' .. labelcase .. ']]', labelcase |
|||
elseif label and parameter == 'internallink' then |
|||
return '[[' .. label .. '|' .. labelcase .. ']]', labelcase |
|||
else |
|||
return '[[wikidata:' .. entityId .. '|' .. (labelcase or entityId) .. ']]', labelcase or entityId |
|||
end |
|||
end |
|||
end |
|||
local function printDatavalueMonolingualText(data, parameter) |
|||
-- data fields: language [string], text [string] |
|||
local result = nil |
|||
if parameter == "language" or parameter == "text" then |
|||
result = data[parameter] |
|||
elseif parameter then |
|||
if data["language"] == wiki.langcode then |
|||
if parameter then |
|||
result = printDatavalueString(data["text"], parameter) |
|||
else |
|||
result = data["text"] |
|||
end |
|||
end |
|||
else |
|||
result = mw.ustring.gsub(mw.ustring.gsub(i18n.monolingualtext, "%%language", data["language"]), "%%text", data["text"]) |
|||
end |
|||
return result |
|||
end |
|||
local function printError(key) |
|||
return '<span class="error">' .. i18n.errors[key] .. '</span>' |
|||
end |
|||
local function findClaims(entity, property) |
|||
if not property or not entity or not entity.claims then return end |
|||
if mw.ustring.match(property, "^P%d+$") then |
|||
-- if the property is given by an id (P..) access the claim list by this id |
|||
return entity.claims[property] |
|||
else |
|||
-- otherwise, iterate over all properties, fetch their labels and compare this to the given property name |
|||
for k, v in pairs(entity.claims) do |
|||
if mw.wikibase.label(k) == property then return v end |
|||
end |
|||
return |
|||
end |
|||
end |
|||
local function getSnakValue(snak, parameters) |
|||
local parameter = parameters.formatting |
|||
if snak.snaktype == 'value' then |
|||
-- call the respective snak parser |
|||
if snak.datavalue.type == "string" then |
|||
return printDatavalueString(snak.datavalue.value, parameter) |
|||
elseif snak.datavalue.type == "globecoordinate" then |
|||
return printDatavalueCoordinate(snak.datavalue.value, parameter) |
|||
elseif snak.datavalue.type == "quantity" then |
|||
return printDatavalueQuantity(snak.datavalue.value, parameter) |
|||
elseif snak.datavalue.type == "time" then |
|||
return printDatavalueTime(snak.datavalue.value, parameter) |
|||
elseif snak.datavalue.type == 'wikibase-entityid' then |
|||
return printDatavalueEntity(snak.datavalue.value, parameters) |
|||
elseif snak.datavalue.type == 'monolingualtext' then |
|||
return printDatavalueMonolingualText(snak.datavalue.value, parameter) |
|||
end |
|||
end |
|||
return mw.wikibase.renderSnak(snak) |
|||
end |
|||
local function getQualifierSnak(claim, qualifierId, parameters) |
|||
-- a "snak" is Wikidata terminology for a typed key/value pair |
|||
-- a claim consists of a main snak holding the main information of this claim, |
|||
-- as well as a list of attribute snaks and a list of references snaks |
|||
if qualifierId then |
|||
-- search the attribute snak with the given qualifier as key |
|||
if claim.qualifiers then |
|||
local qualifier = claim.qualifiers[qualifierId] |
|||
if qualifier then |
|||
-- iterate over monolingualtext qualifiers to get local language |
|||
for idx in pairs(qualifier) do |
|||
if qualifier[idx].datavalue.value.language == wiki.langcode then |
|||
return qualifier[idx] |
|||
end |
|||
end |
|||
if parameters.list then |
|||
return qualifier |
|||
else |
|||
return qualifier[1] |
|||
end |
|||
end |
|||
end |
|||
return nil, printError("qualifier-not-found") |
|||
else |
|||
-- otherwise return the main snak |
|||
return claim.mainsnak |
|||
end |
|||
end |
|||
local function getValueOfClaim(claim, qualifierId, parameters) |
|||
local error |
|||
local snak |
|||
snak, error = getQualifierSnak(claim, qualifierId, parameters) |
|||
if not snak then |
|||
return nil, nil, error |
|||
elseif snak[1] then -- a multi qualifier |
|||
local result = {} |
|||
local sortkey = {} |
|||
for idx in pairs(snak) do |
|||
result[#result + 1], sortkey[#sortkey + 1] = getSnakValue(snak[idx], parameters) |
|||
end |
|||
return mw.text.listToText(result, parameters.qseparator, parameters.qconjunction), sortkey[1] |
|||
else -- a property or a qualifier |
|||
return getSnakValue(snak, parameters) |
|||
end |
|||
end |
|||
-- Return the site link (for the current site) for a given data item. |
|||
function p.getSiteLink(frame) |
|||
if frame.args[1] == nil then |
|||
entity = mw.wikibase.getEntityObject() |
|||
if not entity then |
|||
return nil |
|||
end |
|||
id = entity.id |
|||
else |
|||
id = frame.args[1] |
|||
end |
|||
return mw.wikibase.sitelink(id) |
|||
end |
|||
-- A la consola de depuració useu: =p.proves({item="Q...", property="P...", ...}) |
|||
function p.proves(args) |
|||
return _main(args) |
|||
end |
|||
function p.claim(frame) |
|||
local args = frame.args |
|||
return _main(args) |
|||
end |
|||
function _main(args) |
|||
--If a value is already set, use it |
|||
if args.value and args.value ~= '' then |
|||
return args.value |
|||
end |
|||
-- arguments |
|||
local property = args["property"] or "" |
|||
local id = args["item"]; if id == "" then id = nil end |
|||
local qualifierId = {} |
|||
qualifierId[1] = args["qualifier"] or args["qualifierProperty"] -- compatibilitat de transició |
|||
for i = 2, 9 do |
|||
qualifierId[i] = args["qualifier" .. i] |
|||
end |
|||
local parameter = args["formatting"] or ''; if parameter == "" then parameter = nil end |
|||
local case = args.case |
|||
local list = args["list"] or true; if list == "false" then list = false end |
|||
local sorting = args.tablesort |
|||
local separator = args.separator |
|||
local conjunction = args.conjunction |
|||
local rowformat = args.rowformat |
|||
local showerrors = args["showerrors"] |
|||
local default = args["default"] |
|||
property = property:gsub("^p(%d)", "P%1") |
|||
if qualifierId[1] then qualifierId[1] = qualifierId[1]:gsub("^p(%d)", "P%1") end |
|||
if not parameter and args["rank"] == "ca" then parameter = "ca" end -- compatibilitat de transició |
|||
if args["rank"] and args["rank"] ~= '' then list = false end -- compatibilitat de transició |
|||
local parameters = {["formatting"] = parameter, ["list"] = list, ["sorting"] = sorting, ["case"] = case, |
|||
["separator"] = separator, ["conjunction"] = conjunction, ["qseparator"] = separator, ["qconjunction"] = conjunction} |
|||
local preformat = "" |
|||
local postformat = "" |
|||
if parameters.formatting == "table" then |
|||
parameters.separator = parameters.separator or "<br />" |
|||
parameters.conjunction = parameters.conjunction or "<br />" |
|||
parameters.qseparator = ", " |
|||
parameters.qconjunction = ", " |
|||
if not rowformat then |
|||
rowformat = "$0 ($1" |
|||
for i = 2, 9 do |
|||
if qualifierId[i] then |
|||
rowformat = rowformat .. ", $" .. i |
|||
end |
|||
end |
|||
rowformat = rowformat .. ")" |
|||
elseif mw.ustring.find(rowformat, "^[*#]") then |
|||
parameters.separator = "</li><li>" |
|||
parameters.conjunction = "</li><li>" |
|||
if mw.ustring.match(rowformat, "^[*#]") == "*" then |
|||
preformat = "<ul><li>" |
|||
postformat = "</li></ul>" |
|||
else |
|||
preformat = "<ol><li>" |
|||
postformat = "</li></ol>" |
|||
end |
|||
rowformat = mw.ustring.gsub(rowformat, "^[*#] ?", "") |
|||
end |
|||
end |
|||
if default then showerrors = nil end |
|||
-- get wikidata entity |
|||
local entity = mw.wikibase.getEntityObject(id) |
local entity = mw.wikibase.getEntityObject(id) |
||
if not entity then |
if not entity then return end |
||
if showerrors then return printError("entity-not-found") else return default end |
|||
end |
|||
-- fetch the first claim of satisfying the given property |
|||
local claims = findClaims(entity, property) |
|||
if not claims or not claims[1] then |
|||
if showerrors then return printError("property-not-found") else return default end |
|||
end |
|||
local claims = entity.claims[property] |
|||
if not claims then return end |
|||
-- get initial sort indices |
-- get initial sort indices |
||
local sortindices = {} |
local sortindices = {} |
||
Línia 391: | Línia 49: | ||
table.sort(sortindices, comparator) |
table.sort(sortindices, comparator) |
||
local snak = claims[sortindices[1]].mainsnak |
|||
local result |
|||
local error |
|||
local entityId, result |
|||
if parameters.list or parameters.formatting == "table" then |
|||
if snak.datavalue.type == 'wikibase-entityid' then |
|||
-- iterate over all elements and return their value (if existing) |
|||
entityId = "Q" .. tostring(snak.datavalue.value['numeric-id']) |
|||
local value, valueq |
|||
local label = mw.wikibase.label(entityId) |
|||
local sortkey, sortkeyq |
|||
local |
local sitelink = mw.wikibase.sitelink(entityId) |
||
if parameter == 'label' then |
|||
local sortkeys = {} |
|||
result = (label or entityId) |
|||
for idx in pairs(claims) do |
|||
elseif sitelink then |
|||
values[#values + 1] = {} |
|||
if label then |
|||
sortkeys[#sortkeys + 1] = {} |
|||
result = '[[' .. sitelink .. '|' .. label .. ']]' |
|||
local claim = claims[sortindices[idx]] |
|||
if parameters.formatting == "table" then |
|||
value, sortkey, error = getValueOfClaim(claim, nil, parameters) |
|||
for i, qual in ipairs(qualifierId) do |
|||
valueq, sortkeyq, _ = getValueOfClaim(claim, qual, parameters) |
|||
values[#values]["col" .. i] = valueq |
|||
sortkeys[#sortkeys]["col" .. i] = sortkeyq or valueq |
|||
end |
|||
else |
else |
||
result = '[[' .. sitelink .. ']]' |
|||
value, sortkey, error = getValueOfClaim(claim, qualifierId[1], parameters) |
|||
end |
end |
||
elseif label then |
|||
if not value and showerrors then value = error end |
|||
result = '[[' .. label .. ']]' |
|||
else |
|||
values[#values]["col0"] = value |
|||
result = '[[wikidata:' .. entityId .. '|' .. (label or entityId) .. ']]' |
|||
sortkeys[#sortkeys]["col0"] = sortkey or value |
|||
end |
end |
||
-- sort and format results |
|||
if parameters.sorting then |
|||
local comparator = function(a, b) |
|||
valuea = sortkeys[a]["col" .. parameters.sorting] or '' |
|||
valueb = sortkeys[b]["col" .. parameters.sorting] or '' |
|||
return valuea < valueb |
|||
end |
|||
table.sort(sortindices, comparator) |
|||
end |
|||
result = {} |
|||
for idx in pairs(claims) do |
|||
local valuerow = values[sortindices[idx]] |
|||
value = valuerow["col0"] |
|||
if parameters.formatting == "table" then |
|||
value = mw.ustring.gsub(rowformat, "$0", value) |
|||
for i, _ in ipairs(qualifierId) do |
|||
valueq = valuerow["col" .. i] |
|||
if args["rowsubformat" .. i] and valueq then |
|||
valueq = mw.ustring.gsub(args["rowsubformat" .. i], "$" .. i, valueq) |
|||
end |
|||
value = mw.ustring.gsub(value, "$" .. i, valueq or '') |
|||
end |
|||
end |
|||
result[#result + 1] = value |
|||
end |
|||
result = preformat .. mw.text.listToText(result, parameters.separator, parameters.conjunction) .. postformat |
|||
else |
|||
-- return first element |
|||
local claim = claims[sortindices[1]] |
|||
result, _, error = getValueOfClaim(claim, qualifierId[1], parameters) |
|||
end |
|||
-- This is used to get the unit name for a numeric value |
|||
local suffix = "" |
|||
if parameters.formatting == "unit" or parameters.formatting == "unitcode" then |
|||
local result_unit = entity:formatPropertyValues(property, mw.wikibase.entity.claimRanks).value |
|||
if claims[1] and claims[1].mainsnak.snaktype == "value" and claims[1].mainsnak.datavalue.type == "quantity" then |
|||
if mw.ustring.find(result_unit, " ") then |
|||
local unit_label = mw.ustring.sub(result_unit, mw.ustring.find(result_unit, " ")+1, -1) |
|||
-- get the url for the unit entry on Wikidata: |
|||
local unitID = claims[1].mainsnak.datavalue.value.unit |
|||
-- and just return the last bit from "Q" to the end (which is the QID): |
|||
unitID = mw.ustring.sub(unitID, mw.ustring.find(unitID, "Q"), -1) |
|||
suffix = " " .. require("Module:Wikidata/Units").getUnit(result, unit_label, unitID, parameters.formatting == "unitcode") |
|||
end |
|||
end |
|||
end |
|||
if result then return result .. suffix else |
|||
if showerrors then return error else return default end |
|||
end |
end |
||
return entityId, result |
|||
end |
end |
||
function p.proves(args) |
|||
-- Return the statements with a format |
|||
return _main(args) |
|||
function p.formatStatements(frame) |
|||
return p.claim(frame) -- compatibilitat de transició |
|||
end |
end |
||
function p.tree_instance(frame) |
|||
--Return the qualifiers with a format |
|||
local args = frame.args |
|||
function p.formatQualifiers(frame) |
|||
return _main(args) |
|||
return p.claim(frame) -- compatibilitat de transició |
|||
end |
end |
||
function _main(args) |
|||
-- This is used to get the TA98 (Terminologia Anatomica first edition 1998) values like 'A01.1.00.005' (property P1323) |
|||
local id = args["item"]; if id == "" then id = nil end |
|||
-- which are then linked to http://www.unifr.ch/ifaa/Public/EntryPage/TA98%20Tree/Entity%20TA98%20EN/01.1.00.005%20Entity%20TA98%20EN.htm |
|||
local propertyFetch = args["property"]; if propertyFetch == nil then propertyFetch = "P131" end |
|||
-- uses the newer mw.wikibase calls instead of directly using the snaks |
|||
local stop = args["stop"] |
|||
-- 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 |
|||
local target = args["target"] |
|||
p.getTAValue = function(frame) |
|||
local rowformat = args["rowformat"]; if rowformat == nil then rowformat = "$0 = $1" end |
|||
local ent = mw.wikibase.getEntityObject() |
|||
local props = ent:formatPropertyValues('P1323') |
|||
local loop = tonumber(stop) or 10 |
|||
local out = {} |
|||
local lastone = uc_first(stop or '') |
|||
local t = {} |
|||
for k, v in pairs(props) do |
|||
local filter = {} |
|||
if k == 'value' then |
|||
if target then |
|||
t = mw.text.split( v, ", ") |
|||
for i, v in ipairs(mw.text.split(target, "/")) do |
|||
filter[uc_first(v)] = true |
|||
out[#out + 1] = "[http://www.unifr.ch/ifaa/Public/EntryPage/TA98%20Tree/Entity%20TA98%20EN/" .. string.sub(v2, 2) .. "%20Entity%20TA98%20EN.htm " .. v2 .. "]" |
|||
end |
|||
end |
|||
end |
|||
local ret = table.concat(out, "<br> ") |
|||
if #ret == 0 then |
|||
ret = "Invalid TA" |
|||
end |
|||
return ret |
|||
end |
|||
-- look into entity object |
|||
function p.ViewSomething(frame) |
|||
local f = (frame.args[1] or frame.args.item) and frame or frame:getParent() |
|||
local id = f.args.item |
|||
if id and (#id == 0) then |
|||
id = nil |
|||
end |
|||
local data = mw.wikibase.getEntityObject(id) |
|||
if not data then |
|||
return nil |
|||
end |
|||
local i = 1 |
|||
while true do |
|||
local index = f.args[i] |
|||
if not index then |
|||
if type(data) == "table" then |
|||
return mw.text.jsonEncode(data, mw.text.JSON_PRESERVE_KEYS + mw.text.JSON_PRETTY) |
|||
else |
|||
return tostring(data) |
|||
end |
|||
end |
end |
||
data = data[index] or data[tonumber(index)] |
|||
if not data then |
|||
return |
|||
end |
|||
i = i + 1 |
|||
end |
|||
end |
|||
-- Dump data tree structure |
|||
-- From pl:Module:Wikidane, by User:Paweł Ziemian |
|||
-- Funció pensada com a eina d'ajuda en previsualització. |
|||
function p.Dump(frame) |
|||
local data = mw.wikibase.getEntityObject() |
|||
if not data then |
|||
return i18n.warnDump |
|||
end |
end |
||
local propertyInstance = "P31" |
|||
local f = frame.args[1] and frame or frame:getParent() |
|||
local loops = 0 |
|||
local |
local result = {} |
||
while true do |
|||
while id and (loops < loop) do |
|||
local index = f.args[i] |
|||
loops = loops + 1 |
|||
local label, link |
|||
return "<pre>" .. mw.dumpObject(data) .. "</pre>" .. i18n.warnDump |
|||
id, link = getEntityValue(id, propertyFetch, "link") |
|||
_, label = getEntityValue(id, propertyInstance, "label") |
|||
if label and link then |
|||
label = fixInfoboxLabel(label, link) |
|||
if target == nil or filter[label] then |
|||
result[#result + 1] = {label, link} |
|||
end |
|||
if label == lastone then |
|||
break |
|||
end |
|||
end |
end |
||
data = data[index] or data[tonumber(index)] |
|||
if not data then |
|||
return i18n.warnDump |
|||
end |
|||
i = i + 1 |
|||
end |
|||
end |
|||
-- Look into entity object |
|||
-- From pl:Module:Wikidane, function V, by User:Paweł Ziemian |
|||
function p.getEntityFromTree(frame) |
|||
local data = mw.wikibase.getEntityObject() |
|||
if not data then |
|||
return nil |
|||
end |
end |
||
local ret = {} |
|||
local f = frame.args[1] and frame or frame:getParent() |
|||
for i = 1, #result do |
|||
ret[#ret +1] = mw.ustring.gsub(rowformat, "$[01]", {["$0"] = result[i][1], ["$1"] = result[i][2]}) |
|||
local i = 1 |
|||
while true do |
|||
local index = f.args[i] |
|||
if not index then |
|||
return tostring(data) |
|||
end |
|||
data = data[index] or data[tonumber(index)] |
|||
if not data then |
|||
return |
|||
end |
|||
i = i + 1 |
|||
end |
end |
||
return mw.text.listToText(ret, args.separator or '<br />', args.separator or '<br />') |
|||
end |
end |
||
Revisió del 12:09, 12 des 2016
![Icona de documentació de mòdul](http://upload.wikimedia.org/wikipedia/commons/thumb/1/19/Test_Template_Info-Icon.svg/50px-Test_Template_Info-Icon.svg.png)
A continuació es mostra la documentació transclosa de la subpàgina /ús. [salta a la caixa de codi]
Pàgina de proves de Mòdul:Wikidades. Per fer comprovacions, canvieu #invoke:Wikidades per #invoke:Wikidades/proves.
Comparació de diferències amb la versió original: diff.
Comprovació ràpida d'infotaules en viu i en proves: Mòdul:Wikidades/proves/test/ús.
local p = {}
local labels = {
["capital"] = "ciutat",
["departament francès"] = "departament",
["districte francès"] = "districte",
["estat sobirà"] = "estat",
["reialme de la Commonwealth"] = "estat",
["zona de govern local d'Austràlia"] = "govern local"
}
local function uc_first(word)
return mw.ustring.upper(mw.ustring.sub(word, 1, 1)) .. mw.ustring.sub(word, 2)
end
local function fixInfoboxLabel(label, link)
if labels[label] then
label = labels[label]
else
label = string.gsub(label, " del? .+", "")
label = string.gsub(label, " d'.+", "")
label = string.gsub(label, " %(.+", "")
if label == 'estat' then
label = 'estat federat'
end
end
return mw.text.trim(uc_first(label))
end
local function getEntityValue(id, property, parameter)
local entity = mw.wikibase.getEntityObject(id)
if not entity then return end
local claims = entity.claims[property]
if not claims then return end
-- get initial sort indices
local sortindices = {}
for idx in pairs(claims) do
sortindices[#sortindices + 1] = idx
end
-- sort by claim rank
local comparator = function(a, b)
local rankmap = { deprecated = 2, normal = 1, preferred = 0 }
local ranka = rankmap[claims[a].rank or "normal"] .. string.format("%08d", a)
local rankb = rankmap[claims[b].rank or "normal"] .. string.format("%08d", b)
return ranka < rankb
end
table.sort(sortindices, comparator)
local snak = claims[sortindices[1]].mainsnak
local entityId, result
if snak.datavalue.type == 'wikibase-entityid' then
entityId = "Q" .. tostring(snak.datavalue.value['numeric-id'])
local label = mw.wikibase.label(entityId)
local sitelink = mw.wikibase.sitelink(entityId)
if parameter == 'label' then
result = (label or entityId)
elseif sitelink then
if label then
result = '[[' .. sitelink .. '|' .. label .. ']]'
else
result = '[[' .. sitelink .. ']]'
end
elseif label then
result = '[[' .. label .. ']]'
else
result = '[[wikidata:' .. entityId .. '|' .. (label or entityId) .. ']]'
end
end
return entityId, result
end
function p.proves(args)
return _main(args)
end
function p.tree_instance(frame)
local args = frame.args
return _main(args)
end
function _main(args)
local id = args["item"]; if id == "" then id = nil end
local propertyFetch = args["property"]; if propertyFetch == nil then propertyFetch = "P131" end
local stop = args["stop"]
local target = args["target"]
local rowformat = args["rowformat"]; if rowformat == nil then rowformat = "$0 = $1" end
local loop = tonumber(stop) or 10
local lastone = uc_first(stop or '')
local filter = {}
if target then
for i, v in ipairs(mw.text.split(target, "/")) do
filter[uc_first(v)] = true
end
end
local propertyInstance = "P31"
local loops = 0
local result = {}
while id and (loops < loop) do
loops = loops + 1
local label, link
id, link = getEntityValue(id, propertyFetch, "link")
_, label = getEntityValue(id, propertyInstance, "label")
if label and link then
label = fixInfoboxLabel(label, link)
if target == nil or filter[label] then
result[#result + 1] = {label, link}
end
if label == lastone then
break
end
end
end
local ret = {}
for i = 1, #result do
ret[#ret +1] = mw.ustring.gsub(rowformat, "$[01]", {["$0"] = result[i][1], ["$1"] = result[i][2]})
end
return mw.text.listToText(ret, args.separator or '<br />', args.separator or '<br />')
end
return p