Jump to content

User:Greg Tyler/categorise.js

From Wikipedia, the free encyclopedia
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
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.
// DISCLAIMER THING
//
// This script is an extension of Twinkle (WP:TW), using the inner-workings of the Twinkle program
// to do a new task (categorising AfD discussions).
//
// I DO NOT recommend using this yourself. I'm a fairly novice javascript coder and there may be
// several things wrong with it.
//
// I use this program as a shortcut, because I'm lazy. I wouldn't recommend this, as it'll probably
// generate more problems than it solves.
//
// If you find this script working incorrectly under my control, tell me about it at my talk page.
// If you find this script working incorrectly under /someone else/'s control, tell them about it
// and remind them that I don't condone its use at all.
//
// Happy editing!
//


if( typeof( TwinkleConfig ) == 'undefined' ) {
	TwinkleConfig = {};
}
TwinkleConfig.summaryAd = " ([[User:Greg Tyler/categorise.js|TW extension]])";

function twinklecategorise() {
	if( wgNamespaceNumber != 4 ) return;

	mw.util.addPortletLink( 'p-cactions', "javascript:twinklecategorise.callback()", "cat", "tw-cat", "Categorise deletion discussion", "");
}
addOnloadHook(twinklecategorise);

twinklecategorise.callback = function twinklecategoriseCallback() {
	var Window = new SimpleWindow( 800, 500 );
	Window.setTitle( "Choose category" );

	var form = new QuickForm( twinklecategorise.callback.evaluate );

		form.append( { type:'header', label:'Select a category' } );
		form.append ( {
				type: 'radio',
				name: 'cat',
				list: [
					{ 
						label: 'Media and music',
						value: 'M'
					},
					{ 
						label: 'Organisation, corporation, or product',
						value: 'O'
					},
					{ 
						label: 'Biographical',
						value: 'B'
					},
					{
						label: 'Society topics', 
						value: 'S'
					},
					{
						label: 'Web or internet',
						value: 'W'
					},
					{
						label: 'Games or sports',
						value: 'G'
					},
					{ 
						label: 'Science and technology',
						value: 'T'
					},
					{
						label: 'Fiction and the arts',
						value: 'F'
					},
					{
						label: 'Places and transportation',
						value: 'P'
					}
				]
			} );
	form.append( { type:'submit', label:'Categorise' } );

	var result = form.render();
	Window.setContent( result );
	Window.display();
};

twinklecategorise.catHash = {
	'M': 'Media and music',
	'O': 'Organisation, corporation, or product',
	'B': 'Biographical',
	'S': 'Society topics',
	'W': 'Web or internet',
	'G': 'Games or sports',
	'T': 'Science and technology',
	'F': 'Fiction and the arts',
	'P': 'Places and transportation',
}


twinklecategorise.callbacks = {
	user: {
		main: function( self ) {
			var xmlDoc = self.responseXML;
 
			var exists = xmlDoc.evaluate( 'boolean(//pages/page[not(@missing)])', xmlDoc, null, XPathResult.BOOLEAN_TYPE, null ).booleanValue;
 
			if( ! exists ) {
				self.statelem.error( "It seems that the page doesn't exists, perhaps it has already been deleted" );
				return;
			}
			var query = { 
				'title': wgPageName, 
				'action': 'submit'
			};
 
			var wikipedia_wiki = new Wikipedia.wiki( 'Tagging page', query, twinklecategorise.callbacks.user.tagPage );
			wikipedia_wiki.params = self.params;
			wikipedia_wiki.followRedirect = false;
			wikipedia_wiki.get();
		},

		tagPage: function( self ) {
			form = this.responseXML.getElementById( 'editform' );
 
			var text = form.wpTextbox1.value;
 
			var tag = /\{\{REMOVE THIS TEMPLATE WHEN CLOSING THIS AfD\|([^\r\n]*)\}\}/.exec( text );
			if( tag == null ) {
				self.statelem.error( [ htmlNode( 'strong', '' ) , "Template not found!" ] );
				return;
			}

			categoryName = twinklecategorise.catHash[ self.params.value ];
			text = text.replace(/\{\{REMOVE THIS TEMPLATE WHEN CLOSING THIS AfD\|([^\r\n]*)\}\}/g,"\{\{REMOVE THIS TEMPLATE WHEN CLOSING THIS AfD|" + self.params.value + "\}\}");

			var summaryText = "Categorising deletion discussion to [[:Category:AfD debates (" + categoryName + ")]].";
			

		var postData = {
			'wpMinoredit': undefined,
			'wpWatchthis': undefined,
			'wpStarttime': form.wpStarttime.value,
			'wpEdittime': form.wpEdittime.value,
			'wpAutoSummary': form.wpAutoSummary.value,
			'wpEditToken': form.wpEditToken.value,
			'wpSection': '',
			'wpSummary': summaryText + TwinkleConfig.summaryAd,
			'wpTextbox1': text
		};
		self.post( postData );
		TwinkleConfig.summaryAd = " ([[WP:TW|TW]])";
		}
	}
};

twinklecategorise.callback.evaluate = function twinklecategoriesCallbackEvaluate(e) {
	wgPageName = wgPageName.replace( /_/g, ' ' ); // for queen/king/whatever and country!
	var form = e.target;
 
	var params = {
		value: ''
	};

	var l = form.cat.length;
	for(var i = 0; i < l; i++) {
		if(form.cat[i].checked) {
			params.value = form.cat[i].value;
		}
	}

 
	Status.init( form );
 
	Wikipedia.actionCompleted.redirect = wgPageName;
	Wikipedia.actionCompleted.notice = "Tagging complete";
 
	var query = { 
		'action': 'query',
		'titles': wgPageName
	};
 
	var wikipedia_api = new Wikipedia.api( 'Checking if page exists', query, twinklecategorise.callbacks.user.main );
	wikipedia_api.params = params;
	wikipedia_api.post();
}