Wikipedia talk:Lua
![]() | To help centralize discussions and keep related topics together, all Wikipedia:Lua subpages' talk pages and Help talk:Lua redirect here. |
![]() | The contents of the Wikipedia:Lua/Requests page were merged into Wikipedia talk:Lua on 5 February 2016. For the contribution history and old versions of the redirected page, please see its history. |
![]() | Wikipedia:Lua/To do was nominated for deletion on 21 May 2015. The result of the discussion was keep. |
This page has archives. Sections older than 90 days may be automatically archived by ClueBot III when more than 1 section is present. |
Localization help needed for Module:Protection banner
[edit]Extended content
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
Hello, I want to import Module:Protection banner on kowiki. (We are still using old Template:Pp-meta) I want both English and Korean parameters and values to work for compatibility, but it seems that the module is a little bit complicated. I checked the modules in other languages, but they all used only English parameters and values. Is it possible to make an i18n table for aliases?--Namoroka (talk) 18:11, 24 January 2025 (UTC)
- you may want to look at he:Module:תרגום יחידה. Basically it's a wrapper that accepts a module, function and a translation table names, and using the translation table invokes the function with a clone of original frame, augmented with translated params. For more details please read the documentation at the top. If i correctly understood your requirements, i think this module matches them to a T.
- Peace קיפודנחש (aka kipod) (talk) 15:51, 25 January 2025 (UTC)
- Thank you for your answer. I will take a look at the code you provided.--Namoroka (talk) 15:52, 25 January 2025 (UTC)
- oh, i forgot the disclaimer: i wrote it and tested it, but as far as i know, it is not actually used in production - I'm not very good at promoting my work. I can't guarantee you'll be happy, but i stil give it at least 82.73% chance.
- Peace קיפודנחש (aka kipod) (talk) 15:55, 25 January 2025 (UTC)
- An 82.73% probability is good enough for me. lol --Namoroka (talk) 15:57, 25 January 2025 (UTC)
- oh, one more thing: if you do adopt it, please let me know - i haven't bothered to generate WD item for this module, but if it gets migrated to other wikis i should.
- Peace קיפודנחש (aka kipod) (talk) 16:20, 25 January 2025 (UTC)
- I checked your module, and while it can be used for translating other modules, it seems that it cannot be used for Module:Protection banner since the value also needs to be translated.--Namoroka (talk) 11:22, 27 January 2025 (UTC)
- An 82.73% probability is good enough for me. lol --Namoroka (talk) 15:57, 25 January 2025 (UTC)
- Thank you for your answer. I will take a look at the code you provided.--Namoroka (talk) 15:52, 25 January 2025 (UTC)
Module calling Wikidata to list European parties' seats in various institutions
[edit]Hi,
Total newbie to Lua here (but happy to discover)! Here, I asked for help to make a template that would grab information from Wikidata (using the wikidata module and expressions using that module) based on two arguments (the name of the institution and the name of a party -- or some wildcards).
@Ahecht kindly responded with a proposal, but saying that the conditional structure I was looking for would be better achieved in Lua. Since this looked like a good opportunity to make something better and to learn, I thought I would come here and ask for support.
I won't recap the whole proposal here for the sake of brevity (but the above link should be clear enough, hopefully), but I welcome ideas and pointers! Thanks a lot in advance. Julius Schwarz (talk) 19:32, 17 March 2025 (UTC)
- Any ideas? Julius Schwarz (talk) 06:01, 21 March 2025 (UTC)
- Your code from the original thread would work. Lua is just written with the long form of if statements, so "else if", not "elif" like in python. Also, unlike python, you have to use the word "end" at the end of the if statement, it will not take into account the indentation like python does.
- You could just put your original code with some minor adjustment like that into a function, return the function, save it into the Module namespace and use it with {{#invoke:<script name>|<function name>|<arguments>}}. <script name>, <function name> and <arguments> should all be replaced and filled in. Snævar (talk) 10:39, 21 March 2025 (UTC)
- Thanks for your answer @Snævar. I decided to follow your advice and get started with a module. However, as I try to build it step by step, I first try without arguments, just to see whether the module can spit out a basic call to Wikidata. I have tried using something like
return {{#invoke:wd|property|Q55|P395}}
(and variations on this), but to no avail. The error message says that arguments are expected near "|". Couldn't find the answer online; any pointers? In the meantime, I put quotation marks around the code, otherwise it would not let me save it. See Module:SeatsEUPPs Julius Schwarz (talk) 12:06, 22 March 2025 (UTC)
- Thanks for your answer @Snævar. I decided to follow your advice and get started with a module. However, as I try to build it step by step, I first try without arguments, just to see whether the module can spit out a basic call to Wikidata. I have tried using something like
{{wikidata}}
is a template so cannot be '#invoked'. But, you can expand templates from a lua module. You might want to try this:require ('strict'); --[[--------------------------< S E A T S >-------------------------------------------------------------------- expands {{wikidata}} templates: {{wikidata|property|Q208242|P1410|P208=Q8880}} → '11' {{wikidata|property|Q55|P395}} → 'NL' ]] local function seats (frame) return frame:expandTemplate ({title='wikidata', args = {'property', 'Q208242', 'P1410', P208 = 'Q8880' }}); -- returns '11' -- return frame:expandTemplate ({title='wikidata', args = {'property', 'Q55', 'P395'}}); -- returns 'NL' end --[[--------------------------< E X P O R T S >---------------------------------------------------------------- ]] return { seats = seats, }
- I don't know if Module:Wd exports any functions and I don't care to spend any time looking. But, the above should be sufficient to get you unstuck.
- —Trappist the monk (talk) 14:20, 22 March 2025 (UTC)
- Fantastic @Trappist the monk, thank you so much! I will continue working based on that. Really appreciate the help! Julius Schwarz (talk) 14:26, 22 March 2025 (UTC)
- Hi @Trappist the monk, so I have worked on that and am progressively getting somewhere -- well, at my own pace :) One more question, if I may: if you check the current code of Module:SeatsEUPPs, you will see that I did a first if check testing on the name of institutions when no party is given as argument (in this case the function should return the number of seats of the institution itself). So far so good.
- However, with four institutions and twelve parties, plus cases where you want the sum of all party seats or the number of seats not occupied by parties, it is going to have a lot of lines and feel very repetitive.
- Would it be possible to create one constant per party and per institution and define it as that entity's qID from Wikidata, and then find a way to use that in the
frame:expandTemplate
? - For instance, one would call
{{#invoke:SeatsEUPPs|seats|EC|EPP}}
and this would return the result offrame:expandTemplate ({title='wikidata', args = {'property', EPP, 'P1410', P208 = EC }})
, with EPP previously defined as Q208242 and EC as Q8880. Julius Schwarz (talk) 21:36, 22 March 2025 (UTC)- See Module:SeatsEUPPs and your sandbox. We should probably move that module out of main module space and into your module sandbox until it is ready to use.
- —Trappist the monk (talk) 22:23, 22 March 2025 (UTC)
- Umm, I said
move [Module:SeatsEUPPs] ... into your module sandbox
(emphasis added). To be safe, you should copy-and-paste your latest version from Module:Module sandbox to overwrite the content of Module:SeatsEUPPs. Then move Module:SeatsEUPPs to your sandbox at Module:Sandbox/Julius Schwarz/SeatsEUPPs. Don't bother to leave a redirect behind. This will correctly preserve the edit history and allow you to work on your module without someone coming along and commandeering the public sandbox. - —Trappist the monk (talk) 00:10, 23 March 2025 (UTC)
- Wow, talk about above and beyond the call of duty! Thanks a lot for the help, that's really great. -- oops, I just saw that I hadn't sent this. As for the sandbox, yeah it did seem weird and I was doing regular copy-pastes. Will move now. Julius Schwarz (talk) 00:13, 23 March 2025 (UTC)
- OK, this is what I get so far: Module:Sandbox/Julius Schwarz/SeatsEUPPs
- It seems to work -- see User:Julius Schwarz/sandbox (the last empty line is normal).
- Only improvement now would be a for function to easily sum the values for all parties, but it's too late for me to keep trying :) Julius Schwarz (talk) 00:29, 23 March 2025 (UTC)
- Side question @Trappist the monk: how come when I try to get a value for independent politicians in institutions other than EUCO (and, therefore, that has none recorded) do I simply get nothing instead of the planned error? (see test query in my sandbox) Should it not return "unknown party", since IND is not in the
parties_t
table? Julius Schwarz (talk) 08:24, 23 March 2025 (UTC){{wikidata}}
apparently returns an empty string whenparties_t['IND']
getsnil
. That then becomes:return institutions_t[institution] and '' or 'error - EP-COR unknown party'
- The 'and' portion of that expression, to Lua, is 'true' so the returned value is the empty string. I rewrote that bit of the code.
- I added simple error detection: must have the institution positional parameter and must be known. May have party positional parameter and if present must be known. Created function
sum()
to sum the total number of seats for the institution. - Created
pseudo_parties_t{}
for those keywords that aren't parties:ALL
,NONE
,IND
. I wonder about that last one. - —Trappist the monk (talk) 16:00, 23 March 2025 (UTC)
- Thanks a lot for the help, @Trappist the monk! It is quite a few changes and I still have work to do, but I will check that first thing tomorrow. All I can say for now is that the regular call with party/institution seems broken (see:
{{#invoke:SeatsEUPPs|seats|EC|EPP}}
→ {{#invoke:Sandbox/Julius Schwarz/SeatsEUPPs|seats|EC|EPP}}). Maybe I find more details tomorrow morning. Julius Schwarz (talk) 21:45, 23 March 2025 (UTC) – nowiki'd broken invoke to avoid glaring red error message;—Trappist the monk (talk) 17:02, 24 March 2025 (UTC)- fixed.
- —Trappist the monk (talk) 21:58, 23 March 2025 (UTC)
- Wonderful @Trappist the monk!
- I just had a go at it to include the ability to give out shares of seats (relative to a given institution), and not just absolute values.
- Happy if you got a second look to make sure it was done well! Test cases seem to be in order (User:Julius Schwarz/sandbox). Julius Schwarz (talk) 09:39, 24 March 2025 (UTC)
- Then, can we safely use this to make the module global? (after moving it to a proper module page and creating a Wikidata entity) Julius Schwarz (talk) 09:46, 24 March 2025 (UTC)
- Also added "thisparty" as a special parameter to make less expensive calls to Wikidata when using the module from the page of a European political party. Julius Schwarz (talk) 11:42, 24 March 2025 (UTC)
- I don't see any glaring errors. I do think that
seats()
is getting to big. It seems to me that the share and not-share portions of that function should be split off into their own separate functions.seats()
should handle input error detection, reporting, and function dispatching. There should be a common error messaging function and an external ~/config module that holds translatable error messages and perhaps other constant data (institutions_t
,parties_t
, etc). - For consistency with the rest of the world (if that is important), might then want to rename
seats()
→main()
; name the not-share function →seats()
; name the share function →seats_share()
. - As for Synchronizer, my confidence in a tool that has no obvious documentation approaches zero. Documentation should be obvious. The tool's author should be ashamed. I suck a documentation, but even I, were I the author of the tool, could provide at least a scrap of more-or-less meaningful documentation.
- —Trappist the monk (talk) 14:24, 24 March 2025 (UTC)
- Ahahah. I actually like the documentation you put in the code, felt clear to me. As for the documentation of the module, I hope it is clear enough -- I tried to make it as clear as possible.
- I will try and split off the functions as you suggest. Julius Schwarz (talk) 15:30, 24 March 2025 (UTC)
- Is this what you had in mind, then? @Trappist the monk Julius Schwarz (talk) 15:56, 24 March 2025 (UTC)
- I moved Module:Sandbox/Julius Schwarz/SeatsEUPPs to Module:SeatsEUPPs over your copy/paste update so that the edit history is preserved. Your sandbox module now redirects to the live module.
- Yeah, that sort of split is what I was thinking about. I'll add i18n error messaging support.
- —Trappist the monk (talk) 17:02, 24 March 2025 (UTC)
- Thanks, I had first done a move, but it didn't quite work for some reason and the edit history didn't seem super important, but thanks nonetheless. Also I am not sure I understand I understand your last sentence but that sounds helpful! I will try and get it ranked and then start using it. The Multilingual part will come later. Julius Schwarz (talk) 19:40, 24 March 2025 (UTC)
- See Module:SeatsEUPPs/sandbox, Module:SeatsEUPPs/config and the tests that you deleted (why did you do that) from User:Julius Schwarz/sandbox.
- —Trappist the monk (talk) 20:46, 24 March 2025 (UTC)
- Just reverted the deletion; I was confused by that -- it seems like a duplicated and I blamed it on the move-copy/paste-move or something. Now I see the difference. I was actually going to create the sandbox and testcastes subpages but you beat me to it :) Is there anything that I can do here to help? Julius Schwarz (talk) 21:36, 24 March 2025 (UTC)
- We should probably discontinue this discussion in favor of another at Template talk:SeatsEUPPs (the various module talk pages should redirect there). Other things to do:
- create Template:SeatsEUPPs/sandbox so that it uses Module:SeatsEUPPs/sandbox
- create Template:SeatsEUPPs/testcases
- create module testcases; see mw:Multilingual Templates and Modules#Testcases
- make sure that at least one and probably both of the above testcases have a test for each of the error messages
- the description text above functions
seats()
andseats_share()
(in both the live and sandbox modules) needs to be replaced with something meaningful. You might want to go through all of the comments in those modules and make sure that they say what they should say. - look at the error message text in Module:SeatsEUPPs/config and make sure that those messages say what they should say
- for the 'share' outputs, should the output have a '%' suffix?
- I have in mind a few cosmetic tweaks to make and then I will likely step away. If you have questions, you know where I am.
- —Trappist the monk (talk) 22:00, 24 March 2025 (UTC)
- Thanks for the roadmap, I will get to it today! Julius Schwarz (talk) 06:03, 25 March 2025 (UTC)
- We should probably discontinue this discussion in favor of another at Template talk:SeatsEUPPs (the various module talk pages should redirect there). Other things to do:
- Just reverted the deletion; I was confused by that -- it seems like a duplicated and I blamed it on the move-copy/paste-move or something. Now I see the difference. I was actually going to create the sandbox and testcastes subpages but you beat me to it :) Is there anything that I can do here to help? Julius Schwarz (talk) 21:36, 24 March 2025 (UTC)
- Thanks, I had first done a move, but it didn't quite work for some reason and the edit history didn't seem super important, but thanks nonetheless. Also I am not sure I understand I understand your last sentence but that sounds helpful! I will try and get it ranked and then start using it. The Multilingual part will come later. Julius Schwarz (talk) 19:40, 24 March 2025 (UTC)
- I don't see any glaring errors. I do think that
- Thanks a lot for the help, @Trappist the monk! It is quite a few changes and I still have work to do, but I will check that first thing tomorrow. All I can say for now is that the regular call with party/institution seems broken (see:
- Umm, I said
- Fantastic @Trappist the monk, thank you so much! I will continue working based on that. Really appreciate the help! Julius Schwarz (talk) 14:26, 22 March 2025 (UTC)
Completely AI generated script
[edit]Hi, I was working on a medal tally or table. after each row it needed to be shown the sum of total medal. It was very tricky using wikitext, but i tried my best. then i seek help from an AI chatBot. I have zero expertise on Lua, yet i tried to implement lua on my template. I don't know if it's ok or not. if someone could see Module:Sandbox/KEmel49 and see through human eye. My template test is ongoing on User:KEmel49/sandbox 6.––kemel49(connect)(contri) 00:49, 22 March 2025 (UTC)
- That is very good code. Spaces are used for indents instead of standard tabs, but the formatting, variable names, and logic are all perfect. The "local gold =" line is not quite correct. The "
or 0
" should be outside the parentheses. As it is, if someone puts "x" or "-" for the number of gold, the module would crash when it tries to add gold (which is now nil). Same for silver and bronze. Rather than outputting "0", some may prefer an en dash but zero would be correct. I haven't fully studied the HTML but a quick look suggests it's good. Johnuniq (talk) 01:29, 22 March 2025 (UTC)- @Johnuniq, Should i move that to Module:Medal tally?––kemel49(connect)(contri) 03:58, 22 March 2025 (UTC)
- Probably. Later I could fix those points I mentioned. First, you might like to look around to see if there is already something to do this. Although that doesn't really matter because if there is, someone will eventually tell you. Johnuniq (talk) 05:03, 22 March 2025 (UTC)
- @Johnuniq, Should i move that to Module:Medal tally?––kemel49(connect)(contri) 03:58, 22 March 2025 (UTC)