Difference between revisions 40378 and 40379 on zhwikivoyage

//<nowiki>
// vim: set noet sts=0 sw=8:


(function($){


/*
 ****************************************
 *** twinkleprotect.js: Protect/RPP module
 ****************************************
 * Mode of invocation:     Tab ("PP"/"RPP")
 * Active on:              Non-special pages
 * Config directives in:   TwinkleConfig
 */

// Note: a lot of code in this module is re-used/called by batchprotect.

Twinkle.protect = function twinkleprotect() {
	if ( mw.config.get('wgNamespaceNumber') < 0 ) {
		return;
	}

	Twinkle.addPortletLink(Twinkle.protect.callback, Morebits.userIsInGroup('sysop') ? "保护" : "保护", "tw-rpp",
		Morebits.userIsInGroup('sysop') ? "保护页面" : "请求保护页面" );
};

Twinkle.protect.callback = function twinkleprotectCallback() {
	Twinkle.protect.protectionLevel = null;

	var Window = new Morebits.simpleWindow( 620, 530 );
	Window.setTitle( Morebits.userIsInGroup( 'sysop' ) ? "施行或请求保护页面" : "请求保护页面" );
	Window.setScriptName( "Twinkle" );
	Window.addFooterLink( "保护模板", "Template:Protection templates" );
	Window.addFooterLink( "保护方针", "WP:PROT" );
	Window.addFooterLink( "Twinkle帮助", "WP:TW/DOC#protect" );

	var form = new Morebits.quickForm( Twinkle.protect.callback.evaluate );
(contracted; show full)
	// We must init the controls
	var evt = document.createEvent( "Event" );
	evt.initEvent( 'change', true, true );
	result.actiontype[0].dispatchEvent( evt );

	// get current protection level asynchronously
	
Morebits.wiki.actionCompleted.postfix = false;  // avoid Action: completed notice
	if (Morebits.userIsInGroup('sysop')) {
		var query = {
			action: 'query',
			prop: 'info',
			inprop: 'protection',
			titles: mw.config.get('wgPageName')
		};
		Morebits.status.init($('div[name="currentprot"] span').last()[0]);
		var statelem = new Morebits.status("当前保护级别");
		var wpapi = new Morebits.wiki.api("抓取…", query, Twinkle.protect.callback.protectionLevel, statelem);
		wpapi.post();
	}
};

Twinkle.protect.protectionLevel = null;

Twinkle.protect.callback.protectionLevel = function twinkleprotectCallbackProtectionLevel(apiobj) {
	var xml = apiobj.getXML();
	var result = [];

	$(xml).find('pr, flagged').each(function(index, protectionEntry) {
		var $protectionEntry = $(protectionEntry);
		var type, level, expiry, cascade = false;
		
		type = Morebits.string.toUpperCaseFirstChar($protectionEntry.attr('type'));
		level = $protectionEntry.attr('level');
		expiry = $protectionEntry.attr('expiry');
		cascade = $protectionEntry.attr('cascade') === '';
		
		var boldnode = document.createElement('b');
		boldnode.textContent = type + ": " + level;
		result.push(boldnode);
		if (expiry === 'infinity') {
			result.push("(永久)");
		} else {
			result.push("(过期:" + new Date(expiry).toUTCString() + ")");
		}
		if (cascade) {
			result.push("(联锁)");
		}
	});

	if (!result.length) {
		var boldnode = document.createElement('b');
		boldnode.textContent = "未被保护";
		result.push(boldnode);
	}
	Twinkle.protect.protectionLevel = result;
	apiobj.statelem.info(result);
	window.setTimeout(function() { Morebits.wiki.actionCompleted.postfix = "完成"; }, 500);  // restore actionCompleted messageif (Morebits.userIsInGroup('sysop')) {
		Morebits.wiki.actionCompleted.postfix = false;  // avoid Action: completed notice
		Morebits.status.init($('div[name="currentprot"] span').last()[0]);
	}
	Twinkle.protect.fetchProtectionLevel();
};

Twinkle.protect.protectionLevel = null;  // a string, or null if no protection (only filled for sysops)
Twinkle.protect.currentProtectionLevels = null;  // an array of objects { type, level, expiry, cascade }

Twinkle.protect.fetchProtectionLevel = function twinkleprotectFetchProtectionLevel() {

	var api = new mw.Api();
	api.get({
		format: 'json',
		indexpageids: true,
		action: 'query',
		prop: 'info',
		inprop: 'protection',
		titles: mw.config.get('wgPageName')
	})
	.done(function(data){
		var pageid = data.query.pageids[0];
		var page = data.query.pages[pageid];
		var result = [];
		var current = [];

		var updateResult = function(label, level, expiry, cascade) {
			// for sysops, stringify, so they can base their decision on existing protection
			if (Morebits.userIsInGroup('sysop')) {
				var boldnode = document.createElement('b');
				boldnode.textContent = label + ":" + level;
				result.push(boldnode);
				if (expiry === 'infinity') {
					result.push("(无限期)");
				} else {
					result.push("(过期:" + new Date(expiry).toUTCString() + ")");
				}
				if (cascade) {
					result.push("(联锁)");
				}
			}
		};

		$.each(page.protection, function( index, protection ) {
			if (protection.type !== "aft") {
				current.push({
					type: protection.type,
					level: protection.level,
					expiry: protection.expiry,
					cascade: protection.cascade && protection.cascade === ''
				});
				updateResult( Morebits.string.toUpperCaseFirstChar(protection.type), protection.level, protection.expiry, protection.cascade );
			}
		});

		// show the protection level to sysops
		if (Morebits.userIsInGroup('sysop')) {
			if (!result.length) {
				var boldnode = document.createElement('b');
				boldnode.textContent = "无保护";
				result.push(boldnode);
			}
			Twinkle.protect.protectionLevel = result;
			Morebits.status.init($('div[name="currentprot"] span').last()[0]);
			Morebits.status.info("当前保护等级", Twinkle.protect.protectionLevel);
		}

		Twinkle.protect.currentProtectionLevels = current;
	});
};

Twinkle.protect.callback.changeAction = function twinkleprotectCallbackChangeAction(e) {
	var field_preset;
	var field1;
	var field2;
	var isTemplate = mw.config.get("wgNamespaceNumber") === 10 || mw.config.get("wgNamespaceNumber") === 828;
(contracted; show full)		var evt = document.createEvent( "Event" );
		evt.initEvent( 'change', true, true );
		e.target.form.category.dispatchEvent( evt );

		// re-add protection level text, if it's available
		if (Twinkle.protect.protectionLevel) {
			Morebits.status.init($('div[name="currentprot"] span').last()[0]);
			
// seems unneeded
			//Morebits.status.info("当前保护", Twinkle.protect.protectionLevel);
		}

		// reduce vertical height of dialog
		$(e.target.form).find('fieldset[name="field2"] select').parent().css({ display: 'inline-block', marginRight: '0.5em' });
	}
};

