Wikipedia:Lua
| 40x40px|link=|alt= | Artikel ieu keur dikeureuyeuh, ditarjamahkeun tina basa Inggris. Bantuanna didagoan pikeun narjamahkeun. |
| WP:Lua Project | WT:Lua Project talk | Modules | Help | To do | Requests | Resources en: m: mw: external |
- Project subpages
- m:Lua – "central" page at Meta-Wiki
- mw:Lua – index at www.mediawiki.org
| This is an information page that describes communal consensus on some aspect of Wikipedia norms and practices. It is intended to supplement or clarify some other guidance or process. This is not a Wikipedia policy or guideline; please defer to such in a case of inconsistency with this page. |
| Ngaranspasi Wikipedia | |||
|---|---|---|---|
| Subyék ngaranspasi | Obrolan ngaranspasi | ||
| 0 | (Utama/Artikel) | Obrolan | 1 |
| 2 | Pamaké | Obrolan pamaké | 3 |
| 4 | Wikipedia | Obrolan Wikipedia | 5 |
| 6 | Gambar | Obrolan gambar | 7 |
| 8 | MédiaWiki | Obrolan MédiaWiki | 9 |
| 10 | Citakan | Obrolan citakan | 11 |
| 12 | Pitulung | Obrolan pitulung | 13 |
| 14 | Kategori | Obrolan kategori | 15 |
| 100 | Portal | Obrolan portal | 101 |
| 108 | [[Wikipedia:Books|]] | [[Pitulung:Maké kaca obrolan|]] | 109 |
| 118 | [[Wikipedia:Drafts|]] | [[Pitulung:Maké kaca obrolan|]] | 119 |
| 446 | [[Wikipedia:Kaca kursus|]] | [[Pitulung:Maké kaca obrolan|]] | 447 |
| 710 | TimedText | TimedText talk | 711 |
| 828 | Modul | Pembicaraan Modul | 829 |
| 2600 | [[Wikipedia:Flow|]] | ||
| Ngaranspasi virtual | |||
| -1 | Husus | ||
| -2 | Média | ||
Lua mangrupakeun basa pamrograman nu ayeuna geus sadia di Wikipedia Sunda ngaliwatan ekstensi MediaWiki Scribunto.
Kode Lua ayeuna bisa diasupkeun kana témplat wiki ku maké fungsi parser {{#invoke:}} dina kaca pamaké anjeun.
Halo, dunya! dina Lua
[édit sumber]The Lua source code is stored in a wiki page with the prefix Modul: (e.g., Modul:Bananas). These individual modules are then invoked (by code {{#invoke:}}) on another wiki page (e.g., Modul:Bananas/doc uses the code {{#invoke:Bananas|hello}} to print the text "Hello, world!").
Modules are run on normal wiki pages using the #invoke parser function. The syntax of #invoke is similar to template syntax, but with some differences. The most important difference is that you need to specify a function name.
A function is a set of Lua instructions that takes input values, processes them, and returns an output value.[1]
This is much like what a template does: you give it arguments, it processes them, and you get a result.
However, you can define many functions in one Lua module, whereas you can only define one template on one page.
Furthermore, you can't just run a Lua module directly - you can only run one of the module's functions. The module is just a container for the functions, and doesn't do anything by itself. So there are two reasons that we need to input a function name:
- We can't run a module by itself.
- Without specifying a function name, Lua will not know which function it is we want to run.
Run a module from your user page like this:
{{#invoke:NgaranModul|ngaranFungsi}}
For example, Modul:Bananas is the simplest possible script, providing a single function with no arguments.
The function is named hello in lowercase. We can run this module in this way.
Anjeun teu kudu masang atawa nyimpen nanaon.
- Édit kaca pamaké anjeun.
- Témpélkeun panggilan di handap ieu dina awal hiji baris anyar dina kaca pamaké anjeun:
{{#invoke:Bananas|hello}}
- Klik
Témbongkeun sawangan. Panggilan fungsihellodina Modul:Bananas diganti ku ieu di handap: Halo, dunya!
Hello, world!
- Klik
Bolay. Lamun dipenta, klikTinggalkeun. - Kaca pamaké anjeun tetep teu dirobah.
Using arguments
[édit sumber]Arguments are passed to modules in the same way that they are passed to templates. Note, however, that the text after the first pipe character | is always the function name; the first positional argument is the text after the second pipe.
{{#invoke:NgaranModul|ngaranFungsi|arguménPosisional1}}
For example, another Modul:BananasArgs demonstrates how to access and use parameters.
The hello function greets different people depending on the first positional argument. It works like this:
Dina kaca pamaké anjeun:
# {{#invoke:BananasArgs|hello|Mariam}} # {{#invoke:BananasArgs|hello|Mohamed}}
- Hello, Mariam!
- Hello, Mohamed!
- Named arguments key=value
{{#invoke:NgaranModul|ngaranFungsi|argNgaran1=nilai1|argNgaran2=nilai2}}
The Modul:BananasArgs also has a function named count_fruit which uses the named arguments bananas and apples to count the number of bananas and apples we have. It can be run switching the order of the parameters:
Dina kaca pamaké anjeun:
# {{#invoke:BananasArgs|count_fruit|apples=3|bananas=4}} # {{#invoke:BananasArgs|count_fruit|bananas=5|apples=2}}
- I have 4 bananas and 3 apples
- I have 5 bananas and 2 apples
Most modules have a documentation page explaining what arguments can be used and what their effects will be.
Konsol debug: Mimiti diajar Lua
[édit sumber]Debug Console dina Lua Scribunto nyaéta alat anu aman tur interaktif pikeun diajar Lua dina Wikipedia.
- Klik
Témbongkeun sumberdina kaca Modul:Yesno. - Gulung ka handap kaca nepi ka bagian
Debug console. - Témpélkeun komentar jeung panggilan kana kotak asup kulawu (di luhur tombol
Kosongkan).
--[[Program Lua munggaran anjeun nu mirip Hello World]]-- print("Wilujeng sumping di " .. _VERSION .. "!") -- Ngarangkep vérsi Lua ku `..`
- Pencét ↵ Enter sakali pikeun ngalaksanakeun dua paréntah Lua éta.
- Hasilna bakal muncul di handap garis paréntah Lua
print:
Wilujeng sumping di Lua 5.1!
- Pencét ↑
Panah ka luhurpikeun ngurilingan paréntah Lua saméméhna. - Opsional, klik
Kosongkanpikeun ngaberesihan nganDebug Console; éditor Lua di luhur moal teu robah.
- Generator HTML dina Debug console
Panggil pungsi hello dina Modul:Bananas:
local modCau = require('Modul:Bananas') -- muat modul dina Debug console print(modCau.hello()) -- tampilkeun "Hello, world!"
Hello, world!
- Argumén
local modCau = require('Modul:BananasArgs') local frame = mw.getCurrentFrame() frame.args = {"Mariam"}; print(modCau.hello(frame)) -- "Hello, Mariam!" frame.args = {"Mohamed"}; print(modCau.hello(frame)) frame.args = {apples="3", bananas="4"}; print(modCau.count_fruit(frame)) frame.args = {bananas="5", apples="2"}; print(modCau.count_fruit(frame))
Hello, Mariam! Hello, Mohamed! I have 4 bananas and 3 apples I have 5 bananas and 2 apples
- Fungsi idéntitas
callParserFunction #if
local yesno = require('Modul:Yesno') -- #if yesno teras mulangkeun parameter asupanna local frame = mw.getCurrentFrame() print(frame:callParserFunction("#if", yesno("Yes"), "Yes", 'henteu')) print(frame:callParserFunction("#if", yesno('teu'), "Leres", 'teu'))
Yes teu
- Tutup
Modul:Yesno anu dijaga anu tetep teu robah.
Request a script
[édit sumber]Visit en:Wikipedia talk:Lua to request help in writing a Lua script to perform a specific task on Wikipedia or another Wikimedia Foundation project.
History
[édit sumber]Sordid history. ParserFunctions, Lua extension, wiki scripting language debated (JavaScript v. Lua), mw:Extension:WikiScripts, Tim writes Scribunto with initial support for Lua.
Discussed for years, Lua was installed in 2012 for testing on test2.wikipedia.org, with open invitation to all editors to experiment with developing Lua modules. Lua was installed on the English Wikipedia in February 2013, after testing on mediawiki.org and Wikimedia test wikis.
About Lua
[édit sumber]Lua is a scripting language which can be used to analyze data, calculate expressions, and format results using functions or object-oriented programming. 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.
Lua also supports recursion of re-nested functions, so care should be taken to avoid excessive complexity where other users would not understand how to maintain a Lua module.
The following is an example of Lua source code for the hello function contained in Modul:Bananas:
--[[Modul:Bananas]]--
local p = {} -- tabel modul nu diperlukeun, diinisialisasi minangka tabel kosong
function p.hello() -- Nangtukeun fungsi hello.
return "Hello, world!" -- Mulangkeun hasil fungsi dina wangun string.
end -- Ahir fungsi.
return p -- Mulangkeun tabel modul.
A sample of Lua is highlighted by tag <syntaxhighlight lang="lua" line highlight=1 copy> ... </syntaxhighlight> placed around the Lua source code.
For instructions on how to use Lua within MediaWiki (and hence Wikipedia), see mw:Extension:Scribunto/Lua reference manual.
Unit testing
[édit sumber]A unit testing framework for Lua scripts on Wikipedia is available at Modul:ScribuntoUnit.
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 Modul:Bananas are placed in Modul: Bananas/testcases.
They are executed on Pembicaraan Modul:Bananas/testcases with {{#invoke:Bananas/testcases|run}}.
Dina kaca pamaké anjeun:
{{#invoke:Bananas/testcases|run}}
Sakabéh tés lulus.
Ngaran Diarepkeun Saleresna test_halo
Each test method must begin with p:test_.
An example from Modul:Bananas/testcases is below.
--[[Modul:Bananas/testcases]]--
local p = require('Modul:ScribuntoUnit').new()
local modCau = require('Modul:Bananas') -- Didatangan kaca obrolan pikeun ngajalankeun tés.
function p:test_halo() -- Tés unit pikeun [[Modul:Bananas]].
self:assertEquals('Hello, world!', modCau.hello())
end
return p
| Prinsip uji hijian |
|---|
|
Di konsol debug modul:Yesno:
Lamun tésna hasil, nilai anu diarepkeun jeung nilai sabenerna teu dipintonkeun supaya tabel hasil jadi leuwih basajan:
Ngajalankeun Modul:ScribuntoUnit langsung dina Modul:Bananas tanpa nyimpen Modul:Bananas/testcases:
HTML anu dihasilkeun sarua. |
Modul:ScribuntoUnit originates from Hungarian Wikipedia.
It throws real errors that are trapped using protected calls. It also has a method to compare floats within a given precision.
Pikeun daptar sakabéh modul nu ngagunakeun tés unit, tingali nu dipaké.
Wikipedia-specific features
[édit sumber]Overall: Lua can only get input as text strings passed to the {{#invoke:}} and what can be fetched via mw.title.new(...):getContent() and frame:expandTemplate(). Lua output will not be preprocessed unless frame:preprocess() is explicitly called, meaning that template calls and parser functions in the output will not work correctly.
Langkung hade ngagunakeun fungsi mw.ustring.len tibatan string.len:
Di konsol debug modul:Yesno:
--[[Alfabét Latin]]-- local content_lang = mw.getContentLanguage().code -- su local lang = mw.getCurrentFrame():callParserFunction("#language", content_lang) local lenUnicode, lenByte, lenSharp = mw.ustring.len(lang), string.len(lang), #lang print(("lang: \"%s\". Mw.ustring.len = %d String.len = %d Sharp = %d"):format(lang, lenUnicode, lenByte, lenSharp)) --[[Unicode]]-- lang = "اَكسارا ڤَيڮَون" -- Aksara Pégon lenUnicode, lenByte, lenSharp = mw.ustring.len(lang), string.len(lang), #lang print(("lang: \"%s\". Mw.ustring.len = %d String.len = %d Sharp = %d"):format(lang, lenUnicode, lenByte, lenSharp))
Konsol debug ngarojong Unicode, tapi henteu ngarojong téks katuhu-ka-kenca:
lang: "Sunda". Mw.ustring.len = 5 String.len = 5 Sharp = 5 lang: "اَكسارا ڤَيڮَون". Mw.ustring.len = 15 String.len = 29 Sharp = 29
| Alfabét Latin | Aksara | Arah | Jumlah karakter | Panjang dina bait |
|---|---|---|---|---|
| Sunda | Basa Sunda. | → kenca ka katuhu. | 5 | 5 |
| Hurup Pégon | اَكسارا ڤَيڮَون. | ← Katuhu ka kenca | 15 (Unicode) + 1 titik | 29 + 1 |
| number of octet:s : 30 |
| index 0 beg code $D8=#216 length 2 extra $A7 codepoint U+$0627 dec #1'575 ا |
| index 2 beg code $D9=#217 length 2 extra $8E codepoint U+$064E dec #1'614 َ |
| index 4 beg code $D9=#217 length 2 extra $83 codepoint U+$0643 dec #1'603 ك |
| index 6 beg code $D8=#216 length 2 extra $B3 codepoint U+$0633 dec #1'587 س |
| index 8 beg code $D8=#216 length 2 extra $A7 codepoint U+$0627 dec #1'575 ا |
| index 10 beg code $D8=#216 length 2 extra $B1 codepoint U+$0631 dec #1'585 ر |
| index 12 beg code $D8=#216 length 2 extra $A7 codepoint U+$0627 dec #1'575 ا |
| index 14 beg code $20=#32 length 1 SPACE |
| index 15 beg code $DA=#218 length 2 extra $A4 codepoint U+$06A4 dec #1'700 ڤ |
| index 17 beg code $D9=#217 length 2 extra $8E codepoint U+$064E dec #1'614 َ |
| index 19 beg code $D9=#217 length 2 extra $8A codepoint U+$064A dec #1'610 ي |
| index 21 beg code $DA=#218 length 2 extra $AE codepoint U+$06AE dec #1'710 ڮ |
| index 23 beg code $D9=#217 length 2 extra $8E codepoint U+$064E dec #1'614 َ |
| index 25 beg code $D9=#217 length 2 extra $88 codepoint U+$0648 dec #1'608 و |
| index 27 beg code $D9=#217 length 2 extra $86 codepoint U+$0646 dec #1'606 ن |
| index 29 beg code $2E=#46 length 1 . |
| number of UTF8 char:s : 16 |
Lua code in a wiki 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).
Relative to standard Lua, Scribunto offers safe functions (see the differences from standard Lua).
Lua input limitations
[édit sumber]Lua code in Scribunto is only run when the page is being parsed. Therefore, the only user input that Lua can receive is by page editing - it cannot create an input box that allows you to type a Fahrenheit temperature and get back the corresponding Celsius temperature when you press a button, or recalculate a piece of the Mandelbrot set depending on which part of the parent set you click on.
Dina konsol debug modul:Yesno:
local function toCelsius(tempFahrenheit) return (tempFahrenheit - 32) * 5 / 9 end print(("toCelsius(32°F) = %s°C. toCelsius(68°F) = %d°C"):format(toCelsius(32), toCelsius(68)))
toCelsius(32°F) = 0°C. toCelsius(68°F) = 20°C
The Lua input can be any Wikipedia text page that can be transcluded (included on another page). This does not include graphics files (not even .SVG files, although they are actually text, unless you cut and paste it onto a Wiki text page), the list of pages listed in a category, nor the contents of non-transcludeable special pages.
Wikitext
[édit sumber]Transcluded Wikipedia 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.
print(string.reverse("UNIQ")) -- Urutan sabalikna tina "UNIQ" nyaéta "QINU"
QINU
Wikilinks of the type [[Wikipedia:Pitulung|]] won't work if returned as output - they need to be written explicitly as [[Wikipedia:Pitulung|Pitulung]]. Other pre-save transforms, such as replacing ~~~~ with signatures, will also fail to be processed. Template transclusions, parser function calls, and variable substitutions (i.e. anything with a {{...}}) will not be processed, nor will tags such as <ref> or <nowiki>.
Debugging
[édit sumber]- Galat nu katingali dina kaca
Kaca wiki Jatisura, Cikedung, Indramayu ngahasilkeun dua galat:
| Modul:Navbar Judul teu valid | |||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Pikeun misahkeun galat Lua, uji Citakan:Cikedung, Indramayu: Daptar 17 kaca nu kapangaruhan ku ieu masalah: Husus:NumbuKaDieu/Citakan:Cikedung, Indramayu
Dina konsol debug modul:Yesno:
Kode patali dina Modul:Navbar#L-58 nyaéta:
Nambahkeun tanda petik dina
Dina kaca preview, pake template ieu:
Pesen galat jadi:
Ieu nuduhkeun yén koma
Pikeun nyaho template nu nelepon, lebetkeun:
Sanggeus
Pesen galat jadi:
Citakan:kac-2 dialihkeun ka Citakan:Navigasi ID Desa, jadi bugna lain dina modul Lua, tapi dina parameter nu dipake ku caller. Dina konsol debug modul:Yesno:
Parameter Sunda
Perhatikeun koma Citakan:Navigasi ID Desa dimimitian ku:
Jadi nilai standar
Henteu aya argumén anu ditetepkeun.
Sintésis
Pake ngaran parameter basa Indonésia dina caller supaya teu kudu ngarobah modul Lua:
HTML navbox hasilna hasil dibangkitkeun kalawan suksés:
Dina konsol debug modul:Yesno:
| |||||||||||||||||||||||||||||||
Dina kaca pamaké anjeun:
1. {{cross}}: {{kac-2 |propinsi=Jawa Barat |dati2=Kabupatén |nami dati2=Indramayu |nami kacamatan=Cikedung}}<br /> 2. {{tick}}: {{kac-2 |provinsi=Jawa Kulon |dati2=Kabupatén |kabupaten=Indramayu |nama kecamatan=Cikedung}}
1.
:
2.:
Labeling converted templates
[édit sumber]| Citakan ieu ngagunakeun Lua: |
Please place the {{lua}} template on the documentation subpage of all templates that use Lua. It will help to better communicate Lua usage and template conversions.
See also
[édit sumber]- Husus:IndeksAwalan/Modul: daptar modul Lua
- Kategori:Lua-based templates – l-groups ta templates based kan Lua
- English Wikipedia-specific resources
- mw:Manual:Coding conventions/Lua – standards to improve the readability of code through consistency
- en:Help:Lua debugging – a how-to guide about debugging Lua modules
- en:Module:Sandbox provides a pseudo-namespace for experimenting with Lua modules
- en:Wikipedia talk:Lua – requests for Lua-based templates or tasks
Notes
[édit sumber]- ↑ You can also have multiple output values, but functions that do this are not normally meant to be accessed from wiki pages.
| WP:Lua Project | WT:Lua Project talk | Modules | Help | To do | Requests | Resources en: m: mw: external |
- Project subpages
- m:Lua – "central" page at Meta-Wiki
- mw:Lua – index at www.mediawiki.org