Anonymous user
wikishia:Lua: Difference between revisions
m
no edit summary
imported>Haghani (Created page with "{{WikiProject Lua/header}} {{Information page|WS:Lua}} {{Namespaces}} Lua is a programming language that is now available, via the mw:Extensi...") |
imported>Haghani mNo edit summary |
||
Line 3: | Line 3: | ||
{{Namespaces}} | {{Namespaces}} | ||
[[Lua (programming language)|Lua]] is a programming language that is now available, via the [[mw:Extension:Scribunto|Scribunto]] MediaWiki extension, on the English Wikishia. Lua code can now be embedded into wiki templates by employing the "<nowiki>{{#invoke:}}</nowiki>" functionality of the Scribunto MediaWiki extension. | [[Lua (programming language)|Lua]] is a programming language that is now available, via the [[mw:Extension:Scribunto|Scribunto]] MediaWiki extension, on the English Wikishia. Lua code can now be embedded into wiki templates by employing the " | ||
<nowiki>{{#invoke:}}</nowiki> " 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 code <code><nowiki>{{#invoke:}}</nowiki></code>) on template pages (e.g., [[Module:Bananas/doc]] uses the code <code><nowiki>{{#invoke:Bananas|hello}}</nowiki></code> to print the text "Hello, world!"). | The Lua [[source code]] is stored in pages called modules (e.g., [[Module:Bananas]]). These individual modules are then invoked (by code <code> | ||
<nowiki>{{#invoke:}}</nowiki> </code>) on template pages (e.g., [[Module:Bananas/doc]] uses the code <code> | |||
<nowiki>{{#invoke:Bananas|hello}}</nowiki> </code> to print the text "Hello, world!"). | |||
== Running a module == | == Running a module == | ||
Line 19: | Line 22: | ||
For example, we can run [[Module:Bananas]] in this way, which has one function named "hello". | For example, we can run [[Module:Bananas]] in this way, which has one function named "hello". | ||
* <code><nowiki>{{#invoke:Bananas|hello}}</nowiki></code> → {{#invoke:Bananas|hello}} | * <code> | ||
<nowiki>{{#invoke:Bananas|hello}}</nowiki> </code> → {{#invoke:Bananas|hello}} | |||
=== Using arguments === | === Using arguments === | ||
Line 28: | Line 32: | ||
For example, in [[Module:BananasArgs]], the "hello" function greets different people depending on the first positional argument. It works like this: | For example, in [[Module:BananasArgs]], the "hello" function greets different people depending on the first positional argument. It works like this: | ||
* <code><nowiki>{{#invoke:BananasArgs|hello|Kate}}</nowiki></code> → {{#invoke:BananasArgs|hello|Kate}} | * <code> | ||
* <code><nowiki>{{#invoke:BananasArgs|hello|Fred}}</nowiki></code> → {{#invoke:BananasArgs|hello|Fred}} | <nowiki>{{#invoke:BananasArgs|hello|Kate}}</nowiki> </code> → {{#invoke:BananasArgs|hello|Kate}} | ||
* <code> | |||
<nowiki>{{#invoke:BananasArgs|hello|Fred}}</nowiki> </code> → {{#invoke:BananasArgs|hello|Fred}} | |||
BananasArgs also has a function named "count_fruit" which uses the named arguments <code>bananas</code> and <code>apples</code> to count the number of bananas and apples we have. It can be run like this: | BananasArgs also has a function named "count_fruit" which uses the named arguments <code>bananas</code> and <code>apples</code> to count the number of bananas and apples we have. It can be run like this: | ||
* <code><nowiki>{{#invoke:BananasArgs|count_fruit|apples=3|bananas=4}}</nowiki></code> → {{#invoke:BananasArgs|count_fruit|apples=3|bananas=4}} | * <code> | ||
* <code><nowiki>{{#invoke:BananasArgs|count_fruit|bananas=5|apples=2}}</nowiki></code> → {{#invoke:BananasArgs|count_fruit|bananas=5|apples=2}} | <nowiki>{{#invoke:BananasArgs|count_fruit|apples=3|bananas=4}}</nowiki> </code> → {{#invoke:BananasArgs|count_fruit|apples=3|bananas=4}} | ||
* <code> | |||
<nowiki>{{#invoke:BananasArgs|count_fruit|bananas=5|apples=2}}</nowiki> </code> → {{#invoke:BananasArgs|count_fruit|bananas=5|apples=2}} | |||
Most modules will have a documentation page explaining what arguments can be used and what their effects will be. | Most modules will have a documentation page explaining what arguments can be used and what their effects will be. | ||
Line 58: | Line 66: | ||
-- Add a function to the variable. These are callable in Wikishia via the #invoke command. | -- Add a function to the variable. These are callable in Wikishia via the #invoke command. | ||
-- "frame" will contain the data that Wikishia sends this function when it is called. | -- "frame" will contain the data that Wikishia sends this function when it is called. | ||
my_object.hello = function( frame ) | my_object.hello = function( frame ) | ||
-- Declare a local variable and assign data to it. | -- Declare a local variable and assign data to it. | ||
local str = "Hello World!" | local str = "Hello World!" | ||
-- Quit this function and send the information in "str" back to Wikishia. | -- Quit this function and send the information in "str" back to Wikishia. | ||
-- The "print" function is not allowed, so all output is accomplished via | -- The "print" function is not allowed, so all output is accomplished via | ||
-- returning strings in this fashion. | -- returning strings in this fashion. | ||
return str | return str | ||
-- End the function. | -- End the function. | ||
Line 86: | Line 94: | ||
A unit testing framework for Lua scripts on Wikishia 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. | A unit testing framework for Lua scripts on Wikishia 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. <code><nowiki>{{#invoke: Bananas/testcases | run_tests}}</nowiki></code>. Test methods must begin with "test". A simple example from [[Module:Bananas/testcases]] is below. | 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. <code> | ||
<nowiki>{{#invoke: Bananas/testcases | run_tests}}</nowiki> </code>. Test methods must begin with "test". A simple example from [[Module:Bananas/testcases]] is below. | |||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
-- Unit tests for [[Module:Bananas]]. Click talk page to run tests. | -- Unit tests for [[Module:Bananas]]. Click talk page to run tests. | ||
local p = require('Module:UnitTests') | local p = require('Module:UnitTests') | ||
function p:test_hello() | function p:test_hello() | ||
self:preprocess_equals('{{#invoke:Bananas | hello}}', 'Hello, world!') | self:preprocess_equals('{{#invoke:Bananas | hello}}', 'Hello, world!') | ||
end | end | ||
return p | return p | ||
</syntaxhighlight> | </syntaxhighlight> | ||
For a list of all modules using unit tests, see [[Special:Whatlinkshere/Module:UnitTests]]. | For a list of all modules using unit tests, see [[Special:Whatlinkshere/Module:UnitTests]]. | ||
There's also an alternative unit testing framework [[Special:Whatlinkshere/Module:ScribuntoUnit|in use]] called [[Module:ScribuntoUnit]], that originates from Hungarian Wikishia. Feature-wise it's very similar to [[Module:UnitTests]], but it has a different coding style, and throws real errors that are trapped using protected calls. It also has a method to compare floats within a given precision. | There's also an alternative unit testing framework [[Special:Whatlinkshere/Module:ScribuntoUnit|in use]] called [[Module:ScribuntoUnit]], that originates from Hungarian Wikishia. Feature-wise it's very similar to [[Module:UnitTests]], but it has a different coding style, and throws real errors that are trapped using protected calls. It also has a method to compare floats within a given precision. | ||
== Wikishia-specific features == | == Wikishia-specific features == | ||
Overall: Lua can only get input as text strings passed to the <code><nowiki>{{#invoke:}}</nowiki></code> and what can be fetched via frame:expandTemplate, frame:preprocess, and the like. Lua on Wikishia can only output wikitext not including pre-save transforms or transclusions and other <code><nowiki>{{...}}</nowiki></code> 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]]). | Overall: Lua can only get input as text strings passed to the <code> | ||
<nowiki>{{#invoke:}}</nowiki> </code> and what can be fetched via frame:expandTemplate, frame:preprocess, and the like. Lua on Wikishia can only output wikitext not including pre-save transforms or transclusions and other <code> | |||
<nowiki>{{...}}</nowiki> </code> 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 input limitations === | ||
Line 110: | Line 121: | ||
Transcluded Wikishia 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. | Transcluded Wikishia 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 '''<nowiki>[[Wikishia:Help|]]</nowiki>''' won't work if returned as output - they need to be written explicitly as '''<nowiki>[[Wikishia:Help|Help]]</nowiki>'''. Other pre-save transforms, such as replacing <code>~~<nowiki/>~~</code> with signatures, will also fail to be processed. Template transclusions, parser function calls, and variable substitutions (i.e. anything with a <code><nowiki>{{...}}</nowiki></code>) will not be processed, nor will tags such as {{tag|ref|o}} or {{tag|nowiki|o}}. | Wikilinks of the type ''' | ||
<nowiki>[[Wikishia:Help|]]</nowiki> ''' won't work if returned as output - they need to be written explicitly as ''' | |||
<nowiki>[[Wikishia:Help|Help]]</nowiki> '''. Other pre-save transforms, such as replacing <code>~~<nowiki/>~~</code> with signatures, will also fail to be processed. Template transclusions, parser function calls, and variable substitutions (i.e. anything with a <code> | |||
<nowiki>{{...}}</nowiki> </code>) will not be processed, nor will tags such as {{tag|ref|o}} or {{tag|nowiki|o}}. | |||
== Labeling converted templates == | == Labeling converted templates == | ||
Line 130: | Line 144: | ||
{{WikiProject Lua/header}} | {{WikiProject Lua/header}} | ||