Mòdul:Wikidades/debug

-- Helper functions for debugging Wikidata data, do not use them on any article or templatelocal 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 thenreturnendlocal i = 1while true dolocal index = f.args[i]if not index thenreturn frame:extensionTag('syntaxhighlight', mw.dumpObject(data), {lang = 'json'})enddata = data[index] or data[tonumber(index)]if not data thenreturnendi = i + 1endend-- 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.itemif id and (#id == 0) thenid = nilendlocal data = mw.wikibase.getEntity(id)if not data thenreturn nilendlocal i = 1while true dolocal index = f.args[i]if not index thenif type(data) == "table" thenreturn frame:extensionTag('syntaxhighlight', mw.text.jsonEncode(data, mw.text.JSON_PRETTY), {lang = 'json'})elsereturn tostring(data)endenddata = data[index] or data[tonumber(index)]if not data thenreturnendi = i + 1endend-- Look into entity object-- From pl:Module:Wikidane, function V, by User:Paweł Ziemianfunction p.getEntityFromTree(frame)local data = mw.wikibase.getEntity()if not data thenreturn nilendlocal f = frame.args[1] and frame or frame:getParent()local i = 1while true dolocal index = f.args[i]if not index thenreturn tostring(data)enddata = data[index] or data[tonumber(index)]if not data thenreturnendi = i + 1endend-- 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 consolelocal qid, pidqid = mw.text.trim(args[1] or ""):upper()if qid:sub(1,1) ~= "Q" thenpid = qidqid = mw.wikibase.getEntityIdForCurrentPage()elsepid = mw.text.trim(args[2] or ""):upper()endif not qid then return "Ítem no trobat" endif pid:sub(1,1) ~= "P" then return "Cal una propietat" endlocal statements = mw.wikibase.getAllStatements(qid, pid)if args == frame thenreturn mw.dumpObject(statements)elsereturn frame:extensionTag('syntaxhighlight', mw.text.jsonEncode(statements, mw.text.JSON_PRETTY), {lang = 'json'})endend-- 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 codesfunction p.getFallbacks(frame)local args = frame.args or frame -- from invoke or from debug consolelocal lang = args[1] and mw.text.trim(args[1]) or mw.language.getContentLanguage().codereturn table.concat(mw.language.getFallbacksFor(lang), ', ')end-- Validate Qids in a given page: ok, does not exist, or it is a Wikidata redirectfunction p.validateIds(frame)local content = mw.title.new(frame.args[1]):getContent()if not content then return "Page not found" endlocal qids = {}for qid in string.gmatch(content or '', '(Q%d+)') doif not qids[qid] thenqids[qid] = trueendendif not next(qids) then return "None Qid found" endlocal ret = {}for qid, _ in pairs(qids) dolocal entity = mw.wikibase.getEntity(qid)if not entity thentable.insert(ret, '* ' .. qid .. ' [[File:Red x.svg|13px|link=|Nay]]')elseif entity.id == qid thentable.insert(ret, '* [[d:' .. qid .. '|' .. qid .. ']] [[File:Symbol OK.svg|13px|link=|Ok]]')elsetable.insert(ret, '* ' .. qid .. '[[File:Redirectltr.png|40px|link=|#REDIRECT]][[d:' .. qid .. '|' .. entity.id .. ']]')endendreturn #ret > 0 and table.concat(ret, '\n')endreturn p