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
[edit]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
[edit]- 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 version history
- What is different from JS and OO languages we could have used?
- Scribunto: How is the Lua implementation in MediaWiki different?
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
- How you adapt for these differences
Demonstration
[edit]Suggested Templates You Want to See Adapted During the Demo
[edit]Please post the link to a template that you'd like to see adapted to Lua as a demo, on-stage during the tutorial.
- (first template suggestion)
Hands-on Lua Scripting
[edit]- 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]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:
- Access them directly as
frame.args[1]
andframe.args[2]
, respectively (named arguments can also be used, such asframe.args["title"]
orframe.args.title
) - 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:
- 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. - 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" - With explicitly named (or numbered) parameters, leading and trailing whitespace is stripped from name and value. This is similar to template parameters.
- 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] beingnil
(which will cause a script error in the example code above), 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]External Resources for general/non-wiki Lua Scripting
[edit]
Tutorial Feedback
[edit]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.