Jump to content

Lua/Overview: Difference between revisions

From mediawiki.org
< Lua
Content deleted Content added
m skip Lua redirection page on w:
m add translate sections
Line 18: Line 18:
<translate><!--T:11--> The extension is called <tvar name=1>{{ll|Extension:Scribunto|Scribunto}}</tvar>.</translate>
<translate><!--T:11--> The extension is called <tvar name=1>{{ll|Extension:Scribunto|Scribunto}}</tvar>.</translate>


[[w:Lua|Lua]] is a programming language that is available via the {{ll|Extension:Scribunto|Scribunto}} {{ll|MediaWiki extension}} on the English Wikipedia.
<translate>[[w:Lua|Lua]] is a programming language that is available via the <tvar name=1>{{ll|Extension:Scribunto|Scribunto}}</tvar> <tvar name=2>{{ll|MediaWiki extension}}</tvar> on the English Wikipedia.</translate>
Lua code can now be embedded into wiki templates (this acts as the host client) by employing the "<nowiki>{{#invoke:}}</nowiki>" functionality of Scribunto.
<translate>Lua code can now be embedded into wiki templates (this acts as the host client) by employing the "<tvar name=1><nowiki>{{#invoke:}}</nowiki></tvar>" functionality of Scribunto.</translate>


<translate>
== Differences between Lua and Scribunto==
== Differences between Lua and Scribunto==


[[w:Lua (programming language)|Lua]] is a programming language that is available via the {{ll|Extension:Scribunto|Scribunto}} {{ll|MediaWiki extension}} on the English Wikipedia.
[[w:Lua|Lua]] is a programming language that is available via the <tvar name=1>{{ll|Extension:Scribunto|Scribunto}}</tvar> <tvar name=2>{{ll|MediaWiki extension}}</tvar> on the English Wikipedia.</translate>
The Scribunto extension allows for embedding scripting languages in MediaWiki.
<translate>The Scribunto extension allows for embedding scripting languages in MediaWiki.</translate>
Lua code can now be embedded into wiki templates by employing the "<nowiki>{{#invoke:}}</nowiki>" functionality of Scribunto.
<translate>Lua code can now be embedded into wiki templates by employing the "<tvar name=1><nowiki>{{#invoke:}}</nowiki></tvar>" functionality of Scribunto.</translate>
This extension supports Lua 5.1 {{as of|lc=y|2015|07}}.
<translate>This extension supports Lua 5.1 <tvar name=1>{{as of|lc=y|2015|07}}</tvar>.</translate>


Currently, the only supported scripting language is [[w:Wikipedia:Lua|Lua]].
<translate>Currently, the only supported scripting language is [[w:Wikipedia:Lua|Lua]].</translate>


