Wikipedia talk:Lua

Page contents not supported in other languages.
From Wikipedia, the free encyclopedia
(Redirected from Wikipedia talk:Lua/To do)

Template:Category disambiguation[edit]

Can {{category disambiguation}} be made to use Lua? There has to be a better way than using 50 conditionals. Thanks! HouseBlaster (talk · he/him) 22:49, 13 February 2024 (UTC)[reply]

Templates are not converted to Lua simply because they are repetitious. They can be, but I don't think I would here. Izno (talk) 00:29, 14 February 2024 (UTC)[reply]
@Izno If I'm honest, this seems like the perfect sort of template to convert to Lua, for three reasons:
  • It would massively reduce the amount of code, because it would be a simple loop. Repetitious code is a maintenance headache and annoying to edit.
  • There's no arbitrary upper limit to the number of arguments which can be passed into it (currently 100).
  • Tons of template calls like this is very slow by comparison, which can impact large pages.
Theknightwho (talk) 14:29, 14 February 2024 (UTC)[reply]
Is anyone really going to want to disambiguate more than 50 different categories? Will there ever a time when {{category disambiguation}} is not the only thing on the page, and thus it's performance on large pages matters? I don't think so. Given the average edit rate of one edit per two years I don't think maintainablility is an issue - more time is spent discussing and doing the conversion than will ever be saved by it having been done. * Pppery * it has begun... 14:49, 14 February 2024 (UTC)[reply]
These are all benefits of Lua in general. Do they aid this specific template? No, not really. There isn't anything complex about it such that the positive of the different language outweigh the negatives of the different language.
That said, given this feedback, maybe you should try converting it. Simple templates are the best ones to start with. Izno (talk) 18:10, 14 February 2024 (UTC)[reply]
You're replying to Theknightwho, author of one of the most complex Lua modules I have ever seen. I don't think they need simple templates to start with :) — Martin (MSGJ · talk) 21:17, 14 February 2024 (UTC)[reply]
I think that comment was meant to be addressed to HouseBlaster, despite its indentation. * Pppery * it has begun... 21:43, 14 February 2024 (UTC)[reply]
No, it was intended for Theknightwho, I just 1) didn't realize they were a different person from House, and 2) don't know everyone's credits. :) I'm not stopping anyone from converting anything to Lua if they want to take the time for it (WP:VOLUNTEER), I just am singularly convinced that this template doesn't need it for already stated reasons. But I am happy to redirect the suggestion to House. Izno (talk) 22:01, 14 February 2024 (UTC)[reply]
@Pppery @Izno You make good points, and I had only looked at the raw code when I made my earlier comment. That being said, I've done a sandboxed version at Module:Sandbox/Theknightwho/Category disambiguation, which is designed to replace the Cmbox call in the template, since this seemed relatively straightforward. The one difference is that throws an error if you supply fewer than four parameters (since that's the minimum to specify two categories), but that can easily be taken out. Theknightwho (talk) 01:14, 15 February 2024 (UTC)[reply]

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]

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: https://ln.wikipedia.org/w/index.php?title=Cat%C3%A9gorie:Pages_avec_des_erreurs_de_script&action=edit&redlink=1

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

{{YearCategory
|__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 = mw.title.new(link)
	if page.exists == true then
		if page.redirectTarget then
			local redir = page.redirectTarget.fullText
						or page.redirectTarget.prefixedText
			return "[[" .. redir .. "|" .. text .. "]]"
		else
			return "[[" .. link ..  "|" .. text .. "]]"
		end
	else
		return text
	end
end

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:
  • mw.title.new() 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 mw.title.new() 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 = mw.title.new(pageStr)
	if pageObj.redirectTarget then
		local redir = pageObj.redirectTarget.fullText
					or pageObj.redirectTarget.prefixedText
		return "[[" .. redir .. "|" .. text .. "]]"
	elseif pageObj.exists == true then
		return "[[" .. pageStr ..  "|" .. text .. "]]"
	else
		return text
	end
end
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]