This Lua module is used in system messages. Changes to it can cause immediate changes to the Wikipedia user interface. To avoid major disruption, any changes should be tested in the module's /sandbox or /testcases subpages, or in your own module sandbox. The tested changes can be added to this page in a single edit. Please discuss changes on the talk page before implementing them.
This module is subject to page protection. It is a highly visible module in use by a very large number of pages, or is substituted very frequently. Because vandalism or mistakes would affect many pages, and even trivial editing might cause substantial load on the servers, it is protected from editing.
This module implements title suggestions for the "Bad title" interface message at MediaWiki:Title-invalid-characters. When the user asks for a page with invalid characters, this module checks for a page with the given title up to the first invalid character. If it exists, {{Did you mean box}} is displayed.
localgetArgs=require("Module:Arguments").getArgslocalp={}localfunctiondedab(title)returntitle:gsub(" ?%(.-%)","")endfunctionp.main(frame)localargs=getArgs(frame)-- The invalid character, e.g. ">" or "}"localchr=args[1]-- The escaped bad title, e.g. "Foobar>" or "Foobar|text"localtitle=args[2]-- A pipe (|) as the invalid character is a special case; it is not-- escaped, so instead the module thinks it got two empty arguments-- and the title as the third argument.ifchr==nilandtitle==nilthenchr="|"title=args[3]endifchr==nilortitle==nilthenreturn""end-- Determine the suggested title by taking a prefix of the bad title-- up to the first invalid character. Only display the suggestion box-- if the page exists.localindex=mw.ustring.find(title,mw.text.nowiki(chr),1,true)localtruncate=""ifindexthenlocalpage=mw.title.new(mw.ustring.sub(title,1,index-1))ifpageandpage.existsthentruncate='<div class="mw-parser-output">'..frame:expandTemplate{title="Did you mean box",args={page.fullText}}..'</div>'endendlocalsubstitute=""localsuccess,overrides=pcall(function()returnmw.loadJsonData("Module:Bad title suggestion/override.json")end)localspagetitle=mw.text.decode(title):gsub("_"," ")ifsuccessandoverrides[title]thenspage=mw.title.new(overrides[title])elseifsuccessandoverrides[title:lower()]thenspage=mw.title.new(overrides[title:lower()])elseifsuccessandoverrides[dedab(title)]thenspage=mw.title.new(overrides[dedab(title)])elseifchr=="["orchr=="]"thenlocalreplaced=title:gsub("%[","("):gsub("%]",")")spage=mw.title.new(replaced)endifspageandspage.existsthensubstitute='<div class="mw-parser-output">'..frame:expandTemplate{title="Did you mean box",args={spage.fullText}}..'</div>'endreturnsubstitute..truncateendreturnp