User:Plastikspork/massmove.js
// Adapted from User:Animum/massdelete.js
jQuery(document).ready(function($) {
var config = mw.config.get(['wgNamespaceNumber', 'wgTitle', 'wgUserGroups', 'skin']);
function removeBlanks(arr) {
var ret = [];
var i, len;
for (i = 0, len = arr.length; i < len; i++) {
var s = arr[i];
s = s.trim();
if (s) {
ret.push(s);
}
}
return ret;
}
function replacePrefix(s, oldPrefix, newPrefix) {
if (s.indexOf(oldPrefix) === 0) {
s = s.substr(oldPrefix.length);
return newPrefix + s;
}
return s;
}
function doMassMove() {
document.getElementById("wpMassMoveSubmit").disabled = true;
var articles = document.getElementById("wpMassMovePages").value.split("\n");
articles = removeBlanks(articles);
if (!articles.length) {
return;
}
var
api = new mw.Api(),
wpMassMoveReason = document.getElementById("wpMassMoveReason").value,
wpMassMovePrefix1 = document.getElementById("wpMassMovePrefix1").value,
wpMassMovePrefix2 = document.getElementById("wpMassMovePrefix2").value,
wpMassMoveWatch = document.getElementById("wpMassMoveWatch").value,
wpMassMoveNoRedirect = document.getElementById("wpMassMoveNoRedirect").checked,
wpMassMoveMoveTalk = document.getElementById("wpMassMoveMoveTalk").checked,
moved = 0,
failed = [],
error = [],
deferreds = [],
onSuccess = function () {
moved++;
document.getElementById("wpMassMoveSubmit").value = "(" + moved + ")";
};
function makeMoveFunc(article) {
return function () {
return $.Deferred(function (deferred) {
var options = {
format: 'json',
action: 'move',
watchlist: wpMassMoveWatch,
from: article,
to: replacePrefix(article, wpMassMovePrefix1, wpMassMovePrefix2),
reason: wpMassMoveReason
};
if (wpMassMoveNoRedirect) {
options.noredirect = '';
}
if (wpMassMoveMoveTalk) {
options.movetalk = '';
}
var promise = api.postWithToken('move', options);
promise.done(onSuccess);
promise.fail(function (code, obj) {
failed.push(article);
error.push(obj.error.info);
});
promise.always(function () {
deferred.resolve();
});
});
};
}
// Make a chain of deferred objects. We chain them rather than execute them in
// parallel so that we don't make 1000 simultaneous move requests and bring the
// site down. We use deferred objects rather than the promise objects returned
// from the API request so that the chain continues even if some articles gave
// errors.
var deferred = makeMoveFunc(articles[0])();
for (var i = 1, len = articles.length; i < len; i++) {
deferred = deferred.then(makeMoveFunc(articles[i]));
}
// Show the output and do cleanup once all the requests are done.
$.when(deferred).then(function () {
document.getElementById("wpMassMoveSubmit").value = "Done (" + moved + ")";
if (failed.length) {
var $failedList = $('
- ');
- ');
if (failedTitle) {
$failedItem.append( $('')
.attr('href', failedTitle.getUrl())
.text(failed[x])
);
} else {
$failedItem.text(failed[x]);
}
$failedItem.append(document.createTextNode(': ' + error[x]));
$failedList.append($failedItem);
}
$('#wpMassMoveFailedContainer')
.append($('
')).append($('')
.text('Failed moves:')
)
.append($failedList);
}
});
}
function massMoveForm() {
var bodyContent;
switch (config.skin) {
case 'modern':
bodyContent = 'mw_contentholder';
break;
case 'cologneblue':
bodyContent = 'article';
break;
case 'monobook':
case 'vector':
default:
bodyContent = 'bodyContent';
break;
}
document.getElementsByTagName("h1")[0].textContent = "Plastikspork's mass-move tool";
document.title = "Plastikspork's mass-move tool - Wikipedia, the free encyclopedia";
document.getElementById(bodyContent).innerHTML = '
From Wikipedia, the free encyclopedia' +'
Adapted from Animum\'s mass-delete tool and Timotheus Canens\'s mass-edit tool
' +'
' + 'This tool is restricted to editors in thesysop
orextendedmover
groups.' +'
' + 'Your user groups are: ' + mw.config.get('wgUserGroups') + '
' +'
for(var x = 0; x < failed.length; x++) {
// Link the titles in the "failed" array
var failedTitle = mw.Title.newFromText(failed[x]);
var $failedItem = $('