User:LightForce/twinkletag.js

From Simple English Wikipedia, the free encyclopedia

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)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
// If TwinkleConfig aint exist. <nowiki>
if( typeof( TwinkleConfig ) == 'undefined' ) {
	TwinkleConfig = {};
}
 
/**
 TwinkleConfig.summaryAd (string)en.wikipedia.org
 If ad should be added or not to summary, default [[WP:TWINKLE|TWINKLE]]
 */
if( typeof( TwinkleConfig.summaryAd ) == 'undefined' ) {
	TwinkleConfig.summaryAd = " [[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 = " [[WP:TW|(TW)]]";
}
 
 
/**
 TwinkleConfig.watchSpeedyPages (array)
 What types of actions that should result in forced addition to watchlist
 */
if( typeof( TwinkleConfig.watchSpeedyPages ) == 'undefined' ) {
	TwinkleConfig.watchSpeedyPages = [ 'G3', 'G10', 'G11', 'G12' ];
}
 
/**
 TwinkleConfig.markSpeedyPagesAsMinor (boolean)
 If, when applying speedy template to page, to mark the edit as minor, default true
 */
if( typeof( TwinkleConfig.markSpeedyPagesAsMinor ) == 'undefined' ) {
	TwinkleConfig.markSpeedyPagesAsMinor = true;
}
 
/**
 TwinkleConfig.notifyUserOnSpeedyDeletionNomination (array)
 What types of actions that should result that the author of the page should be notified of nomination
 */
if( typeof( TwinkleConfig.notifyUserOnSpeedyDeletionNomination ) == 'undefined' ) {
	TwinkleConfig.notifyUserOnSpeedyDeletionNomination = [ 'G1', 'G2', 'G3', 'G4', 'G10', 'G11', 'G12', 'A1', 'A2', 'A3', 'A4', 'A5', 'I1','T2' ];
} 
 
/**
 TwinkleConfig.userTalkPageMode may take arguments:
 'window': open a new window, remmenber the opened window
 'tab': opens in a new tab, if possible.
 'blank': force open in a new window, even if a such window exist
 */
if( typeof( TwinkleConfig.userTalkPageMode ) == 'undefined' ) {
	TwinkleConfig.userTalkPageMode = 'window';
}
 
/**
 TwinkleConfig.deleteTalkPageOnDelete
 If talk page if exists should also be deleted (QD G8) when speedying a page (admin only)
 */
if( typeof( TwinkleConfig.deleteTalkPageOnDelete ) == 'undefined' ) {
	TwinkleConfig.deleteTalkPageOnDelete = false;
}
 
/**
 TwinkleConfig.toolboxButtons (string)
 If id defined in this array, the button of the action is located in
the toolbox instead of in the actions bar.
 */
if( typeof( TwinkleConfig.toolboxButtons ) == 'undefined' ) {
	TwinkleConfig.toolboxButtons = [];
}
 
/**
 TwinkleConfig.orphanNormalPagesOnSpeedyDelete (hash)
 Defines if all backlinks to a page should be removed.
 property 'exclude' defined actions not to orphan
 */
if( typeof( TwinkleConfig.orphanBacklinksOnSpeedyDelete ) == 'undefined' ) {
	TwinkleConfig.orphanBacklinksOnSpeedyDelete = { exclude: ['G6'], orphan:true };
}
 
function twinkletag() {
	if( wgNamespaceNumber == 0 ) {
		twAddPortletLink( "javascript:twinkletag.callback()", "Stub", "tw-tag", "Add stub template(s)", "");
	}
		return;
}
$(twinkletag);
 
