User:Reschultzed/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;	}

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 = $('');				for(var x = 0; x < failed.length; x++) {					// Link the titles in the "failed" array					var failedTitle = mw.Title.newFromText(failed[x]);					var $failedItem = $('');					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 = (config.skin == "cologneblue" ? "article" : "bodyContent"); 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 ' + '

' +			'' + 'If you abuse this tool, it\'s your fault, not mine.' + ' ' +			'

' +				'Pages to move (one on each line, please): ' +					' ' + '

' +				' Prefix to remove from the old name (e.g., Template:): ' + '  ' + ' Prefix to add to the new name (e.g., User:Reschultzed/): ' + '  ' + ' Move talk: ' + '  ' + ' No redirect: ' + '  ' + ' Watch Pages: ' + ' ' + ' No change ' + ' User preferences ' + ' Add to watch list ' + ' Remove from watch list ' + ' ' +				'  Edit summary: ' + '  ' + '  ' + ' ';		document.getElementById("wpMassMoveSubmit").addEventListener("click", function (e) {			doMassMove;		}); }	if(mw.config.get('wgNamespaceNumber') === -1 		&& (mw.config.get('wgPageName') === "Special:Massmove" || mw.config.get('wgPageName') === "Special:MassMove")		&& (/sysop/.test(wgUserGroups) || /extendedmover/.test(wgUserGroups))	) { $.when( $.ready, mw.loader.using(['mediawiki.util'])).done( massMoveForm ); } });