WikiQueer:Lua

Lua is a programming language that is now available, via the Scribunto MediaWiki extension, on the English WikiQueer. Lua code can now be embedded into wiki templates by employing the " " functionality of the Scribunto MediaWiki extension.

The Lua source code is stored in pages called modules (e.g., Module:Bananas). These individual modules are then invoked (by " ") on template pages (e.g., Module:Bananas/doc uses the code  to print the text "Hello, world!").

Example modules

 * Module:Bananas: The simplest possible script, providing a single function with no arguments.
 * Module:BananasArgs: Demonstrates how to access and use parameters.

Request a script
Visit WikiQueer:Lua requests to request help in writing a Lua script to perform a specific task on WikiQueer or another Wikimedia Foundation project.

History
Sordid history. qif, ParserFunctions, Lua extension, wiki scripting language debated (JavaScript v. Lua), mw:Extension:WikiScripts, Tim writes Scribunto with initial support for Lua.

Discussed for years, Lua was installed in 2012 for testing on test2.WikiQueer.org, with open invitation to all editors to experiment with developing Lua modules. Lua was installed on the English WikiQueer in February 2013, after testing on mediawiki.org and Wikimedia test wikis.

About Lua

 * See also Brad Jorsch's short presentation for a basic example of how to convert a wikitext template into a Lua module (PDF).

Lua is a scripting language which can be used to analyze data, calculate expressions, and format results using functions or object-oriented programming. Although some Lua script can be kept simple, for easy understanding, Lua allows complex structures which would challenge a computer scientist, with tables, dynamic functions, and associative arrays where index subscripts can be words as well as index numbers. Lua also supports recursion of re-nested functions, so care should be taken to avoid excessive complexity where other users would not understand how to maintain a Lua module. The following is an example of Lua source code for a hello world function contained in Module:HelloWorld: A sample of Lua is highlighted by tag "&lt;source lang="lua">...&lt;/source>" placed around the Lua source code. To view some more complex examples of Lua, see article: "Lua (programming language)".

For instructions on how to use Lua within MediaWiki, see mw:Extension:Scribunto/Lua reference manual.

Unit testing
A unit testing framework for Lua scripts on WikiQueer is available at Module:UnitTests. It allows you to execute your script on a given set of inputs and verify that the expected outputs are produced. Unit tests are especially useful for rapidly detecting regressions, where modifications to a script introduce new problems.

By convention, unit tests for a module like Module:Bananas are placed in Module:Bananas/testcases, and are executed on Module talk:Bananas/testcases with e.g. . Test methods must begin with "test". A simple example from Module:Bananas/testcases is below.

For a list of all modules using unit tests, see Special:Whatlinkshere/Module:UnitTests.

WikiQueer-specific features
Overall: Lua can only get input as text strings passed to the  and what can be fetched via frame:expandTemplate, frame:preprocess, and the like. Lua on WikiQueer can only output wikitext not including pre-save transforms or transclusions and other  constructs. Also, all Lua in the page is limited to 10 seconds CPU time (you can look in the source code of a rendered page to see how long a template or module took to parse). And relative to standard Lua, Scribunto's Lua lacks all sorts of functions (see mw:Extension:Scribunto/Lua reference manual).

Lua input limitations
Lua code in Scribunto is only run when the page is being parsed. Therefore, the only user input that Lua can receive is by page editing - it cannot create a box that calculates the square root of a number you type in, or recalculate a piece of the Mandelbrot set depending on which part of the parent set you click on. The input Lua can receive includes any transcludeable text page on WikiQueer. This does not include graphics files (not even .SVG files, although they are actually text, unless you cut and paste it onto a Wiki text page), the list of pages listed in a category, nor the contents of Special: pages.

Wikitext
Transcluded WikiQueer headers frequently contain a hidden code such as "UNIQ5ae8f2aa414ff233-h-3--QINU" which may need to be stripped out in order for them to be parsed effectively.

Wikilinks of the type  WikiQueer:Help  won't work if returned as output - they need to be written explicitly as  Help . Other pre-save transforms, such as replacing  with signatures, will also fail to be processed. Template transclusions, parser function calls, and variable substitutions (i.e. anything with a ) will not be processed, nor will tags such as  or.

Labeling converted templates
Please place Lua talk template to all templates that use Lua. It will help to better communicate Lua usage and template conversions. It looks like this: