Wikipedia talk:Lua

Methods
If x is a string why can I use  as a shortcut for   but if y is a table then I can't use   as a shortcut for  ? &mdash; Martin (MSGJ · talk) 13:01, 22 March 2024 (UTC)
 * This is because string objects in Lua have a metatable where the  points to the the   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)
 * Does that mean if you use  you could then use  ? —  Jts1882 &#124; talk 14:29, 22 March 2024 (UTC)
 * Not unless  is a function.
 * is defined as.
 * For the latter to work,  has to be a function.
 * A working example:
 * Johnuniq (talk) 05:33, 23 March 2024 (UTC)
 * 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
 * &mdash; Martin (MSGJ · talk) 09:34, 25 March 2024 (UTC)
 * No, there is no good way to alter the way  works. I recommend using standard   and living with it. However, it is possible to muck around with something like this for a single table.
 * Search for  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  . Johnuniq (talk) 04:24, 26 March 2024 (UTC)
 * That seems like it could be worth splitting off into its own module&hellip; jlwoodwa (talk) 07:18, 24 June 2024 (UTC)

Process template parameter in Lua before MediaWiki does its thing
I'm working on a module that let's the user to use some predefined placeholders to add categories/text. For example, on the "Category:144 births" category they can add the following template

And it will add the 140s births, Births by year, 2nd-century births and 144 beginnings categories to the page and will display a header with "In this category you can find people who were born on 144." content. The code is at hy:Մոդուլ:ԺամանակիԿատեգորիա and an example of usage at hy:Կատեգորիա:1944 ծնունդներ. It work fine except when I want to use those placeholders inside a template or an {{#if: block. As I understand, mediawiki parses the text before sending it to the Lua module. Is there a way to change this? ԱշոտՏՆՂ (talk) 01:26, 21 April 2024 (UTC)
 * {{replyto|ԱշոտՏՆՂ}} Why not simply copy our Template:Birth year category header and its underlying modules? -- Red rose64 &#x1f339; (talk) 07:24, 21 April 2024 (UTC)
 * The module I made works without typing the parameters and it is very generic, the same module can be used for Category:Buildings and structures completed in the 1850s and it adds all the necessary categories (e.g. Buildings and structures completed in the 19th century, 1850s establishments1850s works, 1850s architecture, Buildings and structures by decade of completion). Basically, as long as the category title contains a year/decade/century, the module will work and will create the navigation box too, see hy:Կատեգորիա:1990-ականների կառուցումն ավարտված շենքեր և կառույցներ as an example. The only thing I can't do with this is using wikitext logic inside the header. For example, I can't write {{#ifexist:__YEAR__ in film|See the main article at __YEAR__ in film}} but for some reason I can do " See the main article at __YEAR__ in film " and it will replace __YEAR__ with the appropriate year. I feel like it does the ifexist check before my script can replace the placeholder __YEAR__ with an actual number. ԱշոտՏՆՂ (talk) 07:45, 21 April 2024 (UTC)
 * ԱշոտՏՆՂ, I assume you'd need to wrap it in nowiki tags and then unstrip it in the module, to stop the wikitext being processed. — Qwerfjkl  talk  11:19, 21 April 2024 (UTC)
 * For example, Module:Demo does it, as can be seen in function . —⁠andrybak (talk) 01:05, 22 April 2024 (UTC)
 * An alternative approach is to use nested templates, e.g.  to implement the decade handling.  You may need {{((}}PAGENAME{{))}} or something derived from it as the template parameter. Certes (talk) 09:34, 22 April 2024 (UTC)
 * ... which is done by the {{tl|Title year}} family of templates, heavily used in the {{c|Chronology category header templates}}.
 * Sometimes this is done in two steps to avoid extra calculations. For example, Template:Sport clubs (dis)established in YYYY category header and Template:Sport clubs (dis)established in YYYY category header/core. The "core" template does include a conversion from {{param|year}} to a decade using {{Tlx|DECADE}}. —⁠andrybak (talk) 15:44, 22 April 2024 (UTC)

"too many expensive function calls"
I wrote the following function:

It works on a small scale, but on a larger scale I'm told. Can anyone help me find something cheaper that still fulfills the goals of: Eievie (talk) 03:35, 23 April 2024 (UTC)
 * If the page exists, link to that. If the page doesn't exist, don't link to anything.
 * If the page is a redirect, link to the redirect target.
 * It can't be done. Each check requires a page to be examined and that is expensive. MediaWiki protects itself from denial-of-service attacks and other bad ideas that would affect performance. A workaround, if really necessary, would be to have a bot periodically do the checking and update the page of links but that won't be suitable for many cases. Johnuniq (talk) 04:44, 23 April 2024 (UTC)
 * This doesn't ping the same response, though. What makes it different?
 * Eievie (talk) 05:44, 23 April 2024 (UTC)
 * Are you sure? HELP:ifexist (which Link if exists uses) says it has the same limit of 500 although multiple checks of the same title are counted as one. Johnuniq (talk) 06:08, 23 April 2024 (UTC)
 * Yes, I'm sure it actually works that way in my testcases. I agree it conceptually seems odd, that's why I'm asking. Eievie (talk) 06:41, 23 April 2024 (UTC)
 * I tried it at User:Johnuniq/sandbox2 and only 500 calls are allowed. Johnuniq (talk) 08:06, 23 April 2024 (UTC)
 * Yeah, I had this issue at Module:ATP rankings and its related template, in that checking to see if every redirect existed was causing issues. We ended up hard-coding known outliers into a separate submodule and had the main module check that first before doing any sort of #ifexist. Primefac (talk) 11:16, 23 April 2024 (UTC)
 * I'm doing this at Module:Unicode chart/sandbox. There are 2 types of links needed:
 * The header link
 * Don't need to check page existence (I'm pretty sure wikipedia has pages for all the blocks)
 * Need to link to the redirect target, so that the bolding feature works correctly (ie. if the template was being included on the page Greek and Coptic, the link would turn bold if the link said  but not if it said  )
 * The unicode character links
 * Need to check of page exists (many characters do not have pages)
 * Linking to redirect target would be ideal, but isn't absolutely necessary
 * Right now I'm using Module:Redirect for the former and for the latter. This does work (see testcases here). It creates the links I need and doesn't create a "too many expensive function calls" error.
 * I just tried to create the above helper function to cover both, and I'm confused as to why that got that error when this doesn't. My understanding is that:
 * is the expensive part
 * once you've already done that, checking  and   are both cheap
 * Module:Redirect and both do the   call
 * So how is the above function more expensive than either and Module:Redirect? Eievie (talk) 16:14, 23 April 2024 (UTC)
 * No, creating a title object alone doesn't increase the expensive count, nor does redirectTarget. So you can check redirectTarget (which returns false if the page doesn't exist) before checking exists, which does increase it. Nardog (talk) 16:20, 23 April 2024 (UTC)
 * You're saying that  is the expensive part? So ordering the function this way would be cheaper?
 * I'm trying that now, and it's still creating Eievie (talk) 16:34, 23 April 2024 (UTC)
 * Yes, that should at least reduce the expensive parser function count, even if it still exceeds the limit on a particular page. You can compare them in the parser profiling data at the bottom of the edit form. Nardog (talk) 16:50, 23 April 2024 (UTC)
 * Ok, I figured it out: Calling the other templates, rather than calling the functions myself, just silences the error messages. Eievie (talk) 17:34, 23 April 2024 (UTC)
 * Yes, that should at least reduce the expensive parser function count, even if it still exceeds the limit on a particular page. You can compare them in the parser profiling data at the bottom of the edit form. Nardog (talk) 16:50, 23 April 2024 (UTC)
 * Ok, I figured it out: Calling the other templates, rather than calling the functions myself, just silences the error messages. Eievie (talk) 17:34, 23 April 2024 (UTC)

Discussion at MediaWiki talk:Scribunto-doc-page-header
You are invited to join the discussion at MediaWiki talk:Scribunto-doc-page-header § Category:Module documentation pages. —⁠andrybak (talk) 09:27, 14 May 2024 (UTC)

parsing the first sentence
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)


 * Module:Excerpt is at least one or two users' attempt to date. Izno (talk) 02:23, 3 June 2024 (UTC)
 * Using  gets the first paragraph of the Lion article (minus infoboxes, templates, etc). Add no to get plain text. It should be straightforward to get the first sentence from that.  —  Jts1882  &#124; talk 11:49, 3 June 2024 (UTC)

Editing the metatable of a child frame
Module:Params offers the possibility to map or rename parameters using either helper templates or modules or parser functions. Currently, in the case of  and , 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  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)


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

