Talk:Lua/Tutorial

(Redirected from Talk:Lua beginning scripting for MediaWiki/Tutorial)
Latest comment: 1 year ago by Sokote zaman in topic strict.lua

Your Two Cents

edit

As all things wiki, this is a living document. I encourage you to improve it. While it is new (or undergoing any significant changes), you may want to share your ideas, here, on the Talk page. Thanks for your contributions! Deeb (talk) 00:31, 31 May 2012 (UTC)Reply

On performance, loading data, etc.

edit

Someone asked Brad what the constraints of Lua within Scribunto were. He said:

It depends on what the question means by "constraints". It certainly is a constraint that Lua can only get input as text strings passed to the
{{#invoke:}}
and what can be fetched via frame:expandTemplate, frame:preprocess, and the like. There's also the constraint that it can only output wikitext not including pre-save transforms or transclusions and other
{{...}}
constructs. It's also a constraint that all Lua in the page is limited to 10 seconds CPU time. And relative to standard Lua, Scribunto's Lua lacks all sorts of functions (see mw:Extension:Scribunto/Lua reference manual#Differences from standard Lua).
Performance gains: Some templates are used to store data, and are invoked with #ifexist: in a main template. There are thousands of such "data" templates. With Lua, it's a lot more efficient to deal with a large data structure in one module than a
{{#switch}}
in wikitext can handle. The mw.loadData() function (probably coming to your wiki with MediaWiki version 1.21wmf11; see <a href="https://www.mediawiki.org/wiki/MediaWiki_1.21/Roadmap">the deployment schedule</a>) helps there too by only having to load that data once per page no matter how many times the module is #invoked. Lua is pretty good about loading large blobs of static data, and mw.loadData avoids the overhead of reparsing for each #invoke, so in general things should be pretty good there.

Sharihareswara (WMF) (talk) 03:29, 13 March 2013 (UTC)Reply

Tutorial not novice friendly

edit

I'm the old syntax user to write Wiki templates without actual knowledge of other programming languages like C. But the tutorial in this page has skipped way too much detail. What is "local", "function" or "return"? No explanation. And where should I insert this local arg2 = frame.args[2] or "" into the previous codes? I tried myself many times but all I get is just script error. I know veterans hate babysitting, but this is not the way you attract people to help contribute the project. I would like to see a tutorial truly customized to Wiki users that shows a side-by-side comparison between a syntax template and lua module which use the same input and output the same result. Sorry if I'm being too rude in this message. -- Sameboat (talk) 05:08, 18 March 2013 (UTC)Reply

This tutorial, at least in my view, is not a tutorial for Lua per se; rather, it is a tutorial for using Lua with MediaWiki. Again, the tutorial on the official Lua website is an excellent source of information. -happy5214 09:30, 18 March 2013 (UTC)Reply
I am surprised that this is the only discussion I see about how lua can be used by a novice (sans C-like). I Was expecting a little better clue on how to implement even a simple variable function on a wiki page using lua. (Not the syntax of lua itself, but the structural syntax within Wiki.) Seems not working. Could someone elaborate the doc page a little bit more please? Viswaprabha (talk) 17:46, 29 April 2013 (UTC)Reply
Oh I just saw this one! That is what I was looking for! So, it may be a good idea to keep a link here in the tutorial page too! Viswaprabha (talk) 18:41, 29 April 2013 (UTC)Reply

Calling Lua from template code

edit

I tried to call a Lua routine from inside a template code:

{{#invoke:Prova|center|{{{1|}}} }}

My aim was to test the possibility od a "double-step" template->scribuntu conversion, replacing template code with a call to a scribuntu script while keeping template call identical, so allowing a transparent-for-users conversion and deferring any change into page code. I searched a little bit but I couldn't find the trick. Have you a suggestion/a link for me? --Alex brollo (talk) 06:58, 22 May 2013 (UTC)Reply

Explanation of Code

edit

As someone else alluded to earlier, this tutorial isn't terribly clear for beginners. Now I often like that kind of tutorial, because I'm a programmer and can probably figure it out by context. But it certainly can't hurt to give relative newbies the option of seeing a clearer explanation of what's going on.

Then again, we don't want to clutter the tutorial in a way that will annoy the 1337 among us. So I made it collapsed, like this:

Explanation of Code
local this tells lua the "scope" of the variable that follows. It can only be accessed right here, locally. The opposite is "global"
p this is a (local) object (variable) we are creating. We have decided to call it p. We could call it anything we wanted. We will address it as p later, whenever we want to do something with it.
= {} The equal sign means we're assigning what comes after it to what came before it. The {} means "object", in this and many other script languages like JavaScript. It could have had stuff inside it like {foo = 'bar'}, but we're making it empty and will assign stuff inside it later.
function Now we're creating a function, which like in calculus is a piece of code that takes values and returns other values. The function will include all code until we get to a keyword "end". This is similar to most popular programming languages.
p.hello Remember when we made the object "p"? Now we are creating a property inside of that object, called "hello", which is this function. The dot means "property inside" in this and many other script languages.
(frame) The parens() wrap whatever values we are passing to the function we are creating, the one called "hello". "frame" is the name we give a container of all parameters we'll pass to it. We could call it anything, as long as we consistently refer to it that way later.
return 'Hello' "return" says we send this back out of the function we're in, which is called p.Hello, and it will be returned to the object we created, p. "Hello" is the thing we're returning. The single quotes around it mean it's a string, not a variable named Hello that contains some value.
end Remember when we said that the function would go on until we got to a keyword end? This is that keyword.
return p Remember the other "return" that send 'Hello' out of the function called p.Hello? Well THIS return takes whatever happened inside the entire object (which we called p) and sends it outside of that object. In this case, it'll end up being printed to the screen for the user to see or wiki server to parse.

It's only a rough draft explanation, of course. Any additional clarity people could add to it would be welcome.
Kazvorpal (talk) 16:02, 27 December 2016 (UTC)Reply

Some changes to the example code

edit

Changed the "p" to "export", since that makes more logical sense (following example as seen in existing code).

Changed "hello" to "show", because, again that makes more logical sense. (following example as seen in existing code).

Many installations specifically require the returned object to be named "p", so your first suggestion should not be taken. As for the second, that's not really following the typical convention of such an introductory fragment of code. DSquirrelGM𝓣𝓟𝓒 06:16, 7 January 2019 (UTC)Reply
I undid these changes primarily because they (inadvertently I'm sure) broke both the code itself and the Explanation of Code section. I suppose I could have fixed what was broken, but this was easier. I don't have an opinion on whether or not the name changes made the code more readable, and I have no input on DSquirrelGM's concerns (I simply don't know), so others should feel free to improve things. Gurnec (talk)

strict.lua

edit

I have run into an issue when importing a Template into my wiki:

Lua error in package.lua at line 80: module 'strict' not found.

Upon inspecting the error, I see that Module:Message box is using require('strict').

Upon researching require('strict'), I found on this page under Lua/Tutorial#Usage that in order to use it, one should create a file named strict.lua.

Please forgive my ignorance, but where exactly am I supposed to create this file? Somewhere under the Scribunto extension folder? I have tried creating a similar file both under /w/extensions/Scribunto/includes/engines/LuaStandalone and under /w/extensions/Scribunto/includes/engines/LuaCommon/lualib to no avail. Lwangaman (talk) 15:12, 23 October 2022 (UTC)Reply

Upon reading Extension:Scribunto/Lua reference manual#Strict library, it seems that the strict library should be already included in a mediawiki instance?
In any case, I found on gerrit that it should in fact be included under LuaCommon/lualib: https://gerrit.wikimedia.org/r/c/mediawiki/extensions/Scribunto/+/834623/4/includes/Engines/LuaCommon/lualib/strict.lua
And the module 'strict' not found error seems to have gone away (I'm getting other errors now!) Lwangaman (talk) 19:44, 23 October 2022 (UTC)Reply
You need to update Lua or use the following command instead of the above command:
require('strict')
require('Module:No globals') Sokote zaman (talk) 12:18, 17 January 2023 (UTC)Reply
@Lwangaman Sokote zaman (talk) 12:19, 17 January 2023 (UTC)Reply
Return to "Lua/Tutorial" page.