Jump to content

MediaWiki:Gadget-globalSuppress.js

From Meta, a Wikimedia project coordination wiki

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
/**
 * Glaisher's Global Suppress Script
 * @author User:Glaisher <https://meta.wikimedia.org/wiki/User:Glaisher>
 * Workaround for https://phabricator.wikimedia.org/T25310
 */

mw.loader.using( 'mediawiki.ForeignApi' ).done( function() {
	if ( mw.config.get( 'wgCanonicalSpecialPageName' ) !== 'CentralAuth' ) {
		return;
	}

	// TODO: Make this more robust.. this is a freely modifiable text field..
	var username = $( 'form input[name=target]' ).val();

	// Add a start button. This is currently placed at the bottom of the page
	// because there isn't a nice id which could select and append to.
	$startButton = $( '<button id="mw-ca-globalsuppressblock"/>' ).text( 'Suppress all local accounts' );
	$startButton
		.appendTo( $( '#mw-content-text' ) )
		.on( 'click', function() {
			if ( !confirm( 'Are you sure you want to suppress the accounts?' ) ) {
				return;
			}
			$startButton.prop( 'disabled', true );
			initGlobalSuppressor( username );
		} );

	function initGlobalSuppressor( username ) {
		getGlobalAccountInfo( username ).done(
			function ( globalInfo ) {
				var wikis = globalInfo.merged;
				if ( !wikis ) {
					console.log( 'Invalid account!' );
					return;
				}

				if ( typeof globalInfo.hidden === 'undefined' ) {
					console.log( 'Global account is not hidden. Please suppress the account first.' );
					return;
				}

				var wikiCount = wikis.length;
				console.log( 'Starting script across ' +  wikiCount + ' wikis' );

				for ( var i = 0; i < wikiCount; i++ ) {
					var wikiInfo = wikis[i];
					// ForeignApi won't work for the current wiki
					var api = ( wikiInfo.wiki === mw.config.get( 'wgDBname' ) )
						? new mw.Api() : new mw.ForeignApi( wikiInfo.url + '/w/api.php' );
					getHiddenStatus( api, wikiInfo.wiki ).done( function ( result ) {
						if ( !result.hidden ) {
							console.log( result.wiki + ': Not hidden. Attempting block...' );
							doBlock( result.apiObj, result.wiki );
						} else {
							console.log( result.wiki + ': already hidden' );
						}
					} );
				}
			}
		);
	}

	function getGlobalAccountInfo( username ) {
		return new mw.Api().get( {
			action: 'query',
			meta: 'globaluserinfo',
			guiprop: [ 'merged' ],
			guiuser: username
		  } ).then( function ( result ) {
			return result.query.globaluserinfo;
		  } );
	}

	function getHiddenStatus( api, wikiName ) {
		 return api.get( {
			action: 'query',
			list: 'blocks',
			bkusers: username,
			bkprop: 'flags'
		 } ).then( function ( result ) {
			var blocks = result.query.blocks;
			for ( var i = 0; i < blocks.length; i++ ) {
				if ( typeof blocks[i].hidden !== 'undefined' ) {
					return { apiObj: api, wiki: wikiName, hidden: true };
				}
			}
			return { apiObj: api, wiki: wikiName, hidden: false };
		} );
	}

	function doBlock( api, wikiName ) {
		api.postWithToken( 'csrf', {
			action: 'block',
			user: username,
			expiry: 'infinite',
			reason: 'Globally suppressed account',
			nocreate: true,
			noemail: true,
			autoblock: true,
			hidename: true,
			reblock: true,
		} ).then( function () {
			console.log( 'Blocked account at ' + wikiName );
		}, function ( e ) {
			console.log( 'Blocking failed at ' + wikiName, e );
		} );
	}

} );