Jump to content

User:Greg Tyler/categorise.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Greg Tyler (talk | contribs) at 18:54, 30 June 2009 (You need to create the config /first/?). 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.
if( typeof( TwinkleConfig ) == 'undefined' ) {
	TwinkleConfig = {};
}
TwinkleConfig.summaryAd = "([[User:Greg Tyler/categorise.js|TW extension]])";

function twinklecategorise() {
	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\|(.*?)\}\}/.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\|(.*?)\}\}/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 );
		}
	}
};

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();
}