विकिपीडिया:Lua
WP:Lua Project | WT:Lua Project talk | Modules | Help Coding and testing | Requests | Resources en: m: mw: external |
This is an information page that describes communal consensus on some aspect of Wikipedia norms and practices. While it is not a policy or guideline itself, it is intended to supplement or clarify other Wikipedia practices and policies. Please defer to the relevant policy or guideline in case of inconsistency between that page and this one. |
Wikipedia namespaces | |||
---|---|---|---|
Basic namespaces | Talk namespaces | ||
0 | (Main/Article) | सम्भाषणम् | 1 |
2 | सदस्यः | सदस्यसम्भाषणम् | 3 |
4 | विकिपीडिया | विकिपीडियासम्भाषणम् | 5 |
6 | सञ्चिका | सञ्चिकासम्भाषणम् | 7 |
8 | मीडियाविकि | मीडियाविकिसम्भाषणम् | 9 |
10 | फलकम् | फलकसम्भाषणम् | 11 |
12 | साहाय्यम् | साहाय्यसम्भाषणम् | 13 |
14 | वर्गः | वर्गसम्भाषणम् | 15 |
100 | विकिद्वारम्प्रवेशद्वारम् | विकिद्वारस्य सम्भाषणम्प्रवेशद्वारसम्भाषणम् | 101 |
108 | पुस्तकानि | पुस्तकसम्भाषणम् | 109 |
446 | कार्यशाला | कार्यशालासम्भाषणम् | 447 |
710 | TimedMediaHandlerTimedText | TimedMediaHandlerTimedText talk | 711 |
828 | पटलम् | पटलसम्भाषणम् | 829 |
Virtual namespaces | |||
-1 | Special | ||
-2 | Media | ||
Lua is a programming language that is now available, via the Scribunto MediaWiki extension, on the English Wikipedia. Lua code can now be embedded into wiki templates by employing the "{{#invoke:}}" 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 "{{#invoke:}}") on template pages (e.g., Module:Bananas/doc uses the code {{#invoke:Bananas|hello}} 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 Wikipedia:Lua requests to request help in writing a Lua script to perform a specific task on Wikipedia 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.wikipedia.org, with open invitation to all editors to experiment with developing Lua modules. Lua was installed on the English Wikipedia in February 2013, after testing on mediawiki.org and Wikimedia test wikis.
About Lua
[सम्पादयतु]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 including 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:
-- All Lua modules on Wikipedia must begin by defining a variable that will hold their
-- externally accessible functions. They can have any name and may also hold data.
my_object = {};
-- Add a function to the variable. These are callable in Wikipedia via the #invoke command.
-- "frame" will contain the data that Wikipedia sends this function when it is called.
my_object.hello = function( frame )
-- Declare a local variable and assign data to it.
local str = "Hello World!"
-- Quit this function and send the information in "str" back to Wikipedia.
-- The "print" function is not allowed, so all output is accomplished via
-- returning strings in this fashion.
return str
-- End the function.
end
-- All modules end by returning the variable containing its functions to Wikipedia.
return my_object
-- We can now use this module by calling {{#invoke: HelloWorld | hello }}.
-- The #invoke command begins with the module's name, in this case "HelloWorld",
-- then takes the name of one of its functions as an argument, in this case "hello".
A sample of Lua is highlighted by tag "<source lang="lua">...</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 Wikipedia 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. {{#invoke: Bananas/testcases | run_tests}}
. Test methods must begin with "test". A simple example from Module:Bananas/testcases is below.
-- Unit tests for [[Module:Bananas]]. Click talk page to run tests.
local p = require('Module:UnitTests')
function p:test_hello()
self:preprocess_equals('{{#invoke:Bananas | hello}}', 'Hello, world!')
end
return p
For a list of all modules using unit tests, see Special:Whatlinkshere/Module:UnitTests.
Wikipedia-specific features
[सम्पादयतु]Overall: Lua can only get input as text strings passed to the {{#invoke:}}
and what can be fetched via frame:expandTemplate, frame:preprocess, and the like. Lua on Wikipedia 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#Differences from standard Lua).
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 Wikipedia. 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 Wikipedia 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 [[Wikipedia:Help|]] won't work if returned as output - they need to be written explicitly as [[Wikipedia:Help|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 <ref>
or <nowiki>
.
Labeling converted templates
[सम्पादयतु]Please place the {{lua}} template on the documentation subpage of all templates that use Lua. It will help to better communicate Lua usage and template conversions. It looks like this:
Error: no modules specified |
See also
[सम्पादयतु]- English Wikipedia-specific resources
- Wikipedia:Lua style guide – standards to improve the readability of code through consistency
- Special:PrefixIndex/Module: – tracking of Lua modules can be done by using Special:PrefixIndex
- Help:Lua debugging – a how-to guide about debugging Lua modules
- Category:Lua-based templates – groups of Lua-based templates
- Module:Sandbox provides a pseudo-namespace for experimenting with Lua modules.
- WP:Lua requests – requests for Lua-based templates or tasks
WP:Lua Project | WT:Lua Project talk | Modules | Help Coding and testing | Requests | Resources en: m: mw: external |