(function($) {

/*

Twinkle.batchprotect = function twinklebatchprotect {
	if (Morebits.userIsSysop && ((mw.config.get('wgArticleId') > 0 && (mw.config.get('wgNamespaceNumber') === 2 ||
		mw.config.get('wgNamespaceNumber') === 4)) || mw.config.get('wgNamespaceNumber') === 14 ||
		mw.config.get('wgCanonicalSpecialPageName') === 'Prefixindex')) {
		Twinkle.addPortletLink;

Twinkle.batchprotect.unlinkCache = {};
Twinkle.batchprotect.callback = function twinklebatchprotectCallback {
	var Window = new Morebits.simpleWindow;
	Window.setTitle('Batch protection');
	Window.setScriptName('Twinkle');
	Window.addFooterLink;
	Window.addFooterLink;

	var form = new Morebits.quickForm(Twinkle.batchprotect.callback.evaluate);
	form.append({
		type: 'checkbox',
		event: Twinkle.protect.formevents.editmodify,
		list: [
				label: 'Modify edit protection',
				value: 'editmodify',
				name: 'editmodify',
				tooltip: 'Only for existing pages.',
				checked: true
		]
	var editlevel = form.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'
	editlevel.append({
		type: 'option',
		label: 'Template editor',
		value: 'templateeditor'
	editlevel.append({
		type: 'option',
		label: 'Sysop',
		value: 'sysop',
		selected: true
	form.append({
		type: 'select',
		name: 'editexpiry',
		label: 'Expires:',
		event: function(e) {
			if (e.target.value === 'custom') {
				Twinkle.protect.doCustomExpiry(e.target);
		list: [
		]

	form.append({
		type: 'checkbox',
		event: Twinkle.protect.formevents.movemodify,
		list: [
				label: 'Modify move protection',
				value: 'movemodify',
				name: 'movemodify',
				tooltip: 'Only for existing pages.',
				checked: true
		]
	var movelevel = form.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'
	movelevel.append({
		type: 'option',
		label: 'Template editor',
		value: 'templateeditor'
	movelevel.append({
		type: 'option',
		label: 'Sysop',
		value: 'sysop',
		selected: true
	form.append({
		type: 'select',
		name: 'moveexpiry',
		label: 'Expires:',
		event: function(e) {
			if (e.target.value === 'custom') {
				Twinkle.protect.doCustomExpiry(e.target);
		list: [
		]

	form.append({
		type: 'checkbox',
		event: function twinklebatchprotectFormCreatemodifyEvent(e) {
			e.target.form.createlevel.disabled = !e.target.checked;
			e.target.form.createexpiry.disabled = !e.target.checked || (e.target.form.createlevel.value === 'all');
			e.target.form.createlevel.style.color = e.target.form.createexpiry.style.color = e.target.checked ? '' : 'transparent';
		list: [
				label: 'Modify create protection',
				value: 'createmodify',
				name: 'createmodify',
				tooltip: 'Only for pages that do not exist.',
				checked: true
		]
	var createlevel = form.append({
		type: 'select',
		name: 'createlevel',
		label: 'Create protection:',
		event: Twinkle.protect.formevents.createlevel
	createlevel.append({
		type: 'option',
		label: 'All',
		value: 'all'
	createlevel.append({
		type: 'option',
		label: 'Autoconfirmed',
		value: 'autoconfirmed'
	createlevel.append({
		type: 'option',
		label: 'Extended confirmed',
		value: 'extendedconfirmed'
	createlevel.append({
		type: 'option',
		label: 'Template editor',
		value: 'templateeditor'
	createlevel.append({
		type: 'option',
		label: 'Sysop',
		value: 'sysop',
		selected: true
	form.append({
		type: 'select',
		name: 'createexpiry',
		label: 'Expires:',
		event: function(e) {
			if (e.target.value === 'custom') {
				Twinkle.protect.doCustomExpiry(e.target);
		list: [
		]

	form.append({
		type: 'header',
		label: '' // horizontal rule
	form.append({
		type: 'input',
		name: 'reason',
		label: 'Reason: ',
		size: 60,
		tooltip: 'For the protection log and page history.'

	var query = {
		'action': 'query',
		'prop': 'revisions|info',
		'rvprop': 'size',
		'inprop': 'protection'

	if (mw.config.get('wgNamespaceNumber') === 14) { // categories
		query.generator = 'categorymembers';
		query.gcmtitle = mw.config.get('wgPageName');
		query.gcmlimit = Twinkle.getPref('batchMax');
		query.generator = 'allpages';
		query.gapnamespace = mw.util.getParamValue('namespace') || $('select[name=namespace]').val;
		query.gapprefix = mw.util.getParamValue('prefix') || $('input[name=prefix]').val;
		query.gaplimit = Twinkle.getPref('batchMax');
		query.generator = 'links';
		query.titles = mw.config.get('wgPageName');
		query.gpllimit = Twinkle.getPref('batchMax');

	var statusdiv = document.createElement('div');
	statusdiv.style.padding = '15px'; // just so it doesn't look broken
	Window.setContent(statusdiv);
	Morebits.status.init(statusdiv);
	Window.display;

	var statelem = new Morebits.status('Grabbing list of pages');

	var wikipedia_api = new Morebits.wiki.api('loading...', query, function(apiobj) {
		var xml = apiobj.responseXML;
		var $pages = $(xml).find('page');
		var list = [];
		$pages.each(function {
			var $page = $(page);
			var title = $page.attr('title');
			var isRedir = $page.attr('redirect') === ''; // XXX ??
			var missing = $page.attr('missing') === ''; // XXX ??
			var size = $page.find('rev').attr('size');
			var $editProt;

			var metadata = [];
			if (missing) {
				metadata.push('page does not exist');
				$editProt = $page.find('pr[type="create"][level="sysop"]');
				if (isRedir) {
					metadata.push('redirect');
				metadata.push(mw.language.convertNumber(size) + ' bytes');
				$editProt = $page.find('pr[type="edit"][level="sysop"]');
			if ($editProt.length > 0) {
				metadata.push('fully' + (missing ? ' create' : '') + ' protected' +
				($editProt.attr('expiry') === 'infinity' ? ' indefinitely' : ', expires ' + new Morebits.date($editProt.attr('expiry')).calendar('utc') + ' (UTC)'));

			list.push({ label: title + (metadata.length ? ' (' + metadata.join('; ') + ')' : ''), value: title, checked: true, style: $editProt.length > 0 ? 'color:red' : '' });
		form.append;
		form.append({
			type: 'button',
			label: 'Select All',
			event: function(e) {
				$(Morebits.quickForm.getElements).prop;
		form.append({
			type: 'button',
			label: 'Deselect All',
			event: function(e) {
				$(Morebits.quickForm.getElements).prop;
		form.append({
			type: 'checkbox',
			name: 'pages',
			shiftClickSupport: true,
			list: list
		form.append({ type: 'submit' });

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

	wikipedia_api.post;

Twinkle.batchprotect.currentProtectCounter = 0;
Twinkle.batchprotect.currentprotector = 0;
Twinkle.batchprotect.callback.evaluate = function twinklebatchprotectCallbackEvaluate(event) {
	Morebits.wiki.actionCompleted.notice = 'Batch protection is now complete';

	var form = event.target;

	var numProtected = $(Morebits.quickForm.getElements).filter(function {
		return element.checked && element.nextElementSibling.style.color === 'red';
	if (numProtected > 0 && !confirm('You are about to act on ' + mw.language.convertNumber(numProtected) + ' fully protected page(s). Are you sure?')) {
		return;

	var input = Morebits.quickForm.getInputData(form);

	if (!input.reason) {
		alert;
		return;

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

	if (input.pages.length === 0) {
		Morebits.status.error;
		return;

	var batchOperation = new Morebits.batchOperation('Applying protection settings');
	batchOperation.setOption('chunkSize', Twinkle.getPref('batchChunks'));
	batchOperation.setOption;
	batchOperation.setPageList(input.pages);
	batchOperation.run(function(pageName) {
		var query = {
			'action': 'query',
			'titles': pageName
		var wikipedia_api = new Morebits.wiki.api;
		wikipedia_api.params = $.extend({
			page: pageName,
			batchOperation: batchOperation
		wikipedia_api.post;

Twinkle.batchprotect.callbacks = {
	main: function(apiobj) {
		var xml = apiobj.responseXML;
		var normal = $(xml).find('normalized n').attr('to');
		if (normal) {
			apiobj.params.page = normal;

		var exists = $(xml).find('page').attr('missing') !== '';

		var page = new Morebits.wiki.page;
		var takenAction = false;
		if (exists && apiobj.params.editmodify) {
			page.setEditProtection;
			takenAction = true;
		if (exists && apiobj.params.movemodify) {
			page.setMoveProtection;
			takenAction = true;
		if (!exists && apiobj.params.createmodify) {
			page.setCreateProtection;
			takenAction = true;
		if (!takenAction) {
			Morebits.status.warn('Protecting ' + apiobj.params.page, 'page ' + (exists ? 'exists' : 'does not exist') + '; nothing to do, skipping');
			apiobj.params.batchOperation.workerFailure(apiobj);
			return;

		page.setEditSummary(apiobj.params.reason);
		page.protect;

Twinkle.addInitCallback;