(contracted; show full)						thispage.setEditProtection(form.editlevel.value, form.editexpiry.value);
					}
					if (form.movemodify.checked) {
						thispage.setMoveProtection(form.movelevel.value, form.moveexpiry.value);
					}
				} else {
					thispage.setCreateProtection(form.createlevel.value, form.createexpiry.value);

					thispage.setWatchlist(false);
				}

				if (form.protectReason.value) {
					thispage.setEditSummary(form.protectReason.value);
				} else {
					alert("您必须输入保护理由,这将被记录在保护日志中。");
					return;
				}
(contracted; show full)		var text = rppPage.getPageText();
		var statusElement = rppPage.getStatusElement();

		var rppRe = new RegExp( '===\\s*[[:?' + RegExp.escape( mw.config.get('wgPageName'), true ) + ']]\\s*===', 'm' );
		var tag = rppRe.exec( text );

		var rppLink = document.createElement('a');
		rppLink.setAttribute('href', mw.util.
wikiGetlinkgetUrl(rppPage.getPageName()) );
		rppLink.appendChild(document.createTextNode(rppPage.getPageName()));

		if ( tag ) {
			statusElement.error( [ '已有对此条目的保护提名,在 ', rppLink, ',取消操作。' ] );
			return;
		}

		var newtag = '=== [[:' + mw.config.get('wgPageName') +  ']] ===' + "\n";
		if( ( new RegExp( '^' + RegExp.escape( newtag ).replace( /\s+/g, '\\s*' ), 'm' ) ).test( text ) ) {
			statusElement.error( [ '已有对此条目的保护提名,在 ', rppLink, ',取消操作。' ] );
			return;
		}

		var words;
		switch( params.expiry ) {
		case 'temporary':
			words = "临时";
			break;
		case 'indefinite':
			words = "永久";
			break;
		default:
			words = "";
			break;
		}

		words += params.typename;

		newtag += "请求" + Morebits.string.toUpperCaseFirstChar(words) + ( params.reason !== '' ? ":" + Morebits.string.formatReasonText(params.reason) : "。" ) + "--~~~~";

		var reg;


		if ( params.category === 'unprotect' ) {
			reg = /(==\s*请求解除保护\s*==\n)/;
		} else {
			reg = /(\/header2}}\n)/;
		}
		var originalTextLength = text.length;
		text = text.replace( reg, "$1" + newtag + "\n");
		if (text.length === originalTextLength)
		{
			var linknode = document.createElement('a');
			linknode.setAttribute("href", mw.util.wikiGetlinkgetUrl("Wikipedia:Twinkle/修复RFPP") );
			linknode.appendChild(document.createTextNode('如何修复RFPP'));
			statusElement.error( [ '无法在WP:RFPP上找到相关位点标记,要修复此问题,请参见', linknode, '。' ] );
			return;
		}
		statusElement.status( '添加新提名…' );
		rppPage.setEditSummary( '请求对[[' + Morebits.pageNameNorm + ']]' + params.typename + Twinkle.getPref('summaryAd') );
		rppPage.setPageText( text );
		rppPage.setCreateOption( 'recreate' );
		rppPage.save();
	}
};
})(jQuery);


//</nowiki>