Jump to content

Wikipedia talk:Lua

Page contents not supported in other languages.
From Wikipedia, the free encyclopedia
(Redirected from Wikipedia:LRQ)

Methods

[edit]

If x is a string why can I use x:lower() as a shortcut for string.lower(x) but if y is a table then I can't use y:concat() as a shortcut for table.concat(y)? — Martin (MSGJ · talk) 13:01, 22 March 2024 (UTC)[reply]

@MSGJ: This is because string objects in Lua have a metatable where the __index points to the the string table in the standard library. Tables, by default, do not have a metatable (presumably as programmers often use custom metatables for tables in their programs). To unpack that explanation, you need to know how metatables work; there is a pretty good explanation in chapter 13 in the Programming in Lua book. Also there is a little bit more detailed treatment of the string library metatable here. — Mr. Stradivarius ♪ talk ♪ 13:51, 22 March 2024 (UTC)[reply]
Does that mean if you use setmetatable(t, y) you could then use t:concat()? —  Jts1882 | talk  14:29, 22 March 2024 (UTC)[reply]
Not unless t.concat is a function.
t:concat() is defined as t.concat(t).
For the latter to work, t.concat has to be a function.
A working example:
t = { 'abc', 'def', 'z' }
y = { __index = { concat = function(x) return string.reverse(table.concat(x)) end } }
setmetatable(t, y)
z = t:concat()  -- z = 'zfedcba'
Johnuniq (talk) 05:33, 23 March 2024 (UTC)[reply]
Thanks both for your replies. A lot to process there, but looks very useful/interesting. Is there an easy way to do the following:
  • Associate a particular table with the standard table library
  • Associate all tables by default with the standard table library
— Martin (MSGJ · talk) 09:34, 25 March 2024 (UTC)[reply]
No, there is no good way to alter the way table works. I recommend using standard table.concat and living with it. However, it is possible to muck around with something like this for a single table.
t = { 'def', 'abc', 'A' }
mt = { __index = { concat = table.concat, sort = table.sort } }
setmetatable(t, mt)
a = t:concat()  -- 'defabcA'
t:sort()
b = t:concat()  -- 'Aabcdef'
Search for Collection at Module:Age to see how I have sometimes defined a simple list. It's very weird to follow but Module:IPblock has more examples includings :sort(). Johnuniq (talk) 04:24, 26 March 2024 (UTC)[reply]
That seems like it could be worth splitting off into its own module… jlwoodwa (talk) 07:18, 24 June 2024 (UTC)[reply]

parsing the first sentence

[edit]

Does anybody know a parsing method to reliably get the first sentence of an article on a Lua module, removing templates such as infoboxes completely? -1ctinus📝🗨 00:13, 3 June 2024 (UTC)[reply]

Module:Excerpt is at least one or two users' attempt to date. Izno (talk) 02:23, 3 June 2024 (UTC)[reply]
Using {{excerpt|Lion|paragraph=1|only=paragraphs|hatnote=no}} gets the first paragraph of the Lion article (minus infoboxes, templates, etc). Add |links=no to get plain text. It should be straightforward to get the first sentence from that.  —  Jts1882 | talk  11:49, 3 June 2024 (UTC)[reply]

Editing the args metatable of a child frame

[edit]

