[go: up one dir, main page]
More Web Proxy on the site http://driver.im/Yiɣi chaŋ yɛligu maŋamaŋa puuni

Wikipedia:Lua

Diyila Dagbani Wikipedia
Wikipedia data structure
Namespaces
Subject namespacesTalk namespaces
0 (Main/Article)Yɛltɔɣa 1
2 Ŋun suŊun su yɛltɔɣa 3
4 WikipediaWikipedia yɛltɔɣa 5
6 Lahabali kɔliguLahabali kɔligu yɛltɔɣa 7
8 MiidiyaWikiMiidiyaWiki yɛltɔɣa 9
10 TɛmpletTɛmplet yɛltɔɣa 11
12 SɔŋsimSɔŋsim yɛltɔɣa 13
14 PubuPubu yɛltɔɣa 15
100 [[Wikipedia:Portal|]] 101
118 [[Wikipedia:Drafts|]] 119
710 TimedTextTimedText talk 711
828 ModuleModule talk 829
Deprecated
2300 [[Wikipedia:Gadget|]] 2301
2302 [[Wikipedia:Gadget|]] 2303
Not installed
90 ThreadThread talk 91
92 SummarySummary talk 93
108 BookBook talk 109
442 CourseCourse talk 443
444 InstitutionInstitution talk 445
446 Education ProgramEducation Program talk 447
2600 Topic 2601
Virtual namespaces
-1 Diŋ'gahim
-2 Miidiya
Current list (API call)

Lua is a programming language that is available via the Scribunto MediaWiki extension on the English Wikipedia. Since February 2013, Lua code can be embedded into wiki templates by employing the "{{#invoke:}}" functionality of Scribunto. This extension supports Lua 5.1 as of October 2022.

The Lua source code is stored in pages called modules (e.g., Module:Example). These individual modules are then invoked (by code {{#invoke:<Module name>|<Function name>|(optional) param1 | param2...}}). Example:

Wikitext Result
{{#invoke:Example|hello}} Script error: No such module "Example".

Modules are run on normal wiki pages using the #invoke parser function. The syntax of #invoke is similar to template syntax, but with some differences. The most important difference is that you need to specify a function name. A function is a set of instructions that takes input values, processes them, and returns an output value.[1] This is much like what a template does: you give it arguments, it processes them, and you get a result. However, you can define many functions in one Lua module, whereas you can only define one template on one page.

Furthermore, you can't just run a Lua module directly – you can only run one of the module's functions. The module is just a container for the functions, and doesn't do anything by itself. So there are two reasons that we need to input a function name: we can't run a module by itself, and without specifying a function name, Lua will not know which function it is we want to run.

The simplest way to run a module from a wiki page is like this:

{{#invoke:module name|function name}}

For example, we can run Module:Example in this way, which has a function named "hello".

  • {{#invoke:Example|hello}}Script error: No such module "Example".

Arguments are passed to modules in the same way that they are passed to templates. Note, however, that the text after the first pipe character is always the function name; the first positional argument is the text after the second pipe.

{{#invoke:module name|function name|first positional argument|second positional argument|named argument = value}}

In Module:Example, the "hello_to" function greets different people depending on the first positional argument. It works like this:

  • {{#invoke:Example|hello_to|Kate}}Script error: No such module "Example".
  • {{#invoke:Example|hello_to|Fred}}Script error: No such module "Example".

A third function in Module:Example, named "count_fruit", uses the named arguments bananas and apples to count the number of bananas and apples we have. It can be run like this:

  • {{#invoke:Example|count_fruit|apples=3|bananas=4}}Script error: No such module "Example".
  • {{#invoke:Example|count_fruit|bananas=5|apples=2}}Script error: No such module "Example".

Most modules have a documentation page explaining what arguments can be used and what their effects will be.

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

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.

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

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 scripts 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 the source code of the module used for the examples above.

A sample of Lua is highlighted by tag "<syntaxhighlight lang="lua">...</syntaxhighlight>" 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 (and hence Wikipedia), see mw:Extension:Scribunto/Lua reference manual.

A few unit testing frameworks are available for Lua scripts on Wikipedia. These allow an editor to execute the module with a given set of inputs and verify that the expected outputs are produced. They are useful for rapidly detecting software regressions, where modifications to a script introduce new (or identify old) problems.

By convention, unit tests for a module like Module:Example are placed in Module:Example/testcases, and are executed on Module talk:Example/testcases.

Module:ScribuntoUnit and Module:UnitTests are widely used test frameworks. Category:Modules for test tools has a few other to review which may be interesting.

MediaWiki-specific features

[mali mi di yibu sheena n-niŋ]

Overall: Lua can only get input as text strings passed to the {{#invoke:}} and what can be fetched via mw.title.new(...):getContent() and frame:expandTemplate(). Lua output will not be preprocessed unless frame:preprocess() is explicitly called, meaning that template calls, parser functions, etc. in the output will not work correctly. 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

[mali mi di yibu sheena n-niŋ]

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 non-transcludeable special pages.

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 using the pipe trick [[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>. Use frame:extensionTag to add tags like <ref> or <syntaxhighlight> to output.

Labeling converted templates

[mali mi di yibu sheena n-niŋ]

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.

  1. You can also have multiple output values, but functions that do this are not normally meant to be accessed from wiki pages.