Wikipedia talk:Lua

(Redirected from Wikipedia talk:Lua/Requests)
Latest comment: 18 days ago by DotCoder in topic Legislationuk / Legislationlistuk

Request for Lua code review: New language module

edit

Reposting this village pump post for visibility: Wikipedia:Village_pump_(technical)#Request_for_Lua_code_review:_New_language_module

Feedback welcome on the talk page here.

--Nonabelian (talk) 15:27, 25 July 2024 (UTC)Reply

Is there an expensive-parser-function-count counter?

edit

It would be very useful to know how much overhead remains at any given time during execution.   ~ Tom.Reding (talkdgaf)  14:20, 10 August 2024 (UTC)Reply

Not that I know of. It is interesting that there is a function mw.incrementExpensiveFunctionCount() to increment the count but none (documented) to fetch the current count. I suspect that mw.incrementExpensiveFunctionCount() is used internally by MediaWiki functions to bump the counter when an expensive function is executed.
Trappist the monk (talk) 14:48, 10 August 2024 (UTC)Reply
There is the extremely ugly hack Module:Preview expense, but that's almost certainly not what you want. * Pppery * it has begun... 00:27, 11 August 2024 (UTC)Reply
If you are willing to look further than lua, there is parser_function_count.py in pywikipediabot which counts ALL parser functions. In theory, that could be changed to count expensive parser functions. Then you need another script to get all templates used on a page. That would result in a list of pages with their maximum possible expensive parser function counts. (some of those parser functions are guarded with if statements, which the script would not take account for) Snævar (talk) 09:01, 17 August 2024 (UTC)Reply
You could also do the same with getContent() in lua, with the same limitations. Snævar (talk) 09:03, 17 August 2024 (UTC)Reply
Thanks, but I'm looking for something like mw.getExpensiveFunctionCount() that I can easily query inside a lua loop to know when to break out of it.   ~ Tom.Reding (talkdgaf)  10:41, 17 August 2024 (UTC)Reply

Category detection (using new feature!)

edit

After 11 years in Phabricator purgatory, T50175 has now been completed, and Lua can now detect the categories used on a page. Would someone be able to implement this at {{If in category}} to make it less expensive/less error-prone/substable? Sdkbtalk 15:23, 16 August 2024 (UTC)Reply

Thanks for letting us know. Sounds as though it could be useful — Martin (MSGJ · talk) 16:29, 22 August 2024 (UTC)Reply

Problem with escape character

edit

This is from Module:Chessboard:

res = mw.ustring.gsub( res,'\| \|', '| |' )

This is being identified as an error when you try to save it, but not exactly sure how to fix. — Martin (MSGJ · talk) 16:28, 22 August 2024 (UTC)Reply

Use % as the Lua escape character.  —  Jts1882 | talk  16:34, 22 August 2024 (UTC)Reply
Umm, pipe isn't a special character in lua patterns so does not need to be escaped. See mw:Extension:Scribunto/Lua_reference_manual#Patterns.
Trappist the monk (talk) 16:39, 22 August 2024 (UTC)Reply
If you look at the code in the module you will see
Error: [243:31] invalid escape sequence near '\|'
Perhaps it is the pipe which needs escaping — Martin (MSGJ · talk) 06:18, 23 August 2024 (UTC)Reply
It seems the syntax highlighter is pointing out that the escape sequence doesn't make any sense (it's expecting something like \120 or \n), but it's still legal code, and just gets interpreted as | since a pipe has no special meaning when escaped. Aidan9382 (talk) 08:01, 23 August 2024 (UTC)Reply
Is there anyway this can be coded which prevents the warning? At the moment you get a warning every time you try to save the page — Martin (MSGJ · talk) 08:04, 23 August 2024 (UTC)Reply
Just remove the \, since "\|" and "|" are the same in lua (e.g. try printing it). Aidan9382 (talk) 08:09, 23 August 2024 (UTC)Reply
That would render the gsub completely redundant/useless, so maybe this is not doing what it was supposed to do ... — Martin (MSGJ · talk) 08:19, 23 August 2024 (UTC)Reply
Have just realised that the function that contains this appears to be completely unused, and it is also not documented. So it is simplest if I just remove this function from the module and the error disappears — Martin (MSGJ · talk) 08:26, 23 August 2024 (UTC)Reply
Sorry Aidan, just noticed your edit to the sandbox. I had not noticed the extra space between the pipes, so that makes sense. If it turns out the function is being used, then I will put your version back in — Martin (MSGJ · talk) 08:30, 23 August 2024 (UTC)Reply
Did you try using %| as I suggested above?  —  Jts1882 | talk  08:27, 23 August 2024 (UTC)Reply
I think I did, but it doesn't make the syntax highlighter any happier — Martin (MSGJ · talk) 08:31, 23 August 2024 (UTC)Reply
It gets rid of the error messages on the lines for me.  —  Jts1882 | talk  08:46, 23 August 2024 (UTC)Reply
(Lua patterns are not regex.) Izno (talk) 19:07, 22 August 2024 (UTC)Reply

Legislationuk / Legislationlistuk

edit

Module:Legislationuk is used by Template:Legislationuk is used by Template:Legislationlistuk List of acts of the Parliament of Northern Ireland does not fit into the module there are a couple aspects that I am not sure on how too change

The main thing i am struggling with is that the the regnal citations for uk acts last happened in 1962, so that's how it's been set up but the regnal citation for NI acts last happened in 1942. When I say "regnal citation" I mean, "10 & 11 Geo. 5. No. 1" or "14 Geo. 6. c. 1" or "13 & 14 Geo. 5. c. 7 (N.I.)".

The module uses data from Module:Legislationuk/data.

The citation happens at line 124, I think.

The pseudocode for the idea I am trying to implement is something like changing ``year > 1962`` to ``year > 1962 or (year > 1942 and jurisdiction = "northern ireland")``. DotCoder (talk) 00:57, 20 September 2024 (UTC)Reply

Mainly I just don't know how to adjust the module in the correct way to apply the citation system it uses for the NI acts properly. DotCoder (talk) 00:59, 20 September 2024 (UTC)Reply
Sorry, I'm not going anywhere near that mess. The author(s) couldn't be bothered to document the code (shame. shame. shame.), couldn't be bothered to use meaningful variable names, couldn't be bothered to organize all of that text in a data module.
Trappist the monk (talk) 14:40, 20 September 2024 (UTC)Reply
If it were documented, I wouldn't be asking for help. DotCoder (talk) 16:01, 20 September 2024 (UTC)Reply