User:Ahecht/Scripts/massmove.js
Appearance
< User:Ahecht | Scripts
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
This user script seems to have a documentation page at User:Ahecht/Scripts/massmove. |
//jshint maxerr:512
var massMoveTitle = "Mass-move tool";
/*Forked version of [[User:Plastikspork/massmove.js]] that adds a link to the left column
and allows adding and removing both prefixes and suffixes
Add the following line to [[Special:MyPage/common.js]] or [[:meta:Special:MyPage/global.js]] to install:
mw.loader.load( '//en.wikipedia.org/w/index.php?title=User:Ahecht/Scripts/massmove.js&action=raw&ctype=text/javascript' ); //[[User:Ahecht/Scripts/massmove.js]] [[User:Plastikspork/massmove.js]]
Click on "Mass move" under "Tools" to use the script.*/
if (/sysop|extendedmover/.test(mw.config.get("wgUserGroups"))) {
mw.util.addPortletLink("p-tb", "/wiki/Special:Massmove", "Mass move", "p-massmove", "Mass move");
}
// Adapted from [[User:Animum/massdelete.js]]
function massMoveGetValues() {
return {reason: document.getElementById("wpMassMoveReason").value,
oldPrefix: document.getElementById("wpMassMovePrefix1").value,
newPrefix: document.getElementById("wpMassMovePrefix2").value,
oldSuffix: document.getElementById("wpMassMoveSuffix1").value,
newSuffix: document.getElementById("wpMassMoveSuffix2").value,
watch: document.getElementById("wpMassMoveWatch").value,
pipeTrick: document.getElementById("wpMassMovePipeTrick").checked,
leaveRedirect: document.getElementById("wpMassMoveLeaveRedirect").checked,
noRatelimit: document.getElementById("wpMassMoveNoRatelimit").checked,
moveTalk: document.getElementById("wpMassMoveMoveTalk").checked,
moveSubPages: document.getElementById("wpMassMoveMoveSubPages").checked
};
}
function massMoveReplace(s, values) {
s = s.trim();
if (values.pipeTrick) {
s = s.replace(/^(?:\:)?(?:.*\:)?(.*?)(?:, .*)?$/,"$1").replace(/(.*?)(?: ?\(.*\))?$/, "$1");
}
if (s.substring(0,values.oldPrefix.length) == values.oldPrefix) {
s = s.substring(values.oldPrefix.length);
}
if (s.substring(s.length - values.oldSuffix.length) == values.oldSuffix) {
s = s.substring(0, s.length - values.oldSuffix.length);
}
return (values.newPrefix + s + values.newSuffix).trim();
}
function massMoveGetArticles() {
var articles = document.getElementById("wpMassMovePages").value.split("\n");
var ret = [];
var i, len;
for (i = 0, len = articles.length; i < len; i++) {
var s = articles[i];
s = s.trim();
if (s) {
ret.push(s);
}
}
return ret;
}
function massMoveUpdatePreview() {
var articles = massMoveGetArticles();
if (articles.length > 0) {
var values = massMoveGetValues();
var preview = [articles[0] + " → " + massMoveReplace(articles[0], values)];
for (var i = 1, len = articles.length; i < len; i++) {
preview.push(articles[i] + " → " + massMoveReplace(articles[i], values));
}
document.getElementById("wpMassMovePreview").value = preview.join("\n");
document.getElementById("wpMassMoveSubmit").disabled = false;
} else {
document.getElementById("wpMassMovePreview").value = '';
document.getElementById("wpMassMoveSubmit").disabled = true;
}
}
jQuery(document).ready(function($) {
var config = mw.config.get(['wgNamespaceNumber', 'wgTitle', 'wgUserGroups', 'skin']);
function now() {
return new Date().getTime();
}
function doMassMove() {
var articles = massMoveGetArticles();
if (!articles.length) {
return;
}
var
api = new mw.Api(),
values = massMoveGetValues(),
moved = 0,
failed = [],
error = [],
deferreds = [],
lastMoved = 0,
onSuccess = function () {
moved++;
console.log(now() + ": Moved " + moved);
mw.notify("Success! " + moved + " pages moved.", {type: 'success', tag: 'status', autoHide: true});
};
function delay(len) {
return function() {
return $.Deferred(function (deferred) {
var interval = lastMoved + config.wait - now();
if ( (len <= config.hits) || ((lastMoved + config.wait - now()) < 0) || values.noRatelimit ) {
interval = 0;
}
console.log(now() + ': Waiting ' + interval + 'ms...');
setTimeout(function () {
console.log(now() + ': Done waiting.');
deferred.resolve();
}, interval);
});
};
}
function makeMoveFunc(article) {
return function () {
return $.Deferred(function (deferred) {
var options = {
format: 'json',
action: 'move',
watchlist: values.watch,
from: article,
to: massMoveReplace(article, values),
reason: values.reason + ' ([[User:Ahecht/Scripts/massmove.js|' + massMoveTitle + ']])'
};
if (!values.leaveRedirect) {
options.noredirect = '';
}
if (values.moveTalk) {
options.movetalk = '';
}
if (values.moveSubPages) {
options.movesubpages = '';
}
console.log(now() + ": Moving " + options.from + "→" + options.to);
mw.notify("Moving " + options.from + " → " + options.to, {type: 'info', tag: 'status', autoHide: true});
lastMoved = now();
var promise = api.postWithEditToken(options);
promise.done(onSuccess);
promise.fail(function (code, obj) {
failed.push(article);
error.push(obj.error.info);
console.warn(now() + ": Move failed (" + obj.error.info + ").");
mw.notify("Move failed: " + obj.error.info, {type: 'error', autoHide: true});
});
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(delay(len));
deferred = deferred.then(makeMoveFunc(articles[i]));
}
// Show the output and do cleanup once all the requests are done.
$.when(deferred).then(function () {
console.log(now() + ": Done! Moved " + moved);
if (failed.length) {
mw.notify("Done. " + moved + " pages moved, " + failed.length + " errors.", {type: 'warn', tag: 'mainNotify', autoHide: false});
var $failedList = $('<ul>');
for(var x = 0; x < failed.length; x++) {
// Link the titles in the "failed" array
var failedTitle = mw.Title.newFromText(failed[x]);
var $failedItem = $('<li>');
if (failedTitle) {
$failedItem.append( $('<a>')
.attr('href', failedTitle.getUrl())
.text(failed[x])
);
} else {
$failedItem.text(failed[x]);
}
$failedItem.append(document.createTextNode(': ' + error[x]));
$failedList.append($failedItem);
}
$('#wpMassMoveFailedContainer')
.append($('<br />'))
.append($('<b>')
.text('Failed moves:')
)
.append($failedList);
} else {
mw.notify("Done. " + moved + " pages moved.", {type: 'success', tag: 'mainNotify', autoHide: false});
}
document.getElementById("wpMassMoveSubmit").value = "Move";
$("*", "#wpMassMove").not("#wpMassMovePreview").prop('disabled',false);
});
}
function getWait() {
config.hits=8; // default rate limit is 8/minute
config.seconds=60; // default rate limit is 8/minute
config.wait = Math.ceil(config.seconds/config.hits) * 1000;
new mw.Api().get({
meta: 'userinfo',
uiprop: 'ratelimits'
}).fail(function(code, error) {
console.warn(error);
doMassMove();
}).done( function(d) {
if (d && d.query && d.query.userinfo && d.query.userinfo.ratelimits
&& d.query.userinfo.ratelimits.move)
{
for (const property in d.query.userinfo.ratelimits.move) {
var rlm = d.query.userinfo.ratelimits.move[property];
if (rlm && rlm.hits && rlm.seconds) {
console.log(property + " rate limit: " + rlm.hits + " moves every " + rlm.seconds + " seconds.");
var thisWait = Math.ceil(rlm.seconds/rlm.hits) * 1000;
if (thisWait < config.wait) {
config.hits = rlm.hits;
config.seconds = rlm.seconds;
config.wait = thisWait;
console.log("Calculated " + config.wait + "-millisecond wait between queries");
}
}
}
}
mw.notify("Rate limit: " + config.hits + " moves every " + config.seconds + " seconds.", {type: 'info', tag: 'mainNotify', autoHide: false});
doMassMove();
});
}
function massMoveForm() {
wpMassMoveStyle = document.createElement('style');
wpMassMoveStyle.type = 'text/css';
document.getElementsByTagName('head')[0].appendChild(wpMassMoveStyle);
wpMassMoveStyle = document.styleSheets[document.styleSheets.length-1];
if (typeof(wpMassMoveStyle.media) === 'string') { //IE compatability
wpMassMoveStyle.addRule('td.mincol', 'width:1%; white-space:nowrap;');
wpMassMoveStyle.addRule('td.maxcol', 'width:auto');
} else if (typeof(wpMassMoveStyle.media) === 'object') { //Modern browsers
wpMassMoveStyle.insertRule('td.mincol {width:1%; white-space:nowrap;}', wpMassMoveStyle.cssRules.length);
wpMassMoveStyle.insertRule('td.maxcol {width:auto;}', wpMassMoveStyle.cssRules.length);
}
document.getElementById(config.bodyContent).innerHTML = config.wpMassMoveIntro +
'<form id="wpMassMove" name="wpMassMove">' +
'<b>If you abuse this tool, it\'s <i>your</i> fault, not mine.</b>' +
'<div id="wpMassMoveFailedContainer"></div>' +
'<br /><br />' +
'Pages to move (one on each line, please):<br />' +
'<textarea tabindex="1" accesskey="," name="wpMassMovePages" id="wpMassMovePages" rows="10" cols="80" oninput="massMoveUpdatePreview()"></textarea>' +
'<br /><br /><table style="background-color:transparent">' +
'<tr><td>Apply <a href="' + (mw.config.get('wgServer')+mw.config.get('wgArticlePath')).replace('$1','Help:Pipe_trick') + '">"Pipe Trick"</a> to old name:</td>' +
'<td colspan="5"><input type="checkbox" id="wpMassMovePipeTrick" name="wpMassMovePipeTrick"/ oninput="massMoveUpdatePreview()"></td></tr>' +
'<tr><td class="mincol">Prefix to remove from the old name (e.g., Template:):</td>' +
'<td class="mincol"><input type="text" id="wpMassMovePrefix1" name="wpMassMovePrefix1" maxlength="255" oninput="massMoveUpdatePreview()"/></td>' +
'<td class="maxcol"> </td>' +
'<td class="mincol">Prefix to add to the new name (e.g., User:Plastikspork/):</td>' +
'<td class="mincol"><input type="text" id="wpMassMovePrefix2" name="wpMassMovePrefix2" maxlength="255" oninput="massMoveUpdatePreview()"/></td>' +
'<td class="maxcol"> </td></tr>' +
'<tr><td class="mincol">Suffix to remove from the old name (e.g., /sandbox):</td>' +
'<td class="mincol"><input type="text" id="wpMassMoveSuffix1" name="wpMassMoveSuffix1" maxlength="255" oninput="massMoveUpdatePreview()"/></td>' +
'<td class="maxcol"> </td>' +
'<td class="mincol">Suffix to add to the new name (e.g., /Archive_1):</td>' +
'<td class="mincol"><input type="text" id="wpMassMoveSuffix2" name="wpMassMoveSuffix2" maxlength="255" oninput="massMoveUpdatePreview()"/></td>' +
'<td class="maxcol"> </td></tr>' +
'<tr><td class="mincol">Move associated talk page:</td>' +
'<td class="mincol"><input type="checkbox" id="wpMassMoveMoveTalk" name="wpMassMoveMoveTalk" checked/></td>' +
'<td class="maxcol"> </td>' +
'<td class="mincol">Leave a redirect behind:</td>' +
'<td class="mincol"><input type="checkbox" id="wpMassMoveLeaveRedirect" name="wpMassMoveLeaveRedirect" checked/></td></tr>' +
'<tr><td class="mincol">Move subpages (up to 100):</td>' +
'<td class="mincol"><input type="checkbox" id="wpMassMoveMoveSubPages" name="wpMassMoveMoveSubPages" checked/></td>' +
'<td class="maxcol"> </td>' +
'<td class="mincol">Ignore ratelimit (may cause errors):</td>' +
'<td class="mincol"><input type="checkbox" id="wpMassMoveNoRatelimit" name="wpMassMoveNoRatelimit"/></td></tr>' +
'<tr><td>Watch source page and target page:</td>' +
'<td colspan="5"><select id="wpMassMoveWatch">' +
'<option value="nochange">No change</option>' +
'<option value="preferences">User preferences</option>' +
'<option value="watch">Add to watch list</option>' +
'<option value="unwatch">Remove from watch list</option>' +
'</select></td></tr>' +
'<tr><td>Edit summary:</td>' +
'<td colspan="4"><input type="text" id="wpMassMoveReason" name="wpMassMoveReason" maxlength="500" style="width:100%;" /></td>' +
'<td class="maxcol"> </td></tr></table>' +
'<br /><br />Preview:<br />' +
'<textarea disabled name="wpMassMovePreview" id="wpMassMovePreview" rows="10" cols="80"></textarea>' +
'<br /><br /><input disabled type="button" id="wpMassMoveSubmit" name="wpMassMoveSubmit" value="Move" />' +
'</form>';
document.getElementById("wpMassMoveSubmit").addEventListener("click", function (e) {
$('#wpMassMoveFailedContainer').empty();
$("*", "#wpMassMove").prop('disabled',true);
document.getElementById("wpMassMoveSubmit").value = "Moving...";
getWait();
});
}
function massMoveError() {
document.getElementById(config.bodyContent).innerHTML = config.wpMassMoveIntro +
'For more information, please feel free to contact the <a href="' + (mw.config.get('wgServer')+mw.config.get('wgArticlePath')).replace('$1','User:Ahecht') + '">script author</a>!';
}
if(mw.config.get('wgNamespaceNumber') === -1
&& (mw.config.get('wgTitle') === "Massmove" ||
mw.config.get('wgTitle') === "MassMove")
) {
document.getElementsByTagName("h1")[0].textContent = massMoveTitle;
document.title = massMoveTitle + " - Wikipedia, the free encyclopedia";
config.wpMassMoveIntro = '<div id="siteSub">From Wikipedia, the free encyclopedia</div>' +
'<p>Adapted from Plastikspork\'s mass-move tool, which is in turn adapted from Animum\'s mass-delete tool and Timotheus Canens\'s mass-edit tool</p>' +
'<br />' + 'This tool is restricted to editors in the <code>sysop</code> or <code>extendedmover</code> groups.' +
'<br />' + 'Your user groups are: ' + mw.config.get('wgUserGroups') + '<br /><br />';
if (config.skin == 'modern') {
config.bodyContent = 'mw_contentholder';
} else if (config.skin == 'cologneblue') {
config.bodyContent = 'article';
} else {
config.bodyContent = 'bodyContent';
}
if (/sysop/.test(config.wgUserGroups) || /extendedmover/.test(config.wgUserGroups)) {
$.when( $.ready, mw.loader.using(['mediawiki.util'])).done( massMoveForm );
} else {
$.when( $.ready, mw.loader.using(['mediawiki.util'])).done( massMoveError );
}
}
});