Lua/Tutorial/fr

Bienvenu sur le tutoriel Scribunto-Lua destiné à aider les débutants à apprendre Lua pour écrire des script pour MediaWiki. Vous pouvez aussi lire le tutoriel présenté au Hackathon 2012 à Berlin (en anglais).

Comment utiliser ce tutoriel
Ce tutoriel a été développé à l’origine pour le Hackathon 2012 à Berlin, comme événement en face-à-face. Il a été adapté pour le wiki, à distance, tel que vous le voyez ici. Comme tout être wiki, c’est un document vivant et nous vous encourageons à participer en anglais ou en français pour le développer et le maintenir.

Le contenu ci-dessous se focalise sur l’apprentissage pour des développeurs de modèles MediaWiki. Tout le monde est invité à l’utiliser. Si vous êtes nouveau dans l’écriture de modèles MediaWiki mais que vous avez de l’expérience en Lua, vous pouvez vous renseigner plus sur les modèles. Notez que quelques détails dans Scribunto ne s’appliquent pas à la distribution Lua. Si vous êtes un gourou des modèles MediaWiki, mais nouveau en Lua, vous apprendrez une nouvelle magie sympa. Si vous êtes nouveau dans les deux, vous verrez que la communauté MediaWiki a plusieurs personnes qui peuvent vous aider à apprendre.

Introduction à Lua
WikiText does not parse 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
 * Lua scripting general information and brief historical context of its planned use in MediaWiki.
 * Status of Lua scripting on MediaWiki
 * What is Lua? Check out the Lua.org demo if you don't want to download Scribunto just yet.
 * Lua evolution PDF and |Lua version history
 * What is different from JS and OO languages we could have used?
 * Scribunto: How is the Lua implementation in MediaWiki different?


 * How you adapt for these differences

Suggested Templates You Want to See Adapted During the Demo
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

 * Create a page in the Module namespace
 * Write some Lua code in it. Here is the boilerplate:


 * Go to the module talk page
 * Put a #invoke in it to invoke your new module

Accessing template parameters
Functions can access the  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:

Our Lua function can use the parameters "hair" and "brown" in either of two ways:
 * 1) Access them directly as   and , respectively (named arguments can also be used, such as   or  )
 * 2) Iterate them using the   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:

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   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   and   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  ; for example,    results in frame.args[2] being   (which will cause a script error in the example code above), but    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.,

Lua Implementation Plan as Part of MediaWiki Roadmap

 * Return value expansion, access to the frame object and bug fixing

External Resources for general/non-wiki Lua Scripting

 * Lua 5.1 Reference Manual
 * Programming in Lua

Tutorial Feedback
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