User:AzaToth/whackaspammer.js

From Wikipedia, the free encyclopedia
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.
// If TwinkleConfig aint exist.
if( typeof( TwinkleConfig ) == 'undefined' ) {
	TwinkleConfig = {};
}

/**
 TwinkleConfig.summaryAd (string)
 If ad should be added or not to summary, default [[WP:TWINKLE|TWINKLE]]
 */
if( typeof( TwinkleConfig.summaryAd ) == 'undefined' ) {
	TwinkleConfig.summaryAd = " using [[WP:TW|TW]]";
}

/**
 TwinkleConfig.deletionSummaryAd (string)
 If ad should be added or not to deletion summary, default [[WP:TWINKLE|TWINKLE]]
 */
if( typeof( TwinkleConfig.deletionSummaryAd ) == 'undefined' ) {
	TwinkleConfig.deletionSummaryAd = " using [[WP:TW|TW]]";
}

function whackaspammer() {
	mw.util.addPortletLink( 'p-cactions', "javascript:whackaspammer.callback()", "whackaspammer", "tw-csd", "Whack a spammer", "");
}
addOnloadHook(whackaspammer);

whackaspammer.callback = function twinklesbatchdeleteCallback() {
	var Window = new SimpleWindow( 800, 400 );
	Window.setTitle( "Whack A Spammer!" );
	var form = new QuickForm( whackaspammer.callback.evaluate );

	form.append( {
			type: 'textarea',
			name: 'reason',
			label: 'Reason: '
		} );
	form.append( { type:'submit' } )
	var result = form.render();
	Window.setContent( result );
	Window.display();
}
whackaspammer.callback.evaluate = function whackaspammerCallbackEvaluate(event) {

	var reason = event.target.reason.value;
	Status.init( event.target );	
	var query = { 
		'action': 'query',
		'prop': 'info',
		'intoken': 'delete',
		'titles': wgPageName
	};

	var wikipedia_api = new Wikipedia.api( 'Grabbing delete token', query, function( self ) {
			var xmlDoc = self.responseXML;
			var token = xmlDoc.evaluate( '//page/@deletetoken', xmlDoc, null, XPathResult.STRING_TYPE, null ).stringValue;

			var query = { 
				'action': 'delete',
				'token': token,
				'title': wgPageName,
				'reason': "Speedy deleted per [[WP:CSD#G11|CSD G11]], was unambiguous [[WP:Spam|advertising]] or promotion" + reason + TwinkleConfig.deletionSummaryAd
			};
			var wikipedia_api = new Wikipedia.api( 'deleting page', query, function( self ) {} );
			wikipedia_api.post();

		});
	wikipedia_api.post();
	var query = { 
		'action': 'query',
		'prop': 'info',
		'intoken': 'block',
		'titles': wgTitle
	};

	var wikipedia_api = new Wikipedia.api( 'Grabbing block token', query, function( self ) {
			var xmlDoc = self.responseXML;
			var token = xmlDoc.evaluate( '//page/@blocktoken', xmlDoc, null, XPathResult.STRING_TYPE, null ).stringValue;

			var query = { 
				'action': 'block',
				'token': token,
				'autoblock': true,
				'nocreate': true,
				'allowusertalk': true,
				'user': wgTitle,
				'reason': "[[WP:Spam|Spam]] / [[WP:Advertising|advertising]]-only account" + reason
			};
			var wikipedia_api = new Wikipedia.api( 'blcoking user', query, function( self ) {} );
			wikipedia_api.post();

		});
	wikipedia_api.post();
}