Modulo:Wikidades/debug
Inpostasion de letura
-- Helper functions for debugging Wikidata data, do not use them on any article or template
local p = {}
-- Dump data tree structure
-- From pl:Module:Wikidane, by User:Paweł Ziemian
-- On any page associated with Wikidata, preview {{#invoke:Wikidata/debug|Dump}}. Do not save.
function p.Dump(frame)
local f = (frame.args[1] or frame.args.id) and frame or frame:getParent()
local data = mw.wikibase.getEntityObject(f.args.id)
if not data then
return
end
local i = 1
while true do
local index = f.args[i]
if not index then
return frame:extensionTag('syntaxhighlight', mw.dumpObject(data), {lang = 'json'})
end
data = data[index] or data[tonumber(index)]
if not data then
return
end
i = i + 1
end
end
-- Look into entity object
-- Add parameters as needed. Example: {{#invoke:Wikidata/debug|ViewSomething|claims|P17|1|mainsnak}}
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.getEntity(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 frame:extensionTag('syntaxhighlight', mw.text.jsonEncode(data, mw.text.JSON_PRETTY), {lang = 'json'})
else
return tostring(data)
end
end
data = data[index] or data[tonumber(index)]
if not data then
return
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.getEntity()
if not data then
return nil
end
local f = frame.args[1] and frame or frame:getParent()
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
-- helper function for debugging mw.wikibase.getAllStatements(id, P)
-- on debug console use: =p.ViewAllStatements({'Qid', 'Pid'})
function p.ViewAllStatements(frame)
local args = frame.args or frame -- from invoke or from debug console
local qid, pid
qid = mw.text.trim(args[1] or ""):upper()
if qid:sub(1,1) ~= "Q" then
pid = qid
qid = mw.wikibase.getEntityIdForCurrentPage()
else
pid = mw.text.trim(args[2] or ""):upper()
end
if not qid then return "Ítem no trobat" end
if pid:sub(1,1) ~= "P" then return "Cal una propietat" end
local statements = mw.wikibase.getAllStatements(qid, pid)
if args == frame then
return mw.dumpObject(statements)
else
return frame:extensionTag('syntaxhighlight', mw.text.jsonEncode(statements, mw.text.JSON_PRETTY), {lang = 'json'})
end
end
-- utility for tracking how the module is used
-- see documentation at [[wikt:en:Template:tracking]]
-- see your tracking at Special:WhatLinksHere/Template:track/wikidata/<your label>
function p.track(label)
local frame = mw.getCurrentFrame()
pcall(frame.expandTemplate, frame, {title = 'track/wikidata/' .. label})
end
-- Return fall back language codes
function p.getFallbacks(frame)
local args = frame.args or frame -- from invoke or from debug console
local lang = args[1] and mw.text.trim(args[1]) or mw.language.getContentLanguage().code
return table.concat(mw.language.getFallbacksFor(lang), ', ')
end
-- Validate Qids in a given page: ok, does not exist, or it is a Wikidata redirect
function p.validateIds(frame)
local content = mw.title.new(frame.args[1]):getContent()
if not content then return "Page not found" end
local qids = {}
for qid in string.gmatch(content or '', '(Q%d+)') do
if not qids[qid] then
qids[qid] = true
end
end
if not next(qids) then return "None Qid found" end
local ret = {}
for qid, _ in pairs(qids) do
local entity = mw.wikibase.getEntity(qid)
if not entity then
table.insert(ret, '* ' .. qid .. ' [[File:Red x.svg|13px|link=|Nay]]')
elseif entity.id == qid then
table.insert(ret, '* [[d:' .. qid .. '|' .. qid .. ']] [[File:Symbol OK.svg|13px|link=|Ok]]')
else
table.insert(ret, '* ' .. qid .. '[[File:Redirectltr.png|40px|link=|#REDIRECT]][[d:' .. qid .. '|' .. entity.id .. ']]')
end
end
return #ret > 0 and table.concat(ret, '\n')
end
return p