Module:Bad title suggestion
| 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 rated as ready for general use. It has reached a mature state, is considered relatively stable and bug-free, and may be used wherever appropriate. It can be mentioned on help pages and other Wikipedia resources as an option for new users. To minimise server load and avoid disruptive output, improvements should be developed through sandbox testing rather than repeated trial-and-error editing. |
| This module is currently protected from editing. See the protection policy and protection log for more details. Please discuss any changes on the talk page; you may submit an edit request to ask an administrator to make an edit if it is uncontroversial or supported by consensus. You may also request that this page be unprotected. |
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.
Usage
{{#invoke:Bad title suggestion|main|invalid_char|bad_title_encoded}}
In the case of MediaWiki:Title-invalid-characters, this is:
{{#invoke:Bad title suggestion|main|$1|$2}}
Examples
- Foobar>:
{{#invoke:Bad title suggestion|main|>|Foobar>}}
- Wikipedia:Village pump}}:
{{#invoke:Bad title suggestion|main|}|Wikipedia:Village pump}}}}
- Main Page|title text!:
{{#invoke:Bad title suggestion|main|||Main Page|title text!}}
- This page absolutely does not exist>:
{{#invoke:Bad title suggestion|main|>|This page absolutely does not exist>}}
(nothing displayed)
- Category:Contents>:
{{#invoke:Bad title suggestion|main|>|Category:Contents>}}
- <Foobar>:
{{#invoke:Bad title suggestion|main|<|<Foobar>}}
(nothing displayed)
- Solo [3]:
{{#invoke:Bad title suggestion|main|[|Solo [3]}}
- M>Train:
{{#invoke:Bad title suggestion|main|>|M>Train}}
require("strict")
local getArgs = require("Module:Arguments").getArgs
local p = {}
local function dedab(title)
return title:gsub(" ?%(.-%)","")
end
function p.main(frame)
local args = getArgs(frame)
-- The invalid character, e.g. ">" or "}"
local chr = args[1]
-- The escaped bad title, e.g. "Foobar>" or "Foobar|text"
local title = 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.
if chr == nil and title == nil then
chr = "|"
title = args[3]
end
if chr == nil or title == nil then
return ""
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.
local index = mw.ustring.find(title, mw.text.nowiki(chr), 1, true)
local truncate = ""
if index then
local page = mw.title.new(mw.ustring.sub(title, 1, index - 1))
if page and page.exists then
truncate = frame:expandTemplate{
title = "Did you mean box",
args = { page.fullText }
}
end
end
local substitute = p._substitute(frame, title, chr)
if substitute ~= "" or truncate ~= "" then
return '<div class="mw-parser-output">' .. substitute .. truncate .. '</div>'
end
return ""
end
function p._substitute(frame, title, chr)
-- Since the overrides page has a lower protection level than this one don't crash if it's invalid
local success, overrides = pcall(function() return mw.loadJsonData("Module:Bad title suggestion/override.json") end)
local spage
local substitute = ""
title = mw.text.decode(title):gsub("_", " ")
if success and overrides[title] then
spage = mw.title.new(overrides[title])
elseif success and overrides[title:lower()] then
spage = mw.title.new(overrides[title:lower()])
elseif success and overrides[dedab(title)] then
spage = mw.title.new(overrides[dedab(title)])
elseif not chr or chr == "[" or chr == "]" then
local replaced = title:gsub("%[","("):gsub("%]",")")
if replaced ~= title then
spage = mw.title.new(replaced)
end
end
if spage and spage.exists then
substitute = frame:expandTemplate{
title = "Did you mean box",
args = { spage.fullText }
}
end
return substitute
end
function p.substitute(frame)
local subst = p._substitute(frame, frame.args[1], nil)
if subst ~= "" then
return '<div class="mw-parser-output">' .. p._substitute(frame, frame.args[1], nil) .. '</div>'
end
return ""
end
return p
Content Disclaimer
Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.
- The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
- There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
- It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
- Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
- Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.