Module:Params offers the possibility to map or rename parameters using either helper templates or modules or parser functions. Currently, in the case of mapping_by_invoking and renaming_by_invoking, while iterating, a new child frame gets created for each parameter (#1, #2). This means that if the parameters to map or rename are more than about a hundred the code breaks, because, as the Scribunto-Lua manual says, “The number of frames that may be created at any one time is limited”.

I have tried to solve the problem in Module:Params/sandbox, but all my attempts at keeping only one child frame throughout the loop failed (#3, #4, #5, #6). The reason is that once the child frame object gets created I can no longer edit its .args metatable. Yet, I would need to do that, because each module invocation needs to receive different parameters.

Testcases are available at Module:Params/testcases/tmaps and Module:Params/testcases/tmaps sandbox. Any idea on how to solve the problem and being able to map more than 100 parameters using a module? --Grufo (talk) 15:41, 3 June 2024 (UTC)[reply]

Update. I have mentioned the problem at MediaWiki. --Grufo (talk) 12:09, 4 June 2024 (UTC)[reply]

Looking for an existing module that takes a page name and converts it into wikitext

[edit]

Is there an existing Lua module like that either (a) takes a page name and converts it into wikitext or (b) takes a page name and regex and returns the result text (something similar to Module:String2#findpagetext)? Don't want to write a new module if one already exists and I thought something like this exists but I can't find it. Gonnym (talk) 09:20, 6 June 2024 (UTC)[reply]

I am not sure I understood exactly what you are looking for, but for getting the wikitext of a page there is {{msgnw}} – you should use the double colon if the page is in the main namespace, as in {{msgnw::Richard Feynman}}. That text transcluded in this way can then be passed as a parameter to a template or a module (and this includes {{#invoke:string|match}}, if that's what you are looking for). And so
{{#invoke:string|match|s={{msgnw::Richard Feynman}}|pattern=known%s+for%s+([^.]+)|nomatch=}}
yields
his work in the [[path integral formulation]] of [[quantum mechanics]], the theory of [[quantum electrodynamics]], the physics of the [[superfluidity]] of supercooled [[liquid helium]], as well as his work in [[particle physics]] for which he proposed the [[parton model]]
--Grufo (talk) 11:01, 6 June 2024 (UTC)[reply]
I think that was what I was looking for, thanks! Gonnym (talk) 11:04, 6 June 2024 (UTC)[reply]
I am glad that helped! --Grufo (talk) 11:16, 6 June 2024 (UTC)[reply]
Isn't "takes a page name and converts it into wikitext" just getContent? Izno (talk) 15:58, 6 June 2024 (UTC)[reply]
Yes, but I was asking how to access it from wikitext and not Lua. Gonnym (talk) 16:00, 6 June 2024 (UTC)[reply]
That sounds like transclusion: {{:Page name}}. Certes (talk) 17:12, 6 June 2024 (UTC)[reply]
Already solved above. Gonnym (talk) 17:56, 6 June 2024 (UTC)[reply]

Looking for module that processes black/white text.

[edit]

Hi. I'm looking for a module that can output 000000 or FFFFFF depending on the color code inputted. Is one available? Thanks. '''[[User:CanonNi]]''' (talkcontribs) 04:20, 11 June 2024 (UTC)[reply]

How would the module decide which of the two colors to output? Izno (talk) 06:25, 11 June 2024 (UTC)[reply]
I saw this post on Stack Overflow. Could this be implemented in Lua? '''[[User:CanonNi]]''' (talkcontribs) 09:36, 11 June 2024 (UTC)[reply]
{{Infobox television season}} uses {{Greater color contrast ratio|{{If empty|{{{bg_colour|}}}|#CCCCFF}}|black|white}} (simplified it), if this is can help you. Gonnym (talk) 09:36, 11 June 2024 (UTC)[reply]
@Gonnym this should work, thanks! '''[[User:CanonNi]]''' (talkcontribs) 09:38, 11 June 2024 (UTC)[reply]

Executing and scraping results from an advanced search url

[edit]

Is a module capable of executing an WP:Advanced search via a url param that is passed to it, and then processing the output of the search? I would like to be able to pass this url to a Module, have it execute the search, and return the total number of results by scraping it out of the content. For example, it should return value 4 given this url:

https://en.wikipedia.org/w/index.php?search=Draft%3A+hastemplate%3AOKA++hastemplate%3AAfC_submission%2Fdraft&title=Special:Search&profile=default&fulltext=1

The resulting Html contains the following which looks easily scrapable, if a module can access it:

<div class="results-info" data-mw-num-results-offset="0" data-mw-num-results-total="4">Results <strong>1 – 4</strong> of <strong>4</strong></div>

The module should be capable of handling any url that represents an advanced search, and find the total results. Is that possible? Mathglot (talk) 07:04, 6 July 2024 (UTC)[reply]

No. Searching uses Special:Search and modules cannot access special pages which are not based on wikitext. I don't know what API may be available for use by JavaScript. Johnuniq (talk) 08:01, 6 July 2024 (UTC)[reply]
Thanks. I suspected as much, but wasn't sure. I'll look into other avenues. Mathglot (talk) 08:06, 6 July 2024 (UTC)[reply]

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]