Talk:Lua scripting

From mediawiki.org
Latest comment: 8 years ago by Nemo bis in topic Ongoing

script call template ?[edit]

18:57, 2 April 2012 (UTC), Rical wrote:

Sory if my edit is not in a right place. The question is about the transition between template and lua. There are lots of templates and js scripts and they will not disapear. Is it a good idea to permit to call them from lua scripts ?

I well understand this is a very bad way because the time and ram uses. But this permit to re-use a large number of existing template, waiting the time when they will be replaced by lua scripts.

Bots could replace template calls with lua script calls each time a replacement is OK.

-- Rical (talk) 21:11, 2 April 2012 (UTC)Reply

I'm not sure at the moment whether invoking templates from Lua should be included in the initial deployment, since it's complex from the perspective of security and resource limiting. A simpler feature to implement would be allowing Lua functions to return some unexpanded wikitext for the parser to later expand. Do you think that would be useful? -- Tim Starling (talk) 00:28, 10 April 2012 (UTC)Reply
Yes, this is an other way for same goal.
  • A template call a Lua fonction with parameters
  • Then the Lua fonction generate an unexpanded wikitext, including adapted template call with adapted parameters
  • The unexpanded wikitext can include one or some template calls.
  • Then the parser expand wikitext including the template call(s).
I think this is simple to code in parser and in Lua users-scripts and this re-use existing template and would be usefull.
But ...
If we hope to replace existing template by more efficient Lua script, this way do not permit to easy replace the calls of them. The next step should be :
A common extra fonction could be defined in Lua library to do the same as before. Exemple :
s = templateCall( s, templateName, param1, param2, ... )
Then replace the template call by a future Lua equivalent script become 'easy', even by bot. The replacement give :
s = equivalentLua( s, param1, param2, ... )
Is this a right way ? Do we want replace many templates ? Will we be able to translate-convert many tempates by equivalent Lua scripts automatically ?
Rical (talk) 17:02, 10 April 2012 (UTC)Reply

var in template[edit]

