사용자:Theoteryi/twinkleprod.js

참고: 설정을 저장한 후에 바뀐 점을 확인하기 위해서는 브라우저의 캐시를 새로 고쳐야 합니다. 구글 크롬, 파이어폭스, 마이크로소프트 엣지, 사파리: ⇧ Shift 키를 누른 채 "새로 고침" 버튼을 클릭하십시오. 더 자세한 정보를 보려면 위키백과:캐시 무시하기 항목을 참고하십시오.

// If TwinkleConfig aint exist.
if( typeof( TwinkleConfig ) == 'undefined' ) {
	TwinkleConfig = {};
}
 
/**
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]]";
}
 
/**
TwinkleConfig.watchProdPages (boolean)
If, when applying prod template to page, watch it, default true
*/
if( typeof( TwinkleConfig.watchProdPages ) == 'undefined' ) {
	TwinkleConfig.watchProdPages = true;
}
 
function twinkleprod() {
	if( wgNamespaceNumber != 0 || wgCurRevisionId == false ) {
		return;
	}
	if (twinkleConfigExists)
	{
		mw.util.addPortletLink( 'p-cactions', "javascript:twinkleprod.callback()", "prod", "tw-prod", "Propose deletion via WP:PROD", "");
	}
	else
	{
		mw.util.addPortletLink('p-cactions', 'javascript:alert("Your account is too new to use Twinkle.");', 'prod', 'tw-prod', 'Propose deletion via WP:PROD', '');
	}
}
$(twinkleprod);
 
twinkleprod.callback = function twinkleprodCallback() {
	var Window = new SimpleWindow( 800, 400 );
	Window.setTitle( "WP:PROD" );
	var form = new QuickForm( twinkleprod.callback.evaluate );
	form.append( {
			type: 'checkbox',
			list: [
				{
					label: 'Notify if possible',
					value: 'notify',
					name: 'notify',
					tooltip: 'If a notification is defined in the configuration, then notify if this is true, else no notify',
					checked: true
				}
			]
		}
	);
	var field = form.append( {
			type: 'field',
			label: 'Reason for proposed deletion'
		} );
	field.append( {
			type: 'textarea',
			name: 'reason',
			label: 'Reason:'
		} );
	field.append( { type:'submit' } );
 
	var result = form.render();
	Window.setContent( result );
	Window.display();
}
 
twinkleprod.callbacks = {
	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, twinkleprod.callbacks.tagPage );
		wikipedia_wiki.params = self.params;
		wikipedia_wiki.followRedirect = false;
		wikipedia_wiki.get();
	},
	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" );
		}
 
		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 );
	},
	userNotification: function( self ) {
		var form = this.responseXML.getElementById( 'editform' );
		var text = form.wpTextbox1.value;
		text += "\n\{\{subst:prodwarning|1=" + wgPageName + "|concern=" + self.params.reason + "\}\} \~\~\~\~";
		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': 'PROD nomination of \[\[' + wgPageName + '\]\].' + TwinkleConfig.summaryAd,
			'wpTextbox1': text
		};
 
		self.post ( postData );
	}
}
 
twinkleprod.callback.evaluate = function twinkleprodCallbackEvaluate(e) {
	wgPageName = wgPageName.replace( /_/g, ' ' ); // for queen/king/whatever and country!
	var form = e.target;
	var usertalk = form.notify.checked
	var reason = form.reason.value;
 
	var params = {
		usertalk: usertalk,
		reason: reason
	};
 
	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, twinkleprod.callbacks.main );
	wikipedia_api.params = params;
	wikipedia_api.post();
}