User:PiRSquared17/twinklexfd.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.
if( typeof( TwinkleConfig ) == 'undefined' ) {
	TwinkleConfig = {};
}

/**
 TwinkleConfig.xfdWatchDiscussion (string)
 The watchlist setting of the newly created XfD page. Either "yes", "no", or "default". Default is "default" (Duh).
 */
if( typeof( TwinkleConfig.xfdWatchDiscussion ) == 'undefined' ) {
	TwinkleConfig.xfdWatchDiscussion = "default";
}

/**
 TwinkleConfig.xfdWatchPage (string)
 The watchlist setting of the page listed for XfD. Either "yes", "no", or "default". Default is "default" (Duh).
 */
if( typeof( TwinkleConfig.xfdWatchPage) == 'undefined' ) {
	TwinkleConfig.xfdWatchPage= "default";
}

/**
 TwinkleConfig.xfdWatchUser (string)
 The watchlist setting of the user if he receives a notification. Either "yes", "no", or "default". Default is "default" (Duh).
 */
if( typeof( TwinkleConfig.xfdWatchUser ) == 'undefined' ) {
	TwinkleConfig.xfdWatchUser = "default";
}

/**
 TwinkleConfig.xfdWatchList (string)
 The watchlist setting of xfd list page, *if* the discussion is on a separate page. Either "yes", "no", or "default". Default is "no" (Hehe. Seriously though, who wants to watch it? Sorry in advance for any false positives.).
 */
if( typeof( TwinkleConfig.xfdWatchList ) == 'undefined' ) {
	TwinkleConfig.xfdWatchList = "no";
}

/**
 TwinkleConfig.xfdWatchUsages (string)
 The watchlist setting of usages of the tagged page (image, usually). Either "yes", "no", or "default". Default is "no".
 */
if( typeof( TwinkleConfig.xfdWatchUsages ) == 'undefined' ) {
	TwinkleConfig.xfdWatchUsages = "no";
}