A Scribunto module in itself is really a large function: it runs from top to bottom and is expected to return a value.
<translate>A Scribunto module in itself is really a large function: it runs from top to bottom and is expected to return a value.</translate>
It could return a table of strings, a table containing other tables, or even a single value.
<translate>It could return a table of strings, a table containing other tables, or even a single value.</translate>
Normally, it returns a table of exported functions that can be used with <code><nowiki>{{#invoke: }}</nowiki></code>
<translate>Normally, it returns a table of exported functions that can be used with <tvar name=1><code><nowiki>{{#invoke: }}</nowiki></code></tvar></translate>


<translate>
== Benefit of Lua scripting ==
== Benefit of Lua scripting ==


Templates and {{ll|Extension: ParserFunctions|ParserFunctions}} were introduced to allow end-users of MediaWiki to replicate content easily and build tools using basic logic, effectively turning wikitext into a limited programming language.
Templates and <tvar name=1>{{ll|Extension: ParserFunctions|ParserFunctions}}</tvar> were introduced to allow end-users of MediaWiki to replicate content easily and build tools using basic logic, effectively turning wikitext into a limited programming language.


However, complex templates have caused performance issues and bottlenecks.
However, complex templates have caused performance issues and bottlenecks.


This project aims to make it possible for MediaWiki end-users to use a proper [[:w: Scripting language|scripting language]] that will be more powerful and efficient than ad-hoc ParserFunctions-based logic.
This project aims to make it possible for MediaWiki end-users to use a proper [[:w:Scripting language|scripting language]] that will be more powerful and efficient than ad-hoc ParserFunctions-based logic.


== Getting started with Lua on Wikipedia ==
== Getting started with Lua on Wikipedia ==


The software used by Wikipedia, called MediaWiki, has an extension that provides a version of Lua that can be used within Wikipedia pages.
The software used by Wikipedia, called MediaWiki, has an extension that provides a version of Lua that can be used within Wikipedia pages.</translate>
The extension is called Scribunto.
<translate>The extension is called Scribunto.</translate>


The Lua [[w:source code | source code]] is stored in pages called modules.
<translate>The Lua [[w:source code|source code]] is stored in pages called modules.</translate>
The module itself must return a Lua table containing the functions that may be called by <code><nowiki>{{#invoke:}}</nowiki></code>.
<translate>The module itself must return a Lua table containing the functions that may be called by <tvar name=1><code><nowiki>{{#invoke:}}</nowiki></code></tvar>.</translate>
Any functions that are not added to this table, whether local or global, will not be accessible by <code><nowiki>{{#invoke:}}</nowiki></code>, but globals might be accessible from other modules loaded using <code>[[#require|require()]]</code>.
<translate>Any functions that are not added to this table, whether local or global, will not be accessible by <tvar name=1><code><nowiki>{{#invoke:}}</nowiki></code></tvar>, but globals might be accessible from other modules loaded using <tvar name=2><code>[[#require|require()]]</code></tvar>.</translate>


It is generally a good style for the module to declare all functions and variables '''local'''.
<translate>It is generally a good style for the module to declare all functions and variables '''local'''.</translate>


These individual modules can be invoked following this format <code><nowiki>{{#invoke:<Module name>|<Function name>|(optional) param1 | param2...}}</nowiki></code>).
<translate>These individual modules can be invoked following this format <code><nowiki>{{#invoke:<Module name>|<Function name>|(optional) param1 | param2...}}</nowiki></code>).</translate>
In the example below, we will be working with Lua on Wikipedia using module sandbox (contain the Lua code to be run) and user sandbox (contain the wiki-text that runs the code and displays the results).
<translate>In the example below, we will be working with Lua on Wikipedia using module sandbox (contain the Lua code to be run) and user sandbox (contain the wiki-text that runs the code and displays the results).</translate>


<translate>
=== Prerequisite ===
=== Prerequisite ===
</translate>
* <translate>Create a Wikipedia account if you don't already have one – see [[Wikipedia: Why create an account?]]</translate>
* <translate>After you have logged in to your account, use the link at the top right of your page that has your username to create your user page.</translate> <translate>In the ''edit box'', write a sentence, but don't add personal information.</translate> <translate>Preview your edit and '''save''' it.</translate>
* <translate>Use the link at the top right of your page to create your sandbox. Write a brief note saying that this is your user sandbox, preview it and '''save''' it.</translate> <translate>Remember that what you type can potentially be seen by anybody, so be sensible!</translate>
* <translate>Read about Scribunto, the Lua implementation embedded in Wikimedia software: [[Wikipedia:Lua]]</translate>
* <translate>Create an empty module sandbox for yourself as a subfolder of [[Module: Sandbox]] For example, if your username is "Gechy", then create your module at [[w:Module: Sandbox/Gechy/ogechi | Module:Sandbox/Gechy/ogechi]].</translate> <translate>Note that any spaces will be converted to underscores <samp>_</samp> in the page URL that you can see in your browser's address bar.</translate> <translate>This is called the URL-encoding.</translate>
* <translate>In your module sandbox add a line of text starting with two hyphens: <tvar name=1><code>--</code></tvar></translate>
* <translate>After the two hyphens, type your username followed by <code> Introduction to Lua in Wikipedia</code>.</translate> <translate>Text beginning with two hyphens is used in Lua to designate a comment.</translate>
* <translate>''' Save''' your module sandbox.</translate>


<translate>
* Create a Wikipedia account if you don't already have one – see [[Wikipedia: Why create an account?]]
* After you have logged in to your account, use the link at the top right of your page that has your username to create your user page. In the ''edit box'', write a sentence, but don't add personal information. Preview your edit and '''save''' it.
* Use the link at the top right of your page to create your sandbox. Write a brief note saying that this is your user sandbox, preview it and '''save''' it. Remember that what you type can potentially be seen by anybody, so be sensible!
* Read about Scribunto, the Lua implementation embedded in Wikimedia software: [[Wikipedia: Lua]]
* Create an empty module sandbox for yourself as a subfolder of [[Module: Sandbox]] For example, if your username is "Gechy", then create your module at [[w:Module: Sandbox/Gechy/ogechi | Module:Sandbox/Gechy/ogechi]]. Note that any spaces will be converted to underscores <samp>_</samp> in the page URL that you can see in your browser's address bar. This is called the URL-encoding.
* In your module sandbox add a line of text starting with two hyphens: <code>--</code>
* After the two hyphens, type your username followed by <code> Introduction to Lua in Wikipedia</code>. Text beginning with two hyphens is used in Lua to designate a comment.
* ''' Save''' your module sandbox.

=== Running Lua code on Wikipedia ===
=== Running Lua code on Wikipedia ===


A Wikipedia page can call a Lua module to do calculations, process text, format citations, fetch information from Wikidata, and many other jobs where a programming language is needed to get a result.
A Wikipedia page can call a Lua module to do calculations, process text, format citations, fetch information from Wikidata, and many other jobs where a programming language is needed to get a result.</translate>
A Lua module is used inside a Wikipedia page by using a call something like
<translate>A Lua module is used inside a Wikipedia page by using a call something like</translate>
<code><nowiki>{{#invoke:RexxS|carousel}}</nowiki></code>
<code><nowiki>{{#invoke:RexxS|carousel}}</nowiki></code>


* In your user sandbox (not your module sandbox), leave a blank line after your brief note, then type this: <code><nowiki>== Task 1 ==</nowiki></code>
* <translate>In your user sandbox (not your module sandbox), leave a blank line after your brief note, then type this: <tvar name=1><code><nowiki>== Task 1 ==</nowiki></code></tvar></translate>


Putting == around a phrase creates a level 2 HTML heading.
<translate>Putting <tvar name=1>==</tvar> around a phrase creates a level 2 HTML heading.</translate>
We can use that to break up our user sandbox into sections for different tasks.
<translate>We can use that to break up our user sandbox into sections for different tasks.</translate>


* On the line below that, type or copy <code><nowiki>{{#invoke:RexxS|carousel}}</nowiki></code> and '''save it'''.
* <translate>On the line below that, type or copy <tvar name=1><code><nowiki>{{#invoke:RexxS|carousel}}</nowiki></code></tvar> and '''save it'''.</translate>


'''Please don't miss out saving your user sandbox each time you are instructed to.'''
<translate>'''Please don't miss out saving your user sandbox each time you are instructed to.'''</translate>


You should see the filename of a JPG image.
<translate>You should see the filename of a JPG image.</translate>
The line makes use of a module called [[w:Module:RexxS|Module:Rexxs]].
<translate>The line makes use of a module called [[w:Module:RexxS|Module:Rexxs]].</translate>
Modules can contain many functions and the line you have entered calls a function in that module called "carousel".
<translate>Modules can contain many functions and the line you have entered calls a function in that module called "carousel".</translate>


* Add another new line in your user sandbox that reads <code><nowiki>[[File:{{#invoke:RexxS|carousel}} | thumb]]</nowiki></code> and '''save it'''.
* <translate>Add another new line in your user sandbox that reads <tvar name=1><code><nowiki>[[File:{{#invoke:RexxS|carousel}} | thumb]]</nowiki></code></tvar> and '''save it'''.</translate>


This uses the standard Wikipedia image syntax to display the image.
<translate>This uses the standard Wikipedia image syntax to display the image.</translate>
You can read a lot more about image syntax at [[Wikipedia: Extended image syntax]].
<translate>You can read a lot more about image syntax at [[Wikipedia:Extended image syntax]].</translate>


* Look at [[w:Module: RexxS | Module:Rexxs]] and examine the code. See if you can work out how the function picks a filename. You may find the documentation at http://www.lua.org/manual/5.3/ helps you to learn how Lua works.
* <translate>Look at [[w:Module:RexxS|Module:Rexxs]] and examine the code.</translate> <translate>See if you can work out how the function picks a filename.</translate> <translate>You may find the documentation at <tvar name=url>http://www.lua.org/manual/5.3/</tvar> helps you to learn how Lua works.</translate>


You should be able to work out that the filenames of the images which can be returned are kept in a list.
<translate>You should be able to work out that the filenames of the images which can be returned are kept in a list.</translate>
In Lua, lists are tables.
<translate>In Lua, lists are tables.</translate>
The table is the only data structure used and all other types of data structures, arrays, sequences, objects, etc. are created from tables.
<translate>The table is the only data structure used and all other types of data structures, arrays, sequences, objects, etc. are created from tables.


In the Lua Module, we can have as many functions as we want and this can be called by simply identifying the function name.
In the Lua Module, we can have as many functions as we want and this can be called by simply identifying the function name.</translate>


{| class="wikitable"
{| class="wikitable"
Line 107: Line 111:
| {{:en:#invoke:Example|hello}}
| {{:en:#invoke:Example|hello}}
|}
|}
In the example above the module used is called [[w:Module: Example | Module:Example]] and a Function name called '''hello''' is invoked see a snippet of the hello function from the [[w:Module: Example | Module:Example]] below:
<translate>In the example above the module used is called [[w:Module:Example|Module:Example]] and a Function name called '''hello''' is invoked see a snippet of the hello function from the [[w:Module:Example|Module:Example]] below:</translate>
<syntaxhighlight lang="Lua">
<syntaxhighlight lang="Lua">
local p = {} --All Lua modules on Wikipedia must begin by defining a variable
local p = {} --All Lua modules on Wikipedia must begin by defining a variable
Line 131: Line 135:
</syntaxhighlight>
</syntaxhighlight>


The [[w:Module: Example | Module:Example]] used above contains up to five different functions('''''hello,hello_to,count_fruit,lucky and Name2''''') that can be called independently using <code><nowiki>{{#invoke:}}</nowiki></code>.
<translate>The [[w:Module:Example|Module:Example]] used above contains up to five different functions('''''hello,hello_to,count_fruit,lucky and Name2''''') that can be called independently using <tvar name=1><code><nowiki>{{#invoke:}}</nowiki></code></tvar>.


== Lua concept ==
== Lua concept ==


In Lua, there are no type definitions in the language.
In Lua, there are no type definitions in the language.</translate>
All values carry their own type.
<translate>All values carry their own type.</translate>
This means that all values can be stored in variables, passed as arguments to other functions, and returned as results.
<translate>This means that all values can be stored in variables, passed as arguments to other functions, and returned as results.</translate>
In Scribunto we make use of six out of the eight types available in the Lua programming language.
<translate>In Scribunto we make use of six out of the eight types available in the Lua programming language.


=== Nil ===
=== Nil ===


The type nil has one single value, nil, whose main property is to be different from any other value.
The type nil has one single value, nil, whose main property is to be different from any other value.</translate>
It often represents the absence of a useful value.
<translate>It often represents the absence of a useful value.


Both nil and false make a condition false, they are collectively called ''false values''.
Both nil and false make a condition false, they are collectively called ''false values''.</translate>
Any other value makes a condition true.
<translate>Any other value makes a condition true.</translate>
If you try getting a variable, that doesn't exist you will get ''nil''.
<translate>If you try getting a variable, that doesn't exist you will get ''nil''.


=== Boolean ===
=== Boolean ===


The type boolean has two values, false and true when converted to a string.
The type boolean has two values, false and true when converted to a string.</translate>
And unlike many other languages, only false and nil are considered false for boolean conversion; the number 0 and the empty string are both considered true.
<translate>And unlike many other languages, only false and nil are considered false for boolean conversion; the number 0 and the empty string are both considered true.


=== Number ===
=== Number ===


The type number represents both integer numbers and real (floating-point) numbers, using two subtypes: integer and float.
The type number represents both integer numbers and real (floating-point) numbers, using two subtypes: integer and float.</translate>
Lua has explicit rules about when each subtype is used, but it also converts between them automatically at run time.
<translate>Lua has explicit rules about when each subtype is used, but it also converts between them automatically at run time.


In a conversion from '''integer''' to '''float''', if the integer value has an exact representation as a float, that is the result.
In a conversion from '''integer''' to '''float''', if the integer value has an exact representation as a float, that is the result.</translate>
Otherwise, the conversion gets the nearest higher or the nearest lower representable value.
<translate>Otherwise, the conversion gets the nearest higher or the nearest lower representable value.</translate>
This kind of conversion never fails.
<translate>This kind of conversion never fails.


The conversion from '''float''' to '''integer''' checks whether the float has an exact representation as an ''integer'' (that is, the float has an integral value and it is in the range of integer representation).
The conversion from '''float''' to '''integer''' checks whether the float has an exact representation as an ''integer'' (that is, the float has an integral value and it is in the range of integer representation).</translate>
If it does, that representation is the result.
<translate>If it does, that representation is the result.</translate>
Otherwise, the conversion fails.
<translate>Otherwise, the conversion fails.


=== String ===
=== String ===


The type string represents immutable sequences of bytes.
The type string represents immutable sequences of bytes.</translate>
Lua is 8-bit clean: strings can contain any 8-bit value, including embedded zeros ('\0').
<translate>Lua is 8-bit clean: strings can contain any 8-bit value, including embedded zeros ('\0').</translate>
A string is converted to an integer or a float following its syntax and the rules of the Lua lexer.
<translate>A string is converted to an integer or a float following its syntax and the rules of the Lua lexer.</translate>
The string may have also leading and trailing whitespaces and a sign.
<translate>The string may have also leading and trailing whitespaces and a sign.


=== Function ===
=== Function ===


Functions in Lua are first-class values.
Functions in Lua are first-class values.</translate>
They may be created anonymously, passed as arguments, assigned to variables.
<translate>They may be created anonymously, passed as arguments, assigned to variables.


Functions are created using the <code>function</code> keyword, and called using parentheses.
Functions are created using the <code>function</code> keyword, and called using parentheses.</translate>
[[w: syntactic sugar|Syntactic sugar]] is available for named functions, local functions, and functions that act like member functions to a table.
<translate>[[w:syntactic sugar|Syntactic sugar]] is available for named functions, local functions, and functions that act like member functions to a table.


=== Table ===
=== Table ===


The Lua table implements associative arrays, that is, arrays that can be indexed not only with numbers but with any value (except nil).
The Lua table implements associative arrays, that is, arrays that can be indexed not only with numbers but with any value (except nil).</translate>
Tables can contain values of all types (except nil).
<translate>Tables can contain values of all types (except nil).</translate>
To represent records, Lua uses the field name as an index.
<translate>To represent records, Lua uses the field name as an index.</translate>
The Lua table type supports this representation by providing a.name as syntactic sugar for <code>a[name]</code>.
<translate>The Lua table type supports this representation by providing a.name as syntactic sugar for <tvar name=1><code>a[name]</code></tvar>.


Tables are created using curly braces.
Tables are created using curly braces.</translate>
The empty table is {}.
<translate>The empty table is <tvar name=1>{}</tvar>.</translate>
They store a set of key/value pairs.
<translate>They store a set of key/value pairs.</translate>
In a Key/Value pair you can store a value under a key and then later retrieve the value using that key.
<translate>In a Key/Value pair you can store a value under a key and then later retrieve the value using that key.


== Data type conversion ==
== Data type conversion ==


Lua will automatically convert string and number types to the correct format to perform calculations.
Lua will automatically convert string and number types to the correct format to perform calculations.</translate>
This automatic conversion of types is called '''''coercion.'''''.
<translate>This automatic conversion of types is called '''''coercion.'''''.</translate>
Performing arithmetic in Lua to a string it first converts the string to a number else the operation will return an exception when string can't be converted.
<translate>Performing arithmetic in Lua to a string it first converts the string to a number else the operation will return an exception when string can't be converted.</translate>
See example below:
<translate>See example below:</translate>


<syntaxhighlight lang="Lua">
<syntaxhighlight lang="Lua">
Line 212: Line 216:
</syntaxhighlight>
</syntaxhighlight>


The string "John" cannot be converted to a number and so an exception occurs.
<translate>The string "John" cannot be converted to a number and so an exception occurs.</translate>
Similarly, in comparison operators, data conversion plays a major role as shown in the example below:
<translate>Similarly, in comparison operators, data conversion plays a major role as shown in the example below:</translate>


<syntaxhighlight lang="Lua">
<syntaxhighlight lang="Lua">
Line 233: Line 237:
</syntaxhighlight>
</syntaxhighlight>


In Lua (in)equality operators consider a number to be not equal to its string representation (or any non-number type in fact) as shown in the example above.
<translate>In Lua (in)equality operators consider a number to be not equal to its string representation (or any non-number type in fact) as shown in the example above.</translate>
It's advised to not rely on automatic coercion, explicitly define a type for performance-sensitive computation.
<translate>It's advised to not rely on automatic coercion, explicitly define a type for performance-sensitive computation.</translate>


<translate>
== Basic operation in lua ==
== Basic operation in lua ==


=== The print function ===
=== The print function ===


We'll use the print() function to print out values or calculations on those values.
We'll use the print() function to print out values or calculations on those values.</translate>
The parentheses around the arguments are important and will cause an error if omitted.
<translate>The parentheses around the arguments are important and will cause an error if omitted.</translate>


<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
Line 251: Line 256:
</syntaxhighlight>
</syntaxhighlight>


<translate>
=== Assignment ===
=== Assignment ===


Lua allows for multiple assignments.
Lua allows for multiple assignments.</translate>
We can assign strings to variables just like we can numbers:
<translate>We can assign strings to variables just like we can numbers:</translate>


<syntaxhighlight lang="Lua">
<syntaxhighlight lang="Lua">
Line 265: Line 271:
</syntaxhighlight>
</syntaxhighlight>


<translate>
=== Concatenate ===
=== Concatenate ===


The string concatenation operator in Lua is denoted by two dots ('..').
The string concatenation operator in Lua is denoted by two dots (<tvar name=1>'..'</tvar>).</translate>
We can concatenate (join together) strings together using the .. operator:
<translate>We can concatenate (join together) strings together using the <tvar name=1>..</tvar> operator:</translate>


<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
Line 277: Line 284:
</syntaxhighlight>
</syntaxhighlight>


<translate>
=== Comment ===
=== Comment ===
</translate>


A comment starts with a double hyphen (--) anywhere outside a string.
<translate>A comment starts with a double hyphen (--) anywhere outside a string.</translate>
Long comments are frequently used to disable code temporarily.
<translate>Long comments are frequently used to disable code temporarily.</translate>


Other operations such as String manipulation, tables, numbers will be discussed intensively in [[:mw: Lua/Tutorial | Lua tutorials]]
<translate>Other operations such as String manipulation, tables, numbers will be discussed intensively in [[:mw:Lua/Tutorial|Lua tutorials]]</translate>


<translate>
== References ==
== References ==
</translate>


* [https://www.lua.org/manual/5.4/manual.html Lua Official Documentation]
* <translate>[<tvar name=url>https://www.lua.org/manual/5.4/manual.html</tvar> Lua Official Documentation]</translate>
* [https://www.lua.org/manual/5.4/manual.html#3.4.3 Lua Manual]
* <translate>[<tvar name=url>https://www.lua.org/manual/5.4/manual.html#3.4.3</tvar> Lua Manual]</translate>
* {{ll|Extension:Scribunto/Lua reference manual}}
* {{ll|Extension:Scribunto/Lua reference manual}}
* [[w:User:RexxS/GCI|RexxS Docs]]
* <translate>[[w:User:RexxS/GCI|RexxS Docs]]</translate>
* [http://lua-users.org/ lua-user Docs]
* <translate>[<tvar name=1>http://lua-users.org/</tvar> lua-user Docs]</translate>
* The [https://exercism.org/tracks/lua Lua Track on Exercism]
* <translate>The [<tvar name=url>https://exercism.org/tracks/lua</tvar> Lua Track on Exercism]</translate>


[[Category:Lua{{#translation:}}]]
[[Category:Lua{{#translation:}}]]

Revision as of 11:41, 7 May 2025

About lua

Lua is a scripting language which can be used to analyse data and calculate expressions. It formats results which support procedural programming, object-oriented programming, functional programming, data-driven programming, and data description. 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. It is dynamically typed (This means that variables do not have types; only values do), runs by interpreting bytecode with a register-based virtual machine, and has automatic memory management with a generational garbage collection, making it ideal for configuration, scripting, and rapid prototyping.

Lua is implemented as a library, written in clean C, the common subset of Standard C and C++. The Lua distribution includes a host program called Lua, which uses the Lua library to offer a complete, standalone Lua interpreter, for interactive or batch use.

Lua is a lightweight programming language that is particularly suited for embedding within other software. The software used by Wikipedia, called MediaWiki, has an extension that provides a version of Lua that can be used within Wikipedia pages. The extension is called Scribunto .

Lua is a programming language that is available via the Scribunto MediaWiki extension on the English Wikipedia. Lua code can now be embedded into wiki templates (this acts as the host client) by employing the "{{#invoke:}}" functionality of Scribunto.

Differences between Lua and Scribunto

Lua is a programming language that is available via the Scribunto MediaWiki extension on the English Wikipedia. The Scribunto extension allows for embedding scripting languages in MediaWiki. Lua code can now be embedded into wiki templates by employing the "{{#invoke:}}" functionality of Scribunto. This extension supports Lua 5.1 as of July 2015.

Currently, the only supported scripting language is Lua.

A Scribunto module in itself is really a large function: it runs from top to bottom and is expected to return a value. It could return a table of strings, a table containing other tables, or even a single value. Normally, it returns a table of exported functions that can be used with {{#invoke: }}

Benefit of Lua scripting

Templates and ParserFunctions were introduced to allow end-users of MediaWiki to replicate content easily and build tools using basic logic, effectively turning wikitext into a limited programming language.

However, complex templates have caused performance issues and bottlenecks.

This project aims to make it possible for MediaWiki end-users to use a proper scripting language that will be more powerful and efficient than ad-hoc ParserFunctions-based logic.

Getting started with Lua on Wikipedia

The software used by Wikipedia, called MediaWiki, has an extension that provides a version of Lua that can be used within Wikipedia pages. The extension is called Scribunto.

The Lua source code is stored in pages called modules. The module itself must return a Lua table containing the functions that may be called by {{#invoke:}}. Any functions that are not added to this table, whether local or global, will not be accessible by {{#invoke:}}, but globals might be accessible from other modules loaded using require().

It is generally a good style for the module to declare all functions and variables local.

These individual modules can be invoked following this format {{#invoke:<Module name>|<Function name>|(optional) param1 | param2...}}). In the example below, we will be working with Lua on Wikipedia using module sandbox (contain the Lua code to be run) and user sandbox (contain the wiki-text that runs the code and displays the results).

Prerequisite

  • Create a Wikipedia account if you don't already have one – see Wikipedia: Why create an account?
  • After you have logged in to your account, use the link at the top right of your page that has your username to create your user page. In the edit box, write a sentence, but don't add personal information. Preview your edit and save it.
  • Use the link at the top right of your page to create your sandbox. Write a brief note saying that this is your user sandbox, preview it and save it. Remember that what you type can potentially be seen by anybody, so be sensible!
  • Read about Scribunto, the Lua implementation embedded in Wikimedia software: Wikipedia:Lua
  • Create an empty module sandbox for yourself as a subfolder of Module: Sandbox For example, if your username is "Gechy", then create your module at Module:Sandbox/Gechy/ogechi. Note that any spaces will be converted to underscores _ in the page URL that you can see in your browser's address bar. This is called the URL-encoding.
  • In your module sandbox add a line of text starting with two hyphens: --
  • After the two hyphens, type your username followed by Introduction to Lua in Wikipedia. Text beginning with two hyphens is used in Lua to designate a comment.
  • Save your module sandbox.

Running Lua code on Wikipedia

A Wikipedia page can call a Lua module to do calculations, process text, format citations, fetch information from Wikidata, and many other jobs where a programming language is needed to get a result. A Lua module is used inside a Wikipedia page by using a call something like {{#invoke:RexxS|carousel}}

  • In your user sandbox (not your module sandbox), leave a blank line after your brief note, then type this: == Task 1 ==

Putting == around a phrase creates a level 2 HTML heading. We can use that to break up our user sandbox into sections for different tasks.

  • On the line below that, type or copy {{#invoke:RexxS|carousel}} and save it.

Please don't miss out saving your user sandbox each time you are instructed to.

You should see the filename of a JPG image. The line makes use of a module called Module:Rexxs. Modules can contain many functions and the line you have entered calls a function in that module called "carousel".

  • Add another new line in your user sandbox that reads [[File:{{#invoke:RexxS|carousel}} | thumb]] and save it.

This uses the standard Wikipedia image syntax to display the image. You can read a lot more about image syntax at Wikipedia:Extended image syntax.

You should be able to work out that the filenames of the images which can be returned are kept in a list. In Lua, lists are tables. The table is the only data structure used and all other types of data structures, arrays, sequences, objects, etc. are created from tables.

In the Lua Module, we can have as many functions as we want and this can be called by simply identifying the function name.

Wikitext Result
{{#invoke:Example|hello}} hello}}

In the example above the module used is called Module:Example and a Function name called hello is invoked see a snippet of the hello function from the Module:Example below:

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.
p.hello = function( frame )     --Add a function to "p".
                                        --Such functions are callable in Wikipedia
                                        --via the #invoke the command.
                                        --"frame" will contain the data that Wikipedia
                                        --sends this function when it runs.
                                 -- 'Hello' is the 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"

return p  -- must return p so the module is accessible

The Module:Example used above contains up to five different functions(hello,hello_to,count_fruit,lucky and Name2) that can be called independently using {{#invoke:}}.

Lua concept

In Lua, there are no type definitions in the language. All values carry their own type. This means that all values can be stored in variables, passed as arguments to other functions, and returned as results. In Scribunto we make use of six out of the eight types available in the Lua programming language.

Nil

The type nil has one single value, nil, whose main property is to be different from any other value. It often represents the absence of a useful value.

Both nil and false make a condition false, they are collectively called false values. Any other value makes a condition true. If you try getting a variable, that doesn't exist you will get nil.

Boolean

The type boolean has two values, false and true when converted to a string. And unlike many other languages, only false and nil are considered false for boolean conversion; the number 0 and the empty string are both considered true.

Number

The type number represents both integer numbers and real (floating-point) numbers, using two subtypes: integer and float. Lua has explicit rules about when each subtype is used, but it also converts between them automatically at run time.

In a conversion from integer to float, if the integer value has an exact representation as a float, that is the result. Otherwise, the conversion gets the nearest higher or the nearest lower representable value. This kind of conversion never fails.

The conversion from float to integer checks whether the float has an exact representation as an integer (that is, the float has an integral value and it is in the range of integer representation). If it does, that representation is the result. Otherwise, the conversion fails.

String

The type string represents immutable sequences of bytes. Lua is 8-bit clean: strings can contain any 8-bit value, including embedded zeros ('\0'). A string is converted to an integer or a float following its syntax and the rules of the Lua lexer. The string may have also leading and trailing whitespaces and a sign.

Function

Functions in Lua are first-class values. They may be created anonymously, passed as arguments, assigned to variables.

Functions are created using the function keyword, and called using parentheses. Syntactic sugar is available for named functions, local functions, and functions that act like member functions to a table.

Table

The Lua table implements associative arrays, that is, arrays that can be indexed not only with numbers but with any value (except nil). Tables can contain values of all types (except nil). To represent records, Lua uses the field name as an index. The Lua table type supports this representation by providing a.name as syntactic sugar for a[name].

Tables are created using curly braces. The empty table is {}. They store a set of key/value pairs. In a Key/Value pair you can store a value under a key and then later retrieve the value using that key.

Data type conversion

Lua will automatically convert string and number types to the correct format to perform calculations. This automatic conversion of types is called coercion.. Performing arithmetic in Lua to a string it first converts the string to a number else the operation will return an exception when string can't be converted. See example below:

= 10 + "7"   -- Addition of a number and a string
result = 17   --returns a number

= "John" + 43
stdin:1: attempt to perform arithmetic on a string value
stack traceback:
        stdin:1: in main chunk
        [C]: ?

The string "John" cannot be converted to a number and so an exception occurs. Similarly, in comparison operators, data conversion plays a major role as shown in the example below:

 = 100 == "100"  --comparing a number and a string
result =  False  -- they are not equal

 = 100 ~= "hello"  -- a number is not equal to a string
result = true   --- they are not equal

 = 100 == tonumber("100")  --tonumber converts the string to a number
result = true         -- they are equal after the conversion

 = 100 <= "100"
result = stdin:1: attempt to compare number with string
stack traceback:
        stdin:1: in main chunk
        [C]: ?

In Lua (in)equality operators consider a number to be not equal to its string representation (or any non-number type in fact) as shown in the example above. It's advised to not rely on automatic coercion, explicitly define a type for performance-sensitive computation.

Basic operation in lua

The print function

We'll use the print() function to print out values or calculations on those values. The parentheses around the arguments are important and will cause an error if omitted.

print("hello")    -- print the string hello.

hello            -- the result

Assignment

Lua allows for multiple assignments. We can assign strings to variables just like we can numbers:

who = "John"       -- assign who to "John"

print(who)

John               -- Result of the print function

Concatenate

The string concatenation operator in Lua is denoted by two dots ('..'). We can concatenate (join together) strings together using the .. operator:

print("hello " .. who)       -- the variable "who" was assigned above

hello John        --Result

Comment

A comment starts with a double hyphen (--) anywhere outside a string. Long comments are frequently used to disable code temporarily.

Other operations such as String manipulation, tables, numbers will be discussed intensively in Lua tutorials

References