User:Greg Tyler/categorise.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
Documentation for this user script can be added at User:Greg Tyler/categorise. |
// 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();
}