Wikipedia talk:Lua

Active discussions
 To do
mw:reference manual

Script to fetch the nutshell text of a given pageEdit

Per this discussion at the VPT (start reading at "related idea"), we'd like to have a module that can take as input a given page and return the text from the {{Nutshell}} on that page if one exists (and return "null" or something if the page doesn't have a nutshell). Would that be possible to create without too much effort? {{u|Sdkb}}talk 21:14, 4 April 2020 (UTC)

Scripts and lua are not the same thing. At least I don't see them as the same thing. A script runs on your browser. Lua runs when MediaWiki compiles wikitext into the html that your browser displays as an article. If you are looking for a script, then this place is not the right place to be looking.
If you are looking to create shortcut templates that have tooltips where the text of the tooltip is taken from a {{nutshell}} template in the target page, I think that can be done. But shortcuts often link to sections that don't have {{nutshell}} and for for those that do the search has to be constrained to that section and you have to look for {{nutshell}} and all of its redirects:
{{Essay in a nutshell}}
{{Guideline in a nutshell}}
{{Guideline one liner}}
{{In a nutshell}}
{{Naming convention in a nutshell}}
{{Policy in a nutshell}}
{{Policy proposal in a nutshell}}
{{Proposal in a nutshell}}
Trappist the monk (talk) 21:41, 4 April 2020 (UTC)
See Template:Template parameter value * Pppery * it has begun... 21:43, 4 April 2020 (UTC)
Something else to consider, wiki markup in a nutshell template. For example, this is the parameter value at WP:EP
Improve pages wherever you can, and do not worry about leaving them imperfect. Preserve the value that others add, even if they "did it wrong" (try to fix it rather than [[WP:Deletion policy|delete it]]).
Converting that to a titletip:
[[WP:EP|<span title="Improve pages wherever you can, and do not worry about leaving them imperfect. Preserve the value that others add, even if they 'did it wrong' (try to fix it rather than [[WP:Deletion policy|delete it]]).">WP:EP</span>]]
For the purposes of example, I manually changed the double quotes within the nutshell text to single quotes within the span; those kinds of things will also need to be handled.
Trappist the monk (talk) 22:59, 4 April 2020 (UTC)
@Sdkb and Trappist the monk: I've started this module. See Module:Nutshell, and User:Guywan/Test2, where it is used. Needs some work yet, I'm no Lua expert. guywan (talkcontribs) 14:49, 5 April 2020 (UTC)
We always seem to forget that whitespace around template names is allowed. Here is a table of {{nutshell}} redirect patterns with whitespace; works well with ipairs():
local redirects_nutshell_patterns = {
	'{{%s*[Ee]ssay in a nutshell%s*|',
	'{{%s*[Gg]uideline in a nutshell%s*|',
	'{{%s*[Gg]uideline one liner%s*|',
	'{{%s*[Ii]n a nutshell%s*|',
	'{{%s*[Nn]aming convention in a nutshell%s*|',
	'{{%s*[Pp]olicy in a nutshell%s*|',
	'{{%s*[Pp]olicy proposal in a nutshell%s*|',
	'{{%s*[Pp]roposal in a nutshell%s*|',
I recently wrote a function to strip all wikilinks from a template. You might find this useful because who knows how many wikilinks will appear in nutshell text:
local function wikilink_strip (template)
	for wikilink in template:gmatch ('%[%b[]%]') do								-- get a wikilink
		if wikilink then
			template = template:gsub ('%[%b[]%]', '__57r1P__', 1);				-- install a marker
			if wikilink:match ('%[%[.-|(.-)%]%]') then
				wikilink = wikilink:match ('%[%[.-|(.-)%]%]');					-- extract label from complex [[link|label]] wikilink
				wikilink = wikilink:match ('%[%[(.-)%]%]');						-- extract link from simple [[link]] wikilinks
			wikilink = escape_lua_magic_chars (wikilink);						-- in case there are lua magic characters in wikilink
			template = template:gsub ('__57r1P__', wikilink, 1);				-- replace the marker with the appropriate text

	return template;
The if wikilink then may not be necessary. I think it is a left-over artifact from an earlier version of the code that did not work.
The above doesn't answer the what to do about templates found in a nutshell template (preprocess them, I suppose). What to do about html? I don't think that bullet points are supported in tooltips so string them all together? What about prefixing? Use the default title text or the default title text modified by |title=? Ignore?
Consider using find() to find a particular nutshell pattern then, with the start position returned from find(), use match ('%b{}', start) to get the whole template.
Is it really necessary to use the ustring functions?
Find a better module name. Module:Nutshell implies that it is used to implement all or part of {{nutshell}}; it is not.
Trappist the monk (talk) 15:42, 5 April 2020 (UTC)
@Trappist the monk: Thanks for your suggestions. I'd say strip anything that isn't sentence-like.
Find a better module name. I'm not passionate about the name. You are welcome to give it another one.
Is it really necessary to use the ustring functions? Not anymore :)
escape_lua_magic_chars() Where is this defined, and how important is it?
Regards, guywan (talkcontribs) 22:19, 5 April 2020 (UTC)
--[[--------------------------< E S C A P E _ L U A _ M A G I C _ C H A R S >----------------------------------

Returns a string where all of lua's magic characters have been escaped.  This is important because functions like
string.gsub() treat their pattern and replace strings as patterns, not literal strings.


local function escape_lua_magic_chars (argument)
	argument = argument:gsub("%%", "%%%%");										-- replace % with %%
	argument = argument:gsub("([%^%$%(%)%.%[%]%*%+%-%?])", "%%%1");				-- replace all other lua magic pattern characters
	return argument;
If I can remember, I think that I added this to wikilink_strip() because something that was being stripped had a percent-encoded space (%20) in it that caused lua to choke because it saw that as a capture reference that it had not captured. You may never encounter that problem. I don't remember why I separated the % escape from the rest of the lua magic characters. I've had no reason to change it because it has been working since I don't remember when.
What do you intend to call User:Guywan/Test2 when it becomes a template? Use that name for this module?
Trappist the monk (talk) 23:07, 5 April 2020 (UTC)
@Guywan and Trappist the monk: Glad to see progress being made here! I would prefer for the template to be able to be called using {{WP|BOLD}}. But maybe the WP should just be a shortcut and the template itself should be given a better name. Pppery, I'm having some trouble getting {{Template parameter value}} to work correctly. Do you or anyone else know what I'm doing wrong at User:Guywan/Test2? {{u|Sdkb}}talk 22:26, 7 April 2020 (UTC)
Template:Template parameter value is a bit finnicky. First, you have to pass the redirect target to it, rather than the redirect itself. Secondly, it cares about the capitalization of {{nutshell}} on the target page. For instance,
{{template parameter value|{{#invoke:Redirect|main|WP:BOLD}}|nutshell|1|1|1|1}}

Please feel free to make improvements to Wikipedia in a fair and accurate manner. works, but
{{template parameter value|{{#invoke:Redirect|main|WP:BOLD}}|Nutshell|1|1|1|1}}

does not. Alex 21 (the author of {{template parameter value}}), what do you think about updating the template to automatically bypass redirects and ignore the case of the first character in the template name? * Pppery * it has begun... 22:41, 7 April 2020 (UTC)
@Guywan, Trappist the monk, Pppery, and Alex 21: Okay, so checking in on where we're at, it looks like we're pretty close, since User:Guywan/Test2 currently works based on Module:Nutshell. The only issue is that, when used for pages without a nutshell, it returns a big error message, rather than just returning a link to the page without a tooltip or dotted underline, as would be preferable. Is there any easy way to get that behavior? Once that's done, I think we'll be ready to move out of userspace and into template space and start using. {{u|Sdkb}}talk 02:59, 11 April 2020 (UTC)
I hacked on Module:Nutshell for a bit. I moved the tooltip creation into the module reorganized how the nutshell template is disassembled, added support for multiple positional parameters, and support for extracting a value from |title=.
There is this one:
{{#invoke:WP | main | SIG }}WP:SIG
In the positional parameter it has [[File:OOUI JS signature icon LTR.svg|22px]] ( ) renders in the tooltip as 22px; what to do about that?
Trappist the monk (talk) 19:22, 11 April 2020 (UTC)
Code tweaked to replace the image with text: [IMAGE]. I suppose that we could parse apart the image wikilink for alt text or caption. Is it worth it? We might get some text to go in the tooltip but that text might not fit grammatically in the rest of the tooltip.
Trappist the monk (talk) 19:37, 11 April 2020 (UTC)
@Trappist the monk and Guywan: I've gone ahead and launched the template at Template:WP, since it seems to be working pretty well. For a further improvement (and looking at the code, this may already be in progress), it'd be good to have the page title written out in the tooltip instead of "this page", so that the tooltip for WP:BLP reads Biographies of living persons in a nutshell: Material about... instead of This page in a nutshell: Material about... Once you've made all the improvements you want, lmk and we can announce it at the Community bulletin board. {{u|Sdkb}}talk 07:32, 12 April 2020 (UTC)
Here are some questions that I think need answers:
  • when the nutshell holds an image, is it worth the effort to parse apart the File: or Image: wikilink to extract alt text or caption text?
  • there is code in Module:WP that looks for a stub template and if found, causes an error return. Why do we care if the target page is a stub?
  • should this module accept MOS:... or other namespace abbreviations? WP:LIST is a shortcut to a dab page that includes the MOS:LIST target which has a {{nutshell}} template.
Trappist the monk (talk) 17:25, 12 April 2020 (UTC)

@Trappist the monk: Re image, it seems very rare for an image to be in a nutshell, so I'd say no. The alt text might occasionally work, but it's just as (if not more) likely to be missing or incomplete, which would just add to the confusion. The current solution of "[IMAGE]" at least alerts the reader that there's an image there and that they might just want to click through.

Re stubs, it seems perfectly possible that a stub would have a nutshell, so I don't see why we'd want to exclude them from the template. Maybe figure out the WP:FENCE aspect, but if there's nothing technical making it necessary, just get rid of it.

Re other namespace abbreviations, Template:MOS, unlike Template:WP, is taken, so it'd be hard to allow {{MOS|LEAD}}, but WP:LEAD works fine, and goes to the same place.

And I'm glad to see the page titles now being included! (Thanks for coding that!) I notice that it can get a bit confusing for some, as in WP:BITE seems to be talking about biting newcomers in a nutshell (lol) with the current Wikipedia:Please do not bite the newcomers in a nutshell: Do not be... Adding quotes around the title could fix that. We could also use a period or line break to make it clearer that the link goes to the page. Together with those changes, we'd have something like "Wikipedia:Please do not bite the newcomers". In a nutshell: Do not be... I'm also on the fence about whether to we should take out the "Wikipedia:" — do you think it adds enough to be worth including?

Oh, and one thing showing up in WP:NPOV and a few other test cases is that italics and bolding aren't being reflected. Do Wikipedia tooltips have the technical capability to handle formatting like that? (If so, it might be nice to bold the article title to help it stand out a bit.) {{u|Sdkb}}talk 19:45, 12 April 2020 (UTC)

I concur that image alt text or caption text would be problematic in that, when it exists, and the likely hood of that happening is rather remote, whatever text is in the image markup won't fit grammatically with the surrounding text.
This search finds no stubs that use {{nutshell}}. Just to prove that the stub search portion of that isn't broken, this search finds about 2500 stub templates. Given these results, I'm going to delete that snippet of code. If it turns out that there is a reason for it being there, the code is in the history of the module so it can be dredged up and restored.
I was thinking more along the lines of accepting a MOS: prefixed argument ({{WP|MOS:LEAD}}) that would render as MOS:LEAD (with the tool tip and dotted underlining). It is my understanding that MOS shortcuts are preferred for the manual of style. Maybe I'll hack something into this code to see how that would work.
I've added quotes around the article title. I have not added the full stop. You did, showing it outside the quotes. That is one of the grammatical things I almost always get wrong so I don't know if the dot goes inside or outside. So I've left it off until someone confirms your usage. I've been thinking that the 'Wikipedia:' prefix should be shortened to 'WP:' in keeping with the template name: "WP:Please do not bite the newcomers" in a nutshell: Do not be....
Italic and bold markup will not work. To prove that, I did an experiment. I added <b>...</b> around the article title portion of the tooltip. Not surprisingly, html tags inside a title attribute inside a <span> confused MediaWiki so the enclosing <span>...</span> rendered as linked plain text (but the title was bold). Right now, we do nothing with the bold and italic markup so where it exists in the {{nutshell}} text, it shows up as raw markup in the tooltip. Leave it because we all know what it means, or strip it?
Trappist the monk (talk) 22:43, 12 April 2020 (UTC)
My experience elsewhere is that image handling can make a simple module very complex. Images hidden in infoboxes with unusual parameter names, non-free images with rationales for the source but not the destination, complex wikitext within captions and other gotchas can take more coding and CPU time than the module's actual purpose. Avoid if possible. Certes (talk) 22:55, 12 April 2020 (UTC)
Stub code gone.
MOS: prefixed shortcuts:
{{WP|MOS:TM}}MOS:TM not the same as {{WP|WP:TM}}WP:TM and {{WP|TM}}WP:TM
{{WP|MOS:MED}}MOS:MED not the same as {{WP|WP:MED}}WP:MED ?? {{WP|MED}}WP:MED
This facility might also be expanded to the Help namespace.
Trappist the monk (talk) 00:20, 13 April 2020 (UTC)
@Trappist the monk: Regarding the full stop, it saddens me to report this, since I'm very much #TeamInside, but per the MOS, Wikipedia style is to have it outside. And I agree that WP would be good — it's shorter than writing out Wikipedia while making it clearer that the thing in quotes is a page title than leaving it out. {{u|Sdkb}}talk 00:39, 13 April 2020 (UTC)
Support added for Help namespace and Wikipedia namespace alias Project:
Namespaces and namespace aliases are case insensitive but pseudo namespaces are treated like article names so only the first character is case insensitive. The supported pseudo namespaces are H and MOS:
Trappist the monk (talk) 15:01, 13 April 2020 (UTC)
@Trappist the monk: I've made some minor changes, mostly to the main function. I unified namespaces and pseudo_namespaces so we only have to lookup in one table. Namespace input like h, H, pRoJeCt, and mOs are 'normalised' to Help, Project, and MOS. guywan (talkcontribs) 14:27, 14 April 2020 (UTC)
I notice that, with the page previews setting turned on, uses of this template with MOS seem to be displaying a preview, but not others. Can we resolve that discrepancy? {{u|Sdkb}}talk 21:51, 14 April 2020 (UTC)
Not the fault of this module. With page previews enabled, compare:
MOS is a pseudo namespace that actually lives in mainspace. This is why:
[[mOS:MED]]mOS:MED works
[[mos:MED]]mos:MED does not work
MOS:... shortcuts are merely mainspace redirects that look like <namespace>:<title> shortcuts into another namespace. Namespace names and aliases are case insensitive and separated from the shortcut's article title with the colon. The title portion must obey the same case-sensitivity rules as any other page title:
[[wp:MED]]wp:BOLD works; nominal redirect (page ID: 1502969)
[[wp:mED]]wp:bOLD works; same redirect as above (page ID: 1502969) because of the first-character case-insensitivity rule
[[wp:med]]wp:bold works; another redirect from WP:Bold (page ID: 1502965)
[[wp:mEd]]wp:bOLd does not work; no redirect
"So that, as clear as is the summer's sun" (Shakespeare Henry V act 1, scene 2) is why there appears to be an issue, and there may be, but it is nothing that you can do anything about except discontinue support for MOS shortcuts.
Trappist the monk (talk) 00:47, 15 April 2020 (UTC)

can we look up "globalSiteId' of the wiki the code is running on?Edit

Does anyone know how to look up "globalSiteId' of the wiki the code is running on? So "enwiki" on English wikipedia, "commonswiki" for Commons, etc. I am updating c:Module:Interwiki and would like to ensure that the module will not add link to itself, even if copied to other wiki. --Jarekt (talk) 19:39, 22 April 2020 (UTC)

@Jarekt: {{#invoke:WikidataIB |globalSiteID}} → enwiki
The Lua call is mw.wikibase.getGlobalSiteId(). Cheers --RexxS (talk) 19:58, 22 April 2020 (UTC)
Thanks --Jarekt (talk) 01:26, 23 April 2020 (UTC)


Do really a resource included the Lua list here, does not use MediaWiki? :-? BoldLuis (talk) 09:15, 18 May 2020 (UTC)

It uses UseModWiki, which achieves similar results using different syntax.[1] Certes (talk) 11:28, 18 May 2020 (UTC)

Retrieving the display title valueEdit

Is there a way in Lua, magic words or regular template syntax to get the Display title value of a page (which doesn't involve reading the page content)? Wondering since its a value shown in the Page information of a page. --Gonnym (talk) 07:56, 20 May 2020 (UTC)

See phab:T154738 * Pppery * it has begun... 13:18, 20 May 2020 (UTC)
@Gonnym: Is this sufficiently different from {{FULLPAGENAME}} to cause problems for your application?
One workaround might be to use the fact that almost all articles (and some other pages) have an associated Wikidata entry that has a sitelink back to the article using its display title. So for iCarly, season 6 (Q10044351), you can use {{#invoke: WikidataIB |getSiteLink |qid=Q10044351}} to get 'ICarly (season 6)'. Leaving out the |qid= will use the connected article for the page: {{#invoke: WikidataIB |getSiteLink}} → Wikipedia talk:Lua.
Obviously it won't work on pages that don't have a connected Wikidata item. --RexxS (talk) 16:43, 20 May 2020 (UTC)
If the Display title is the desired result then the wikidata won't work because the displayed title is: iCarly (season 6) – italics and first character lowercase. Even if it were possible to get the displayed title, how would it be returned?
as wiki markup? ''iCarly'' (season 6)
as html? <i>iCarly</i> (season 6)
Trappist the monk (talk) 16:54, 20 May 2020 (UTC)
RexxS, I think maybe Wikidata might be overkill for this. The problem isn't that big, so was hoping that there might have been a simple solution. Thanks for the help guys. --Gonnym (talk) 17:11, 20 May 2020 (UTC)
  • Rough Lua code to parse the page content (definitely needs care (not that expensive (about 1.0 out of 50 MB)):
    • {{#invoke:Sandbox/Matroc/Misc|getdisplay|page=ICarly (season 6)}} (can be used with (safesubst) and getdisplay function can be modified as to error checking and whats to be output.
    • iCarly (season 6) -- result is iCarly (season 6) -- Matroc (talk) 06:54, 31 May 2020 (UTC)
You may be able to adapt Module:Template parameter value, which relies on the value being introduced by a pipe rather than a colon. However, many articles have their displaytitle modified by a template such as {{lowercase title}}, or indirectly via an infobox template per WP:ITALICTITLE. Certes (talk) 08:36, 31 May 2020 (UTC)
Thank you for further clarification - my example was a simple solution and does not cover the variety of ways a DISPLAYTILE can appear -- Matroc (talk) 01:56, 1 June 2020 (UTC)

Radio buttons for switching between historical mapsEdit

El'ad (Central Israel)
El'ad (Israel)
El'ad 2020 street map overlaid on Survey of Palestine map from 1941

Please could someone help to create radio button functionality for switching between historical maps?

Currently Module:Location map gives the ability for a reader to switch between contemporary map views using radio buttons (see example on the right). For historical maps, comparing what an area looks like today to how it looked historically, there is no template or module which can do the same. It would benefit articles about historical settlements which look very different today (for example Tenochtitlan in Mexico City, Cahokia in St. Clair County, Illinois, Londinium in London and Lutetia in Paris). The best I have been able to come up with is the gif on the right, but it has been fairly derided as overly "distracting".

Would it be possible to extract the relevant code from Module:Location map into a new "Module:Image select" or similar? It doesn't need any of the other functionality (e.g. coordinates, labels) as it can all be covered by the buttons and captions.

Onceinawhile (talk) 09:21, 24 June 2020 (UTC)

You're looking for {{switcher}} * Pppery * it has begun... 13:56, 24 June 2020 (UTC)
@Pppery: that is perfect, thank you. Onceinawhile (talk) 14:19, 24 June 2020 (UTC)
Return to the project page "Lua".