Wikipedia:Lua requests

From Wikipedia, the free encyclopedia
Jump to: navigation, search
Project talk
  To do
en: m: mw: external

Lua scripts on Wikipedia are similar to templates but useful for performing more complex tasks for which templates are too complex or slow. Common examples include numeric computations, string manipulation and parsing, and decision trees. You can use this page to request help from Lua developers in writing a Lua module for a specific task on Wikipedia or another Wikimedia Foundation project. Both debugging help and full implementation are available.

To start a request, just make a new section below and describe what you need. You may wish to first check Special:PrefixIndex/Module: to see if you can find a suitable existing script.

It may help developers to provide examples of where the task is likely to be useful within Wikipedia. If the proposal would replace or improve upon existing templates, please note which ones.

User:Launchballer/London Buses route layout[edit]

What I am after is Lua coding to recognize when a particular parameter is two characters or less and to top it up with 0s. The latter 'topping-up' part I can do, but it is a length-check I'm after.

  • If 1 is inputted, it should be topped up to 001.
  • If 93 is inputted, it should be topped up to 093.
  • If C1 is inputted, it should be topped up to C01.

Basically I'm after a way of checking the number of characters in a string and then how to display a particular character so I can, if necessary, drop the 0 in the middle.--Launchballer 11:37, 18 March 2015 (UTC)