/**
 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]]";
}

function num2order( num ) {
	switch( num ) {
	case 1: return '';
	case 2: return '2nd';
	case 3: return '3rd';
	default: return num + 'th';
	}
}
function twinklexfd() {
	if( wgNamespaceNumber < 0 || wgCurRevisionId == false ) {
		return;
	}
	if (twinkleConfigExists)
	{
		twAddPortletLink( "javascript:twinklexfd.callback()", "XFD", "tw-xfd", "Anything for deletion", "");
	}
	else
	{
		twAddPortletLink( 'javascript:alert("Your account is too new to use Twinkle.");', 'XFD', 'tw-xfd', 'Anything for deletion', '');
	}
}
window.TwinkleInit = (window.TwinkleInit || []).concat(twinklexfd); //schedule initializer

twinklexfd.callback = function twinklexfdCallback() {

	var Window = new SimpleWindow( 600, 325 );
	Window.setTitle( "Anything for deletion" );
	var form = new QuickForm( twinklexfd.callback.evaluate );
	var categories = form.append( {
			type: 'select',
			name: 'category',
			label: 'Select wanted type of category: ',
			tooltip: 'When activated, a default choice is made, based on what namespace you are in. This default should be the most appropriate',
			event: twinklexfd.callback.change_category
		} );
	categories.append( {
			type: 'option',
			label: 'RfD',
			selected: true,
			value: 'afd'
		} );
	form.append( {
			type: 'checkbox',
			list: [
				{
					label: 'Notify if possible',
					value: 'notify',
					name: 'notify',
					tooltip: 'If a notification if defined in the configuration, then notify if this is true, else no notify',
					checked: true
				}
			]
		}
	);
	form.append( {
			type: 'field',
			label:'Work area',
			name: 'work_area'
		} );

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

	// We must init the
	var evt = document.createEvent( "Event" );
	evt.initEvent( 'change', true, true );
	result.category.dispatchEvent( evt );

}

twinklexfd.callback.change_category = function twinklexfdCallbackChangeCategory(e) {
	var value = e.target.value;
	var root = e.target.form;
	var old_area;
	var childNodes = root.childNodes;
	for( var i = 0; i < childNodes.length; ++i ) {
		var node = childNodes[i];
		if( 
			node instanceof Element &&
			node.getAttribute( 'name' ) == 'work_area' 
		) {
			old_area = node;
			break;
		}
	}
	var work_area = null;
	work_area = new QuickForm.element( { 
			type: 'field',
			label: 'Requests for deletion',
			name: 'work_area'
		} );
	work_area.append( {
			type: 'checkbox',
			list: [
					{
						label: 'Wrap <noinclude>',
						value: 'noinclude',
						name: 'noinclude',
						tooltip: 'Will wrap the template in <noinclude> tags, so that it won\'t transclude'
					}
				]
	} );

	work_area.append( {
			type: 'textarea',
			name: 'xfdreason',
			label: 'Reason: '
	} );
	work_area.append( { type:'submit' } );
	work_area = work_area.render();
	old_area.parentNode.replaceChild( work_area, old_area );
}

twinklexfd.callbacks = {
	afd: {
		main: function ( self ) {
			var year = (new Date()).getUTCFullYear();
			var xmlDoc = self.responseXML;
			var titles = xmlDoc.evaluate( '//allpages/p/@title', xmlDoc, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null );

			// There has been no earlier entries with this prefix, just go on.
			if( titles.snapshotLength <= 0 ) {
				self.params.numbering = self.params.number = '';
				numbering = number = '';
			} else {
				var number = 0;
				for( var i = 0; i < titles.snapshotLength; ++i ) {
					var title = titles.snapshotItem(i).value;

					// First, simple test, is there an instance with this exact name?
					if( title == 'Wikipedia:Requests for deletion/Requests/' + year + '/' + wgPageName ) {
						number = Math.max( number, 1 );
						continue;
					}

					var order_re = new RegExp( '^' +
							RegExp.escape( 'Wikipedia:Requests for deletion/Requests/' + year + '/' + wgPageName, true ) +
							'\\s*\\(\\s*(\\d+)(?:(?:th|nd|rd|st) nom(?:ination)?)?\\s*\\)\\s*$'
					);
					var match = order_re.exec( title );

					// No match; A non-good value
					if( match == null ) {
						continue;
					}

					// A match, set number to the max of current
					number = Math.max( number, Number(match[1]) );
				}
				self.params.number = num2order( parseInt( number ) + 1);
				self.params.numbering = number > 0 ? ' (' + self.params.number + ' nomination)' : '';
			}

			Status.info( 'Next discussion page","[[Wikipedia:Requests for deletion/Requests/' + year + '/' + wgPageName + self.params.numbering + ']]' );

			// Discussion page
			var query = {
				'title': 'Wikipedia:Requests for deletion/Requests/' + year + '/' + wgPageName,
				'action': 'submit'
			};

			// Updating data for the action completed event
			Wikipedia.actionCompleted.redirect = query['title'];
			Wikipedia.actionCompleted.notice = "Nomination completed, now redirecting to the discussion page";

			var wikipedia_wiki = new Wikipedia.wiki( 'Creating article deletion discussion page', query, twinklexfd.callbacks.afd.discussionPage );
			wikipedia_wiki.params = self.params;
			wikipedia_wiki.get();

			// Tagging article
			var query = {
				'title': wgPageName,
				'action': 'submit'
			};
			var wikipedia_wiki = new Wikipedia.wiki( 'Adding deletion tag to article', query, twinklexfd.callbacks.afd.article );
			wikipedia_wiki.params = self.params;
			wikipedia_wiki.get();

			// Today's list
			var date = new Date();

			query = {
				'title': 'Wikipedia:Requests for deletion',
				'action': 'submit'
			};

			var wikipedia_wiki = new Wikipedia.wiki( 'Adding discussion to today\'s list', query, twinklexfd.callbacks.afd.todaysList );
			wikipedia_wiki.params = self.params;
			wikipedia_wiki.followRedirect = true;
			wikipedia_wiki.get();

			// Notification to first contributor

			var query = {
				'action': 'query',
				'prop': 'revisions',
				'titles': wgPageName,
				'rvlimit': 1,
				'rvprop': 'user',
				'rvdir': 'newer'
			}
			var callback = function( self ) {
				var xmlDoc = self.responseXML;
				var user = xmlDoc.evaluate( '//rev/@user', xmlDoc, null, XPathResult.STRING_TYPE, null ).stringValue;
				var query = {
					'title': 'User talk:' + user,
					'action': 'submit'
				};
				var wikipedia_wiki = new Wikipedia.wiki( 'Notifying initial contributor (' + user + ')', query, twinklexfd.callbacks.afd.userNotification );
				wikipedia_wiki.params = self.params;
				wikipedia_wiki.followRedirect = true;
				wikipedia_wiki.get();
			}
			
			if( self.params.usertalk ) {
				var wikipedia_api = new Wikipedia.api( 'Grabbing data of initial contributor', query, callback );
				wikipedia_api.params = self.params;
				wikipedia_api.post();
			}
		},
		article: function( self ) {
			var form = self.responseXML.getElementById('editform');

			//remove some tags that should always be removed on AfD.
			text = form.wpTextbox1.value;
			//then, test if there are SD related templates on the article.
			textNoSd = text.replace(/{\{\s*(db(-\w*)?|wait|qd|(?:hang|hold)[- ]?on)\s*(\|(?:{{[^{}]*}}|[^{}])*)?}}\s*/ig, "");
			if (text!=textNoSd && confirm("Speedy Deletion templates were found on this page. Should they be removed?")) text=textNoSd;
			form.wpTextbox1.value = text;

			var postData = {
				'wpMinoredit': undefined, // Per memo
				'wpWatchthis': (TwinkleConfig.xfdWatchPage=="yes" || (TwinkleConfig.xfdWatchPage=="default"&&form.wpWatchthis.checked) ? '' : undefined),
				'wpStarttime': form.wpStarttime.value,
				'wpEdittime': form.wpEdittime.value,
				'wpAutoSummary': form.wpAutoSummary.value,
				'wpEditToken': form.wpEditToken.value,
				'wpSection': form.wpSection.value,
				'wpSummary': "Nominated for deletion; see [[" + 'Wikipedia:Requests for deletion/Requests/' + ((new Date()).getUTCFullYear()) + '/' + wgPageName + ']].'+ TwinkleConfig.summaryAd,
				'wpTextbox1': ( self.params.noinclude ? "<noinclude>" : "" ) + "\{\{rfd|" + self.params.reason + "\}\}\n" + ( self.params.noinclude ? "</noinclude>" : "" ) + form.wpTextbox1.value
			};
			self.post( postData );
		},
		discussionPage: function( self ) {
			var form = self.responseXML.getElementById('editform');
			var postData = {
				'wpMinoredit': (typeof form.wpMinoredit == "undefined") ? undefined : (form.wpMinoredit.checked ? '' : undefined),
				'wpWatchthis': (TwinkleConfig.xfdWatchDiscussion=="yes" || (TwinkleConfig.xfdWatchDiscussion=="default"&&form.wpWatchthis.checked) ? '' : undefined),
				'wpStarttime': form.wpStarttime.value,
				'wpEdittime': form.wpEdittime.value,
				'wpAutoSummary': form.wpAutoSummary.value,
				'wpEditToken': form.wpEditToken.value,
				'wpSection': form.wpSection.value,
				'wpSummary': "Creating deletion discussion page for \[\[" + wgPageName + '\]\].' + TwinkleConfig.summaryAd,
				'wpTextbox1': "\{\{subst:RfD/Preload/Template|deletereason=" + self.params.reason + "\}\}\n"
			};
			self.post( postData );
		},
		todaysList: function( self ) {
			var form = self.responseXML.getElementById('editform');
			var old_text = form.wpTextbox1.value;

			var text = old_text.replace( /(<\!-- Add new entries to the TOP of the following list -->\n+)/, "$1\{\{Wikipedia:Requests for deletion/Requests/" + ((new Date()).getUTCFullYear()) + '/' + wgPageName + self.params.numbering + "\}\}\n");
			if( text == old_text ) {
				self.statelem.error( 'failed to find target spot for the discussion' );
				return;
			}
			var postData = {
				'wpMinoredit': form.wpMinoredit.checked ? '' : undefined,
				'wpWatchthis': (TwinkleConfig.xfdWatchList=="yes" || (TwinkleConfig.xfdWatchList=="default"&&form.wpWatchthis.checked) ? '' : undefined),
				'wpStarttime': form.wpStarttime.value,
				'wpEdittime': form.wpEdittime.value,
				'wpAutoSummary': form.wpAutoSummary.value,
				'wpEditToken': form.wpEditToken.value,
				'wpSection': form.wpSection.value,
				'wpSummary': "Adding \[\[Wikipedia:Requests for deletion/Requests/" + ((new Date()).getUTCFullYear()) + '/' + wgPageName + self.params.numbering + '\|AfD\]\] for \[\[' + wgPageName + '\]\].' + TwinkleConfig.summaryAd,
				'wpTextbox1': text
			};
			self.post( postData );
		},
		userNotification: function( self ) {
			var form = self.responseXML.getElementById( 'editform' );
			var text = form.wpTextbox1.value;
			text += "\n\{\{subst:RFDNote|1=" + wgPageName + "|2=" + wgPageName + ( self.params.numbering != '' ? ' ' + self.params.numbering : '' ) + "\}\} \~\~\~\~";
			var postData = {
				'wpMinoredit': (typeof form.wpMinoredit == "undefined") ? undefined : (form.wpMinoredit.checked ? '' : undefined),
				'wpWatchthis': (TwinkleConfig.xfdWatchUser=="yes" || (TwinkleConfig.xfdWatchUser=="default"&&form.wpWatchthis.checked) ? '' : undefined),
				'wpStarttime': form.wpStarttime.value,
				'wpEdittime': form.wpEdittime.value,
				'wpAutoSummary': form.wpAutoSummary.value,
				'wpEditToken': form.wpEditToken.value,
				'wpSection': form.wpSection.value,
				'wpSummary': 'AfD nomination of \[\[' + wgPageName + '\]\].' + TwinkleConfig.summaryAd,
				'wpTextbox1': text
			};
			self.post( postData );
			// TODO:...DONE rfd user talk page warning template
		}
	}
}

twinklexfd.callback.evaluate = function(e) {

	wgPageName = wgPageName.replace( /_/g, ' ' ); // for queen/king/whatever and country!

	var type =  e.target.category.value;
	var usertalk = e.target.notify.checked;
	var reason = e.target.xfdreason.value;
	var noinclude = e.target.noinclude.checked;
	
	Status.init( e.target );

	if( type == null ) {
		Status.error( 'Error', 'no action given' );
		return;
	}

	var query = {
		'action': 'query',
		'list': 'allpages',
		'apprefix': 'Requests for deletion/Requests/' + ((new Date()).getUTCFullYear()) + '/' + wgPageName,
		'apnamespace': 4,
		'apfilterredir': 'nonredirects',
		'aplimit': userIsInGroup( 'sysop' ) ? 5000 : 500
	};
	var wikipedia_api = new Wikipedia.api( 'Tagging article with deletion tag', query, twinklexfd.callbacks.afd.main );
	wikipedia_api.params = { usertalk:usertalk, reason:reason, noinclude:noinclude };
	wikipedia_api.post();
}