MediaWiki:Gadget-morebits.js: Difference between revisions
Content deleted Content added
Amorymeltzer (talk | contribs) Repo at 4c64ec3: remove leftover todo; Fix bug parsing terminal template parameters |
Amorymeltzer (talk | contribs) Repo at e2e8fad: Fix handling of circular redirect page load; Handle alternate capitalizations regardless of namespace; Morebits.namespaceRegex for matching all namespace capitalizations and aliases, use in morebits and modules |
||
Line 135:
* Create a string for use in regex matching a page name. Accounts for
* leading character's capitalization, underscores as spaces, and special
* characters being escaped. See also {@link Morebits.namespaceRegex}.
*
* @param {string} pageName - Page name without namespace.
Line 150:
}
return Morebits.string.escapeRegExp(firstChar) + remainder;
};
/**
* Create a string for use in regex matching all namespace aliases, regardless
* of the capitalization and underscores/spaces. Doesn't include the optional
* leading `:`, but if there's more than one item, wraps the list in a
* non-capturing group. This means you can do `Morebits.namespaceRegex([4]) +
* ':' + Morebits.pageNameRegex('Twinkle')` to match a full page. Uses
*
* @param {number[]} namespaces - Array of namespace numbers. Unused/invalid
* namespace numbers are silently discarded.
* @example
* // returns '(?:[Ff][Ii][Ll][Ee]|[Ii][Mm][Aa][Gg][Ee])'
* Morebits.namespaceRegex([6])
* @returns {string} - Regex-suitable string of all namespace aliases.
*/
Morebits.namespaceRegex = function(namespaces) {
if (!Array.isArray(namespaces)) {
namespaces = [namespaces];
}
var aliases = [], regex;
$.each(mw.config.get('wgNamespaceIds'), function(name, number) {
if (namespaces.indexOf(number) !== -1) {
// Namespaces are completely agnostic as to case,
// and a regex string is more useful/compatibile than a RegExp object,
// so we accept any casing for any letter.
aliases.push(name.split('').map(function(char) {
return Morebits.pageNameRegex(char);
}).join(''));
}
});
switch (aliases.length) {
case 0:
regex = '';
break;
case 1:
regex = aliases[0];
break;
default:
regex = '(?:' + aliases.join('|') + ')';
break;
}
return regex;
};
Line 3,549 ⟶ 3,593:
}
var page = response.pages && response.pages[0];
if (page) {
// check for invalid titles if (page.invalid) {
ctx.statusElement.error('The page title is invalid: ' + ctx.pageName);
onFailure(this);
return false; // abort
}
// retrieve actual title of the page after normalization and redirects
if (page.title) {▼
var resolvedName = page.title;
Line 4,493 ⟶ 4,537:
*/
removeLink: function(link_target) {
// Rempve a leading colon, to be handled later
▲ var link_re_string = Morebits.pageNameRegex(link_target);
if (link_target.indexOf(':') === 0) {
link_target = link_target.slice(1);
}
var link_re_string = '', ns = '', title = link_target;
var idx = link_target.indexOf(':');
ns = link_target.slice(0, idx);
title = link_target.slice(idx + 1);
link_re_string = Morebits.namespaceRegex(mw.config.get('wgNamespaceIds')[ns.toLowerCase().replace(/ /g, '_')]) + ':';
}
link_re_string += Morebits.pageNameRegex(title);
// Files and Categories become links with a leading colon, e.g. [[:File:Test.png]]
var colon = new RegExp(Morebits.namespaceRegex([6, 14])).test(ns) ? ':' : ':?';
▲ // Otherwise, allow for an optional leading colon, e.g. [[:User:Test]]
var link_simple_re = new RegExp('\\[\\[' + colon + '(' + link_re_string + ')\\]\\]', 'g');
Line 4,523 ⟶ 4,579:
// Check for normal image links, i.e. [[File:Foobar.png|...]]
// Will eat the whole link
var links_re = new RegExp('\\[\\[' + Morebits.namespaceRegex(
var allLinks = Morebits.string.splitWeightedByKeys(unbinder.content, '[[', ']]');
for (var i = 0; i < allLinks.length; ++i) {
Line 4,537 ⟶ 4,593:
// eventually preceded with some space, and must include File: prefix
// Will eat the whole line.
var gallery_image_re = new RegExp('(^\\s*' + Morebits.namespaceRegex(
unbinder.content = unbinder.content.replace(gallery_image_re, '<!-- ' + reason + '$1 -->');
Line 4,545 ⟶ 4,601:
// Check free image usages, for example as template arguments, might have the File: prefix excluded, but must be preceeded by an |
// Will only eat the image name and the preceeding bar and an eventual named parameter
var free_image_re = new RegExp('(\\|\\s*(?:[\\w\\s]+\\=)?\\s*(?:' + Morebits.namespaceRegex(
unbinder.content = unbinder.content.replace(free_image_re, '<!-- ' + reason + '$1 -->');
// Rebind the content now, we are done!
Line 4,561 ⟶ 4,617:
addToImageComment: function(image, data) {
var image_re_string = Morebits.pageNameRegex(image);
var links_re = new RegExp('\\[\\[' + Morebits.namespaceRegex(
var allLinks = Morebits.string.splitWeightedByKeys(this.text, '[[', ']]');
for (var i = 0; i < allLinks.length; ++i) {
Line 4,586 ⟶ 4,642:
removeTemplate: function(template) {
var template_re_string = Morebits.pageNameRegex(template);
var links_re = new RegExp('\\{\\{(?:
var allTemplates = Morebits.string.splitWeightedByKeys(this.text, '{{', '}}', [ '{{{', '}}}' ]);
for (var i = 0; i < allTemplates.length; ++i) {
|