(function($) {

/*

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

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

	Twinkle.addPortletLink;

Twinkle.protect.callback = function twinkleprotectCallback {
	var Window = new Morebits.simpleWindow;
	Window.setTitle;
	Window.setScriptName('Twinkle');
	Window.addFooterLink;
	Window.addFooterLink;
	Window.addFooterLink;

	var form = new Morebits.quickForm(Twinkle.protect.callback.evaluate);
	var actionfield = form.append({
		type: 'field',
		label: 'Type of action'
	if (Morebits.userIsSysop) {
		actionfield.append({
			type: 'radio',
			name: 'actiontype',
			event: Twinkle.protect.callback.changeAction,
			list: [
					label: 'Protect page',
					value: 'protect',
					tooltip: 'Apply actual protection to the page.',
					checked: true
			]
	actionfield.append({
		type: 'radio',
		name: 'actiontype',
		event: Twinkle.protect.callback.changeAction,
		list: [
				label: 'Request page protection',
				value: 'request',
				tooltip: 'If you want to request protection via WP:RPP' + (Morebits.userIsSysop ? ' instead of doing the protection by yourself.' : '.'),
				checked: !Morebits.userIsSysop
				label: 'Tag page with protection template',
				value: 'tag',
				tooltip: 'If the protecting admin forgot to apply a protection template, or you have just protected the page without tagging, you can use this to apply the appropriate protection tag.',
				disabled: mw.config.get('wgArticleId') === 0 || mw.config.get('wgPageContentModel') === 'Scribunto'
		]

	form.append;
	form.append;
	form.append;

	form.append({ type: 'submit' });

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

	// We must init the controls
	var evt = document.createEvent('Event');
	evt.initEvent;
	result.actiontype[0].dispatchEvent(evt);

	// get current protection level asynchronously
	Twinkle.protect.fetchProtectionLevel;

// Contains the current protection level in an object
// Once filled, it will look something like:
// { edit: { level: "sysop", expiry: , cascade: true }, ... }
Twinkle.protect.currentProtectionLevels = {};
// A list of bots who may be the protecting sysop, for whom we shouldn't
// remind the user contact before requesting unprotection (evaluate)
Twinkle.protect.trustedBots = ['MusikBot II', 'TFA Protector Bot'];

// returns a jQuery Deferred object, usage:
// Twinkle.protect.fetchProtectingAdmin.done(function(admin_username) { ...code... });
Twinkle.protect.fetchProtectingAdmin = function twinkleprotectFetchProtectingAdmin {
	logIds = logIds || [];

	return api.get({
		format: 'json',
		action: 'query',
		list: 'logevents',
		letitle: pageName,
		letype: protType
		// don't check log entries that have already been checked (e.g. don't go into an infinite loop!)
		var event = data.query ? $.grep(data.query.logevents, function(le) {
			return $.inArray;
		if (!event) {
			// fail gracefully
			return null;
			return twinkleprotectFetchProtectingAdmin(api, protType === 'protect' ? event.params.oldtitle_title : event.params.oldtitle, protType, logIds.concat(event.logid));
		return event.user;

// mw.loader.getState('ext.flaggedRevs.review') returns null if the
// FlaggedRevs extension is not registered. Previously, this was done with
// wgFlaggedRevsParams, but after 1.34-wmf4 it is no longer exported if empty
// (https://gerrit.wikimedia.org/r/c/mediawiki/extensions/FlaggedRevs/+/508427)
Twinkle.protect.fetchProtectionLevel = function twinkleprotectFetchProtectionLevel {

	var api = new mw.Api;
	var protectDeferred = api.get({
		format: 'json',
		indexpageids: true,
		action: 'query',
		list: 'logevents',
		letype: 'protect',
		letitle: mw.config.get('wgPageName'),
		prop: mw.loader.getState('ext.flaggedRevs.review') ? 'info|flagged' : 'info',
		inprop: 'protection',
		titles: mw.config.get('wgPageName')
	var stableDeferred = api.get({
		format: 'json',
		action: 'query',
		list: 'logevents',
		letype: 'stable',
		letitle: mw.config.get('wgPageName')

	var earlyDecision = [protectDeferred];
	if (mw.loader.getState('ext.flaggedRevs.review')) {
		earlyDecision.push(stableDeferred);

	$.when.apply.done(function {
		// $.when.apply is supposed to take an unknown number of promises
		// via an array, which it does, but the type of data returned varies.
		// If there are two or more deferreds, it returns an array (of objects),
		// but if there's just one deferred, it retuns a simple object.
		// This is annoying.
		protectData = $(protectData).toArray;

		var pageid = protectData[0].query.pageids[0];
		var page = protectData[0].query.pages[pageid];
		var current = {}, adminEditDeferred;

		$.each(page.protection, function {
			if (protection.type !== 'aft') {
				current[protection.type] = {
					level: protection.level,
					expiry: protection.expiry,
					cascade: protection.cascade === ''
				// logs report last admin who made changes to either edit/move/create protection, regardless if they only modified one of them
				if (!adminEditDeferred) {
					adminEditDeferred = Twinkle.protect.fetchProtectingAdmin(api, mw.config.get('wgPageName'), 'protect');

		if (page.flagged) {
			current.stabilize = {
				level: page.flagged.protection_level,
				expiry: page.flagged.protection_expiry
			adminEditDeferred = Twinkle.protect.fetchProtectingAdmin(api, mw.config.get('wgPageName'), 'stable');

		// show the protection level and log info
		Twinkle.protect.hasProtectLog = !!protectData[0].query.logevents.length;
		Twinkle.protect.hasStableLog = mw.loader.getState('ext.flaggedRevs.review') ? !!stableData[0].query.logevents.length : false;
		Twinkle.protect.currentProtectionLevels = current;

		if (adminEditDeferred) {
			adminEditDeferred.done(function(admin) {
				if (admin) {
					$.each(['edit', 'move', 'create', 'stabilize'], function {
						if (Twinkle.protect.currentProtectionLevels[type]) {
							Twinkle.protect.currentProtectionLevels[type].admin = admin;
				Twinkle.protect.callback.showLogAndCurrentProtectInfo;
			Twinkle.protect.callback.showLogAndCurrentProtectInfo;

Twinkle.protect.callback.showLogAndCurrentProtectInfo = function twinkleprotectCallbackShowLogAndCurrentProtectInfo {
	var currentlyProtected = !$.isEmptyObject(Twinkle.protect.currentProtectionLevels);

	if (Twinkle.protect.hasProtectLog || Twinkle.protect.hasStableLog) {
		var $linkMarkup = $('');

		if (Twinkle.protect.hasProtectLog) {
			$linkMarkup.append(
				$(''),
				Twinkle.protect.hasStableLog ? $('') : null
			);

		if (Twinkle.protect.hasStableLog) {
			$linkMarkup.append($(')'));

		Morebits.status.init($('div[name="hasprotectlog"] span')[0]);
		Morebits.status.warn;

	Morebits.status.init($('div[name="currentprot"] span')[0]);
	var protectionNode = [], statusLevel = 'info';

	if (currentlyProtected) {
		$.each(Twinkle.protect.currentProtectionLevels, function {
			var label = type === 'stabilize' ? 'Pending Changes' : Morebits.string.toUpperCaseFirstChar(type);
			protectionNode.push($('')[0]);
			if (settings.expiry === 'infinity') {
				protectionNode.push(' (indefinite) ');
				protectionNode.push(' (expires ' + new Date(settings.expiry).toUTCString + ') ');
			if (settings.cascade) {
				protectionNode.push('(cascading) ');
			if (settings.admin) {
				var adminLink = '';
				protectionNode.push($('')[0]);
			protectionNode.push($('')[0]);
		protectionNode = protectionNode.slice; // remove the trailing bullet
		statusLevel = 'warn';
		protectionNode.push($('')[0]);

	Morebits.status[statusLevel];

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;

	switch (e.target.values) {
		case 'protect':
			field_preset = new Morebits.quickForm.element;
			field_preset.append({
				type: 'select',
				name: 'category',
				label: 'Choose a preset:',
				event: Twinkle.protect.callback.changePreset,
				list: mw.config.get('wgArticleId') ?
					Twinkle.protect.protectionTypes.filter(function(v) {
						return isTemplate || v.label !== 'Template protection';
					Twinkle.protect.protectionTypesCreate

			field2 = new Morebits.quickForm.element;
			field2.append; // holds the current protection level, as filled out by the async callback
			field2.append;
			// for existing pages
			if (mw.config.get('wgArticleId')) {
				field2.append({
					type: 'checkbox',
					event: Twinkle.protect.formevents.editmodify,
					list: [
							label: 'Modify edit protection',
							name: 'editmodify',
							tooltip: 'If this is turned off, the edit protection level, and expiry time, will be left as is.',
							checked: true
					]
				var editlevel = field2.append({
					type: 'select',
					name: 'editlevel',
					label: 'Edit protection:',
					event: Twinkle.protect.formevents.editlevel
				editlevel.append({
					type: 'option',
					label: 'All',
					value: 'all'
				editlevel.append({
					type: 'option',
					label: 'Autoconfirmed',
					value: 'autoconfirmed'
				editlevel.append({
					type: 'option',
					label: 'Extended confirmed',
					value: 'extendedconfirmed'
				if (isTemplate) {
					editlevel.append({
						type: 'option',
						label: 'Template editor',
						value: 'templateeditor'
				editlevel.append({
					type: 'option',
					label: 'Sysop',
					value: 'sysop',
					selected: true
				field2.append({
					type: 'select',
					name: 'editexpiry',
					label: 'Expires:',
					event: function(e) {
						if (e.target.value === 'custom') {
							Twinkle.protect.doCustomExpiry(e.target);
					// default expiry selection is conditionally set in Twinkle.protect.callback.changePreset
					list: [
					]
				field2.append({
					type: 'checkbox',
					event: Twinkle.protect.formevents.movemodify,
					list: [
							label: 'Modify move protection',
							name: 'movemodify',
							tooltip: 'If this is turned off, the move protection level, and expiry time, will be left as is.',
							checked: true
					]
				var movelevel = field2.append({
					type: 'select',
					name: 'movelevel',
					label: 'Move protection:',
					event: Twinkle.protect.formevents.movelevel
				movelevel.append({
					type: 'option',
					label: 'All',
					value: 'all'
				movelevel.append({
					type: 'option',
					label: 'Extended confirmed',
					value: 'extendedconfirmed'
				if (isTemplate) {
					movelevel.append({
						type: 'option',
						label: 'Template editor',
						value: 'templateeditor'
				movelevel.append({
					type: 'option',
					label: 'Sysop',
					value: 'sysop',
					selected: true
				field2.append({
					type: 'select',
					name: 'moveexpiry',
					label: 'Expires:',
					event: function(e) {
						if (e.target.value === 'custom') {
							Twinkle.protect.doCustomExpiry(e.target);
					// default expiry selection is conditionally set in Twinkle.protect.callback.changePreset
					list: [
					]
				// Namespaces hardcoded until phab:T218479
				if (mw.loader.getState('ext.flaggedRevs.review') && (mw.config.get('wgNamespaceNumber') === 0 || mw.config.get('wgNamespaceNumber') === 4)) {
					field2.append({
						type: 'checkbox',
						event: Twinkle.protect.formevents.pcmodify,
						list: [
								label: 'Modify pending changes protection',
								name: 'pcmodify',
								tooltip: 'If this is turned off, the pending changes level, and expiry time, will be left as is.',
								checked: true
						]
					var pclevel = field2.append({
						type: 'select',
						name: 'pclevel',
						label: 'Pending changes:',
						event: Twinkle.protect.formevents.pclevel
					pclevel.append({
						type: 'option',
						label: 'None',
						value: 'none'
					pclevel.append({
						type: 'option',
						label: 'Pending changes',
						value: 'autoconfirmed',
						selected: true
					field2.append({
						type: 'select',
						name: 'pcexpiry',
						label: 'Expires:',
						event: function(e) {
							if (e.target.value === 'custom') {
								Twinkle.protect.doCustomExpiry(e.target);
						list: [
						]
				var createlevel = field2.append({
					type: 'select',
					name: 'createlevel',
					label: 'Create protection:',
					event: Twinkle.protect.formevents.createlevel
				createlevel.append({
					type: 'option',
					label: 'All',
					value: 'all'
				if (mw.config.get('wgNamespaceNumber') !== 0) {
					createlevel.append({
						type: 'option',
						label: 'Autoconfirmed',
						value: 'autoconfirmed'
				if (isTemplate) {
					createlevel.append({
						type: 'option',
						label: 'Template editor',
						value: 'templateeditor'
				createlevel.append({
					type: 'option',
					label: 'Extended confirmed',
					value: 'extendedconfirmed',
					selected: true
				createlevel.append({
					type: 'option',
					label: 'Sysop',
					value: 'sysop'
				field2.append({
					type: 'select',
					name: 'createexpiry',
					label: 'Expires:',
					event: function(e) {
						if (e.target.value === 'custom') {
							Twinkle.protect.doCustomExpiry(e.target);
					list: [
					]
			field2.append({
				type: 'textarea',
				name: 'protectReason',
				label: 'Reason (for protection log):'
			if (!mw.config.get('wgArticleId') || mw.config.get('wgPageContentModel') === 'Scribunto') { // tagging isn't relevant for non-existing or module pages
				break;
			/* falls through */
		case 'tag':
			field1 = new Morebits.quickForm.element;
			field1.append; // holds the current protection level, as filled out by the async callback
			field1.append;
			field1.append({
				type: 'select',
				name: 'tagtype',
				label: 'Choose protection template:',
				list: Twinkle.protect.protectionTags,
				event: Twinkle.protect.formevents.tagtype
			field1.append({
				type: 'checkbox',
				list: [
						name: 'small',
						label: 'Iconify (small=yes)',
						tooltip: 'Will use the |small=yes feature of the template, and only render it as a keylock',
						checked: true
						name: 'noinclude',
						label: 'Wrap protection template with ',
						tooltip: 'Will wrap the protection template in &lt;noinclude&gt; tags, so that it won\'t transclude',
						checked: mw.config.get('wgNamespaceNumber') === 10
				]
			break;

		case 'request':
			field_preset = new Morebits.quickForm.element;
			field_preset.append({
				type: 'select',
				name: 'category',
				label: 'Type and reason:',
				event: Twinkle.protect.callback.changePreset,
				list: mw.config.get('wgArticleId') ? Twinkle.protect.protectionTypes : Twinkle.protect.protectionTypesCreate

			field1 = new Morebits.quickForm.element;
			field1.append; // holds the current protection level, as filled out by the async callback
			field1.append;
			field1.append({
				type: 'select',
				name: 'expiry',
				label: 'Duration: ',
				list: [
				]
			field1.append({
				type: 'textarea',
				name: 'reason',
				label: 'Reason: '
			break;
		default:
			alert("Something's afoot in twinkleprotect");
			break;

	var oldfield;

	if (field_preset) {
		oldfield = $(e.target.form).find('fieldset[name="field_preset"]')[0];
		oldfield.parentNode.replaceChild;
		$(e.target.form).find('fieldset[name="field_preset"]').css;
	if (field1) {
		oldfield = $(e.target.form).find('fieldset[name="field1"]')[0];
		oldfield.parentNode.replaceChild;
		$(e.target.form).find('fieldset[name="field1"]').css;
	if (field2) {
		oldfield = $(e.target.form).find('fieldset[name="field2"]')[0];
		oldfield.parentNode.replaceChild;
		$(e.target.form).find('fieldset[name="field2"]').css;

	if (e.target.values === 'protect') {
		// fake a change event on the preset dropdown
		var evt = document.createEvent('Event');
		evt.initEvent;
		e.target.form.category.dispatchEvent(evt);

		// reduce vertical height of dialog
		$(e.target.form).find('fieldset[name="field2"] select').parent.css;

	// re-add protection level and log info, if it's available
	Twinkle.protect.callback.showLogAndCurrentProtectInfo;

Twinkle.protect.formevents = {
	editmodify: function twinkleprotectFormEditmodifyEvent(e) {
		e.target.form.editlevel.disabled = !e.target.checked;
		e.target.form.editexpiry.disabled = !e.target.checked || (e.target.form.editlevel.value === 'all');
		e.target.form.editlevel.style.color = e.target.form.editexpiry.style.color = e.target.checked ? '' : 'transparent';
	editlevel: function twinkleprotectFormEditlevelEvent(e) {
		e.target.form.editexpiry.disabled = e.target.value === 'all';
	movemodify: function twinkleprotectFormMovemodifyEvent(e) {
		// sync move settings with edit settings if applicable
		if (e.target.form.movelevel.disabled && !e.target.form.editlevel.disabled) {
			e.target.form.movelevel.value = e.target.form.editlevel.value;
			e.target.form.moveexpiry.value = e.target.form.editexpiry.value;
			e.target.form.movelevel.value = 'sysop';
			e.target.form.moveexpiry.value = 'infinity';
		e.target.form.movelevel.disabled = !e.target.checked;
		e.target.form.moveexpiry.disabled = !e.target.checked || (e.target.form.movelevel.value === 'all');
		e.target.form.movelevel.style.color = e.target.form.moveexpiry.style.color = e.target.checked ? '' : 'transparent';
	movelevel: function twinkleprotectFormMovelevelEvent(e) {
		e.target.form.moveexpiry.disabled = e.target.value === 'all';
	pcmodify: function twinkleprotectFormPcmodifyEvent(e) {
		e.target.form.pclevel.disabled = !e.target.checked;
		e.target.form.pcexpiry.disabled = !e.target.checked || (e.target.form.pclevel.value === 'none');
		e.target.form.pclevel.style.color = e.target.form.pcexpiry.style.color = e.target.checked ? '' : 'transparent';
	pclevel: function twinkleprotectFormPclevelEvent(e) {
		e.target.form.pcexpiry.disabled = e.target.value === 'none';
	createlevel: function twinkleprotectFormCreatelevelEvent(e) {
		e.target.form.createexpiry.disabled = e.target.value === 'all';
	tagtype: function twinkleprotectFormTagtypeEvent(e) {
		e.target.form.small.disabled = e.target.form.noinclude.disabled = (e.target.value === 'none') || (e.target.value === 'noop');

Twinkle.protect.doCustomExpiry = function twinkleprotectDoCustomExpiry(target) {
	var custom = prompt('Enter a custom expiry time. \nYou can use relative times, like "1 minute" or "19 days", or absolute timestamps, "yyyymmddhhmm" .', '');
	if (custom) {
		var option = document.createElement('option');
		option.setAttribute;
		option.textContent = custom;
		target.appendChild(option);
		target.value = custom;
		target.selectedIndex = 0;

Twinkle.protect.protectionTypes = [
		label: 'Full protection',
		list: [
		]
		label: 'Template protection',
		list: [
		]
		label: 'Extended confirmed protection',
		list: [
		]
		label: 'Semi-protection',
		list: [
		]
		label: 'Pending changes',
		list: [
		]
		label: 'Move protection',
		list: [
		]
];

Twinkle.protect.protectionTypesCreate = [
		label: 'Create protection',
		list: [
		]
];

// A page with both regular and PC protection will be assigned its regular
// protection weight plus 2
Twinkle.protect.protectionWeight = {
	sysop: 40,
	templateeditor: 30,
	extendedconfirmed: 20,
	autoconfirmed: 10,
	flaggedrevs_autoconfirmed: 5, // Pending Changes protection alone
	all: 0,
	flaggedrevs_none: 0 // just in case

// NOTICE: keep this synched with MediaWiki:Protect-dropdown
// Also note: stabilize = Pending Changes level
Twinkle.protect.protectionPresetsInfo = {
	'pp-protected': {
		edit: 'sysop',
		move: 'sysop',
		reason: null
	'pp-dispute': {
		edit: 'sysop',
		move: 'sysop',
		reason: 'Edit warring / content dispute'
	'pp-vandalism': {
		edit: 'sysop',
		move: 'sysop',
		reason: 'Persistent vandalism'
	'pp-usertalk': {
		edit: 'sysop',
		move: 'sysop',
		reason: 'Inappropriate use of user talk page while blocked'
	'pp-template': {
		edit: 'templateeditor',
		move: 'templateeditor',
		reason: 'Highly visible template'
	'pp-30-500-arb': {
		edit: 'extendedconfirmed',
		move: 'extendedconfirmed',
		reason: 'Arbitration enforcement',
		template: 'pp-30-500'
	'pp-30-500-vandalism': {
		edit: 'extendedconfirmed',
		move: 'extendedconfirmed',
		reason: 'Persistent vandalism from (auto)confirmed accounts',
		template: 'pp-30-500'
	'pp-30-500-disruptive': {
		edit: 'extendedconfirmed',
		move: 'extendedconfirmed',
		reason: 'Persistent disruptive editing from (auto)confirmed accounts',
		template: 'pp-30-500'
	'pp-30-500-blp': {
		edit: 'extendedconfirmed',
		move: 'extendedconfirmed',
		reason: 'Persistent violations of the biographies of living persons policy from (auto)confirmed accounts',
		template: 'pp-30-500'
	'pp-30-500-sock': {
		edit: 'extendedconfirmed',
		move: 'extendedconfirmed',
		reason: 'Persistent sock puppetry',
		template: 'pp-30-500'
	'pp-semi-vandalism': {
		edit: 'autoconfirmed',
		reason: 'Persistent vandalism',
		template: 'pp-vandalism'
	'pp-semi-disruptive': {
		edit: 'autoconfirmed',
		reason: 'Persistent disruptive editing',
		template: 'pp-protected'
	'pp-semi-unsourced': {
		edit: 'autoconfirmed',
		reason: 'Persistent addition of unsourced or poorly sourced content',
		template: 'pp-protected'
	'pp-semi-blp': {
		edit: 'autoconfirmed',
		reason: 'Violations of the biographies of living persons policy',
		template: 'pp-blp'
	'pp-semi-usertalk': {
		edit: 'autoconfirmed',
		move: 'autoconfirmed',
		reason: 'Inappropriate use of user talk page while blocked',
		template: 'pp-usertalk'
	'pp-semi-template': { // removed for now
		edit: 'autoconfirmed',
		move: 'autoconfirmed',
		reason: 'Highly visible template',
		template: 'pp-template'
	'pp-semi-sock': {
		edit: 'autoconfirmed',
		reason: 'Persistent sock puppetry',
		template: 'pp-sock'
	'pp-semi-protected': {
		edit: 'autoconfirmed',
		reason: null,
		template: 'pp-protected'
	'pp-pc-vandalism': {
		stabilize: 'autoconfirmed', // stabilize = Pending Changes
		reason: 'Persistent vandalism',
		template: 'pp-pc'
	'pp-pc-disruptive': {
		stabilize: 'autoconfirmed',
		reason: 'Persistent disruptive editing',
		template: 'pp-pc'
	'pp-pc-unsourced': {
		stabilize: 'autoconfirmed',
		reason: 'Persistent addition of unsourced or poorly sourced content',
		template: 'pp-pc'
	'pp-pc-blp': {
		stabilize: 'autoconfirmed',
		reason: 'Violations of the biographies of living persons policy',
		template: 'pp-pc'
	'pp-pc-protected': {
		stabilize: 'autoconfirmed',
		reason: null,
		template: 'pp-pc'
	'pp-move': {
		move: 'sysop',
		reason: null
	'pp-move-dispute': {
		move: 'sysop',
		reason: 'Move warring'
	'pp-move-vandalism': {
		move: 'sysop',
		reason: 'Page-move vandalism'
	'pp-move-indef': {
		move: 'sysop',
		reason: 'Highly visible page'
	'unprotect': {
		edit: 'all',
		move: 'all',
		stabilize: 'none',
		create: 'all',
		reason: null,
		template: 'none'
	'pp-create-offensive': {
		create: 'sysop',
		reason: 'Offensive name'
	'pp-create-salt': {
		create: 'extendedconfirmed',
		reason: 'Repeatedly recreated'
	'pp-create-blp': {
		create: 'extendedconfirmed',
		reason: 'Recently deleted BLP'
	'pp-create': {
		create: 'extendedconfirmed',
		reason: ''

Twinkle.protect.protectionTags = [
		label: 'None (remove existing protection templates)',
		value: 'none'
		label: 'None (do not remove existing protection templates)',
		value: 'noop'
		label: 'Edit protection templates',
		list: [
		]
		label: 'Pending changes templates',
		list: [
		]
		label: 'Move protection templates',
		list: [
		]
];

Twinkle.protect.callback.changePreset = function twinkleprotectCallbackChangePreset(e) {
	var form = e.target.form;

	var actiontypes = form.actiontype;
	var actiontype;
	for (var i = 0; i < actiontypes.length; i++) {
		if (!actiontypes[i].checked) {
			continue;
		actiontype = actiontypes[i].values;
		break;

	if (actiontype === 'protect') { // actually protecting the page
		var item = Twinkle.protect.protectionPresetsInfo[form.category.value];

		if (mw.config.get('wgArticleId')) {
			if (item.edit) {
				form.editmodify.checked = true;
				Twinkle.protect.formevents.editmodify({ target: form.editmodify });
				form.editlevel.value = item.edit;
				Twinkle.protect.formevents.editlevel({ target: form.editlevel });
				form.editexpiry.value = '2 days';
				form.editmodify.checked = false;
				Twinkle.protect.formevents.editmodify({ target: form.editmodify });

			if (item.move) {
				form.movemodify.checked = true;
				Twinkle.protect.formevents.movemodify({ target: form.movemodify });
				form.movelevel.value = item.move;
				Twinkle.protect.formevents.movelevel({ target: form.movelevel });
				form.moveexpiry.value = '2 days';
				form.movemodify.checked = false;
				Twinkle.protect.formevents.movemodify({ target: form.movemodify });

			if (form.pcmodify) {
				if (item.stabilize) {
					form.pcmodify.checked = true;
					Twinkle.protect.formevents.pcmodify({ target: form.pcmodify });
					form.pclevel.value = item.stabilize;
					Twinkle.protect.formevents.pclevel({ target: form.pclevel });
					form.pcmodify.checked = false;
					Twinkle.protect.formevents.pcmodify({ target: form.pcmodify });
			if (item.create) {
				form.createlevel.value = item.create;
				Twinkle.protect.formevents.createlevel({ target: form.createlevel });

		var reasonField = actiontype === 'protect' ? form.protectReason : form.reason;
		if (item.reason) {
			reasonField.value = item.reason;
			reasonField.value = '';

		// sort out tagging options, disabled if nonexistent or lua
		if (mw.config.get('wgArticleId') && mw.config.get('wgPageContentModel') !== 'Scribunto') {
			if (form.category.value === 'unprotect') {
				form.tagtype.value = 'none';
				form.tagtype.value = item.template ? item.template : form.category.value;
			Twinkle.protect.formevents.tagtype({ target: form.tagtype });

			if (/template/.test(form.category.value)) {
				form.noinclude.checked = true;
				form.editexpiry.value = form.moveexpiry.value = form.pcexpiry.value = 'infinity';
				form.noinclude.checked = false;

		if (form.category.value === 'unprotect') {
			form.expiry.value = '';
			form.expiry.disabled = true;
			form.expiry.value = '';
			form.expiry.disabled = false;

Twinkle.protect.callback.evaluate = function twinkleprotectCallbackEvaluate(e) {
	var form = e.target;
	var input = Morebits.quickForm.getInputData(form);

	var tagparams;
	if (input.actiontype === 'tag' || (input.actiontype === 'protect' && mw.config.get('wgArticleId') && mw.config.get('wgPageContentModel') !== 'Scribunto')) {
		tagparams = {
			tag: input.tagtype,
			reason: (input.tagtype === 'pp-protected' || input.tagtype === 'pp-semi-protected' || input.tagtype === 'pp-move') && input.protectReason,
			small: input.small,
			noinclude: input.noinclude

	switch (input.actiontype) {
		case 'protect':
			// protect the page
			Morebits.wiki.actionCompleted.redirect = mw.config.get('wgPageName');
			Morebits.wiki.actionCompleted.notice = 'Protection complete';

			var statusInited = false;
			var thispage;

			var allDone = function twinkleprotectCallbackAllDone {
				if (thispage) {
					thispage.getStatusElement.info('done');
				if (tagparams) {
					Twinkle.protect.callbacks.taggingPageInitial(tagparams);

			var protectIt = function twinkleprotectCallbackProtectIt(next) {
				thispage = new Morebits.wiki.page(mw.config.get('wgPageName'), 'Protecting page');
				if (mw.config.get('wgArticleId')) {
					if (input.editmodify) {
						thispage.setEditProtection;
					if (input.movemodify) {
						// Ensure a level has actually been chosen
						if (input.movelevel) {
							thispage.setMoveProtection;
							alert('You must chose a move protection level!');
							return;
					thispage.setCreateProtection;
					thispage.setWatchlist(false);

				if (input.protectReason) {
					thispage.setEditSummary(input.protectReason);
					alert;
					return;

				if (!statusInited) {
					Morebits.simpleWindow.setButtonsEnabled(false);
					Morebits.status.init(form);
					statusInited = true;

				thispage.protect(next);

			var stabilizeIt = function twinkleprotectCallbackStabilizeIt {
				if (thispage) {
					thispage.getStatusElement.info('done');

				thispage = new Morebits.wiki.page(mw.config.get('wgPageName'), 'Applying pending changes protection');
				thispage.setFlaggedRevs;

				if (input.protectReason) {
					thispage.setEditSummary(input.protectReason);
					alert;
					return;

				if (!statusInited) {
					Morebits.simpleWindow.setButtonsEnabled(false);
					Morebits.status.init(form);
					statusInited = true;

				thispage.stabilize(allDone, function(error) {
					if (error.errorCode === 'stabilize_denied') { // phab:T234743
						thispage.getStatusElement.error('Failed trying to modify pending changes settings, likely due to a mediawiki bug. Other actions (tagging or regular protection) may have taken place. Please reload the page and try again.');

			if (input.editmodify || input.movemodify || !mw.config.get('wgArticleId')) {
				if (input.pcmodify) {
					protectIt(stabilizeIt);
					protectIt(allDone);
				stabilizeIt;
				alert;

			break;

		case 'tag':
			// apply a protection template

			Morebits.simpleWindow.setButtonsEnabled(false);
			Morebits.status.init(form);

			Morebits.wiki.actionCompleted.redirect = mw.config.get('wgPageName');
			Morebits.wiki.actionCompleted.followRedirect = false;
			Morebits.wiki.actionCompleted.notice = 'Tagging complete';

			Twinkle.protect.callbacks.taggingPageInitial(tagparams);
			break;

		case 'request':
			// file request at RFPP
			var typename, typereason;
			switch (input.category) {
				case 'pp-dispute':
				case 'pp-vandalism':
				case 'pp-usertalk':
				case 'pp-protected':
					typename = 'full protection';
					break;
				case 'pp-template':
					typename = 'template protection';
					break;
				case 'pp-30-500-arb':
				case 'pp-30-500-vandalism':
				case 'pp-30-500-disruptive':
				case 'pp-30-500-blp':
				case 'pp-30-500-sock':
					typename = 'extended confirmed protection';
					break;
				case 'pp-semi-vandalism':
				case 'pp-semi-disruptive':
				case 'pp-semi-unsourced':
				case 'pp-semi-usertalk':
				case 'pp-semi-sock':
				case 'pp-semi-blp':
				case 'pp-semi-protected':
					typename = 'semi-protection';
					break;
				case 'pp-pc-vandalism':
				case 'pp-pc-blp':
				case 'pp-pc-protected':
				case 'pp-pc-unsourced':
				case 'pp-pc-disruptive':
					typename = 'pending changes';
					break;
				case 'pp-move':
				case 'pp-move-dispute':
				case 'pp-move-indef':
				case 'pp-move-vandalism':
					typename = 'move protection';
					break;
				case 'pp-create':
				case 'pp-create-offensive':
				case 'pp-create-blp':
				case 'pp-create-salt':
					typename = 'create protection';
					break;
				case 'unprotect':
					var admins = $.map(Twinkle.protect.currentProtectionLevels, function(pl) {
						if (!pl.admin || Twinkle.protect.trustedBots.indexOf(pl.admin) !== -1) {
							return null;
						return 'User:' + pl.admin;
					if (admins.length && !confirm('Have you attempted to contact the protecting admins (' + Morebits.array.uniq(admins).join + ') first?')) {
						return false;
					// otherwise falls through
				default:
					typename = 'unprotection';
					break;
			switch (input.category) {
				case 'pp-dispute':
					typereason = 'Content dispute/edit warring';
					break;
				case 'pp-vandalism':
				case 'pp-semi-vandalism':
				case 'pp-pc-vandalism':
				case 'pp-30-500-vandalism':
					typereason = 'Persistent vandalism';
					break;
				case 'pp-semi-disruptive':
				case 'pp-pc-disruptive':
				case 'pp-30-500-disruptive':
					typereason = 'Persistent disruptive editing';
					break;
				case 'pp-semi-unsourced':
				case 'pp-pc-unsourced':
					typereason = 'Persistent addition of unsourced or poorly sourced content';
					break;
				case 'pp-template':
					typereason = 'High-risk template';
					break;
				case 'pp-30-500-arb':
					typereason = 'Arbitration enforcement';
					break;
				case 'pp-usertalk':
				case 'pp-semi-usertalk':
					typereason = 'Inappropriate use of user talk page while blocked';
					break;
				case 'pp-semi-sock':
				case 'pp-30-500-sock':
					typereason = 'Persistent sockpuppetry';
					break;
				case 'pp-semi-blp':
				case 'pp-pc-blp':
				case 'pp-30-500-blp':
					typereason = 'BLP policy violations';
					break;
				case 'pp-move-dispute':
					typereason = 'Page title dispute/move warring';
					break;
				case 'pp-move-vandalism':
					typereason = 'Page-move vandalism';
					break;
				case 'pp-move-indef':
					typereason = 'Highly visible page';
					break;
				case 'pp-create-offensive':
					typereason = 'Offensive name';
					break;
				case 'pp-create-blp':
					typereason = 'Recently deleted BLP';
					break;
				case 'pp-create-salt':
					typereason = 'Repeatedly recreated';
					break;
				default:
					typereason = '';
					break;

			var reason = typereason;
			if (input.reason !== '') {
				if (typereason !== '') {
					reason += '\u00A0\u2013 '; // U+00A0 NO-BREAK SPACE; U+2013 EN RULE
				reason += input.reason;
			if (reason !== '' && reason.charAt(reason.length - 1) !== '.') {
				reason += '.';

			var rppparams = {
				reason: reason,
				typename: typename,
				category: input.category,
				expiry: input.expiry

			Morebits.simpleWindow.setButtonsEnabled(false);
			Morebits.status.init(form);

			var rppName = 'Wikipedia:Requests for page protection';

			// Updating data for the action completed event
			Morebits.wiki.actionCompleted.redirect = rppName;
			Morebits.wiki.actionCompleted.notice = 'Nomination completed, redirecting now to the discussion page';

			var rppPage = new Morebits.wiki.page;
			rppPage.setFollowRedirect(true);
			rppPage.setCallbackParameters(rppparams);
			rppPage.load(Twinkle.protect.callbacks.fileRequest);
			break;
		default:
			alert('twinkleprotect: unknown kind of action');
			break;

Twinkle.protect.callbacks = {
	taggingPageInitial: function(tagparams) {
		if (tagparams.tag === 'noop') {
			Morebits.status.info;
			return;

		var protectedPage = new Morebits.wiki.page(mw.config.get('wgPageName'), 'Tagging page');
		protectedPage.setCallbackParameters(tagparams);
		protectedPage.load(Twinkle.protect.callbacks.taggingPage);
	taggingPage: function(protectedPage) {
		var params = protectedPage.getCallbackParameters;
		var text = protectedPage.getPageText;
		var tag, summary;

		var oldtag_re = /\s*(?:)?\s*/gi;
		var re_result = oldtag_re.exec(text);
		if (re_result) {
			if (params.tag === 'none' || confirm) {
				text = text.replace;

		if (params.tag === 'none') {
			summary = 'Removing protection template' + Twinkle.getPref('summaryAd');
			tag = params.tag;
			if (params.reason) {
				tag += '|reason=' + params.reason;
			if (params.small) {
				tag += '|small=yes';

			if (/^\s*#redirect/i.test(text)) { // redirect page
				// Only tag if no is found
				if (!text.match(/');
					Morebits.status.info;
					return;
				if (params.noinclude) {
					tag = '';
					tag = '\n';

				// Insert tag after short description or any hatnotes
				var wikipage = new Morebits.wikitext.page(text);
				text = wikipage.insertAfterTemplates.getText;
			summary = 'Adding ' + Twinkle.getPref('summaryAd');

		protectedPage.setEditSummary(summary);
		protectedPage.setPageText(text);
		protectedPage.setCreateOption('nocreate');
		protectedPage.suppressProtectWarning; // no need to let admins know they are editing through protection
		protectedPage.save;

	fileRequest: function(rppPage) {

		var params = rppPage.getCallbackParameters;
		var text = rppPage.getPageText;
		var statusElement = rppPage.getStatusElement;

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

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

		if (tag) {
			statusElement.error;
			return;

		var newtag = '=== :' + Morebits.pageNameNorm + ' ===\n';
		if (new RegExp('^' + RegExp.escape(newtag).replace, 'm').test(text)) {
			statusElement.error;
			return;
		newtag += '* \n\n';

		var words;
		switch (params.expiry) {
			case 'temporary':
				words = 'Temporary ';
				break;
			case 'infinity':
				words = 'Indefinite ';
				break;
			default:
				words = '';
				break;

		words += params.typename;

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

		// If either protection type results in a increased status, then post it under increase
		// else we post it under decrease
		var increase = false;
		var protInfo = Twinkle.protect.protectionPresetsInfo[params.category];

		// function to compute protection weights (see comment at Twinkle.protect.protectionWeight)
		var computeWeight = function {
			var result = Twinkle.protect.protectionWeight[mainLevel || 'all'];
			if (stabilizeLevel) {
				if (result) {
					if (stabilizeLevel.level === 'autoconfirmed') {
						result += 2;
					result = Twinkle.protect.protectionWeight['flaggedrevs_' + stabilizeLevel];
			return result;

		// compare the page's current protection weights with the protection we are requesting
		var editWeight = computeWeight;
		if (computeWeight > editWeight ||
			computeWeight(protInfo.move) > computeWeight(Twinkle.protect.currentProtectionLevels.move &&
			Twinkle.protect.currentProtectionLevels.move.level) ||
			computeWeight(protInfo.create) > computeWeight(Twinkle.protect.currentProtectionLevels.create &&
			Twinkle.protect.currentProtectionLevels.create.level)) {
			increase = true;

		var reg;
		if (increase) {
			reg = /(\n==\s*Current requests for reduction in protection level\s*==)/;
			reg = /(\n==\s*Current requests for edits to a protected page\s*==)/;

		var originalTextLength = text.length;
		text = text.replace;
		if (text.length === originalTextLength) {
			var linknode = document.createElement('a');
			linknode.setAttribute('href', mw.util.getUrl('Wikipedia:Twinkle/Fixing RPP'));
			linknode.appendChild(document.createTextNode('How to fix RPP'));
			statusElement.error;
			return;
		statusElement.status('Adding new request...');
		rppPage.setEditSummary('/* ' + Morebits.pageNameNorm + ' */ Requesting ' + params.typename + (params.typename === 'pending changes' ? ' on :' : ' of [[:') +
			Morebits.pageNameNorm + '.' + Twinkle.getPref('summaryAd'));
		rppPage.setPageText(text);
		rppPage.setCreateOption('recreate');
		rppPage.save;

Twinkle.addInitCallback;