MediaWiki:Gadget-MassDelete.js

Wikibooks, manuali e libri di testo liberi.

Nota: dopo aver pubblicato, potrebbe essere necessario pulire la cache del proprio browser per vedere i cambiamenti.

  • Firefox / Safari: tieni premuto il tasto delle maiuscole Shift e fai clic su Ricarica, oppure premi Ctrl-F5 o Ctrl-R (⌘-R su Mac)
  • Google Chrome: premi Ctrl-Shift-R (⌘-Shift-R su un Mac)
  • Internet Explorer / Edge: tieni premuto il tasto Ctrl e fai clic su Aggiorna, oppure premi Ctrl-F5
  • Opera: premi Ctrl-F5.
mw.util.addPortletLink(
	'p-tb',
	'/wiki/Speciale:Massdelete',
	'Mass Delete',
	't-massd',
	'Cancella le pagine massivamente',
	'm',
	'#t-massd'
);

    
	var config = mw.config.get(['wgNamespaceNumber', 'wgTitle', 'wgUserGroups', 'skin']);
	var userGroups = mw.config.get( 'wgUserGroups' );
	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 doMassDelete() {
		document.getElementById("wpMassDeleteSubmit").disabled = true;
		var articles = document.getElementById("wpMassDeletePages").value.split("\n");
		articles = removeBlanks(articles);
		if (!articles.length) {
			return;
		}
		var
			api = new mw.Api(),
			wpMassDeleteReasons = document.getElementById("wpMassDeleteReasons").value,
			wpMassDeleteReason = document.getElementById("wpMassDeleteReason").value,
			deleted = 0,
			failed = [],
			error = [],
			deferreds = [],
			reason = wpMassDeleteReasons == "altra" ?
				wpMassDeleteReason :
				wpMassDeleteReasons + (wpMassDeleteReason ? ": " + wpMassDeleteReason + "" : ""),
			onSuccess = function () {
				deleted++;
				document.getElementById("wpMassDeleteSubmit").value = "(" + deleted + ")";
			};

		function makeDeleteFunc(article) {
			return function () {
				return $.Deferred(function (deferred) {
					var promise = api.postWithToken('delete', {
						format: 'json',
						action: 'delete',
						title: article,
						reason: reason
					});
					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 delete 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 = makeDeleteFunc(articles[0])();
		for (var i = 1, len = articles.length; i < len; i++) {
			deferred = deferred.then(makeDeleteFunc(articles[i]));
		}

		// Show the output and do cleanup once all the requests are done.
		$.when(deferred).then(function () {
			document.getElementById("wpMassDeleteSubmit").value = "Fatto (" + deleted + ")";
			if (failed.length) {
				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);
				}
				$('#wpMassDeleteFailedContainer')
					.append($('<br />'))
					.append($('<b>')
						.text('Cancellazioni fallite:')
					)
					.append($failedList);
			}
		});
	}
        		
	function massdeleteform() {
		var bodyContent = (config.skin == "cologneblue" ? "article" : "bodyContent");
		var siteSub = $('#siteSub').text();
		document.getElementsByTagName("h1")[0].textContent = "Cancellazione in massa delle pagine";
		document.title = "Cancellazione in massa delle pagine - "+mw.config.get( 'wgServerName' );+"";
		document.getElementById(bodyContent).innerHTML = '<h3 id="siteSub">'+siteSub+'</h3><br /><br />' +
			'<form id="wpMassDelete" name="wpMassDelete">' +
			'Benvenuto nel modulo per la cancellazione in massa delle pagine. Se indendi cancellare le pagine create da un solo utente, per favore usa <a href="/wiki/Speciale:Nuke">Speciale:Nuke</a>! Se intendi cancellare <b>più di 10 pagine</b>, <a href="/wiki/Special:UserRights/'+mw.config.get( 'wgUserName' )+'">flaggati</a> prima come flodder in modo che le tue modifiche siano nascoste e quindi non intasino le <a href="/wiki/Special:RecentChanges">Ultime modifiche</a>, grazie!<br />Per iniziare aggiungi la lista delle pagine che vuoi cancellare nello spazio qui sotto avendo cura di metterle in colonna (una pagina per riga). <b>Nota: Gli amministratori che abuseranno di questo strumento potrebbero essere sottoposti a riconferma e perdere i privilegi!</b>'+
			'<div id="wpIsFlood"></div>'+
			'<br />'+
			'<div id="wpMassDeleteFailedContainer"></div>' +
			'<br /><br />' +
				'Pagine da cancellare (una per ogni riga):<br />' +
					'<textarea tabindex="1" accesskey="," name="wpMassDeletePages" id="wpMassDeletePages" rows="10" cols="80"></textarea>' +
				'<br /><br /><table style="background-color:transparent">' +
					'<tr><td>Ragioni più comuni:</td>' +
						'<td><select id="wpMassDeleteReasons">' +
							'<optgroup label="Criteri per la cancellazione immediata">' +
                            '<optgroup label="Ragioni più comuni per la cancellazione" id="RagioniComuni">' +

                                '</optgroup>' +
                                '<optgroup label="Altre ragioni" id="AltreRagioni">' +
								'<option value="Altra">Altre ragioni</option>' +
							'</optgroup>' +
                             '<optgroup label="Personali" id="Personali">' +
                               
                            '</optgroup>' +
                        '</optgroup>' +
						'</select></td></tr>' +
				'<tr><td>Altre/ragioni addizionali:</td>' +
					'<td><input type="text" id="wpMassDeleteReason" name="wpMassDeleteReason" maxlength="255" size="70"/></td></tr>' +
					'<tr><td><input type="button" id="wpMassDeleteSubmit" name="wpMassDeleteSubmit" value="Cancella" /></td>' +
			'</form>';
			
			if ( userGroups.indexOf( 'flood' ) !== -1 ) {
				$('#wpIsFlood').html('<br/><img src="https://upload.wikimedia.org/wikipedia/commons/b/bb/Echo_curation_alt.svg" alt="ATTENZIONE" width="25" /> <u> sei Correttamente flaggato come <b>Flood editor</b></u>');
				
			} else {
				$('#wpIsFlood').html('<br/><img src="https://upload.wikimedia.org/wikipedia/commons/e/e5/OOjs_UI_icon_alert_destructive_black-darkred.svg" alt="ATTENZIONE" width="20" /> <b>ATTENZIONE!</b> <u>non sei flaggato come <b>Flood editor</b></u>');
				
			}


		document.getElementById("wpMassDeleteReasons").onchange = function() {
			var maxlength = (document.getElementById("wpMassDeleteReasons").value == "altre" ? 255 : 252-document.getElementById("wpMassDeleteReasons").value.length); //It's 252 because of the three characters (" ()") in addition to the selected summary.
			document.getElementById("wpMassDeleteReason").setAttribute("maxlength", maxlength);
		};
		document.getElementById("wpMassDeleteSubmit").addEventListener("click", function (e) {
			doMassDelete();
		});
	}
	 
	if (config.wgNamespaceNumber == -1 &&
		config.wgTitle.toLowerCase() == "massdelete" &&
		/sysop/.test(config.wgUserGroups)
	) {
		massdeleteform();
		
		function optionInitialize ()
{
	window.Criteri = function Criteri(numero,spiegazione) {
        		$("#RagioniComuni").last().append('<option value="([[:Categoria:Da cancellare subito#'+numero+'|'+numero+']]) '+spiegazione+'">'+numero+' - '+spiegazione+'</option>')
        		}
   		
	window.AltreMotivazioniSemplici = function AltreMotivazioniSemplici(ID,spiegazione) {
        		$("#"+ID+"").last().append('<option value="'+spiegazione+'">'+spiegazione+'</option>')
        		}
        		        		
	window.AltreMotivazioni = function AltreMotivazioni(ID,testo,spiegazione) {
        		$("#"+ID+"").last().append('<option value="'+testo+'">'+spiegazione+'</option>')
        		}
        		
}
		optionInitialize();
		
function criteriInitialize ()
{
AddMotivazioniPersonali (Criteri,AltreMotivazioniSemplici,AltreMotivazioni);
AddMotivazioniComuni (Criteri,AltreMotivazioniSemplici,AltreMotivazioni);
}
// AGGIUNGO MOTIVAZIONI PERSONALIZZATE
		addOnloadHook (criteriInitialize);
		
		
		mw.util.addPortletLink(
		'p-tb',
		'/wiki/Special:Log/delete?user=' + encodeURIComponent(mw.config.get('wgUserName')),
		'Log di cancellazione',
		't-massdLog',
		'My delete log',
		'',
		'#t-massdLog'
		);
	}