Wikipedia talk:Lua

Page contents not supported in other languages.
Source: Wikipedia, the free encyclopedia.
(Redirected from
Wikipedia:Lua requests
)

How to make multilangual parameters in Module:Navbox

Hello!

I'm translated Navbox module into Kazakh Wikipedia, but have one issue. We've already have a kk:Module:Navbox, but I want to upgrade it. Additionally in the some templates we also used English version of Navbox (english parameters). So I need to add english versions of parameters (Aliases?) to new kk:Module:Шолғы exactly to kk:Module:Шолғы/configuration. I've already ask this question on talk page of Module:Navbox, but nobody replied, that's why I'm writing here Thanks--Amangeldi Mukhamejan (talk) 15:15, 27 October 2023 (UTC)[reply]

@Amangeldi Mukhamejan: i only saw this now, sorry about that. i hope it's not too late.
a while ago i wrote a utility module that enables you to use any module from another language with localized parameter names. the idea is simple - i'm sure you won't have any trouble grasping it and figuring out how it's done. see he:יחידה:תרגום יחידה
in a nutshell: for each module you want "translated", you create a "translation" module (in retrospect i guess it could have been a JSON rather than module). then you call the utility module, passing it the target module, the target function, the name of the "translation module", and any parameter you want to pass directly.
the utility module clones the frame (such that the "parent" of the clone is the real parent, not the utility module), it then enriches the "args" and the parent's args with the translated parameters, and then it calls the target module/function with this enriched frame.
note that i said "enriches", not "replace". this means that the caller can use either the original param name _or_ the translated name, which i think is part of your requirements, you can mix and match - some of the parameters can be the translated ones and others can be the original ones
there is some rudimentary documentation as a comment at the top of the module (the doc is not very long, but it's still 50% of the whole thing - the code is short and sweet and pretty easy to understand - no magic. i wrote it per some request on hewiki, and tested it to verify it works, but eventually the requestor decided not to use it, so you can say it's not "tried and tested", only "tested"...
this of course is not specific to "navbox": once you install it, you can use it for any module, at the cost of creating "translation module" for each imported module you want to use with native language parameter names.
if you do decide to use it, i'll be grateful if you can let me know, and of course, if you find bugs let me know too.
peace. קיפודנחש (aka kipod) (talk) 04:24, 26 January 2024 (UTC)[reply]

Regex question

I am using :gmatch('{{{([^|]+)') to find valid parameters in a template's code. It is not working quite right.

For example the code contains {{{event}}} {{{event-type|edit-a-thon}}} but it seems to be matching with event}}} {{{event-type but not the event-type which I require. — Martin (MSGJ · talk) 13:00, 11 January 2024 (UTC)[reply]

[^|]+ will go as far as it can to capture text before running into a pipe, which is why the first capture is so long and contains text from both event and event-type. gmatch then continues from the pipe it got stopped at last time so won't pick event-type up since its beyond it at this point. I'd recommend adding } inside of that negated list, which should prevent it from over-capturing event (and therefore pick up event-type afterwards too). Aidan9382 (talk) 13:31, 11 January 2024 (UTC)[reply]
I'm guessing here, but try "-" instead of "+" in the capture. You could also try escaping characters that might have another meaning (e.g.{ and | which would need escaping in regex proper). —  Jts1882 | talk  13:36, 11 January 2024 (UTC)[reply]
For some reason I didn't get an edit warning message and missed the reply above. —  Jts1882 | talk  13:38, 11 January 2024 (UTC)[reply]
Apparently - is an alias for *. That would not help here — Martin (MSGJ · talk) 14:06, 11 January 2024 (UTC)[reply]
Perfect, thanks! — Martin (MSGJ · talk) 14:03, 11 January 2024 (UTC)[reply]
Another alternative might be to gmatch the whole parameter and then parse that. In the debug console I get these results using string.match() in place of string.gmatch():
=string.match ('{{{event}}} {{{event-type|edit-a-thon}}}', '{{{([^}]+)}}}')event – this would be the first string.match() result
=string.match ('{{{event-type|edit-a-thon}}}', '{{{([^}]+)}}}')event-type|edit-a-thon – and this would be the second string.match() result
Trappist the monk (talk) 14:39, 11 January 2024 (UTC)[reply]
Beware that the parameter default often contains braces in a nested parameter, magic word or template call, e.g. {{{display_text|{{{title}}}}}} or {{{page_name|{{PAGENAME}}}}. In some very clever templates, they even crop up in the parameter name itself. Certes (talk) 14:50, 11 January 2024 (UTC)[reply]

Escape character

I have a string with a "#" character. When I use the string it produces a numbered list, but I just want to use it as a string. I have tried mw.text.nowiki without success. I have also tried using \u0023 but it just converts it back to #. Any other suggestions? — Martin (MSGJ · talk) 12:01, 9 February 2024 (UTC)[reply]

You can see the issue at the top of Category:WikiProject Women in Red meetup 2017 articles. It is supposed to be producing Category:WikiProject Women in Red #1day1woman articles but that link is not even working so perhaps it is impossible to have "#" in a page name? — Martin (MSGJ · talk) 12:01, 9 February 2024 (UTC)[reply]
Seems to me the problem isn't that # isn't escaped but that a line break is inserted before it where it shouldn't be. Nardog (talk) 12:09, 9 February 2024 (UTC)[reply]
Seems I just need to remove that character because the category name is not valid anyway — Martin (MSGJ · talk) 12:12, 9 February 2024 (UTC)[reply]
Oh, it's a hashtag, not a section link. Yeah, that's impossible. Nardog (talk) 12:16, 9 February 2024 (UTC)[reply]
Page names cannot contain # due to a MediaWiki restriction documented in Wikipedia:Page name#Technical restrictions and limitations. # is reserved for dividing the page name from a following anchor (typically a section header), e.g. Wikipedia talk:Lua#Escape character. Certes (talk) 22:13, 9 February 2024 (UTC)[reply]

As Nardog pointed out above, Lua modules seem to return strings which begin with "#" in an unexpected way. Compare below. My sandbox module just returns the string '#1day1woman' and you can see it behaves differently — Martin (MSGJ · talk) 10:38, 10 February 2024 (UTC)[reply]

  • a#1day1womanb - look no line break
  • a{"suggested":true,"description":"Assess the \"quality\" of the article","label":"class","example":"C","suggestedvalues":["Stub","Start","C","B","A","List","GA","FA","FL"]}b - working now
This is probably the same issue as was experienced at Template talk:R § Space before colon in pop-up, where the # gets interpreted as defining a list item since it was the start of the string returned by the module. You can use mw.text.nowiki or manually encode it depending on your circumstance to get it to display normally. Aidan9382 (talk) 11:46, 10 February 2024 (UTC)[reply]
Thanks Aidan, yes that works as expected — Martin (MSGJ · talk) 14:24, 10 February 2024 (UTC)[reply]

Need help with edge case in my Lua Module

I wanted to port the following templates over to the Afrikaans Wikipedia: Template:Millenniumbox, Template:Centurybox, Template:Decadebox.

After an initial look at how they worked, I was overwhelmed by the sheer amount of nested templates that were used by these templates. I decided to instead copy the look of the templates, but to use Lua Modules instead for the backend logic.

Porting Template:Millenniumbox and Template:Centurybox over to Afrikaans Wikipedia using Lua Modules was easy enough for me and I am happy with the results.

However I am stuck on porting Template:Decadebox. This template has 2 edge cases which do not exist in the Millenniumbox or Centurybox. There is no 0th millennium nor 0th Century. However there is a 0th Decade and 0th Decade BC. See 0s and 0s BC.

I was aware of these edge cases when I was writing my Lua Module, but still the output does not work as I expect.

Here is my ported template af:Sjabloon:Dekadeboks, and here is the Lua Module that it uses : af:Module:Dekadeboks. On the template page documentation, you can see my different "test cases" All the examples work as expected except for my 2 edge cases.

The problem seems to be caused by the Lua not being statically typed.

I am passing the selected decade as an argument. For the 0s the argument value is "0" and for the 0s BC it is "-1".

Most of my calculations read the argument to the function as a number, but for this edge case, I read it as a string instead. So that I can handle "-0" as an argument.

I know it is kinda hacky, but I can't think of a better way to do it. Furthermore when I run my functions in an external IDE, or pass it to a local Lua interpreter, the function behaves as I expect.

For the simplest example see my function to get the category for birth years: "kry_geboortes_kat_str" it is at the very end of the module. Also see my function to get the category for deaths: "kry_sterf_kat_str" which is a slightly different approach, but also doesn't work.

Please can anyone advise how I could get these functions to correctly handle "0" and "-0" as an argument? Am I missing something here?

I would greatly appreciate any feedback/advice on this issue. - Rooiratel (talk) 09:43, 13 February 2024 (UTC)[reply]

I think the 0s should be the first decade. That would resolve the hacky "-0" — Martin (MSGJ · talk) 10:41, 13 February 2024 (UTC)[reply]
That is an idea, and it does make sense in that the 0s is technically the 1st decade AD. But if I went with this method, I would have to rewrite the entire module completely. I also think it will be more confusing for users. The enwiki template also uses 0 as an argument for the page on 0s. But I will definitely consider this approach as a last resort. But I didn't actually notice that I was doing this template different from the others, so I will definitely update the docs to explicitly state what it expects the arguments to be, and what they represent (whichever solution I go with). - Rooiratel (talk) 07:29, 14 February 2024 (UTC)[reply]
It would better if you gave a couple of precise examples and say what the input is, what the output is, and what is wanted. It's not clear to me what the testcases show so please spell it out. I don't want to spend too much time at the moment so I'll just mention some points. Are you aware that -0 is equal to zero and is not negative? At line 230, num will be -0 if args[1] is '-0'. You can use tostring on negative zero and the first character will be a dash if the number was -0 (I see that line 238 covers that). Assuming you are using a normal frame and not something created artificially, frame.args[1] can only be a string (or nil if there is no args[1]). But any argument that exists can only be a string so tostring at line 229 won't help. I'll be away for many hours but will look again if the issue hasn't been resolved. Johnuniq (talk) 10:52, 13 February 2024 (UTC)[reply]
> It would better if you gave a couple of precise examples and say what the input is, what the output is, and what is wanted.
Apologies, I thought the template doc page was enough to give examples of the input and output. I do realize that it was not very clear on what the expected output should be.
> Are you aware that -0 is equal to zero and is not negative?
Yes, which is why I was attempting to use the tostring() method to handle it as a string instead of a number. I am using the "dek" variable for the string representation and "num" for the numeric representation.
> But any argument that exists can only be a string so tostring at line 229 won't help.
I thought as much, but in that case it is "converting" a string to a string, so it should still work as I expect.
My main problem at the moment is not the specific output, but I am trying to hit a conditional-block when the dek variable is equal to "0" or "-0", but the function never seems to reach it.
So essentially I need is: a function that takes a single argument which it stores in one variable as a string, and another variable as a number.
If the number variable is greater than 0 then execute a block of code.
If the number variable is less than 0 then execute a block of code.
Else if the string variable equals "0" then execute a block of code.
Else if the string variable equals "-0" then execute a block of code.
I am not able to get these last 2 conditions to execute.
I have added a very simplified test function which just returns a string saying which conditional block it reached. If you could please assist with that I would be very grateful. On my template page at the very bottom you can see the outputs, for my last 2 cases above those code blocks never execute. - Rooiratel (talk) 07:48, 14 February 2024 (UTC)[reply]
@Rooiratel: The problems is that af:Template:Dekadeboks#Test Function used to show "Reached else block" for the last two cases. In the last case, the parameter is "-0*" where I have written an asterisk where there is actually a space. It is an unnumbered parameter so MediaWiki does not trim the leading/trailing whitespace. You need code to remove the spaces which I did in an edit at af:Module:Dekadeboks. Johnuniq (talk) 09:00, 14 February 2024 (UTC)[reply]
Thank you so much @Johnuniq! I knew it had to be something stupid like that. At least now I will never forget to trim out any whitespace from the arguments. I can apply the same fix to the rest of the functions, and it should resolve all outstanding issues I have with this Module. - Rooiratel (talk) 09:22, 14 February 2024 (UTC)[reply]

A new template parser

Hi - I mostly do work on Wiktionary, but wondered if anyone had a use for a powerful recursive backtracking template parser which I've put together, which is designed to scrape large amounts of template data from pages very quickly: wikt:Module:template parser. It's (very loosely) based on mwparserfromhell, and builds a node tree of objects on the page that can then be traversed in order to retrieve data. The two available functions are described in the module, but - in short - parseTemplate is a function that you feed a rawstring template into, and it'll return the template name and arguments, while findTemplates is an iterator that returns data about each template it finds in the input text. Some features:

  • It can handle arbitrary nesting of templates ({{ }}), arguments ({{{ }}}), headings, wikilinks, HTML comments, parser tags and include tags, and has been designed to be as accurate to the native PHP parser as possible. This includes replicating some of the bugs in the native parser, to ensure full consistency. With one caveat (see below), I haven't yet been able to find a testcase it fails, even with really extreme or bizarre inputs.
  • HTML comments and the <noinclude>, <includeonly> and <onlyinclude> tags are all handled correctly. By default, it assumes that the input text should be treated as transcluded, for the purpose of the include tags. This can be changed with a parameter.
  • It's extremely fast and lightweight for what it is: e.g. wikt:人 scrapes data from hundreds of other pages to retrieve Chinese transliterations, which requires calling this module every time. By far the biggest drain on resources in that is the unavoidable time taken by mw.title's :getContent() method, not the parsing itself (which can be seen in the parse profile on that page).
  • The caveat is that it doesn't (yet) have special handling for parser tags like <ref>. It knows that they're there, but it doesn't yet know the various special ways that each one needs to be handled. In some cases this is very straightforward (e.g. <nowiki>) and in some cases not (e.g. <gallery>), so support for these will take some time. At the moment, anything between such tags is treated as raw text (i.e. as though it were in nowiki tags).

I'm reasonably confident it's the most powerful wiki markup parser that has yet been written in Lua (though I'm happy to be proven wrong!). Do let me know if you think this could come in handy for anything, and I'm happy to help in designing any implementations. Theknightwho (talk) 16:21, 13 February 2024 (UTC)[reply]

Template:Category disambiguation

Can {{category disambiguation}} be made to use Lua? There has to be a better way than using 50 conditionals. Thanks! HouseBlaster (talk · he/him) 22:49, 13 February 2024 (UTC)[reply]

Templates are not converted to Lua simply because they are repetitious. They can be, but I don't think I would here. Izno (talk) 00:29, 14 February 2024 (UTC)[reply]
@Izno If I'm honest, this seems like the perfect sort of template to convert to Lua, for three reasons:
  • It would massively reduce the amount of code, because it would be a simple loop. Repetitious code is a maintenance headache and annoying to edit.
  • There's no arbitrary upper limit to the number of arguments which can be passed into it (currently 100).
  • Tons of template calls like this is very slow by comparison, which can impact large pages.
Theknightwho (talk) 14:29, 14 February 2024 (UTC)[reply]
Is anyone really going to want to disambiguate more than 50 different categories? Will there ever a time when {{category disambiguation}} is not the only thing on the page, and thus it's performance on large pages matters? I don't think so. Given the average edit rate of one edit per two years I don't think maintainablility is an issue - more time is spent discussing and doing the conversion than will ever be saved by it having been done. * Pppery * it has begun... 14:49, 14 February 2024 (UTC)[reply]
These are all benefits of Lua in general. Do they aid this specific template? No, not really. There isn't anything complex about it such that the positive of the different language outweigh the negatives of the different language.
That said, given this feedback, maybe you should try converting it. Simple templates are the best ones to start with. Izno (talk) 18:10, 14 February 2024 (UTC)[reply]
You're replying to Theknightwho, author of one of the most complex Lua modules I have ever seen. I don't think they need simple templates to start with :) — Martin (MSGJ · talk) 21:17, 14 February 2024 (UTC)[reply]
I think that comment was meant to be addressed to HouseBlaster, despite its indentation. * Pppery * it has begun... 21:43, 14 February 2024 (UTC)[reply]
No, it was intended for Theknightwho, I just 1) didn't realize they were a different person from House, and 2) don't know everyone's credits. :) I'm not stopping anyone from converting anything to Lua if they want to take the time for it (
WP:VOLUNTEER), I just am singularly convinced that this template doesn't need it for already stated reasons. But I am happy to redirect the suggestion to House. Izno (talk) 22:01, 14 February 2024 (UTC)[reply
]
@Pppery @Izno You make good points, and I had only looked at the raw code when I made my earlier comment. That being said, I've done a sandboxed version at Module:Sandbox/Theknightwho/Category disambiguation, which is designed to replace the Cmbox call in the template, since this seemed relatively straightforward. The one difference is that throws an error if you supply fewer than four parameters (since that's the minimum to specify two categories), but that can easily be taken out. Theknightwho (talk) 01:14, 15 February 2024 (UTC)[reply]