Wikipedia talk:Lua

Page contents not supported in other languages.
From Wikipedia, the free encyclopedia


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'
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]

Lua errors at ln:[edit]

ln: is a small wikipedia with less than 10 active users per month and non of them with lua knowledge. There are some lua errors in templates that were probably copy pasted in the past from fr: The pages can be seen here:

Could someone who knows lua have a look to the templates ("modèle") and give us a hand or at least some advice? Thank you for helping. Bombo (talk) 21:35, 18 April 2024 (UTC)[reply]

I've cleared all of the Lua errors from mainspace articles. Most of the template errors are due to the template being called but without parameters and will work on mainspace. * Pppery * it has begun... 23:14, 18 April 2024 (UTC)[reply]

Process template parameter in Lua before MediaWiki does its thing[edit]

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

|__DECADE__s births
|Births by year
|RD(__CENTURY__)-century births
|__YEAR__ beginnings
|header=In this category you can find people who were born on [[__YEAR__]].

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)[reply]

@ԱշոտՏՆՂ: Why not simply copy our Template:Birth year category header and its underlying modules? --Redrose64 🌹 (talk) 07:24, 21 April 2024 (UTC)[reply]
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)[reply]
ԱշոտՏՆՂ, I assume you'd need to wrap it in nowiki tags and then unstrip it in the module, to stop the wikitext being processed. — Qwerfjkltalk 11:19, 21 April 2024 (UTC)[reply]
For example, Module:Demo does it, as can be seen in function get. —⁠andrybak (talk) 01:05, 22 April 2024 (UTC)[reply]
An alternative approach is to use nested templates, e.g. {{YearCategory|{{Decade|2024}} births.. 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)[reply]
... which is done by the {{Title year}} family of templates, heavily used in the 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 {{{year}}} to a decade using {{DECADE}}. —⁠andrybak (talk) 15:44, 22 April 2024 (UTC)[reply]

"too many expensive function calls"[edit]

I wrote the following function:

local function linkToWikipage(link, text)
	local page =
	if page.exists == true then
		if page.redirectTarget then
			local redir = page.redirectTarget.fullText
						or page.redirectTarget.prefixedText
			return "[[" .. redir .. "|" .. text .. "]]"
			return "[[" .. link ..  "|" .. text .. "]]"
		return text

It works on a small scale, but on a larger scale I'm told too many expensive function calls. Can anyone help me find something cheaper that still fulfills the goals of:

  • 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.

Eievie (talk) 03:35, 23 April 2024 (UTC)[reply]

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)[reply]
This doesn't ping the same response, though. What makes it different?
frame:expandTemplate{title = 'Link if exists', args = link}
Eievie (talk) 05:44, 23 April 2024 (UTC)[reply]
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)[reply]
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)[reply]
I tried it at User:Johnuniq/sandbox2 and only 500 calls are allowed. Johnuniq (talk) 08:06, 23 April 2024 (UTC)[reply]
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)[reply]
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 Greek and Coptic but not if it said Greek and Coptic (Unicode block))
  • 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 {{Link if exists}} 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 page.exists and page.redirectTarget are both cheap
  • Module:Redirect and {{Link if exists}} both do the call
So how is the above function more expensive than either {{Link if exists}} and Module:Redirect? Eievie (talk) 16:14, 23 April 2024 (UTC)[reply]
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)[reply]
You're saying that page.exists is the expensive part? So ordering the function this way would be cheaper?
local function linkToWikipage(pageStr, text)
	local pageObj =
	if pageObj.redirectTarget then
		local redir = pageObj.redirectTarget.fullText
					or pageObj.redirectTarget.prefixedText
		return "[[" .. redir .. "|" .. text .. "]]"
	elseif pageObj.exists == true then
		return "[[" .. pageStr ..  "|" .. text .. "]]"
		return text
I'm trying that now, and it's still creating Lua error: too many expensive function calls. Eievie (talk) 16:34, 23 April 2024 (UTC)[reply]
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)[reply]
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)[reply]

 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)[reply]