Page MenuHomePhabricator

Template outputting screenfuls of Lua code in pages
Closed, InvalidPublic

Description

At the end of https://meta.wikimedia.org/wiki/Help:User_contributions and other pages using {{h:f}} on Meta, we see several pagefuls of Lua code and errors. Surely there's something wrong in the template, but this is not graceful degradation.

<p>--[=[ Not globally exposed. Internal function only.</p>
<p>language_subpages( frame, transform, options ) Parameters</p>
<pre>
   frame:     The frame that was passed to the method invoked. The first argument or the page argument will be respected.
   transform: A transform function. Example: function( basepagename, subpagename, code, langname ) end
   options:   An object with options. Example: { abort= { on=function() end, time=0.8 }  }
       Following options are available:
       abort: Aborts iterating over the subpages if one of the conditions is met. If the process is aborted, nil is returned!
           on: Function to be called if an abort-condition was met.
           cycles: The maximum number of subpages to run over.
           time: Maximum time to spend running over the subpages.
</pre>
<p>]=] function language_subpages( frame, transform, options )</p>
<pre>
   local args, pargs, options = frame.args, ( frame:getParent() or {} ).args or {}, options or {};
   local title = args.page or args[1] or pargs.page or pargs[1] or "";
   local abort = options.abort or {};
   local at, clock = type( abort.on ), os.clock();
   local ac = function()
       if  at == 'function' or ( at == 'table' and getmetatable(abort.on).__call ) then
           abort.on();
       end
   end
   local tt = type( transform );
   local page = require( 'Module:Page' );
</pre>
<pre>
   title = page.clean(title);
</pre>
<pre>
   if tt == 'function' or ( tt == 'table' and getmetatable(transform).__call ) then
       local fetch, pages, langcode, langname = mw.language.fetchLanguageName, {};
</pre>
<p>--[==[</p>
<pre>
    / \
   / | \
  /  ·  \
  ¯¯¯¯¯¯¯
  Page.subpages() no longer works because it attempted to parse the HTML content generated by
  calling the parser function "Special:Prefixindex:" which is no longer expanded in Lua but
  converted to a "stripped tag" (containing a unique identifier surrounded by ASCII DEL characters)
  representing the tag name and its parameters.
  The actual expansion of stripped tags can no longer be performed in Lua.
  Now unstripping these tags just kills ALL these tags (except "wiki" tags) instead of performing
  their expansion by running the extension code. Only MediaWiki can unstrip these tags in texts after
  they have been returned by Lua.
  For this reason, page.subpages() is now completely empty (Module:Page no longer works).
  This cannot be bypassed, except by using a Scribunto extension library if lifting the limits set by mw.unstrip.
  Note that "Special:Prefixindex:" is also costly, even if it just requires a single database query to
  get all subpages, instead of one costly #ifexist or one costly mw.title() property reading per
  tested subpage to know if it exists.
  For now there's still no reliable way to get a list of subpages, or performing queries similar to
  the <a href="/web/20150422111815/https://meta.wikimedia.org/wiki/Special:PrefixIndex" title="Special:PrefixIndex">Special:Prefixindex</a> page or list members of a category like when viewing a category page.
  Ideally, there should exist a method for such queries on Title objects returned by the mw.title library;
  but for now there's none.
  In Lua now, the only expansion possible with an immediate effect is the expansion of standard templates,
  all special tags or special pages, or parser function extensions do not work (Only the #expr parser
  function is supported by using an external Scribunto library).
</pre>
<p>--]==]</p>
<pre>
       for pg in page.subpages( title, { ignoreNS=true } ) do
           if abort.cycles then
               abort.cycles = abort.cycles - 1
               if 0 == abort.cycles then return ac()  end
           end
           if abort.time then
               if (os.clock() - clock) &gt; abort.time then return ac()  end
           end
           if mw.ustring.len( pg ) &lt;= 12 then
               langcode = string.lower( pg );
               langname = fetch( langcode );
               if langname ~= <i> then</i>
                   table.insert( pages, transform( title, pg, langcode, langname ) );
               end
           end
       end
       return pages;
   end
   return {};
</pre>
<p>end</p>
<p>function cloneArgs(frame)</p>
<pre>
   local args, pargs = {}, {}
   for k,v in pairs( frame.args ) do args[k] = v end
   if frame:getParent() then
       for k,v in pairs( frame:getParent().args ) do pargs[k] = v end
   end
   return args, pargs;
</pre>
<p>end</p>
<p><br /></p>
<p>local p = {};</p>
<p>--[=[ Usage: <strong class="error"><span class="scribunto-error" id="mw-scribunto-error-0">Script error: The function "internal" does not exist.</span></strong> ]=] function p.internal(frame)</p>
<pre>
   pages = language_subpages( frame, function( title, page, code, name )
       return mw.ustring.format( '<span lang="%s" class="language lang-%s"><bdi><a href="/web/20150422111815/https://meta.wikimedia.org/w/index.php?title=%25s/%25s&amp;action=edit&amp;redlink=1" class="new" title="%s/%s (page does not exist)">%s</a></bdi></span>',
           code, code, code, title, page, name
       );
   end);
   return table.concat( pages, ' · ' );
</pre>
<p>end</p>
<p>--[=[ Usage: <strong class="error"><span class="scribunto-error" id="mw-scribunto-error-1">Script error: The function "external" does not exist.</span></strong> ]=] function p.external(frame)</p>
<pre>
   pages = language_subpages( frame, function( title, page, code, name )
       return mw.ustring.format( '<span lang="%s" class="language lang-%s"><bdi>[%s/%s %s]</bdi></span>',
           code, code, code, tostring( mw.uri.fullUrl( title ) ), page:gsub( ' ', '_' ), name
       );
   end);
   return table.concat( pages, ' | ' );
</pre>
<p>end</p>
<p>--[=[ forEachLanguage</p>
<p>This function iterates over all language codes known to MediaWiki based on a maintained list replacing patterns in a pattern-string for each language</p>
<p>Usage: <strong class="error"><span class="scribunto-error" id="mw-scribunto-error-2">Script error: The function "forEachLanguage" does not exist.</span></strong></p>
<p>Parameters</p>
<pre>
   pattern: A pattern string which is processed for each language and which is concatenated at the end and returned as one string
   before: A string that is inserted before the concatenated result
   after: A string that is inserted after the concatenated result
   sep: A string that is inserted between each line created from the pattern while iterating (like ProcessedPattern_sep_ProcessedPattern_sep_ProcessedPattern)
   inLang: Langcode to use for $lnTrP and $lnTrUC1
</pre>
<p>Patterns:</p>
<pre>
   $lc - language code such as en or de
   $lnP - language name in own language (autonym)
   $lnUC1 - language name in own language (autonym), first letter upper case
   $lnTrP - language name translated to the language requested by language code passed to inLang
   $lnTrUC1 - language name translated to the language requested by language code passed to inLang, first letter upper case
</pre>
<p>Example</p>
<pre>
  <strong class="error"><span class="scribunto-error" id="mw-scribunto-error-3">Script error: The function "forEachLanguage" does not exist.</span></strong>
</pre>
<p>]=]</p>
<p>-- =p.forEachLanguage({ args= { pattern = "$lc - $lnTrP\n", inLang = "en" } }) function p.forEachLanguage(frame)</p>
<pre>
   local l = require( "Module:Languages/List" )
</pre>
<pre>
   local ret = {}
   local lang    = mw.language
   local line
   local pattern = frame.args.pattern   or frame.args[1] or ""
   local prefix  = frame.args.before    or frame.args[2] or ""
   local postfix = frame.args.after     or frame.args[3] or ""
   local sep     = frame.args.sep       or frame.args.separator or frame.args[4] or ""
   local inLang  = frame.args.inLang    or frame.args[5] or nil
</pre>
<pre>
   local langNameUCFirstReq           = not not pattern:find( "$lnUC1", 1, true )
   local langNameReq                  = not not pattern:find( "$lnP", 1, true ) or langNameUCFirstReq
   local langNameTranslatedUCFirstReq = not not pattern:find( "$lnTrUC1", 1, true )
   local langNameTranslatedReq        = not not pattern:find( "$lnTrP", 1, true ) or langNameTranslatedUCFirstReq
   local contentLangInstance = mw.language.getContentLanguage()
   local inLangLangInstance
   local l = mw.language.fetchLanguageNames() -- autonyms
   local lTr
   local lcIdList = require( 'Module:Languages/List' ).getSortedList( l )
</pre>
<pre>
   if langNameTranslatedReq then
       inLangLangInstance = --[==[
           mw.getLanguage( inLang ) -- Quota hit in :ucfirst() if using too many langInstances
           --]==] contentLangInstance
       lTr = mw.language.fetchLanguageNames( inLang ) -- translated names
   end
</pre>
<pre>
   for _, lcId in pairs( lcIdList ) do
       local subst = lcId:gsub('%%', '%%%%')
       line = pattern:gsub( "%$lc", subst )
       local langName, langInstance
       -- autonym (name of lcId in locale lcId)
       if langNameReq then
           langName = l[lcId]
           subst = langName:gsub('%%', '%%%%')
           line = line:gsub( "%$lnP", subst )
       end
       if langNameUCFirstReq then
           langInstance = --[==[
               mw.getLanguage( lcId ) -- Quota hit in :ucfirst() if using too many langInstances
               --]==] contentLangInstance
           langName = langInstance:ucfirst( langName )
           subst = langName:gsub('%%', '%%%%')
           line = line:gsub( "%$lnUC1", subst )
       end
</pre>
<pre>
       -- translated name (name of lcId in locale inLang)
       if langNameTranslatedReq then
           langName = lTr[lcId]
           subst = langName:gsub('%%', '%%%%')
           line = line:gsub( "%$lnTrP", subst )
       end
       if langNameTranslatedUCFirstReq then
           langName = inLangLangInstance:ucfirst( langName )
           subst = langName:gsub('%%', '%%%%')
           line = line:gsub( "%$lnTrUC1", subst )
       end
</pre>
<pre>
       table.insert(ret, line)
   end
   return prefix .. table.concat( ret, sep ) .. postfix
</pre>
<p>end</p>
<p>--[=[</p>
<pre>
Provide logic for <a href="/web/20150422111815/https://meta.wikimedia.org/w/index.php?title=Template:Lle&amp;action=edit&amp;redlink=1" class="new" title="Template:Lle (page does not exist)">Template:Lle</a> (Language Links external, to be substituted)
</pre>
<p>]=] function p.lle(frame)</p>
<pre>
   local ret
   local pattern = "{{subst:#ifexist:{{{1}}}/$lc|[{{fullurl:{{{1}}}/$lc}} $lnUC1] {{subst:!}} }}"
   ret = p.forEachLanguage({ args= { pattern = pattern } })
   ret = frame:preprocess(ret)
   return ret
</pre>
<p>end</p>
<p>--[=[</p>
<pre>
Provide logic for <a href="/web/20150422111815/https://meta.wikimedia.org/wiki/Template:Ll" title="Template:Ll" class="mw-redirect">Template:Ll</a> (Language Links, to be substituted)
</pre>
<p>]=] function p.ll(frame)</p>
<pre>
   local ret
   local pattern = "{{subst:#ifexist:{{{1}}}/$lc|[[{{{1}}}/$lc{{subst:!}}$lnUC1]] {{subst:!}} }}"
   ret = p.forEachLanguage({ args= { pattern = pattern } })
   ret = frame:preprocess(ret)
   return ret
</pre>
<p>end</p>
<p><br /></p>
<hr />
<p>--- Different approaches for <a href="/web/20150422111815/https://meta.wikimedia.org/wiki/Template:Lang_links" title="Template:Lang links">Template:Lang links</a> ---</p>
<hr />
<p>--[=[</p>
<pre>
Provide logic for <a href="/web/20150422111815/https://meta.wikimedia.org/wiki/Template:Lang_links" title="Template:Lang links">Template:Lang links</a>
Using a cute Hybrid-Method:
   First check the subpages which is quite fast; if there are too many fall back to checking for each language page individually
</pre>
<p>]=]</p>
<p>-- =p.langLinksNonExpensive({ args= { page='Commons:Picture of the Year/2010' }, getParent=function() end }) -- =p.langLinksNonExpensive({ args= { page='Main Page' }, getParent=function() end }) -- =p.langLinksNonExpensive({ args= { page='Template:No_source_since' }, getParent=function() end }) -- =p.langLinksNonExpensive({ args= { page='MediaWiki:Gadget-HotCat' }, getParent=function() end }) function p.langLinksNonExpensive(frame)</p>
<pre>
   local args, pargs = frame.args, ( frame:getParent() or {} ).args or {};
   local title = args.page or args[1] or pargs.page or pargs[1] or "";
   local contentLangInstance = mw.language.getContentLanguage();
   local pages2
   if frame.preprocess == nil then
       frame = mw.getCurrentFrame()
   end
   --[==[
   local options = { abort= { time=3.5, on=function()
           pages2 = p.forEachLanguage({ args= { pattern = <i> } })</i>
       end } }
   local pages = language_subpages( frame, function( basepagename, subpagename, code, langname )
       return mw.ustring.format( '<span lang="%s" class="language lang-%s" style="white-space:nowrap"><bdi><a href="/web/20150422111815/https://meta.wikimedia.org/w/index.php?title=%25s/%25s&amp;action=edit&amp;redlink=1" class="new" title="%s/%s (page does not exist)">%s</a></bdi></span> | ',
           code, code, code, basepagename, subpagename, contentLangInstance:ucfirst( langname ) )
   end, options );
   return pages2 and frame:preprocess(pages2) or table.concat(  pages, <i> );</i>
   ]==]
   return frame:preprocess( p.forEachLanguage({ args= { pattern = <i> } }) )</i>
