Module:Dump

-- Dump a table to help develop other modules. -- It is also possible to use mw.dumpObject but the result from this -- module is clearer and is close to valid Lua source. -- The main purpose is to allow easy inspection of Wikidata items. -- Preview the following in a sandbox to see entity Q833639 as a Lua table: --  -- Preview the following to dump a built-in table: --

local Collection -- a table to hold items Collection = { add = function (self, item) if item ~= nil then self.n = self.n + 1 self[self.n] = item end end, join = function (self, sep) return table.concat(self, sep) end, remove = function (self, pos) if self.n > 0 and (pos == nil or (0 < pos and pos <= self.n)) then self.n = self.n - 1 return table.remove(self, pos) end end, sort = function (self, comp) table.sort(self, comp) end, new = function return setmetatable({n = 0}, Collection) end } Collection.__index = Collection

local function pre_block(text) -- Pre tags returned by a module do not act like wikitext ... .	return ' \n' .. mw.text.nowiki(text) .. (text:sub(-1) == '\n' and '' or '\n') .. ' \n' end

local function make_tabstr(indent) -- Return a string to generate one level of indent. if indent == 'tab' then -- Tabs do not work well in a browser edit window, but can force them. return '\t' end indent = tonumber(indent) if not (type(indent) == 'number' and 1 <= indent and indent <= 32) then indent = 4 end return string.rep(' ', indent) end

local function _dumphtml(html, tabwidth) -- Return a pretty-text formatted dump of an html string. -- This assumes clean html, for example, tag " ')	end	return lines:join(' \n') end

local function parameters(frame) -- Return text dump of args and parent args from frame. -- This is for debugging a module to show what parameters it received. local control = { autoname = function (var) return tostring(var) end, -- should not be called since keys should be numbers or strings }	local lines = Collection.new lines:add('') for _, f in ipairs({ frame, frame:getParent }) do lines:add( .. f:getTitle .. ) for key, keyrep in iterkeys(f.args, control) do lines:add(' ' .. mw.text.nowiki(keyrep .. '=' .. f.args[key])) end end lines:add('') return lines:join(' \n') end

local function wikidata(frame) local item = frame.args[1] if item then local id = item:match('^%s*([PQ]%d+)%s*$') if id then local entity = mw.wikibase.getEntity(id) return dumper(entity, id, frame.args.indent) end end return 'Parameter should be a Wikidata identifier such as P2386 or Q833639' end

local builtins = { -- Handle preview of wikitext like -- where TEXT is a built-in value that can be dumped. __index = function (self, key) local result local function caller return result end if type(key) == 'string' then local title = key:match('^%s*[\'"]?(.*%.tab)[\'"]?%s*$') if title then -- Assume structured data from Commons at c:Data:. if title:match('^[Dd]ata:') then title = title:sub(6) end local data = mw.ext.data.get(title) -- false if page does not exist result = dumper(data, 'c:Data:' .. title .. '') end end result = result or ('UNKNOWN: ' .. tostring(key)) return caller end }

return setmetatable({	args = dumpargs,	_dump = dumper,	_dumphtml = _dumphtml,	dumphtml = dumphtml,	execute = execute,	parameters = parameters,	testcase = dump_testcase,	wikidata = wikidata, }, builtins)