Nota: Después de publicar, quizás necesite actualizar la caché de su navegador para ver los cambios.

  • Firefox/Safari: Mantenga presionada la tecla Shift mientras pulsa el botón Actualizar, o presiona Ctrl+F5 o Ctrl+R (⌘+R en Mac)
  • Google Chrome: presione Ctrl+Shift+R (⌘+Shift+R en Mac)
  • Edge: mantenga presionada Ctrl mientras pulsa Actualizar, o presione Ctrl+F5
( function ( mw, $ ) {

var api, user;

var stats = {
	rollbackDone: 0,
	rollbackFailed: 0,
	deleteDone: 0,
	deleteFailed: 0
};

var rollbackQuery = {
	markbot: true
};

var msgOptions = {
	autoHide: false
};

var rollbackMsgOptions = {
	tag: 'rollback'
};

var deleteMsgOptions = {
	tag: 'delete'
};

function recursiveRollback( pages, index ) {
	mw.notify(
		'Revirtiendo cambios... ' + ( index = index || 1 ),
		$.extend( {}, msgOptions, rollbackMsgOptions )
	);
	
	return api.rollback( pages.pop(), user, rollbackQuery ).then( function () {
		stats.rollbackDone++;
		return pages.length ? recursiveRollback( pages, ++index ) : undefined;
	}, function () {
		stats.rollbackFailed++;
		return pages.length ? recursiveRollback( pages, ++index ) : undefined;
	} );
}

function recursiveDelete( pages, index ) {
	var query = {
		formatversion: 2,
		action: 'delete',
		title: pages.pop(),
		reason: 'Borrado en masa de las contribuciones de ' + user
	};
	
	mw.notify(
		'Borrando... ' + ( index = index || 1 ),
		$.extend( {}, msgOptions, deleteMsgOptions )
	);
	
	return api.postWithToken( 'delete', query ).then( function () {
		stats.deleteDone++;
		return pages.length ? recursiveDelete( pages, ++index ) : undefined;
	}, function () {
		stats.deleteFailed++;
		return pages.length ? recursiveDelete( pages, ++index ) : undefined;
	} );
}

if ( mw.config.get( 'wgRelevantUserName' ) ) {
	$.when(
		mw.loader.using( [ 'mediawiki.api', 'mediawiki.util' ] ),
		$.ready
	).done( function () {
		api = new mw.Api( { parameters: {
			formatversion: 2,
			errorformat: 'html',
			errorlang: mw.config.get( 'wgUserLanguage' ),
			errorsuselocal: true
		} } );
		
		user = mw.config.get( 'wgRelevantUserName' );
		
		$( mw.util.addPortletLink(
			mw.config.get( 'skin' ) === 'timeless' ? 'p-userpagetools' : 'p-tb',
			'#',
			'Bloquear y eliminar contribuciones'
		) ).on( 'click', function ( e ) {
			var rollbackList, deleteList;
			
			if ( confirm( '¿Estás seguro?' ) ) {
				api.postWithEditToken( {
					action: 'block',
					user: user,
					expiry: prompt( 'Duración del bloqueo', mw.util.isIPAddress( user )
						? '1 month'
						: 'infinite'
					),
					reason: prompt( 'Motivo del bloqueo', 'spam' ),
					nocreate: true,
					autoblock: true,
					reblock: true
				} )
				.then( function () {
					mw.notify( 'Bloqueo exitoso.', msgOptions );
					
					return api.get( {
						list: 'usercontribs',
						ucuser: user,
						uclimit: 'max'
					} );
				}, function ( code, data ) {
					mw.notify( api.getErrorMessage( data ), $.extend( {}, msgOptions, {
						type: 'error'
					} ) );
				} )
				.then( function ( data ) {
					var worklist = {};
					
					data.query.usercontribs.forEach( function ( contrib ) {
						var obj = worklist.hasOwnProperty( contrib.title )
							? worklist[ contrib.title ]
							: {
								top: false,
								new: false
							};
						
						obj.top |= contrib.top;
						obj.new |= contrib.new;
						
						worklist[ contrib.title ] = obj;
					} );
					
					rollbackList = Object.keys( worklist ).filter( function ( title ) {
						var obj = worklist[ title ];
						return obj.top && !obj.new;
					} );
					
					deleteList = Object.keys( worklist ).filter( function ( title ) {
						var obj = worklist[ title ];
						return obj.new;
					} );
					
					mw.notify( mw.format(
						'Análisis: $1 elementos, $2 serán procesados (borrar: $3, revertir: $4).',
						Object.keys( worklist ).length,
						rollbackList.length + deleteList.length,
						deleteList.length,
						rollbackList.length
					), msgOptions );
				} )
				.then( function () {
					if ( rollbackList.length ) {
						return recursiveRollback( rollbackList ).then( function () {
							var msg = mw.format(
								'Cambios revertidos: $1 (errores: $2)',
								stats.rollbackDone,
								stats.rollbackFailed
							);
							
							mw.notify( msg, $.extend( {}, msgOptions, rollbackMsgOptions, {
								type: stats.rollbackFailed ? 'warn' : 'info'
							} ) );
						} );
					}
				} )
				.then( function () {
					if ( deleteList.length ) {
						return recursiveDelete( deleteList ).then( function () {
							var msg = mw.format(
								'Páginas borradas: $1 (errores: $2)',
								stats.deleteDone,
								stats.deleteFailed
							);
							
							mw.notify( msg, $.extend( {}, msgOptions, deleteMsgOptions, {
								type: stats.deleteFailed ? 'warn' : 'info'
							} ) );
						} );
					}
				} );
			}
			
			return false;
		} );
	} );
}

}( mediaWiki, jQuery ) );