Looking for an existing module that takes a page name and converts it into wikitext
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)? 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)


 * I am not sure I understood exactly what you are looking for, but for getting the wikitext of a page there is  – you should use the double colon if the page is in the main namespace, as in  . That text transcluded in this way can then be passed as a parameter to a template or a module (and this includes, if that's what you are looking for). And so
 * 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)
 * I think that was what I was looking for, thanks! Gonnym (talk) 11:04, 6 June 2024 (UTC)
 * I am glad that helped! --Grufo (talk) 11:16, 6 June 2024 (UTC)
 * Isn't "takes a page name and converts it into wikitext" just ? Izno (talk) 15:58, 6 June 2024 (UTC)
 * Yes, but I was asking how to access it from wikitext and not Lua. Gonnym (talk) 16:00, 6 June 2024 (UTC)
 * That sounds like transclusion: {&#123;:Page name}}. Certes (talk) 17:12, 6 June 2024 (UTC)
 * Already solved above. Gonnym (talk) 17:56, 6 June 2024 (UTC)
 * Already solved above. Gonnym (talk) 17:56, 6 June 2024 (UTC)

Looking for module that processes black/white text.
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 ]]  (talk • contribs) 04:20, 11 June 2024 (UTC)


 * How would the module decide which of the two colors to output? Izno (talk) 06:25, 11 June 2024 (UTC)
 * I saw this post on Stack Overflow. Could this be implemented in Lua?   [[User:CanonNi ]]  (talk • contribs) 09:36, 11 June 2024 (UTC)
 * Infobox television season uses  (simplified it), if this is can help you. Gonnym (talk) 09:36, 11 June 2024 (UTC)
 * @Gonnym this should work, thanks!   [[User:CanonNi ]]  (talk • contribs) 09:38, 11 June 2024 (UTC)

Executing and scraping results from an advanced search url
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  given this url:

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

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)
 * 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)
 * Thanks. I suspected as much, but wasn't sure. I'll look into other avenues. Mathglot (talk) 08:06, 6 July 2024 (UTC)