User:LinguistAtLarge/twinkleprodlog.js

From Wikipedia, the free encyclopedia
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.
// Available configuration variables

// To enable logging of all the pages you propose for deletion, set this variable to the name of a sub-page in your user space where you want the pages to be logged to
//TwinkleConfig.logProdPage = '';

// If you want to change the format for each line of the log, you can to it here (for example if you want to format it as a list, table etc.)
//TwinkleConfig.logProdLineTemplate = "\*\[\[%PAGENAME%\]\] — %REASON% (%PRODTYPE%) — \~\~\~\~\~";

// Set this to true to add new lines at the bottom of your log file, or false to add new lines to the top
//TwinkleConfig.logProdAddBottom = true;


if (twinkleprod){(function(){

	// save a backup copy for the fun of it
	twinkleprod.callbacks.tagPageOriginal = twinkleprod.callbacks.tagPage;

	// replace the tagPage function; identical to the original except for the prodlog section
	twinkleprod.callbacks.tagPage = function(self)
	{
		var form = self.responseXML.getElementById('editform');
		var text = form.wpTextbox1.value;
 
		var tag_re = /(\{\{(?:db-?|delete|[aitcmrs]fd|md1)[^{}]*?\|?[^{}]*?\}\})/i;
		if( tag_re.test( text ) ) {
			self.statelem.warn( 'Page already tagged with a deletion template, aborting procedure' );
			return;
		}
 
		var prod_re = /\{\{dated prod.*?\}\}/i;
		if( !prod_re.test( text ) ) {
			// 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 of initial contributor (' + user + ')', query, twinkleprod.callbacks.userNotification );
				wikipedia_wiki.params = self.params;
				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();
			}
 
			var summaryText = "Proposing article for deletion per [[WP:PROD]].";
			text = "\{\{subst:prod|1=" + self.params.reason + "\}\}\n" + text;
		} else {
			var prod2_re = /\{\{prod-?2.*?\}\}/;
			if( prod2_re.test( text ) ) {
				self.statelem.warn( 'Page already tagged with \{\{prod\}\} and \{\{prod-2\}\} templates, aborting procedure' );
				return;
			}
			self.statelem.info( "A \{\{dated prod\}\} tag was already found on this article" );
			if( !confirm( "Would you like to add a \{\{prod-2\}\} tag with your reason?" ) ) {
				self.statelem.info( 'Aborted per user request' );
				return;
			}
 
			var summaryText = "Endorsing proposed deletion per [[WP:PROD]].";
			text = text.replace( prod_re, text.match( prod_re ) + "\n\{\{subst:prod-2|1=" + self.params.reason + "\}\}\n" );
		}

		// ###################################################### 
		// log this action to the user's prodlog
		// ######################################################
		if( typeof( TwinkleConfig.logProdPage ) == 'undefined' )
		{
			TwinkleConfig.logProdPage = '';
		}
		if( typeof( TwinkleConfig.logProdLineTemplate ) == 'undefined' )
		{
			TwinkleConfig.logProdLineTemplate = "\*\[\[%PAGENAME%\]\] — %REASON% (%PRODTYPE%) — \~\~\~\~\~";
		}
		if( typeof( TwinkleConfig.logProdAddBottom ) == 'undefined' )
		{
			TwinkleConfig.logProdAddBottom = true;
		}

		if (TwinkleConfig.logProdPage)
		{
			self.params.prod_type = 'Endorsement';
			if (typeof(prod2_re) == 'undefined')
			{
				self.params.prod_type = 'Proposal'
			}
 
			var prodlogcallback = function(self)
			{
				var form = this.responseXML.getElementById( 'editform' );

				var text = TwinkleConfig.logProdLineTemplate.replace(/%PAGENAME%/, wgPageName);
				text = text.replace(/%REASON%/, self.params.reason);
				text = text.replace(/%PRODTYPE%/, self.params.prod_type);
				if (TwinkleConfig.logProdAddBottom)
				{
					text = form.wpTextbox1.value + text;
				}
				else
				{
					text += "\n" + form.wpTextbox1.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,
					'wpSection': '',
					'wpSummary': 'Logging PROD nomination of \[\[' + wgPageName + '\]\].' + TwinkleConfig.summaryAd,
					'wpTextbox1': text,
				};
				self.post(postData);
			};
			var prodlogquery = {
				'title': 'User:' + wgUserName + '/' + TwinkleConfig.logProdPage,
				'action': 'submit'
			};
			var prodlog_wiki = new Wikipedia.wiki('Logging page to PROD log.', prodlogquery, prodlogcallback);
			prodlog_wiki.params = self.params;
			prodlog_wiki.get();
		}
		// ###################################################### 
		// done logging this action to the user's prodlog
		// ###################################################### 

		var postData = {
			'wpMinoredit': undefined, // Per memo
			'wpWatchthis': TwinkleConfig.watchProdPages ? '' : form.wpWatchthis.checked ? '' : 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 );
	};

	// add fake prod tab (so I can test this in my sandbox)
	//addOnloadHook(function(){addPortletLink( 'p-cactions', "javascript:twinkleprod.callback()", "fake prod", "tw-fake-prod", "Propose deletion via WP:PROD", "")});

})();}