Usuário:DarwIn/nukeRedirectsBox.js

Nota: Depois de publicar, poderá ter de contornar a cache do seu navegador para ver as alterações.

  • Firefox / Safari: Pressione Shift enquanto clica Recarregar, ou pressione Ctrl-F5 ou Ctrl-R (⌘-R no Mac)
  • Google Chrome: Pressione Ctrl-Shift-R (⌘-Shift-R no Mac)
  • Edge: Pressione Ctrl enquanto clica Recarregar, ou pressione Ctrl-F5.
//Tool for nuking all unused redirects created by a certain user
//by [[User:Tks4Fish]], requested by [[User:DarwIn]],
//inspired by [[User:Martin Urbanec]] and [[User:Tks4Fish]]'s [[User:Tks4Fish/nukeAll.js]]
//adaptação do original aqui: mw.loader.load("//meta.wikimedia.org/w/index.php?title=User:Tks4Fish/nukeRedirectsBox.js&action=raw&ctype=text/javascript");

function nukeRedirects() {
	let revids = [];
	
	$("input.revdelIds:checked").each(function() {
	    revids.push($(this).attr("value"));
	});
	
	let titles = [];
	
	new mw.Api().get({
		"action": "query",
		"format": "json",
		"prop": "info",
		"revids": revids,
		"formatversion": "2"
	}).then(function(data) {
		for(var i = 0; i < data.query.pages.length; i++) {
        	titles.push( data.query.pages[i].title );
	    }
	    
	    let deletable = [];
	
		new mw.Api().get({
			"action": "query",
			"format": "json",
			"prop": "info|linkshere",
			"titles": titles,
			"lhprop": "title",
			"lhnamespace": "0",
			"formatversion": "2"
		}).then(function(data) {
	        for(var i = 0; i < data.query.pages.length; i++) {
	            if (data.query.pages[i].linkshere === undefined && data.query.pages[i].redirect === true) {
	        		deletable.push( data.query.pages[i].pageid );
	            }
		    }
		    reason = "[[WP:ER#ERr1|R1]] - Redirecionamento indevido, desnecessário, sem afluentes, para páginas inexistentes ou eliminadas";
			nukeRedirectsProcess(deletable, reason).then(() => {
				mw.notify(countDeleted + ' redirects were successfully deleted.');
				mw.notify("Refreshing...").then(
					setTimeout(function(){
						location.reload(true);
					}, 1000)
				);
			});
		});
	});
}

function nukeRedirectsProcess(pages, reason) {
	if ( pages.length === 0 ) {
		return $.Deferred().resolve();
	}
	let id = pages.shift();
	
	return new mw.Api().postWithEditToken({
		"action": "delete",
		"pageid": id,
	    "reason": reason,
	}).then(function(data){
		if (data.error !== undefined) {
			console.log(data);
		}
		else {
			countDeleted = countDeleted + 1;
		}
		return nukeRedirectsProcess(pages, reason);
	});
}

var countDeleted = 0;

$(document).ready(function()
{
	if( (mw.config.get("wgCanonicalSpecialPageName") == "Contributions") && (/sysop/.test(mw.config.get("wgUserGroups")) ) )
	{
		mw.loader.using("mediawiki.util").done( function ()
		{
			mw.util.addPortletLink('p-cactions','#',"nuke redirects","ca-nukeredirects","nuke all redirects displayed here");
			$("#ca-nukeredirects").click( function(event)
				{
					if (confirm("Are you sure you want to these redirects?"))
					{
						event.preventDefault();
						return nukeRedirects();
					}
					else return;
			});
		});
	}
});