twinkletag.callback = function twinkletagCallback() {
	var Window = new SimpleWindow( 800, 400 );
	Window.setTitle( "Add stub template(s)" );
 
	var form = new QuickForm( twinkletag.callback.evaluateUser, 'change' );
 
	form.append( { type:'header', label:'General' } );
	form.append( {
			type: 'radio',
			name: 'mtpl',
			list: [
				{ 
					label: '{{Stub}}',
					value: 'Stub',
					tooltip: 'for all stubs that can not fit into any stub we have' 
				},
				{ 
					label: '{{List-stub}}',
					value: 'List-stub',
					tooltip: 'for lists only' 
				}
			]
		});
	form.append( { type:'header', label:'Countries & Geography' } );
	form.append( {
			type: 'radio',
			name: 'mtpl',
			list: [
				{ 
					label: '{{Geo-stub}}',
					value: 'Geo-stub',
					tooltip: 'all geographical locations (places, towns, cities, etc)' 
				},
				{ 
					label: '{{France-geo-stub}}',
					value: 'France-geo-stub',
					tooltip: 'France geography topics.'
				},
				{
					label: '{{US-geo-stub}}',
					value: 'US-geo-stub',
					tooltip: 'United States geography topics'
				},
				{
					label: '{{Asia-stub}}',
					value: 'Asia-stub',
					tooltip: 'for use with anything about Asia, except people.'
				},
				{
					label: '{{Japan-stub}}',
					value: 'Japan-stub',
					tooltip: 'for use with anything about Japan, except people.'
				},
				{
					label: '{{Japan-sports-bio-stub}}',
					value: 'Japan-sports-bio-stub',
					tooltip: 'Japanese sport biographies.'
				},
				{
					label: '{{Europe-stub}}',
					value: 'Europe-stub',
					tooltip: 'for use with anything about Europe, except people.'
				},
				{
					label: '{{Switzerland-stub}}',
					value: 'Switzerland-stub',
					tooltip: 'for use with anything about Switzerland, except people.'
				},
				{
					label: '{{North-America-stub}}',
					value: 'North-America-stub',
					tooltip: 'for use with anthing about North America, except people.'
				},
				{
					label: '{{US-stub}}',
					value: 'US-stub',
					tooltip: 'for use with anything about the United States, except people and geography.'
				},
				{
					label: '{{US-bio-stub}}',
					value: 'US-bio-stub',
					tooltip: 'United States biography stubs.'
				},
				{
					label: '{{US-geo-stub}}',
					value: 'US-geo-stub',
					tooltip: 'United States geography topics.'
				}
			]
		});
	form.append( { type:'header', label:'Miscellaneous' } );
	form.append( {
			type: 'radio',
			name: 'mtpl',
			list: [
				{ 
					label: '{{Food-stub}}',
					value: 'Food-stub',
				},
				{ 
					label: '{{History-stub}}',
					value: 'History-stub',
				},
				{
					label: '{{Military-stub}}',
					value: 'Military-geo-stub',
				},
				{
					label: '{{Politics-stub}}',
					value: 'Politics-stub',
				},
				{
					label: '{{Religion-stub}}',
					value: 'Religion-stub',
				},
				{
					label: '{{Transport-stub}}',
					value: 'Transport-stub',
					tooltip: 'any moving object'
				}
			]
		});

	form.append( { type:'header', label:'People' } );
	form.append( {
			type: 'radio',
			name: 'mtpl',
			list: [
				{ 
					label: '{{Actor-stub}}',
					value: 'Actor-stub',
				},
				{ 
					label: '{{US-actor-stub}}',
					value: 'US-actor-stub',
					tooltip: 'Unites States actor biographies'
				},
				{
					label: '{{Bio-stub}}',
					value: 'Bio-stub',
					tooltip: 'all people, no matter who or what profession'
				},
				{
					label: '{{US-bio-stub}}',
					value: 'US-bio-stub',
					tooltip: 'American biographies'
				},
				{
					label: '{{US-actor-stub}}',
					value: 'US-actor-stub',
					tooltip: 'Unites States actor biographies'
				},
				{
					label: '{{Sports-bio-stub}} ',
					value: 'Sports-bio-stub',
					tooltip: 'people from with sport as profession'
				},
				{
					label: '{{Japan-sports-bio-stub}} ',
					value: 'Japan-sports-bio-stub',
					tooltip: 'Japanese athlete biographies'
				}
			]
		});

	form.append( { type:'header', label:'Science' } );
	form.append( {
			type: 'radio',
			name: 'mtpl',
			list: [
				{ 
					label: '{{Sci-stub}}',
					value: 'Sci-stub',
					tooltip: 'anything science related (all branches and their tools)'
				},
				{ 
					label: '{{Biology-stub}}',
					value: 'Biology-stub',
				},
				{
					label: '{{Chem-stub}}',
					value: 'Chem-stub',
				},
				{
					label: '{{Math-stub}}',
					value: 'Math-stub',
				},
				{
					label: '{{Med-stub}}',
					value: 'Med-stub',
				},
				{
					label: '{{Physics-stub}} ',
					value: 'Physics-stub',
				},
				{
					label: '{{Weather-stub}} ',
					value: 'Weather-stub',
				}
			]
		});

	form.append( { type:'header', label:'Sports' } );
	form.append( {
			type: 'radio',
			name: 'mtpl',
			list: [
				{ 
					label: '{{Sports-stub}}',
					value: 'Sports-stub',
					tooltip: 'all sports and sports items, not people' 
				},
				{ 
					label: '{{Sports-bio-stub}}',
					value: 'Sports-bio-stub',
					tooltip: 'people from with sport as profession' 
				},
				{ 
					label: '{{Japan-sports-bio-stub}}',
					value: 'Japan-sports-bio-stub',
					tooltip: 'Japanese athlete biographies' 
				}
			]
		});

	form.append( { type:'header', label:'Technology' } );
	form.append( {
			type: 'radio',
			name: 'mtpl',
			list: [
				{ 
					label: '{{Tech-stub}}',
					value: 'Tech-stub',
					tooltip: 'All technology related stubs' 
				},
				{ 
					label: '{{Video-game-stub}}',
					value: 'Video-game-stub',
					tooltip: 'Stubs to do with video games' 
				}
			]
		});

	form.append( { type:'header', label:'The Arts' } );
	form.append( {
			type: 'radio',
			name: 'mtpl',
			list: [
				{ 
					label: '{{Performing-arts-stub}}',
					value: 'Performing-arts-stub',
					tooltip: 'general stub for the performing arts'
				},
				{ 
					label: '{{Actor-stub}}',
					value: 'Actor-stub',
				},
				{
					label: '{{US-actor-stub}}',
					value: 'US-actor-stub',
					tooltip: 'Unites States actor biographies'
				},
				{
					label: '{{Movie-stub}}',
					value: 'Movie-stub',
					tooltip: 'all movie articles except people'
				},
				{
					label: '{{Music-stub}}',
					value: 'Music-stub',
					tooltip: 'all music articles except people'
				},
				{
					label: '{{TV-stub}}',
					value: 'TV-stub',
					tooltip: 'all television articles except people'
				},
				{
					label: '{{Lit-stub}}',
					value: 'Lit-stub',
					tooltip: 'all literature articles except people'
				}
			]
		});

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

twinkletag.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 been deleted" );
				return;
			}
			var query = { 
				'title': wgPageName, 
				'action': 'submit'
			};
 
			var wikipedia_wiki = new Wikipedia.wiki( 'Tagging page', query, twinkletag.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;
 
			self.statelem.status( 'Checking for tags on the page...' );

			var tag = new RegExp('(\{\{(?:'+self.params.normalized+')\}\})').exec( text );
 
			if( tag ) {
				self.statelem.error( [ htmlNode( 'strong', tag[0] ) , " is already placed on the page." ] )
				return;
			}
 
			var code, content;
			switch( self.params.normalized ) {
 
				case "Stub":
					code = "\{\{" +  self.params.normalized+ "\}\}";
					content = text + "\n" + code;
					break;
				case "List-stub":
					code = "\{\{" +  self.params.normalized+ "\}\}";
					content = text + "\n" + code;
					break;
				case "Geo-stub":
					code = "\{\{" +  self.params.normalized+ "\}\}";
					content = text + "\n" + code;
					break;
				case "France-geo-stub":
					code = "\{\{" +  self.params.normalized+ "\}\}";
					content = text + "\n" + code;
					break;
				case "US-geo-stub":
					code = "\{\{" +  self.params.normalized+ "\}\}";
					content = text + "\n" + code;
					break;
				case "Asia-stub":
					code = "\{\{" +  self.params.normalized+ "\}\}";
					content = text + "\n" + code;
					break;
				case "Japan-stub":
					code = "\{\{" +  self.params.normalized+ "\}\}";
					content = text + "\n" + code;
					break;
				case "Japan-sports-bio-stub":
					code = "\{\{" +  self.params.normalized+ "\}\}";
					content = text + "\n" + code;
					break;
				case "Europe-stub":
					code = "\{\{" +  self.params.normalized+ "\}\}";
					content = text + "\n" + code;
					break;
				case "Switzerland-stub":
					code = "\{\{" +  self.params.normalized+ "\}\}";
					content = text + "\n" + code;
					break;
				case "North-America-stub":
					code = "\{\{" +  self.params.normalized+ "\}\}";
					content = text + "\n" + code;
					break;
				case "US-stub":
					code = "\{\{" +  self.params.normalized+ "\}\}";
					content = text + "\n" + code;
					break;
				case "US-actor-stub":
					code = "\{\{" +  self.params.normalized+ "\}\}";
					content = text + "\n" + code;
					break;
				case "US-bio-stub":
					code = "\{\{" +  self.params.normalized+ "\}\}";
					content = text + "\n" + code;
					break;
				case "US-geo-stub":
					code = "\{\{" +  self.params.normalized+ "\}\}";
					content = text + "\n" + code;
					break;
				case "Food-stub":
					code = "\{\{" +  self.params.normalized+ "\}\}";
					content = text + "\n" + code;
					break;
				case "History-stub":
					code = "\{\{" +  self.params.normalized+ "\}\}";
					content = text + "\n" + code;
					break;
				case "Military-stub":
					code = "\{\{" +  self.params.normalized+ "\}\}";
					content = text + "\n" + code;
					break;
				case "Politics-stub":
					code = "\{\{" +  self.params.normalized+ "\}\}";
					content = text + "\n" + code;
					break;
				case "Religion-stub":
					code = "\{\{" +  self.params.normalized+ "\}\}";
					content = text + "\n" + code;
					break;
				case "Transport-stub":
					code = "\{\{" +  self.params.normalized+ "\}\}";
					content = text + "\n" + code;
					break;
				case "Actor-stub":
					code = "\{\{" +  self.params.normalized+ "\}\}";
					content = text + "\n" + code;
					break;
				case "Bio-stub":
					code = "\{\{" +  self.params.normalized+ "\}\}";
					content = text + "\n" + code;
					break;
				case "Sports-bio-stub":
					code = "\{\{" +  self.params.normalized+ "\}\}";
					content = text + "\n" + code;
					break;
				case "Sci-stub":
					code = "\{\{" +  self.params.normalized+ "\}\}";
					content = text + "\n" + code;
					break;
				case "Biology-stub":
					code = "\{\{" +  self.params.normalized+ "\}\}";
					content = text + "\n" + code;
					break;
				case "Chem-stub":
					code = "\{\{" +  self.params.normalized+ "\}\}";
					content = text + "\n" + code;
					break;
				case "Math-stub":
					code = "\{\{" +  self.params.normalized+ "\}\}";
					content = text + "\n" + code;
					break;
				case "Med-stub":
					code = "\{\{" +  self.params.normalized+ "\}\}";
					content = text + "\n" + code;
					break;
				case "Physics-stub":
					code = "\{\{" +  self.params.normalized+ "\}\}";
					content = text + "\n" + code;
					break;
				case "Weather-stub":
					code = "\{\{" +  self.params.normalized+ "\}\}";
					content = text + "\n" + code;
					break;
				case "Sports-stub":
					code = "\{\{" +  self.params.normalized+ "\}\}";
					content = text + "\n" + code;
					break;
				case "Tech-stub":
					code = "\{\{" +  self.params.normalized+ "\}\}";
					content = text + "\n" + code;
					break;
				case "Video-game-stub":
					code = "\{\{" +  self.params.normalized+ "\}\}";
					content = text + "\n" + code;
					break;
				case "Performing-arts-stub":
					code = "\{\{" +  self.params.normalized+ "\}\}";
					content = text + "\n" + code;
					break;
				case "Actor-stub":
					code = "\{\{" +  self.params.normalized+ "\}\}";
					content = text + "\n" + code;
					break;
				case "Movie-stub":
					code = "\{\{" +  self.params.normalized+ "\}\}";
					content = text + "\n" + code;
					break;
				case "Music-stub":
					code = "\{\{" +  self.params.normalized+ "\}\}";
					content = text + "\n" + code;
					break;
				case "TV-stub":
					code = "\{\{" +  self.params.normalized+ "\}\}";
					content = text + "\n" + code;
					break;
				case "Lit-stub":
					code = "\{\{" +  self.params.normalized+ "\}\}";
					content = text + "\n" + code;
					break;
				default:
					code = "\{\{" +  self.params.normalized+ "\}\}";
					content = code + "\n" + text;
					break;
			}
 
			var postData = {
				'wpMinoredit': TwinkleConfig.markSpeedyPagesAsMinor ? '' : undefined,
				'wpWatchthis': (self.params.watch || form.wpWatchthis.checked ) ? '' : undefined,
				'wpStarttime': form.wpStarttime.value,
				'wpEdittime': form.wpEdittime.value,
				'wpAutoSummary': form.wpAutoSummary.value,
				'wpEditToken': form.wpEditToken.value,
				'wpSummary': "Adding stub template: {{"+self.params.normalized+"}}"+ TwinkleConfig.summaryAd,
				'wpTextbox1': content
			};
			self.post( postData );
		},
		userNotification: function( self ) {
			var form = self.responseXML.getElementById( 'editform' );
			var text = form.wpTextbox1.value;
			text += "\n\{\{subst:QD-notice|page=" + wgPageName + "|cat=" + self.params.normalized + "|reason=" + twinklespeedy.reasonHash[ self.params.value ] + "\}\} \~\~\~\~";
 
			var postData = {
				'wpMinoredit': form.wpMinoredit.checked ? '' : undefined,
				'wpWatchthis': form.wpWatchthis.checked ? '' : undefined,
				'wpStarttime': form.wpStarttime.value,
				'wpEdittime': form.wpEdittime.value,
				'wpAutoSummary': form.wpAutoSummary.value,
				'wpEditToken': form.wpEditToken.value,
				'wpSummary': 'Notification: Quick deletion nomination of \[\[' + wgPageName + '\]\].' + TwinkleConfig.summaryAd,
				'wpTextbox1': text
			};
			self.post( postData );
		}
	}
}
 
 
twinkletag.callback.evaluateUser = function twinkletagCallbackEvaluateUser(e) {
	wgPageName = wgPageName.replace( /_/g, ' ' ); // for queen/king/whatever and country!
	var value = e.target.value;
	//var normalized = twinkletag.normalizeHash[ value ];
 
	var params = {
		value: value,
		normalized: value,
		watch: false,
		usertalk: false
	};
 
	Status.init( e.target.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, twinkletag.callbacks.user.main );
	wikipedia_api.params = params;
	wikipedia_api.post();
 
}
//</nowiki>