Jump to content

User:Ahecht/Scripts/watchlistcleaner.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Ahecht (talk | contribs) at 22:10, 10 November 2021 (Version 1). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
// Watchlist cleaner
function cleanWatchlist() {
	var cleanMiss = confirm("Remove redlinked (missing) pages from Watchlist?\n\n(OK for yes, Cancel for no)");
	var cleanRedir = confirm("Remove redirects from Watchlist?\n\n(OK for yes, Cancel for no)");
	var unwatchPages = [], unwatchPagesCount = 0;
	var statusText = "Fetching watchlist...";

	function doUnwatch() { // Recursively unwatch pages in batches of 50
		if (unwatchPages.length > 0) { // Still have pages to unwatch
			console.log("Pages to unwatch: ");
			console.log(unwatchPages);
			statusText = "Removing " + unwatchPages.length + " pages from watchlist...";
			mw.notify(statusText, {type: 'info', tag: 'status'});
			
			var uwTitles = unwatchPages.splice(0,50).join("|"); // Remove 50 items from top of list
			var params = {
				action: "watch",
				unwatch: "true",
				titles: uwTitles
			};
			
			new mw.Api().postWithToken("watch", params ).done( function(reslt) {
				console.log("Unwatch successful: ");
				console.log(reslt);
				doUnwatch();
			} ).fail( function(code, reslt) {
				console.error("API error when unwatching pages: ");
				console.error(reslt);
				statusText = "API error when unwatching pages: " + code;
				mw.notify(statusText, {type: 'error', tag: 'status'});
				return;
			} );
		} else { // No more pages to unwatch
			statusText = "Done. Removed " + unwatchPagesCount + " pages from watchlist";
			mw.notify(statusText, {type: 'success', tag: 'status'});
		}
	}
	
	function fetchWatchlist(cont) { // Recursively fetch watchlist
		var query = {
			action: "query",
			prop: "info",
			generator: "watchlistraw",
			gwrlimit: "max",
			formatversion: "2"
		};
		if (cont) {
			query = Object.assign(query, cont);
		}
		mw.notify(statusText, {type: 'info', tag: 'status'});
		statusText = statusText + ".";
		new mw.Api().get( query )
			.done (function (d) {
				if (d && d.query && d.query.pages) { //API query returned pages
					d.query.pages.forEach( function(i) {
						if(i.ns % 2 == 0) { // Page isn't a talk page
							if(cleanMiss && i.missing){ // Add missing page to list
								mw.notify("Found missing page [[" + i.title + "]].", {type: 'warn', tag: 'found'});
								unwatchPages.push(i.title);
							} else if (cleanRedir && i.redirect) { // Add redirect to list
								mw.notify("Found redirect [[" + i.title + "]].", {type: 'warn', tag: 'found'});
								unwatchPages.push(i.title);
							}
						}
					} );
				}
				if (d && d.continue) { // More results are available
					fetchWatchlist(d.continue);
				} else { // No more results, start removing from watchlist
					unwatchPagesCount = unwatchPages.length;
					mw.notify("Found " + unwatchPagesCount + " pages to remove.", {type: 'warn', tag: 'found'});
					doUnwatch();
				}
			} ).fail (function(code, error) {
				console.error("API error when fetching watchlist: ");
				console.error(error);
				statusText = "API error fetching watchlist: " + code;
				mw.notify(statusText, {type: 'error', tag: 'status'});
			} );
		return;
	}
	
	if (cleanMiss || cleanRedir) { // Cancel wasn't selected for both options
		fetchWatchlist();
	}
}

$(document).ready( function() { // Add "Clean" link to toolbar
	if( /Watchlist$/.test(mw.config.get('wgCanonicalSpecialPageName')) ) {
		var cleanLink = ' | <a href="#" title="Run cleanwatchlist.js" id="clean-watchlist-link">Clean the watchlist</a>';
		$('.mw-watchlist-toollinks').find('a').last().after(cleanLink);
		$("#clean-watchlist-link").on("click", cleanWatchlist );
	}
} );