Module talk:Template invocation: Difference between revisions

Content deleted Content added
Editor-hostile wikitext: better to correct that template as well
No edit summary
Line 60:
:: Fine with me. [[User:Pppery|* Pppery *]] [[User talk:Pppery|<sub style="color:#800000">it has begun...</sub>]] 15:46, 13 July 2024 (UTC)
:: {{Hidden ping|andrybak}}It's a tolerable workaround, but in principle, templates that use positional parameters must use {{tlx|trim}} in cases where leading/trailing spaces don't have any functional purpose (besides readability) but can break the result. — [[User:Mikhail Ryazanov|Mikhail Ryazanov]] ([[User talk:Mikhail Ryazanov|talk]]) 20:23, 13 July 2024 (UTC)
 
== Title clash in a few edge cases ==
 
{{editprotected}}
 
Currently, the "name" function returns the wrong result in three edge cases. Two of these are relatively straightforward to fix, but the third is more tricky:
# A page name like "Template:User:Theknightwho" should not return "User:Theknightwho", but instead should remain "Template:User:Theknightwho".
# The function should throw an error if it receives a title with an interwiki prefix (e.g. {{fr:foo}}) or one which starts with "#" (e.g. {{#foo}}), since both of these generate valid title objects, but neither can be used with template invocations.
# (Harder to fix): when a template has the same name as a magic word, the "Template:" prefix is required in order to disambiguate it.
I've done a full implementation at [[wikt:Module:template parser#L-218]], but it relies on loading data for point 3, which might not be desirable to import to Wikipedia. However, points 1 and 2 are pretty simple to deal with. I've drafted a new version of the function below, incorporating fixes for both of them:
<syntaxhighlight lang=lua>
function p.name(title)
if type(title) == 'string' then
title = mw.title.new(title)
if not title or #title.prefixedText > 0 and #title.interwiki == 0 then
error("invalid title in parameter #1 of function 'name'", 2)
end
elseif type(title) ~= 'table' or type(title.getContent) ~= 'function' then
error("parameter #1 of function 'name' must be a string or a mw.title object", 2)
end
if title.namespace == 10 then
local text = title.text
local check = mw.title.new(text, 10)
-- Exclude the prefix, unless we have something like "Template:Category:Foo", which can't be abbreviated to "Category:Foo".
return check and mw_title.equals(title, check) and text or title.prefixedText
elseif title.namespace == 0 then
return ':' .. title.prefixedText
else
return title.prefixedText
end
end
</syntaxhighlight>
[[User:Theknightwho|Theknightwho]] ([[User talk:Theknightwho|talk]]) 20:07, 31 August 2024 (UTC)