From Wikipedia, the free encyclopedia
< Wikipedia:Lua  (Redirected from Wikipedia:Lua requests)
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.

Wrap new Graph tag[edit]

in one of the recent software updates, we received a new extension: mw:Extension:Graph. this can and should replace everythig we do, for instance, in Module:Chart: it can do barbraphs, pie charts, and so much more.

the problem with this extension is somewhat similar to the problem with mw:Extension:Timeline, which was the previous charting tool (it still exists, but is obsolete): it does not use syntax which is familiar and convenient to wikipedia editors.

now, some time ago i wrote Module:Chart, which plots pie charts and bar graphs. the implementation is disgusting, if i say so myself, because of the very limited support vanilla html provides for charting: the bars are generated by "div" elements with absolute positioning, and the pie chart is based on some obscure features of the way html renders borders (the exploitation itself is ingenious, IMO, and is taken from Template:Pie chart).

i kinda like the syntax used by Module:Chart - i think it lets the editors provide the meaningful data, and hides from them all the mundane details. i asked on this page, several times, for suggestions to make the syntax even better, but there was not much response.

the request here is simple: create a module that will receive parameters in the same syntax used by Module:Chart, but instead of using obscure and perverse methods to draw the graph, will use then new Graph tag.

as a second round, we should use this extension to support more chart types: line chart, scatter chart, horizontal bar graph, and if someone will gets really ambitious, maybe 3-d bar chart.

as a side, i'll mention that the graph extension can do more than just charts - this should be left as an exercise for other modules.

i believe the extension supports all the charts that can be found here: here, but i cant guarantee this is the case.

peace - קיפודנחש (aka kipod) (talk) 17:00, 8 May 2015 (UTC)

I saw your work and was totally amazed at the brilliant result and excellent usability, but I haven't needed to use the module. The new graph extension is a typical triumph over common sense requiring mumbo-jumbo for the input, and this demo advises that external tools should be used. The first step in your idea of a module to generate the graph wikitext would be to do a simple test of what happens if a module outputs one of the demos—would it render as a graph, or is module processing too late for that? Johnuniq (talk) 23:23, 8 May 2015 (UTC)
You'll probably need to use frame:extensionTag to get the graph to render. Anomie 23:29, 8 May 2015 (UTC)
yes, i assumed as much... peace - קיפודנחש (aka kipod) (talk) 23:00, 11 May 2015 (UTC)
This is the biggest front-end development since Lua implementation, in my humble opinion. But, indeed—working with the tags directly is impossible. I don't think that's a bad thing necessarily, though: by going as verbose as possible the visualization engine is as extensible as possible, and once everything is neatly standardized and parametrized via templates, it'll be quite beautiful, I think. ResMar 03:20, 16 May 2015 (UTC)

Hi קיפודנחש (aka kipod), Anomie, Resident Mario, have a look at this by Mps:

Pong ball in flight.svg Atlasowa: Can't tell you how happy I am to see this; we need to port it over immediately. @DerHexer: you know anyone that would be willing to translate the documentation? ResMar 14:58, 12 June 2015 (UTC)
See also. ResMar 14:59, 12 June 2015 (UTC)
I went ahead and imported two bits: Module:Graph and Template:GraphChart. ResMar 18:04, 12 June 2015 (UTC)
absolutely awesome. now, we just have to wait for "someone" to add "pie" capability (the graph extension has it, but the module does not support it [yet?] ), and we can retire Module:Chart entirely. this is much better and more appealing. peace - קיפודנחש (aka kipod) (talk) 15:41, 16 June 2015 (UTC)
This week I made a module on to duplicate the functionality of {{Pie chart}} (currently it's in alpha testing here it:Modulo:Sandbox/moroboshi/Chart with a test page here it:Template:Pie chart/test. Parameter name are in a configuration file it:Modulo:Sandbox/moroboshi/Chart/Configurazione for easy localization, some code of the html tag must be changed (some difference between the {{Legend}} template and the italian equivalent) but it's a simple fix. If you are intereste I could import it on (talk) 16:20, 5 July 2015 (UTC)
We need an interwiki transclusion bot to handle these templates until the WMF finally wakes up and introduces it as a MediaWiki feature. ResMar 05:17, 12 July 2015 (UTC)

Re-write Template:World topic as a Lua module[edit]

The source code of this template is verbose, redundant, and difficult to read. It should be re-written as a Lua module so that it can be maintained more easily. Jarble (talk) 01:27, 19 June 2015 (UTC)

lua may make life easier a tad (though i actually doubt it), but there is no real need for lua in order to make this more manageable: the template is extremely repetitive, and lends itself easily to improvement by simple use of some helper template (say, one that will accept prefix, suffix, noredlinks and the actual country name, and will do the right thing). the main template then will look something like
{{helper| {{{prefix|}}} | {{{suffix|}}} | {{{noredlinks|}}} | Angola }}
{{helper| {{{prefix|}}} | {{{suffix|}}} | {{{noredlinks|}}} | Denmark }}
... (repeat for all countries)
this may still look a bit cumbersome, but it's a dramatic improvement over current state: all the #if and #ifexist are delegated to the helper template, and of course, the helper is not at all repetitive - it only does whatever it needs to do once.
as a side, i'll mention that #ifexist is in the list of "expensive parser functions" (using lua will make no difference here - calling an expensive function from lua is just as expensive...), and its usage in this template seems to me to be sailing very close to the wind (i did not count, but there's a*lot* of countries...).
peace - קיפודנחש (aka kipod) (talk) 16:00, 19 June 2015 (UTC)
I am taking this task, making an module instead of template:world topic.--Snaevar (talk) 15:06, 2 July 2015 (UTC)
@Snaevar: I've been working on a version of the new Module:World topic that loads the country data from a separate page (incomplete for testing purposes) to allow the main code to be reused for other region-topic templates (as described at the templates' entry in WP:Lua/To do). It includes most of the features of {{Europe topic}} et al. that the current code in Module:World topic doesn't have:
  • Support for multiple navbox groups (e.g. "sovereign states", "states with limited recognition", "dependencies");
  • Country-specific parameters to individually enable or remove entries, or change the link targets (e.g. |GE=Foo of Georgia);
  • Option for a switch to enable/disable multiple entries simultaneously, and to hide specific entries by default;
  • An |article= parameter to either enforce or prevent the definite article (using the current "prefix but no suffix" rule if unspecified);
  • No automatic space between prefix/suffix and country name if unnamed parameters are used, allowing constructions in which a non-space character directly precedes or follows the country name (e.g. Index of Peru-related articles).