</pre>
<p>end</p>
<hr />
<hr />
<p><a href="/web/20150422111815/https://meta.wikimedia.org/w/index.php?title=Template:Autolang&amp;action=edit&amp;redlink=1" class="new" title="Template:Autolang (page does not exist)">Template:Autolang</a> -----------------</p>
<hr />
<p>--[[</p>
<pre>
 Works like <strong class="error"><span class="scribunto-error" id="mw-scribunto-error-4">Script error: No such module "fallback".</span></strong> just allowing an unlimited number of arguments, even named arguments.
 It's doing Magic! No arguments should be passed to <strong class="error"><span class="scribunto-error" id="mw-scribunto-error-5">Script error: You must specify a function to call.</span></strong>
</pre>
<p>]]</p>
<p>function p.autolang(frame)</p>
<pre>
   local args, pargs = cloneArgs( frame )
   if nil == args.useargs then
       if not args.base then args = pargs end
   elseif 'both' == args.useargs then
       for k,v in pairs(args) do pargs[k] = v end
       args = pargs
   elseif 'parent' == args.useargs then
       args = pargs
       if pargs.base and not args.base then
           args.base = pargs.base
       end
   end
   local base = args.base
   local userlang = frame:preprocess( 'en' )
   local tl, tlns = 'Template:', 10
   local tlb, fallback1, currenttemplate
   local fallback, contentlang = mw.text.split( userlang, '-', true )[1], mw.language.getContentLanguage():getCode()
</pre>
<pre>
   local createReturn = function(title)
       local ret
       local tlargs = {}
        -- When LUA is invoked, templates are already expanded. This must be respected.
       return frame:expandTemplate{ title = title, args = args }
   end
</pre>
<pre>
   if not base then
       return ("'autolang' in <a href="/web/20150422111815/https://meta.wikimedia.org/wiki/Module:Languages" title="Module:Languages">Module:Languages</a> was called but the 'base' parameter could not be found." ..
           "The base parameter specifies the template that's subpages will be sought for a suitable translation.")
   end
   tlb = tl .. base .. '/'
</pre>
<pre>
   currenttemplate = tlb .. userlang
   local ok, exists = pcall( function()
       return mw.title.new( currenttemplate, tlns ).exists
   end )
   if ok and exists then
       return createReturn(currenttemplate)
   end
</pre>
<pre>
   fallback1 = frame:preprocess( 'en' )
   if fallback1 ~= contentlang then
       return createReturn(tlb .. fallback1)
   end
</pre>
<pre>
   currenttemplate = tlb .. fallback
   local ok, exists = pcall( function()
       return mw.title.new( currenttemplate, tlns ).exists
   end )
   if ok and exists then
       return createReturn(currenttemplate)
   end
</pre>
<pre>
   currenttemplate = tlb .. contentlang
   local ok, exists = pcall( function()
       return mw.title.new( currenttemplate, tlns ).exists
   end )
   if ok and exists then
       return createReturn(currenttemplate)
   end
   return createReturn(tl .. base)
</pre>
<p>end</p>
<p>--[=[ Usage: <strong class="error"><span class="scribunto-error" id="mw-scribunto-error-6">Script error: The function "isKnownLanguageTag" does not exist.</span></strong> -&gt; 1 <strong class="error"><span class="scribunto-error" id="mw-scribunto-error-7">Script error: The function "isKnownLanguageTag" does not exist.</span></strong> -&gt; ]=] function p.isKnownLanguageTag(frame)</p>
<pre>
   return mw.language.isKnownLanguageTag( frame.args[1] or frame.args.tag or frame.args.code or <i> ) and '1' or </i>
</pre>
<p>end</p>
<p>function p.file_languages(frame)</p>
<pre>
   local M_link = require( 'Module:Link' )
   local contentLangInstance = mw.language.getContentLanguage()
   local pattern = frame.args.pattern or '%s (%s)'
   local original = frame.args.original or mw.title.getCurrentTitle().text
   local ext_start, _ = string.find( original, '\.%w+$' )
   local file_ext = string.sub( original, ext_start )
   original = string.sub( original, 0, ext_start-1 )
