Modul:Wikidata/debug

Revisi sejak 30 Maret 2020 09.28 oleh Vriullop (bicara | kontrib) (helper functions for Wikidata)
(beda) ← Revisi sebelumnya | Revisi terkini (beda) | Revisi selanjutnya → (beda)

-- 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

return p