{{#invoke:String|len|1}} → 1
{{#invoke:String|len|93}} → 2
{{#invoke:String|len|C1}} → 2
Trappist the monk (talk) 11:54, 18 March 2015 (UTC)
To add zeros to the left of a string, you can use the {{padleft:}} magic word. This doesn't add them to the right of any letters, though.
  • {{padleft:1|3|0}} → 001
  • {{padleft:93|3|0}} → 093
  • {{padleft:C1|3|0}} → 0C1
SiBr4 (talk) 12:06, 18 March 2015 (UTC)
let's say you parameters are called P (input string) and L for length (i.e., {{{P}}}), and {{{L}}} ).
wrinting it straight in lua is relatively easy. it looks, more or less, like so:
function padmiddle( source, len )
    local nodigits = ustring.match( source, "^%D*" )
         , digits = ustring.match( source, "%d*$" )
         , padlen = len - ustring.len( source )
         , zero = mw.language:formatNum( 0 )
    return nodigits .. string.rep( zero, len ) .. digits
this simplistic function does not verify correct input, so calling it with, say, ("11cc", 5), will return "0" (no non-digit at the beginning and no digits at the end: theoretically it should return "00000", but it assumes, naively, that the input is correct, so seeing 4 characters, it only uses a single 0 for padding)
you can use existing functions to do it with no new lua code, like so:
admittedly it's ugly, but i believe it should work (again - no sanity check for the correct form of input string). peace - קיפודנחש (aka kipod) (talk) 01:07, 19 March 2015 (UTC)
Okay, and how do I get it to pick out a particular character from within the parameter, and recognize whether it is a letter or a number?

--Launchballer 02:00, 19 March 2015 (UTC)

you do not have to worry about "letter or number" - the "match" function with the correct patterns ("^%D*" for initial non-digits and "%d*$" for terminating digits) takes care of that. in the example above, {{{P}}} represents the input string, and {{{L}}} represents the desired length. if, in your case, if it's always 3, you can replace {{{L}}} in the example with 3. in the general case, i thought it's better to leave it as a parameter. note that {{{P}}} appears several times - you should replace them all with the correct parameter name. alternatively, you can create a new template containing the code above, and pass to it these two params. i want to make one point again: this code does not check for the validity of the input: it assumes that you supply it with a string that contains zero or more non-digit characters, and then zero or more consecutive digits. it should handle your examples above the way you required, but if you pass something like "1C" and length 3, you will get "0" (no non-digits at the beginning and no digits at the end, so all is left is the padding, which only take into account the total length, i.e., 3-2). peace - קיפודנחש (aka kipod) (talk) 12:41, 19 March 2015 (UTC)
@Launchballer: - just curious: did you find the above useful ?
peace - קיפודנחש (aka kipod) (talk) 15:44, 21 March 2015 (UTC)
קיפודנחש: Just tested it at User talk:Launchballer/sandbox. The Lua is used in the URL "Full timetable (PDF)" and a category, both of which seem to be broken.--Launchballer 11:50, 23 March 2015 (UTC)
@Launchballer: yes, the code above contained a bug: it had "#invoke|string" instead of "#invoke:String". i fixed it when testing, but forgot to fix it in my message... sorry about that. to make life tiny bit easier, i created Template:Zpadmiddle ("pad middle of string with zeroes"). if you do not find it useful, please mark it for deletion. peace - קיפודנחש (aka kipod) (talk) 16:02, 24 March 2015 (UTC)

Eclipse dates[edit]

Could a module be written to take a list of dates, link them, and indicate the next coming date? This would be useful for templates like Template:Lunar eclipses which need to be constantly updated by editors. Thanks — Martin (MSGJ · talk) 12:36, 25 March 2015 (UTC)

@MSGJ: Is there a formula that the dates can be calculated from? I assume there must be something, but I'm a complete beginner regarding such calcuations. — Mr. Stradivarius ♪ talk ♪ 14:50, 25 March 2015 (UTC)
I guess there is a formula somewhere, but I'm not asking for that. Just to put a marker (like ) next to the next occurrence. — Martin (MSGJ · talk) 14:54, 25 March 2015 (UTC)
The request is that a module be used to implement Template:Lunar eclipses. There would be a data page somewhere that contains the items to be displayed. The module would output exactly what the template does now, except that it would highlight, in each of two lists, the date that is the first after today + 1 (I would add 1 or even 2 or 3 so the highlight does not move until a couple of days have passed). The current template feeds a bunch of stuff into {{Navbox}}, and the module would do essentially the same. That would all be do-able but unless the functionality is needed in several other places as well, it looks a lot easier to me to just edit the template which needs five minutes of editing in the next two years, whereas the module would need a lot more than that! Johnuniq (talk) 01:01, 26 March 2015 (UTC)
Actually, we don't need to bother with the navbox part, or with making data modules for all the different templates. I've just finished making {{highlighted date list}}, which takes parameters like |item1= and |date1=, sorts them by date, adds a highlighter and outputs them as an unordered list. MSGJ: will that do the job? — Mr. Stradivarius ♪ talk ♪ 04:52, 26 March 2015 (UTC)
Amazing, well done! Johnuniq (talk) 05:39, 26 March 2015 (UTC)
Thank you very much! I've started using it already. — Martin (MSGJ · talk) 09:32, 26 March 2015 (UTC)

Module:Message box: sidebox option[edit]

See Template:Convert, especially the documentation as it shows.

It has a big "Translate this template into Swahili" messagebox in top. But the editors who visit this page are not translators. So I'd like to have this box miniaturized into a side-box (just like the {{Lua}} box already in there). Upstream, it has the Ombox and module:Message box. Where to set which switch? (at the moment, I'm not in to learning that; so just do it please. I'll see). -DePiep (talk) 21:47, 25 March 2015 (UTC)

@DePiep: With this edit you can now pass |small=yes to {{transwiki guide}} to output a small template. — Mr. Stradivarius ♪ talk ♪ 06:43, 26 March 2015 (UTC)
Yes. -DePiep (talk) 11:02, 26 March 2015 (UTC)

Visualization modules[edit]

Hi, can anyone give me a quick greatest-hits of visualization modules that have been built on-wiki here? I'm aware of Module:Chart for starters. ResMar 20:47, 28 March 2015 (UTC)

Template:Chess diagram is using Module:Chessboard (i think). i believe this falls under "visualization". as a side, i'll mention that for a long time i't trying to entice someone to create a lua wrapper that will make working with mw:Extension:EasyTimeline easier, but so far, no takers. if you find something in some other wikipedia that does it, i'll be grateful to hear. peace - קיפודנחש (aka kipod) (talk) 15:46, 30 March 2015 (UTC)
Tut. ResMar 13:25, 31 March 2015 (UTC)

Help debuging[edit]

I wrote a module that one of it's functions returns text formatted using a template. When invoking the function the template doesn't get expanded.

What am I doing wrong?

Thanks, אלישיב ליפא (talk) 08:44, 31 March 2015 (UTC)

A few links:

A link to the module:

An example is (currently) in the sandbox of he.wikisource

The idea of a module is to eliminate templates, so the quick answer is that you cannot return a template as the result of a module. A slightly longer answer would explain some details that I'm not familiar with concerning the fact that MediaWiki invokes modules at a point where it is too late for templates to be processed. I believe you can workaround the issue with expandTemplate, but what should be done is to redesign the module. Preferably, the module should emulate the template and return the wikitext that the template would return. Next option would be to redesign the template to invoke a module to help with the work that the template wants done. Johnuniq (talk) 09:15, 31 March 2015 (UTC)
(edit conflict) @אלישיב ליפא: Output from Lua modules isn't preprocessed automatically, as that might lead to some things being preprocessed twice. (The arguments are preprocessed automatically when you access them from inside Lua.) To expand templates, parser functions and extension tags, you need to preprocess them yourself using one of the methods of the frame object. In your case, the best one to use would be frame:expandTemplate. — Mr. Stradivarius ♪ talk ♪ 09:17, 31 March 2015 (UTC)

lua module breaks lines.[edit]

Hi. I'm using a Lua module to create some functionality, and the call to the module seems to break lines. I have no Idea why this is happening, and I hope someone can help me.

The module:

Example for a break:

אלישיב ליפא (talk) 20:18, 31 March 2015 (UTC)

Looking at the template source it looks like it may be the problem I was having here: Wikipedia talk:Lua/Archive 2#Odd problem with new template/module. Basically if you have a category at the end of a template it inserts a new line after it. Move the category anywhere else and it fixes it.--JohnBlackburnewordsdeeds 21:12, 31 March 2015 (UTC)

Move request (map)[edit]

I have a page User:Ahmetyal/sandbox/2 which I wan't to move from a template to a module, because I have reached the limit with locations. --Ahmetyal (talk) 09:28, 19 April 2015 (UTC)