MediaWiki:Gadget-UnsupportedTitles.js
Jump to navigation
Jump to search
Note: You may have to bypass your browser’s cache to see the changes. In addition, after saving a sitewide CSS file such as MediaWiki:Common.css, it will take 5-10 minutes before the changes take effect, even if you clear your cache.
- Mozilla / Firefox / Safari: hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (Command-R on a Macintosh);
- Konqueror and Chrome: click Reload or press F5;
- Opera: clear the cache in Tools → Preferences;
- Internet Explorer: hold Ctrl while clicking Refresh, or press Ctrl-F5.
- This script lacks a documentation subpage. Please create it.
- This script is a part of the
UnsupportedTitles
gadget (edit definitions)- Description (edit): Change the displayed title of pages for terms that cannot be titles of pages in the MediaWiki software to what the title represents (for instance, Unsupported titles/`amp`amp; to
&
) and redirect you if get a 404 (Not Found) response attempting to access the pages for these terms on by default, disable at your own risk - Other parts: UnsupportedTitles.json
- Description (edit): Change the displayed title of pages for terms that cannot be titles of pages in the MediaWiki software to what the title represents (for instance, Unsupported titles/`amp`amp; to
- Useful links: subpage list • links • redirects
/*
To get template for titleTag.textContent, process MediaWiki:pagetitle,
open your browser's JavaScript console on a Wiktionary page
and run this code:
((msg) => new mw.Api().getMessages(msg).then((messages) => {
mw.messages.set(messages);
const titleTemplate = new mw.Message(mw.messages, msg, ["$1"]).text();
const {
before,
after
} = /^(?<before>.*)\$1(?<after>.*)/.exec(titleTemplate).groups;
console.log(JSON.stringify(before), JSON.stringify(after))
})
.fail(console.error))("pagetitle")
We can do this for action=edit and action=submit by replacing
"pagetitle" with "editing" in this snippet.
*/
function setTitle(newTitle) {
// Set title tag, which determines the title shown in the browser.
var titleTag = document.getElementsByTagName('title')[0];
titleTag.textContent = newTitle + " - Wiktionary, the free dictionary";
// Set the text of the first heading above the page content.
document.getElementById('firstHeading').textContent = newTitle;
}
var UnsupportedTitlesJson = require('./UnsupportedTitles.json');
var prettyPageName = mw.config.get('wgPageName').replace(/_/g, ' ');
function generateCharacterToEscapeMap() {
var map = {};
Object.entries(UnsupportedTitlesJson.UnsupportedCharacterEscapes).forEach(function(entry) {
map[entry[1]] = entry[0];
});
return map;
}
// Helper function accessible from other scripts
function convertUnsupportedTitle(originalTitle) {
originalTitle = originalTitle.replaceAll("_", " ");
var match = /^(?:Talk:)?Unsupported titles\/(.+)$/.exec(originalTitle);
var newTitle;
if (match) {
var subpage = match && match[1];
newTitle = UnsupportedTitlesJson.UnsupportedTitles[subpage]
|| subpage.replace(/`([^`]+)`/g, function(wholeMatch, key) {
const character = UnsupportedTitlesJson.UnsupportedCharacterEscapes[key];
return character != null ? character : key;
});
} else {
newTitle = UnsupportedTitlesJson.TitleDisplay[originalTitle];
}
if (mw.config.get('wgCanonicalNamespace') == 'Talk')
newTitle = 'Talk:' + newTitle;
return newTitle
}
window.convertUnsupportedTitle = convertUnsupportedTitle;
/*
* On subpages of Unsupported titles and Talk:Unsupported titles, show the
* correct title in the header at the top of the page. For instance, on the page
* [[Unsupported titles/Left curly bracket]], show the title {.
*
* This is not enabled in the mobile version of the site.
*
* For all such pages, see [[Special:PrefixIndex/Unsupported titles]].
*/
if (mw.config.get('wgAction') === 'view'
&& (/^(?:Talk:)?Unsupported titles\//.test(prettyPageName)
|| UnsupportedTitlesJson.TitleDisplay.hasOwnProperty(prettyPageName))) {
$(function() {
try {
setTitle(convertUnsupportedTitle(prettyPageName));
} catch (e) {
console.log('Error while changing title: ' + e.message + '.');
}
});
}
/*
* This redirects a user to the correct Unsupported titles page if they attempt
* to access an invalid title. For instance, if you attempt to access the page
* for <
* https://en.wiktionary.org/wiki/%3C
* you will be redirected to
* https://en.wiktionary.org/wiki/Unsupported_titles/Less_than
*/
if (mw.config.get('wgCanonicalSpecialPageName') == 'Badtitle') {
// Avoid redirecting twice or more. Set this parameter after redirecting
// and don't redirect if it is already set.
const alreadyRedirectedParam = "UnsupportedTitleRedirected";
/*
* Generate the name of the page that the user attempted to access using
* either the portion of the URL after "/wiki/" or the value of the "title"
* parameter in the query. That is, the user is accessing the page using
* either the path /wiki/<invalid title> or a path similar to
* /w/index.php?title=<invalid title>.
*
* The actual page displayed if a user attempts to access an invalid title
* is the special page with the canonical title [[Special:Badtitle]].
*/
if (!mw.util.getParamValue(alreadyRedirectedParam)) {
var rxArticlePath = new RegExp('^' + mw.config.get('wgArticlePath').replace('$1', '(.*)') + '$');
var m = rxArticlePath.exec(location.pathname);
var title = m ? decodeURIComponent(m[1]) : mw.util.getParamValue('title');
const characterToEscapeMap = generateCharacterToEscapeMap();
var badTitleSubpage = title.replace(/./g, function (fullMatch) {
var escapeName = characterToEscapeMap[fullMatch];
return escapeName != null ? '`' + escapeName + '`' : fullMatch;
});
if (badTitleSubpage != title) {
const url = new URL(location.href);
const newTitle = 'Unsupported titles/' + badTitleSubpage;
if (url.searchParams.get("title")) {
url.searchParams.set("title", newTitle);
} else {
url.pathname = mw.util.getUrl(newTitle);
}
url.searchParams.set(alreadyRedirectedParam, "1");
location.href = url;
}
}
}