Some time in template i would like to use var that could replace repeated code. This could reduce the interpreting time, simplify and reduce the code.
To do it we could use negative parameters and a syntax similar to switch.
To define {{{-1}}} and {{{-2}}} we could code :
{{#var: -1=after {{{1}}} days | -2=from {{{start}}} to {{{end}}} | -3=... }}
Rical (talk) 23:34, 3 April 2012 (UTC)Reply
No, there won't be any feature which requires wikitext to be parsed in a certain order, such as top to bottom. This gives us some more flexibility in future parser design. Memorization is probably the best option for efficiency. -- Tim Starling (talk) 00:28, 10 April 2012 (UTC)Reply
Yes, '#var:' was not the right word, my idea was to memorize a wikitext value in {{{-1}}} as local constant to re-use it in the next wikitext in the same template at the same level. The parser extented function could be #const: or #constant:
{{#const: -1=after {{{1}}} days | -2=Category:State of {{{end}}} | -3=... }}
Rical (talk) 17:02, 10 April 2012 (UTC)Reply
After a few minutes, i think that the value of a «constant» {{{-1}}} could be replaced by an other value to generate the next wikitext later in the same template. Then the «constant» becomes almost a variable on the same local level. The parser is not more complex. Rical (talk) 21:26, 10 April 2012 (UTC)Reply

Simple users[edit]

How will any simple wikiuser without a programmer's background learn to use this? Now it seems to me that only those who learnt programming will be able to use it. Now even the simplest templates can make some editors cringe, how is this going to work in everyday wikilife? I just can't imagine trying to tell new users to learn scripting in Lua to make templates... Or will this have a user friendly interface? Teemeah (talk) 13:20, 2 June 2012 (UTC)Reply

I think the simple answer is that nobody expects new users to do this. Templates can already be quite complicated, juggling all the {{#if}} and {{{1|}}} and <noinclude>. And if people want to use the “old” template syntax, they still can. Also, it's pretty much impossible to make something that is both powerful and “user friendly” so that new users without any programming background could quickly pick it up. Of course, a dedicated newcomer should be able to pick it up, but Lua is probably one of the better languages for this, because it's quite simple. Svick (talk) 13:32, 2 June 2012 (UTC)Reply
Moreover most of the time important templates − in term of visibility and/or effective usage − are protected and are in pratical only modified by admins (with template-experts in the editing loop). Regards, Hexasoft (talk) 14:42, 18 January 2013 (UTC)Reply

Breaking the server[edit]

Can I try to break the server with this, or is that discouraged? --Carnildo (talk) 23:15, 22 August 2012 (UTC)Reply

Unicode support[edit]

Is there any plan to make Lua function properly with unicode characters? At the moment, string length seems to be being measured by how many bytes are in a string, not how many proper characters. Having string manipulation work with non-ASCII characters would be necessary if this is to be deployed on non-English wikis, I think. --Yair rand (talk) 03:13, 23 August 2012 (UTC)Reply

Just to add to this, there seems to be no problems with the code handing various Asian unicode characters as parameters. But I noticed a problem with the code editor on test2 with lines with non-ASCII characters in: the cursor position would be out by one. The workaround was to copy and paste those lines into another text editor, or even the edit summary box, to work on them, but this would get tedious if there were a lot of non-ASCII text.--JohnBlackburne (talk) 12:12, 9 October 2012 (UTC)Reply
So is this actually a problem with the code-editor failing to handle unicode characters? Maybe that would be a better point of attack. In reference to the original question, we don't seem to have heard anything relating to whether Lua will be made unicode-aware any time soon. —Phil | Talk 12:30, 9 October 2012 (UTC)Reply
There is no such thing as "Unicode aware" or "Unicode support". Any time I hear someone say such a thing, it's instantly apparent they don't even know what Unicode is. Lua has everything you need to handle UTF-8. The slnunicode library can give you a character-wise length just fine. The Unicode standard is immense and covers many things that Lua is never likely to need. Sure, Lua strings are actually byte arrays, but anything else at the language level would be completely and utterly wrong -- as Erlang have acknowledged and Python have found out the hard way. What you need is a library, not the Lua authors to wave a magic wand and give you a check-box "Unicode Support" feature.

Errors[edit]

Script error: You must specify a function to call.

Error handling is terrible. What are we supposed to do when faced with error messages like that? This, that and the other (talk) 11:15, 23 August 2012 (UTC)Reply

Oh, I see... it's an HTML comment. Still, that is rather abstruse. This, that and the other (talk) 11:15, 23 August 2012 (UTC)Reply
It looks like you can click on the error message and it will show a small window with the error message. But that's still really confusing, I wouldn't expect big red error message to be clickable or that clicking will show the error in that window. Svick (talk) 13:27, 23 August 2012 (UTC)Reply
Tim Starling tried to help about this. You can read his answer there :
« I wrote a debug console module and deployed it here, let me know what you think of it. » -- Tim Starling (talk) 04:50, 14 July 2012 (UTC)Reply
You could try it and return your feeling to him and us. --Rical (talk) 19:51, 23 August 2012 (UTC)Reply

API/syntax[edit]

Lua fellow here. The syntax and API described in Lua_scripting/Tutorial could IMO be improved. I hope it is not too late...

Regarding the API, why do you pass the arguements in the frame table? I don't know the purpose of the frame, but I suppose that it contains meta-information. With the current system, reading an argument requires two table lookups, which are far more expensive than fetching an argument (similar to a local lookup). Furthermore, you create the args table, which takes time, space, and time again when the grabage collectore runs. I'd suggest something like this:

   function p.foo(frame, named, bar, baz) ... end

named would be nil unless there are named arguments. In that case, it is a table with appropriate key/value pairs. It would be up to the Lua scripter to handle both named and unnamed arguments if they occur. Alternatively, named arguments could be part of the frame, but only when they exist.

   if frame.named then
     bar, baz -- already locals since they are arguments
         = frame.bar or bar
         , frame.baz or baz
   end

Actually, I'm not sure if the positional args should be preserved when named arguments are used. It would probably be confusing for editors. Either go all positional or all named (but a given functions could support both).

Regarding the invocation syntax, {{%my_module.hello | foo | bar = baz }} (or a variation thereof, I'm not familiar with the meaning of initial sigils in WikiMedia templates) is ligther, more readable and more semantic than {{#invoke: my_module | hello | foo | bar }}. It also allows to have modules that are actually just one function. -- Pierre-Yves 89.90.144.90 15:08, 30 August 2012 (UTC)Reply

Hi, Pierre-Yves. Thanks for your note. Tim Starling discussed it starting around 01:12:10 in these IRC logs. Hope this is helpful. Sumana Harihareswara, Engineering Community Manager (talk) 01:40, 6 September 2012 (UTC)Reply
Just a point: have both named and unnamed arguments seems fine to me. Named args are fine for pre-defined elements but in somes cases templates also has something that is highly variable in syntax/order, and the only way to handle it with named parameters is to make lot of parameters most of the time never used (i.e. param2, param3, param4… until enough for any purpose). Regards, Hexasoft (talk) 14:48, 18 January 2013 (UTC)Reply

Modules calling Modules[edit]

I should just say I've been on WP for years, done a little trivial template work, but complex templates are beyond me. But two days in, learning Lua at the same time, I've ported one of those complex templates to Scribunto: [1].

One thing I encountered though is the difficulty calling one module from another. I ended up adding all of this

local p = {
    language = require "Module:Language",
    fr = {
        args = {}
    }
}
function p.getParent()
    return p.fr
end

Then to invoke the module function need to do something like this

           p.fr.args[1], p.fr.args[2]  = "zh-Hans", s
           s = p.language.lang(p)

This works but seems convoluted just to call one function and is probably not especially efficient. It would be better if there were a function that could be called directly, so if the argument parsing code were separate from the logic that processes them, in a function which could be called from other modules. This could be done in Module:Language or a separate Module:Language0 meant to be called by other Modules. Or is there some other preferred way of doing this that I'm not aware of?--JohnBlackburne (talk) 23:59, 9 October 2012 (UTC)Reply

Various notes/questions[edit]

Hello,
I started playing with modules and now have a basic taxobox (infobox for biology) that works fine. Whatever some points and questions I met during working on it:

  • are there any limitations for Module: namespace? I tried to put a "tools" module dedicated to my taxobox. I puted it in Module:Taxobox-fr/tools but I failed to use it from Module:Taxobox-fr. I renamed it into Module:Taxoboxoutils and now it works fine.
  • does the "local config = frame.args" and then to read "config.pagename" is supposed to work? I get an error because it is 'nil' in my code. I use preprocess("{{PAGENAME}}) rather but it seems not to be the best solution.
  • I did not find how to handle errors (not errors in the code but errors in the template/module usage). I mean it would be nice to be able, in the middle of the module, to detect a bad usage/parameter/whatever and to be able to "leave" the module with a program-generated error page. Sometimes you have to perform complex computations to detect a problem, in the middle of the normal code. Something like calling a function that can perform a "exit 'code to display'" could save writung some complex if/else code. Sorry: I misundertood the "return must be at the end" :) It works fine to return inside code, until it is at the end of a code bloc. Hexasoft (talk) 11:47, 4 December 2012 (UTC)Reply
  • it is not clear for me if "Full deployment of Lua to the production cluster" (in the roadmap) is only about mediawiki or will also applies to wikipedias.

Whatever, a very nice tool! :)

Regards, Hexasoft (talk) 10:48, 4 December 2012 (UTC)Reply

Some updates:
  • using modules in subpages (such as Module:Taxobox-fr/tools) works, but it seems that it is not well handled for cache update: changes in a submodule are not visible in articles that use the main module. If a change is applied to the main module changes in submodules become visible. It is so a bug (that may be corrected) or a feature but in this case it would be fine to prevent using submodules in any way to prevent the problems I met.
  • 1.21wmf7 is deployed everywhere (as seen is Special:version). How scribunto extension may/will/is be activated on wikipedias?
Regards, Hexasoft (talk) 14:34, 18 January 2013 (UTC)Reply

Compare strings[edit]

It seems like both '\126'<'\127' and '\127'<'\126' are false. Moreover,(there was some tempora bug in console while server was trobled, maybe origined in my head) How can I compare UTF-8 strings correctly (by mw.language('qqx') alphabet)? Ignatus (talk) 21:01, 13 March 2013 (UTC)Reply

TemplateSandbox[edit]

It currently doesn't seem to work within Special:TemplateSandbox, if you also want to use modules from user namespace. Are there any plans to integrate support for modules within user space? --Entzücklopädie (talk) 21:56, 13 March 2013 (UTC)Reply

Works for me. Note that you need to use the canonical name of the page in the sandbox, e.g. Benutzer:Entzücklopädie/Spielwiese/Modul:Luatest rather than Benutzer:Entzücklopädie/Spielwiese/Module:Luatest. Anomie (talk) 23:18, 13 March 2013 (UTC)Reply
Thank you! It's working now. --Entzücklopädie (talk) 23:27, 13 March 2013 (UTC)Reply

A few queries[edit]

Hi, It is a great news that Lua has enabled in all the wikis, which has a good number of benefits over the existing solution. I am a contributor of Bengali Wikipedia. Yesterday Lua has deployed there too. I have a few related queries about the this.

  • Do we have/need to convert all the existing templates?
    • if so then is there any road map or guideline for this?
    • if not then which templates should we convert to lua?
  • the existing code editor is not suitable for writing Unicode characters, is there any ongoing development for this code editor? or do we have any plan for this?

thanks --Nasir Khan Saikattalk 15:59, 14 March 2013 (UTC)Reply

You do not need to convert any existing templates, but there are many opportunities. For example, if you have equivalents to the templates in en:Category:String manipulation templates, those would be excellent candidates for conversion. BJorsch (WMF) (talk) 13:24, 15 March 2013 (UTC)Reply
I don't know much about the code editor and Unicode, other than that my compose key seems to work fine with it in Firefox. I'll see if I can ping someone who might know more. BJorsch (WMF) (talk) 13:24, 15 March 2013 (UTC)Reply

Is Lua worth it for 1 template calling many other templates?[edit]

I have a main template (used multiple times per page and on 40% of all my pages) which includes many other simplier templates.

Would Lua be worth it? --Subfader (talk) 16:02, 14 March 2013 (UTC)Reply

It depends on what the template actually does; if it's something that's already straightforward to do with parser functions it may not be, but if it's complicated with lots of {{#if}} and the like it might be worthwhile. Note it may be that the whole set of simpler templates might be able to be converted to a single Lua module. BJorsch (WMF) (talk) 13:27, 15 March 2013 (UTC)Reply

Retrieving categories information in a Lua module[edit]

How can I do the following actions in a Lua module?

  • Check what categories a page is in.
  • Check what categories a category is in.

Thanks, --Gryllida (talk) 05:02, 16 March 2013 (UTC)Reply

These are not currently possible. BJorsch (WMF) (talk) 01:25, 17 March 2013 (UTC)Reply

Userifying[edit]

I can make [[User:Username/Foo]] and include it into any page as a template, using {{User:Username/Foo}}; however, all modules end up being in the Module: namespace. Is it possible to userify them? Thanks. --Gryllida (talk) 05:04, 16 March 2013 (UTC)Reply

No, it's not. For testing, pages in other namespaces may be interpreted as modules using Special:TemplateSandbox. Otherwise it's up to each wiki whether they want to set aside any particular prefix (e.g. "Module:User:Example/" for User:Example) as "userspace" for modules. BJorsch (WMF) (talk) 01:27, 17 March 2013 (UTC)Reply

Lua changes and Job queue[edit]

On fr.wiktionary (and other Wiktionaries) some templates are present on all articles, especially language-based templates. We are in the process of converting said templates to Lua, and we plan to use a Lua table like wikt:fr:Module:langues/data (similar to wikt:en:Module:languages) which contains a list of names of languages to be used in all articles (each time there is a translation or a section in a given language for example). The old way was to have a template for each language and import the data included. The question is : if we change a module that is used in a lot of pages, if only to add a single language name to the list, then all pages may be impacted. Would this be a performance issue (cf Job queue) ? Darkdadaah (talk) 13:03, 16 March 2013 (UTC)Reply

Editing a module that is used in many pages has the same effect on the job queue as editing a template that is used in many pages: any page listed in Special:WhatLinksHere for the edited module as a transclusion will be affected. Whether or not that would be a performance issue in the situation you describe, I don't know. BJorsch (WMF) (talk) 01:32, 17 March 2013 (UTC)Reply

Another tutorial?[edit]

I wanted to note here and this discussion in case they'd be helpful for reshaping the tutorial or some other quick HOWTO. Sharihareswara (WMF) (talk) 02:48, 22 March 2013 (UTC)Reply

Any request big enough for a GSOC project?[edit]

Is there any Lua related request big enough to keep a student busy during 8 weeks? We are looking for Summer_of_Code_2013 projects and mentors. There is a generic suggestion to write Lua templates (that we fully support) but we need a defined project to propose to students. The discussion is happening in this Talk page.--Qgil (talk) 01:20, 26 March 2013 (UTC)Reply

Timeframe for inclusion in end user MediaWiki?[edit]

It's not clear to me when Lua scripting is intended to be included into MediaWiki for end users. This page seems to indicate March 2013 (it is May 2013 as of this writing). It is not mentioned on the MediaWiki_1.21 or MediaWiki_1.22 pages. Anyone know when it is supposed to be released? --Jamesmontalvo3 (talk) 16:56, 7 May 2013 (UTC)Reply

Any wiki with a sufficiently-recent version of MediaWiki may install Extension:Scribunto at any time. March 2013 is when it was enabled on all WMF wikis. Anomie (talk) 13:10, 8 May 2013 (UTC)Reply

Calling to a module from another module?[edit]

Hi, I'm playing with ca:Mòdul:BanderaLuaTest. It works! See it in action showing all the flag icons at ca:Plantilla Discussió:BanderaLuaTest. Still, I want to separate the table of translations into a module apart, so editors can keep that table up to date without risking to touch any code. How should can I make a function call to another module? Thank you.--Qgil (talk) 00:39, 2 June 2013 (UTC)Reply

I think I found it: Extension:Scribunto/Lua_reference_manual#require. It's late today, will try tomorrow. It has been a very exciting Lua Day 1!--Qgil (talk) 07:20, 2 June 2013 (UTC)Reply
Yes, in ca:Mòdul:BanderaTable you can write :
local m = {} -- I prefer "m" to means "this module"
m.t = { your table }
return m
In ca:Mòdul:BanderaLuaTest, you can write first :
local table = require "Mòdul:BanderaTable"
-- and later :
local myvalue = table.t[key] -- for a value in the table
for key, value in pairs(table.t) do -- for the table
--Rical (talk) 10:10, 2 June 2013 (UTC)Reply
Thank you! I should have come here again before trying to fix it myself. But after 3 failed attempts I got it right, looking at the Lua docs. Other will get it right easily thanks to your example.--Qgil (talk) 20:48, 2 June 2013 (UTC)Reply
But... isn't it enough to use local t = require "Module:FlagTranslations" combined with ca:Module:FlagTranslations. I like this approach to have all the logics in one place and table with content in another.--Qgil (talk) 20:55, 2 June 2013 (UTC)Reply
@Rical: You shouldn't use "table" as a local variable in examples, because people are liable to copy that and then wonder why standard library functions such as table.concat suddenly stop working. Anomie (talk) 14:15, 3 June 2013 (UTC)Reply
http://www.lua.org is an excellent reference. Also you can try some other way. But :
  • Lua in wikis is not standard Lua, but adapted to wikis because security, efficiency ...
  • For new users, it is better to learn and transmit good practices.
  • The main part of a module is the one which is returned at the end. In the example "return m".
  • If you use an other way, it is too difficult to manager variable from multiple modules.
  • The question become in long term : I have variables in plural modules, some variables are global or local at required module level or at "m." level, and some variables are re-defined in any module at any level. Which variable am I using ?
  • We are not alone to write and we must keep in mind that other users, even less experimented, need to understand and to continue our job.
  • The good practice is to use only "m.x" declarations and their rename by require like "table.t", then you and others always know what you are doing. --Rical (talk) 22:50, 2 June 2013 (UTC)Reply
It's not clear to me what you're recommending as "good practice" here. But if you're talking about this versus this, the only drawback to the former is that it's harder to add a second list to the first. But there's nothing stylistically wrong with it in my opinion. And further, if you're going with the latter style I'd strongly recommend using real names for the subtable key rather than cryptic letters such as "x". Anomie (talk) 14:15, 3 June 2013 (UTC)Reply
I ended up following the advice and, well, might indeed have a second table one day (e.g. flags that require no border). I also made the variables more verbose with the intention of letting others know easily what is going on. And I moved the modules to en.wiki to gather more eyes and feedback - see the little announcement. Who knows, maybe one day fully supports the functionality behind w:Template:Flagicon. Thank you again for your help.--Qgil (talk) 15:56, 3 June 2013 (UTC)Reply
Note that it also exists loadData() function that is very similar to require but is dedicated to load modules that only contain data. See Extension:Scribunto/Lua_reference_manual#mw.loadData for details. Regards, Hexasoft (talk) 11:32, 5 June 2013 (UTC)Reply

Ongoing[edit]

Is this still an ongoing software project on its own? If not, please set an end date in the infobox. Nemo 21:10, 18 November 2015 (UTC)Reply