Jump to content

Mukääcëtök:Wikidata/debug

Jɔk Wikipedia

Documentation for this module may be created at Mukääcëtök:Wikidata/debug/doc

-- 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.getBestStatements(id, P)
-- on debug console use: =p.ViewBestStatements({'id', 'P'})
function p.ViewBestStatements(frame)
	local args = frame.arg or frame -- from invoke or from Lua table
	local statements = mw.wikibase.getBestStatements(args[1], args[2])
	return mw.text.jsonEncode(statements, mw.text.JSON_PRETTY)
end

-- utility for tracking how the module is used
-- see documentation at [[wikt:en:Template:tracking]]
-- see your tracking at Special:WhatLinkHere/Template:track/wikidata/<your label>
function p.track(label)
	local frame = mw.getCurrentFrame()
	pcall(frame.expandTemplate, frame, {title = 'track/wikidata/' .. label})
end

return p