</pre>
<p>return frame:preprocess('</p>
<ul class="gallery mw-gallery-traditional">
<li class="gallerybox" style="width: 155px">
<div style="width: 155px">
<div class="thumb" style="height: 150px;">Function(linkInfo)</div>
<div class="gallerytext"></div>
</div>
</li>
<li class="gallerybox" style="width: 155px">
<div style="width: 155px">
<div class="thumb" style="height: 150px;">Local filename = mw.ustring.format( pattern, original, linkInfo.text ) .. file ext</div>
<div class="gallerytext"></div>
</div>
</li>
<li class="gallerybox" style="width: 155px">
<div style="width: 155px">
<div class="thumb" style="height: 150px;">Local ok, exists = pcall( function()</div>
<div class="gallerytext"></div>
</div>
</li>
<li class="gallerybox" style="width: 155px">
<div style="width: 155px">
<div class="thumb" style="height: 150px;">Return mw.title.new( filename, 6 ).exists</div>
<div class="gallerytext"></div>
</div>
</li>
<li class="gallerybox" style="width: 155px">
<div style="width: 155px">
<div class="thumb" style="height: 150px;">End )</div>
<div class="gallerytext"></div>
</div>
</li>
<li class="gallerybox" style="width: 155px">
<div style="width: 155px">
<div class="thumb" style="height: 150px;">If ok and exists then</div>
<div class="gallerytext"></div>
</div>
</li>
<li class="gallerybox" style="width: 155px">
<div style="width: 155px">
<div class="thumb" style="height: 150px;">Return mw.ustring.format( '%s</div>
<div class="gallerytext">
<p>%s', filename, contentLangInstance:ucfirst( mw.language.fetchLanguageName( linkInfo.text ) ) )</p>
</div>
</div>
</li>
<li class="gallerybox" style="width: 155px">
<div style="width: 155px">
<div class="thumb" style="height: 150px;">Else</div>
<div class="gallerytext"></div>
</div>
</li>
<li class="gallerybox" style="width: 155px">
<div style="width: 155px">
<div class="thumb" style="height: 150px;">Return nil</div>
<div class="gallerytext"></div>
</div>
</li>
<li class="gallerybox" style="width: 155px">
<div style="width: 155px">
<div class="thumb" style="height: 150px;">End</div>
<div class="gallerytext"></div>
</div>
</li>
<li class="gallerybox" style="width: 155px">
<div style="width: 155px">
<div class="thumb" style="height: 150px;">End</div>
<div class="gallerytext"></div>
</div>
</li>
<li class="gallerybox" style="width: 155px">
<div style="width: 155px">
<div class="thumb" style="height: 150px;">), '\n'))..'\n</div>
<div class="gallerytext"></div>
</div>
</li>
</ul>
<p>')</p>
<p>end</p>
<p>function p.runTests()</p>
<pre>
   return p.langLinksNonExpensive({ args= { page='Module:Languages/testcases/test' }, getParent=function() end }) == "<a href="/web/20150422111815/https://meta.wikimedia.org/w/index.php?title=Module:Languages/testcases/test/de&amp;action=edit&amp;redlink=1" class="new" title="Module:Languages/testcases/test/de (page does not exist)">Deutsch</a> | <a href="/web/20150422111815/https://meta.wikimedia.org/w/index.php?title=Module:Languages/testcases/test/en&amp;action=edit&amp;redlink=1" class="new" title="Module:Languages/testcases/test/en (page does not exist)">English</a> | "
</pre>
<p>end</p>
<p>return p;</p>

Event Timeline

Nemo_bis raised the priority of this task from to Medium.
Nemo_bis updated the task description. (Show Details)
Nemo_bis added subscribers: Nemo_bis, Kaganer, PiRSquared17.
Restricted Application added a subscriber: Aklapper. · View Herald TranscriptApr 22 2015, 11:22 AM
Anomie closed this task as Invalid.Apr 22 2015, 12:57 PM
Anomie claimed this task.
Anomie added a subscriber: Anomie.

When you put Lua code into a template rather than into a module, you shouldn't be surprised that transcluding said template gives you the Lua code rather than executing it as a module.

jeremyb-phone set Security to None.Apr 27 2015, 2:55 AM
jeremyb-phone added a subscriber: jeremyb.
Aklapper removed a subscriber: Anomie.Oct 16 2020, 5:29 PM