Lua/Tutorial

From MediaWiki.org
< Lua(Redirected from Lua scripting/Tutorial)
Jump to: navigation, search
Video of the Lua introduction talk by Tim Starling, mid-2012

Welcome to the Scribunto-Lua Tutorial, aimed at helping beginners learn to use Lua for scripting in MediaWiki. Read the tutorial slide deck from the 2012 Berlin Hackathon.

How to use this tutorial[edit | edit source]

This tutorial was originally developed for the 2012 Berlin Hackathon, a face-to-face event. It has been adapted for on-wiki, remote use, as you see it here. As all things wiki, it is a living document and we encourage you to participate in developing and maintaining it.

The content hereafter is focused on established MediaWiki template developers. Everyone is welcome to use it. If you're new to templates on MediaWiki, but have Lua experience, you may want to find out more about templates. Note that some things in Scribunto do not apply to the Lua distribution. If you're a MediaWiki template guru, but new to Lua, you will learn some cool, new magic. If you're new to both, you will find that the MediaWiki community has many people willing to help you learn.

Introduction to Lua[edit | edit source]

Brad Jorsch’s short presentation for a basic example of how to convert a wikitext template into a Lua module.
  • Lua scripting general information and brief historical context of its planned use in MediaWiki.
  • What is different from JavaScript and OO languages we could have used?
    • Scribunto: how is the Lua implementation in MediaWiki different? Wikitext is not parsed the way you would normally expect. Parser frames are arguments in Scribunto templates. Strip markers replace certain markup, which, if it is passed, will result in placeholders instead of markup. You will need to use the new Namespace:Module.
  • How you adapt for these differences.

Demonstration[edit | edit source]

Suggested templates you want to see adapted during the demo[edit | edit source]

Please post the link to a template that you'd like to see adapted to Lua as a demo, on-stage during the tutorial.

  1. (First template suggestion)

Hands-on Lua scripting[edit | edit source]

  • Create a page in the Module namespace.
  • Write some Lua code in it. Here is the boilerplate:
local p = {}
function p.hello(frame)
    return 'Hello'
end
return p
  • Go to the module talk page.
  • Put a #invoke in it to invoke your new module:
{{#invoke: my_module | hello }}

Accessing template parameters[edit | edit source]

Functions can access the frame object to get access to the parameters that the template was invoked with. For example, let's change the previous module so that it takes two arguments, like this:

{{#invoke: my_module | hello | hair | brown }}

Our Lua function can use the parameters "hair" and "brown" in either of two ways:

  1. Access them directly as frame.args[1] and frame.args[2], respectively (named arguments can also be used, such as frame.args["title"] or frame.args.title).
  2. Iterate them using the frame:argumentPairs() function, which returns a (name, value) pair for each; this is the best way to code a template that can take a variable number of parameters.

Here's a simple example of a function that uses the parameters:

local p = {}
function p.hello(frame)
    return 'Hello, my ' .. frame.args[1] .. ' is ' .. frame.args[2]
end
return p

A few pitfalls to avoid:

  1. The parameter values are always strings, even if they are numeric in form; you can use the Lua function tonumber() if you need to treat a value as a number.
  2. However, numbered parameter keys are numbers, even if they are given explicitly in the template invocation (for example, the parameters in both {{#invoke:my_module | hello | world}} and {{#invoke:my_module | hello | 1=world}} are indexed by the number 1, not the string "1"
  3. With explicitly named (or numbered) parameters, leading and trailing whitespace is stripped from name and value. This is similar to template parameters.
  4. An empty string value is not the same as a missing value, which is nil; for example,
    {{#invoke:my_module | hello | world }}
    
    results in frame.args[2] being nil (which will cause a script error in the example code hereinbefore), but
    {{#invoke:my_module | hello | world | }}
    
    results in frame.args[2] being an empty string (which will not, although the resulting text will look odd). One way to work around this is to assign the desired parameters to a local variable, and replacing nils with empty strings at this step; e.g.,
    local arg2 = frame.args[2] or ""

Lua implementation plan as part of MediaWiki roadmap[edit | edit source]

External resources for general/non-wiki Lua scripting[edit | edit source]


Tutorial feedback[edit | edit source]

If you saw this tutorial live, in Berlin, please give us your feedback on the Talk page, so we can refine it for future hackathons and other events. You can give anonymous feedback if you prefer.

Tutorial slide deck from Berlin.