Module:Dump/doc

This module can dump a table by displaying its contents as text. It can also display formatted html text. That may help develop other modules.

An alternative is to use &#x3c;code&#x3e;mw.dumpObject&#x3c;/code&#x3e; but the result from this module is clearer and is close to valid Lua source.

Dump of a Wikidata entity
When working with Wikidata, it can be useful to examine a table representing an entity.

For example, Southern African Large Telescope is Q833639. That entity can be viewed as a Lua table by previewing:
 * &#x3c;code&#x3e;&#x3c;nowiki&#x3e;&#x3c;/nowiki&#x3e;&#x3c;/code&#x3e;

To do that, edit your sandbox and replace its contents with the above line, then click Show preview. Module talk:Dump shows this example.

If wanted, the width of each indented column can be set, for example to two spaces or to use tab characters:
 * &#x3c;code&#x3e;&#x3c;nowiki&#x3e;&#x3c;/nowiki&#x3e;&#x3c;/code&#x3e;
 * &#x3c;code&#x3e;&#x3c;nowiki&#x3e;&#x3c;/nowiki&#x3e;&#x3c;/code&#x3e;

A property such as can also be displayed:
 * &#x3c;code&#x3e;&#x3c;nowiki&#x3e;&#x3c;/nowiki&#x3e;&#x3c;/code&#x3e;

Dump of a table for another module
If there is a problem debugging a module, it can be helpful to use a sandbox copy of the module to display the contents of a table to confirm that it contains expected data. The following shows how a module can dump a table. &#x3c;syntaxhighlight lang="Lua"&#x3e; local p = {} function p.main(frame) local example = { link = true, fruit = { yellow = 'bannana', red = 'cherry' }, 11, 22, 33,	}	local dump = require('Module:Dump')._dump return dump(example, 'example') end return p &#x3c;/syntaxhighlight&#x3e;

With the above code in Module:Example, the result could be displayed by previewing:
 * &#x3c;code&#x3e;&#x3c;nowiki&#x3e;&#x3c;/nowiki&#x3e;&#x3c;/code&#x3e;

The module contains a complex table for testing. The table can be displayed by previewing:
 * &#x3c;code&#x3e;&#x3c;nowiki&#x3e;&#x3c;/nowiki&#x3e;&#x3c;/code&#x3e;

Dump of a formatted html string
A module can use mw.html to generate html. For testing, it may be useful to display the formatted result. The following shows how a module can create and dump html text.

&#x3c;syntaxhighlight lang="Lua"&#x3e; local function main local tbl = mw.html.create('table') tbl :addClass('wikitable') :tag('caption'):wikitext('Table demonstration'):done :tag('tr') :tag('th'):wikitext('Month'):done :tag('th'):wikitext('Amount'):done :done :tag('tr') :tag('td'):wikitext('January'):done :tag('td'):wikitext('$100&#x3c;br&#x3e;loss'):done :done :tag('tr') :tag('td'):wikitext('February'):done :tag('td'):wikitext('$200') local html = tostring(tbl) local dumphtml = require('Module:Dump')._dumphtml return dumphtml(html) end

return { main = main } &#x3c;/syntaxhighlight&#x3e;

With the above code in Module:Example, the result could be displayed by previewing:
 * &#x3c;code&#x3e;&#x3c;nowiki&#x3e;&#x3c;/nowiki&#x3e;&#x3c;/code&#x3e;

The result is: &#x3c;pre&#x3e; &#x3c;table class="wikitable"&#x3e; &#x3c;caption&#x3e;Table demonstration&#x3c;/caption&#x3e; &#x3c;tr&#x3e; &#x3c;th&#x3e;Month&#x3c;/th&#x3e; &#x3c;th&#x3e;Amount&#x3c;/th&#x3e; &#x3c;/tr&#x3e; &#x3c;tr&#x3e; &#x3c;td&#x3e;January&#x3c;/td&#x3e; &#x3c;td&#x3e;$100&#x3c;br&#x3e;loss&#x3c;/td&#x3e; &#x3c;/tr&#x3e; &#x3c;tr&#x3e; &#x3c;td&#x3e;February&#x3c;/td&#x3e; &#x3c;td&#x3e;$200&#x3c;/td&#x3e; &#x3c;/tr&#x3e; &#x3c;/table&#x3e; &#x3c;/pre&#x3e;

