GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing en.wikipedia.org — Wikipedia:Lua - Wikipedia (Redirected from Help:Modules)
Lua is a lightweight scripting language that can be embedded in templates through the Scribunto MediaWiki extension to create scripts on the English Wikipedia. Since February 2013, Lua code has been available for use on the English Wikipedia via the On Wikipedia, Lua source code is stored on pages called modules in the module namespace (e.g., the source code for a module named "Example" would be at Module:Example). Individual modules are then invoked, using the syntax
Running a moduleModules 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 take inputted values, processes them, and returns an output value.[1] This is much like what a template does: one gives it parameters, 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 cannot 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 does not do anything by itself. So there are two reasons that we need to input a function name: we cannot 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".
Using argumentsArguments, also known as parameters, are passed to modules in the same way that they are passed to templates. Note, however, that the text after the first pipe character ( {{#invoke:module name|function name|first positional argument|second positional argument|named argument = value}}
In Module:Example, the
A third function in Module:Example, named
Most modules have a documentation page explaining what arguments can be used and what their effects will be. In VisualEditorCurrently, VisualEditor does not allow invoking modules through its interface, even though it can edit existing module invocations. A possible workaround for this is to use {{invoke}} instead. The syntax is very similar to #invoke, so, to use it on VisualEditor, its first unnamed argument must be the module's name, the second the function name, and all arguments passed to the module can be specified the same way as for regular templates. It won't work for modules that expect to read data from the calling template (as the calling template is {{invoke}} itself rather than the page that uses it) as the example below demonstrates:
However, that is a pathological case, most modules will behave the same either way. Request a scriptVisit Wikipedia talk:Lua to request help in writing a Lua script to perform a specific task on Wikipedia or another Wikimedia Foundation project. HistorySee also: Wikipedia:Before Lua
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 an 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 LuaLua 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. --- Example module. -- @module example -- @alias p local p = {} --All Lua modules on Wikipedia must begin by defining a variable --that will hold their externally accessible functions. --Such variables can have whatever name you want and may --also contain various data as well as functions. --- Hello world function -- @param {Frame} frame current frame -- @return Hello world -- @usage {{#invoke:Example|hello}} p.hello = function(frame) --Add a function to "p". --Such functions are callable in Wikipedia --via the #invoke command. --"frame" will contain the data that Wikipedia --sends this function when it runs. -- 'Hello' is a name of your choice. The same name needs to be referred to when the module is used. local str = "Hello World!" --Declare a local variable and set it equal to --"Hello World!". return str --This tells us to quit this function and send the information in --"str" back to Wikipedia. end -- end of the function "hello" --- Hello world function -- @param {Frame} frame current frame -- @param {string} frame.args[1] name -- @return Hello world -- @usage {{#invoke:Example|hello_to|name}} function p.hello_to(frame) -- Add another function (order of "function" and the function name does not matter) local name = frame.args[1] -- To access arguments passed to a module, use `frame.args` -- `frame.args[1]` refers to the first unnamed parameter -- given to the module return "Hello, " .. name .. "!" -- `..` concatenates strings. This will return a customized -- greeting depending on the name given, such as "Hello, Fred!" end --- Counts fruit -- @param {Frame} frame current frame -- @param {string} frame.args.bananas number of bananas -- @param {string} frame.args.apples number of apples -- @return Number of apples and bananas -- @usage {{#invoke:Example|count_fruit|apples=number of apples|bananas=number of bananas}} function p.count_fruit(frame) local num_bananas = tonumber(frame.args.bananas) or 0 -- Named arguments ({{#invoke:Example|count_fruit|foo=bar}}) local num_apples = tonumber(frame.args.apples) or 0 -- are likewise accessed by indexing `frame.args` by name (`frame.args["bananas"]`, -- or equivalently `frame.args.bananas`). local conj_bananas = num_bananas == 1 and 'banana' or 'bananas' local conj_apples = num_apples == 1 and 'apple' or 'apples' -- Ternary operators assign values based on a condition in a compact way. -- Here, `conj_bananas` gets `'banana'` if `num_bananas` is 1, else `'bananas'`. -- Similarly, `conj_apples` gets `'apple'` if `num_apples` is 1, else `'apples'`. return 'I have ' .. num_bananas .. ' ' .. conj_bananas .. ' and ' .. num_apples .. ' ' .. conj_apples -- Like above, concatenate a bunch of strings together to produce -- a sentence based on the arguments given. end --- Lucky function -- @param {number} a -- @param {string} b -- @return Whether a is lucky. -- @usage lucky(number, string) return p --All modules end by returning the variable containing their functions to Wikipedia. -- Now we can use this module by calling {{#invoke: Example | hello }}, -- {{#invoke: Example | hello_to | foo }}, or {{#invoke:Example|count_fruit|bananas=5|apples=6}} -- Note that the first part of the invoke is the name of the Module's wikipage, -- and the second part is the name of one of the functions attached to the -- variable that you returned. -- The "print" function is not allowed in Wikipedia. All output is accomplished -- via strings "returned" to Wikipedia. A sample of Lua is highlighted by tag For instructions on how to use Lua within MediaWiki (and hence Wikipedia), see mw:Extension:Scribunto/Lua reference manual. Unit testingSee also: Category:Modules for test tools
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:UnitTests is the prefilled code used when creating a testcases page. You run these unit tests by placing Module:ScribuntoUnit is another widely used test framework. Category:Modules for test tools has a few others to review which may be interesting. For testing and step-by-step debugging of Lua modules outside of MediaWiki you can try using an mw mock (should work for all simple modules and maybe even some complicated ones). MediaWiki-specific featuresOverall: Lua can only get input as text strings passed to the Lua input limitationsLua 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. WikitextTranscluded 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 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. See alsoCategories
Other pages
Notes
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||