It additionally removes the need to explicitly number the countries and manually change over a hundred numbers when adding or removing entries in the first part of the alphabet (like here). Though some more features will be needed to fully replace some continent-topic templates, e.g. sub-lists for England etc. in Europe topic, could this be useful for the Lua-based version of World topic? (I'll do some more extensive testing at Template:World topic/sandbox if so.) SiBr4 (talk) 21:31, 23 July 2015 (UTC)
I think it is not worth making an unified module for world topic, Euro topic, Asia topic etc. Instead I would like to see the country table in Module:World topic to be moved to an submodule that would be shared with another module that would cover Euro topic and Asia topic. Changing Module:World topic to use an country table from another module would be pretty trivial (in therms of difficulty) for me to do.
World topic does take an different approach than the Euro topic and Asia topic templates. I would not want to see the redlink check from World topic in euro topic - for the reasons kipod mentioned earlier. In a similar way, the features of Euro topic are not of much use to world topic.
Writing that though, I still think that the countries in the country table should not be numbered, as I am very much aware how much of an issue that would be to maintain.--Snaevar (talk) 16:31, 24 July 2015 (UTC)
I disagree that the features of the other topic templates could not be useful to World topic or vice versa. For example, country-specific parameters are necessary for individually correcting links and avoiding redirects if not all titles follow the exact same format or the linking names from the data page, and the article parameter is useful if the default rule for including the definite article (prefix and no suffix) gives false positives or negatives.
Automatically omitting non-existing articles would always be optional and opt-in, as it currently is in World topic; it could be easily explained in the template documentation that the feature is expensive and should not be used unless necessary. It would also be much less expensive in continent-topic templates than it is in World topic, since the former have fewer links (<60 as opposed to ±200), and thus fewer existence checks.
I'm not sure I understand your alternative proposal; I read it to be that a single country list is used by all region-topic templates, while a separate formatting module is made for the continents (or even for each continent separately). I don't know how easy a unified data page would be to implement, though, and per the above I think there should be a single module with the main code, if only for consistency. SiBr4 (talk) 22:41, 24 July 2015 (UTC)

String parsing and concatenating[edit]

I want to write a new template that will be invoked as follows:

{{SBN | IT\ICCU\MIL\0205279}}

It should display in the article as follows:


IT\ICCU\MIL\0205279 should be a clickable external link coded as follows:

[ {{{1}}}]

My problem...I don't know how to parse the template's parameter to get "MIL" and "0205279", how to concatenate "MIL" and "0205279" to produce "MIL0205279", and how to stick "MIL0205279" into the middle of the external link's definition. I'm guessing Lua.

If you would please help me this first time, citing relevant Wiki pages along the way, I'll be able to handle it myself in the future. Thanks. Knife-in-the-drawer (talk) 16:13, 28 June 2015 (UTC)

This can be done in either Lua or template syntax (using the string manipulation templates). Lua will probably be a little bit more work to implement, but would certainly be easier to read. For the record, what is "{{{1}}}" above, is that meant to be "IT\ICCU\MIL\0205279" in the example? Dragons flight (talk) 16:47, 28 June 2015 (UTC)
Yes. Knife-in-the-drawer (talk) 18:14, 28 June 2015 (UTC)
Ok, I went a bit over-the-top and wrote two different templates for this. :P The first is at Module:Sbn (and Template:Sbn) and the second is at Template:Sbn/sandbox. The sandbox is the wikitext version - this uses Module:String#match to parse the template parameter. The module uses string.match for the parsing. (They do the same thing - Module:String#match is just a wikitext interface for string.match.) These both use Lua patterns, which you will need to understand to see how this works. The module also uses string.format to join the strings together, but you could just as easily use .., Lua's concatenation operator. ('ab' .. 'cd' will give you the string "abcd".) The sandbox is simpler, but it isn't as elegant if something goes wrong - try putting a bad code into both of them to see what I mean. And please ask if you have any questions about the code. — Mr. Stradivarius ♪ talk ♪ 00:45, 29 June 2015 (UTC)
(module:SBN) Christian75 (talk) 15:08, 29 June 2015 (UTC)

An Infobox feature request is ready for Lua encoding[edit]

I've made a feature request for module:Infobox here: Infobox, header-when-not-empty. Basically, it adds a parameter |headern-cond= that only shows when its section contains data (consider how it has to be encoded today).

There is some good support, it's 'only' needs to be encoded in Lua. Anyone interested? -DePiep (talk) 18:51, 28 June 2015 (UTC)

Nothing wrong with doing that, but it's not that hard to code this in template syntax. Just test for whatever parameter that fills data under the header is essential to that little section being there. Or test for all of the parameters in that section if they're all of equal weight, and the section and its heading need to be there for any of them but be absent if they're all empty. Template:Infobox cat breed takes the latter approach, and hides the breed standards section for landraces or traditional breeds with no published standards. I agree it's more efficient in Lua, though.  — SMcCandlish ¢ ≽ʌⱷ҅ʌ≼  14:45, 18 July 2015 (UTC)

String parsing: Extract number (with decimal, neg) from arbitrary text[edit]

I'm taking first baby steps in Lua. What I want to do I could do in minutes in perl with regular expressions, but even the online Lua help materials aren't much help, because Scribunto's requirements for how it does thing are so rigid. I've barely gotten it to return (sometimes) a valid result for the first part of this I'm testing (just numerals, and without need to <nowiki>...</nowiki> the input). I haven't figure out how to use boolean operators with Lua string manipulation, as in "look for this OR that".

All I really need right now is a tiny one-function module that can be invoked directly to do the following to one string of input:
  • Drop all characters other than -, . and numerals.

My immediate need for this is converting sloppy CSS-related input like -10px, 2.2em; or even style="font-size: 70% ;" to -10, 2.2, or 70, respectively, and return that number. My assumption is that I should be able to wrap the input in <nowiki>...</nowiki>, in case someone inputs something daft like |style=font-size: 70%, with a | symbol, but that's not essential. The #1 use would be conversion of percentile values in particular, like 70%, to bare numbers, like 70. Need a variant function that divides the result by two and rounds to two decimal places, but I'm sure I can add that myself after I see how the main part works.

A more advanced version that could be invoked directly or required by other modules, might do something like this:
  • For any arbitrary input, trim all leading material until it hits any of:
    • a numeral; or
    • a . followed by a numeral; or
    • a - or the proper negative/minus glyph followed by either of:
      • a numeral, or by
      • . followed by a numeral
  • then retain that character;
  • proceed to next character, and retain it if matches either:
  • a numeral, or
  • a ., unless one was matched earlier;
  • repeat until that fails (i.e. a second . is found, or any other non-numeral is found);
  • then trim everything after that;
  • and do all this for multiple values.
A later super-badass version could add:
  • recognize ^ or e, and x or × or * when found in a context that indicates an exponent (23.5x10^8), and a few other such cases (e.g. characters used to indicate a truncated long/endless decimal)
  • find multiple numbers per input string, and separate them by something (e.g. a space or a comma) depending on which function is invoked

Any help on the basic request at the top would be much appreciated, the rest is more of a "what if...?" proposition.  — SMcCandlish ¢ ≽ʌⱷ҅ʌ≼  22:33, 17 July 2015 (UTC)

I'm no Lua expert, so here is a starting point that could probably be done better:
function ExtractNumbers(str)
    local tResult = {}
    str:gsub( "%-?[%d%.]+", function(x) table.insert(tResult, x) end)
    return tResult
and call with
myvar = ExtractNumbers('style="font-size: 70% ;"')
The result is a table, so to get just the first value
myvar = ExtractNumbers('style="font-size: 70% ;"')[1]
Hope this helps. -- WOSlinker (talk) 23:04, 17 July 2015 (UTC)
@WOSlinker: Thanks. I didn't get your pattern to work (plugging it in threw errors), but after a nap, I figured out what I was trying to do, at Module:StripToNumbers, other than it needs more precise pattern matching. Its talk page outlines the remaining problem (short version: input like font-size: 10% will come out as -10%. Your use of a table is probably more robust, too; I just did enough to get the result I immediately needed for a specific application, conversion of "70%" to "70", and the ability to halve it. Anyone should feel free to tinker further.  — SMcCandlish ¢ ≽ʌⱷ҅ʌ≼  14:51, 18 July 2015 (UTC)
@SMcCandlish: Changing onlyNumber = (string.gsub(theString, "[^%d%.-]", "")) to onlyNumber = (string.match(theString, "%-?[%d%.]+")) would help with the problem with font-size: 10%. It would also just retun the first set of numbers rather than all numbers in theString. -- WOSlinker (talk) 16:36, 18 July 2015 (UTC)
[Genuflect] Very nice! Worked like a charm.  — SMcCandlish ¢ ≽ʌⱷ҅ʌ≼  16:54, 18 July 2015 (UTC)
@SMcCandlish: If you want to access data passed to Lua in nowiki tags, you can do so with mw.text.unstripNoWiki, like this. — Mr. Stradivarius ♪ talk ♪ 03:51, 19 July 2015 (UTC)
Nice. Thanks for fixing that in the template.  — SMcCandlish ¢ ≽ʌⱷ҅ʌ≼  18:55, 23 July 2015 (UTC)

Breadcrumb version of Module:Multi-section link[edit]

Please see Template talk:Multi-section link#Development request. Short version: Need variant that links each sectional name, not just the last one.  — SMcCandlish ¢ ≽ʌⱷ҅ʌ≼  18:55, 23 July 2015 (UTC)