The &#x3c;code&#x3e;main&#x3c;/code&#x3e; function in the code above could be modified to return the html table by replacing the last two lines with: &#x3c;syntaxhighlight lang="Lua"&#x3e; return html &#x3c;/syntaxhighlight&#x3e;

In that case, the result could be displayed by previewing the following (the &#x3c;code&#x3e;1=&#x3c;/code&#x3e; is needed if the text contains "&#x3c;code&#x3e;=&#x3c;/code&#x3e;"):
 * &#x3c;code&#x3e;&#x3c;nowiki&#x3e;&#x3c;/nowiki&#x3e;&#x3c;/code&#x3e;

Dumping a navbox
Previewing the following examples in a sandbox may be useful to examine the results of a template, such as navbox, that generates html. &#x3c;pre&#x3e;

&#x3c;/pre&#x3e;

The dumphtml procedure only works reliably with valid html. In the following example, extra text (&#x3c;code&#x3e;&#x26;lt;div&#x26;gt;&#x3c;/code&#x3e;) is inserted at the start because the output from a subgroup (child) navbox starts with &#x3c;code&#x3e;&#x26;lt;/div&#x26;gt;&#x3c;/code&#x3e;. &#x3c;pre&#x3e;

&#x3c;/pre&#x3e;

Dump of arguments
Special:ExpandTemplates is useful if there is a need to view the wikitext returned by a template or module. However, ExpandTemplates does not always show exactly what a module would receive. For example, the following template gives the output shown in ExpandTemplates, but the wikitext passed to a module would actually contains strip markers.
 * &#x3c;code&#x3e;&#x3c;nowiki&#x3e;1+2/3&#x3c;ref&#x3e;Example&#x3c;/ref&#x3e; ft&#x3c;/nowiki&#x3e;&#x3c;/code&#x3e;
 * ExpandTemplates output, rearranged on multiple lines for clarity:
 * &#x3c;code&#x3e;&#x3c;nowiki&#x3e;&#x3c;templatestyles src="Fraction/styles.css"&#x3e;&#x3c;/templatestyles&#x3e;&#x3c;/nowiki&#x3e;&#x3c;/code&#x3e;
 * &#x3c;code&#x3e;&#x3c;nowiki&#x3e;&#x3c;span class="frac" role="math"&#x3e;1&#x3c;span class="sr-only"&#x3e;+&#x3c;/span&#x3e;&#x3c;/nowiki&#x3e;&#x3c;/code&#x3e;
 * &#x3c;code&#x3e;&#x3c;nowiki&#x3e;&#x3c;span class="num"&#x3e;2&#x3c;/span&#x3e;&#x26;frasl;&#x3c;span class="den"&#x3e;3&#x3c;/span&#x3e;&#x3c;/span&#x3e;&#x3c;/nowiki&#x3e;&#x3c;/code&#x3e;
 * &#x3c;code&#x3e;&#x3c;nowiki&#x3e;&#x3c;ref&#x3e;Example&#x3c;/ref&#x3e;&#x3c;/nowiki&#x3e;&#x3c;/code&#x3e;
 * &#x3c;code&#x3e;&#x3c;nowiki&#x3e; feet (20&#x26;nbsp;in)&#x3c;/nowiki&#x3e;&#x3c;/code&#x3e;

The &#x3c;code&#x3e;args&#x3c;/code&#x3e; function shows what a module receives in its arguments.
 * &#x3c;code&#x3e;&#x3c;nowiki&#x3e;&#x3c;/nowiki&#x3e;&#x3c;/code&#x3e;

The output follows. For clarity, it has been rearranged on multiple lines and each delete character has been replaced with &#x3c;code&#x3e;♢&#x3c;/code&#x3e;.
 * &#x3c;code&#x3e;&#x3c;nowiki&#x3e;♢'"`UNIQ--templatestyles-00000002-QINU`"'♢&#x3c;/nowiki&#x3e;&#x3c;/code&#x3e;
 * &#x3c;code&#x3e;&#x3c;nowiki&#x3e;&#x3c;span class="frac" role="math"&#x3e;1&#x3c;span class="sr-only"&#x3e;+&#x3c;/span&#x3e;&#x3c;/nowiki&#x3e;&#x3c;/code&#x3e;
 * &#x3c;code&#x3e;&#x3c;nowiki&#x3e;&#x3c;span class="num"&#x3e;2&#x3c;/span&#x3e;&#x26;frasl;&#x3c;span class="den"&#x3e;3&#x3c;/span&#x3e;&#x3c;/span&#x3e;&#x3c;/nowiki&#x3e;&#x3c;/code&#x3e;
 * &#x3c;code&#x3e;&#x3c;nowiki&#x3e;♢'"`UNIQ--ref-00000001-QINU`"'♢&#x3c;/nowiki&#x3e;&#x3c;/code&#x3e;
 * &#x3c;code&#x3e;&#x3c;nowiki&#x3e; feet (20&#x26;nbsp;in)&#x3c;/nowiki&#x3e;&#x3c;/code&#x3e;

Dump of parameters
A template might invoke the &#x3c;code&#x3e;main&#x3c;/code&#x3e; function in the example above. Any parameters passed to the template or the module can be displayed for debugging. That would be to investigate an unexpected result in a page, for example, Albedo. To see what parameters are received by a module used in that article, edit the module and insert the following line at the start of the main function: &#x3c;syntaxhighlight lang="Lua"&#x3e; if true then return require('Module:Dump').parameters(frame) end &#x3c;/syntaxhighlight&#x3e; Do not save the changes. Instead, enter the name of the article (for example, Albedo) in the box under "Preview page with this module", then click Show preview. Any parameters passed to the module in its frame and parent frame are displayed where the result from the module would normally appear.

Dump of structured data
A Lua program could execute &#x3c;syntaxhighlight lang="Lua"&#x3e; local data = mw.ext.data.get('Wikipedia statistics/data.tab') &#x3c;/syntaxhighlight&#x3e; to read a table of data from c:Data:Wikipedia statistics/data.tab.

An edit in a sandbox can be previewed to see what data the program would receive. To do that, preview the following wikitext:&#x3c;br&#x3e; &#x3c;code&#x3e;&#x3c;nowiki&#x3e;&#x3c;/nowiki&#x3e;&#x3c;/code&#x3e;

The dump module accepts any text as the parameter and will apply special processing if the text is recognized. Structured data is identified as text ending with &#x3c;code&#x3e;.tab&#x3c;/code&#x3e;.

Global table _G
In Lua, &#x3c;code&#x3e;_G&#x3c;/code&#x3e; is a global variable which is a table holding information about all global variables. The &#x3c;code&#x3e;_G&#x3c;/code&#x3e; table can be displayed by previewing (both &#x3c;code&#x3e;G&#x3c;/code&#x3e; and &#x3c;code&#x3e;_G&#x3c;/code&#x3e; work):
 * &#x3c;code&#x3e;&#x3c;nowiki&#x3e;&#x3c;/nowiki&#x3e;&#x3c;/code&#x3e;

If wanted, the width of each indented column can be set, for example to 2 spaces:
 * &#x3c;code&#x3e;&#x3c;nowiki&#x3e;&#x3c;/nowiki&#x3e;&#x3c;/code&#x3e;