Note: After saving, you have to bypass your browser's cache to see the changes. Internet Explorer: press Ctrl-F5, Mozilla: hold down Shift while clicking Reload (or press Ctrl-Shift-R), Opera/Konqueror: press F5, Safari: hold down Shift + Alt while clicking Reload, Chrome: hold down Shift while clicking Reload.
/*jshint scripturl:true*/
mw.util.addPortletLink('p-tb', 'javascript:importScript("MediaWiki:VisualFileChange.js");', 'Perform batch task', 't-AjaxQuickDeleteOnDemand');
/*jshint scripturl:false*/

window.AjaxDeleteExtraButtons = [
	{
		'label': 'Logo violation',
		'tag': '{'+'{Logo}}',
		'img_summary': 'Marking as possible [[Commons:Copyvio|copyright violation]] because this logo exceeds the [[COM:TOO|threshold of originality]] and therefore is subject to copyright.',
		'talk_tag': '{'+'{subst:copyvionote|1=%FILE%}}',
		'talk_summary': 'Notification of possible copyright violation for %FILE%',
	}
	];
/** PermissionOTRS.js
// Adds OTRS permission template via the API. Other templates such as {OTRS pending} will be removed.
 * @revision 22:04, 29 July 2015 (UTC)
 * @license: under the terms of the MIT license
 * @authors: 
  *	Maintainer: [[User:Rillke]]
  *	Created by Bryan Tong Minh
  *	Amended by [[:de:User:DerHexer]], [[User:Guandalug]], DieBuche, [[User:Steinsplitter]]
  *	Partially rewritten by Perhelion
 * @required modules: mediawiki.util, jquery.ui
// <nowiki>
**/
/*global mediaWiki, jQuery*/
/*jshint curly:false, scripturl:true*/
(function ($, mw) {
'use strict';
var user = mw.config.get('wgUserName');
if ( mw.config.get('wgNamespaceNumber') !== 6 || !user )
	return;

var sLink = '([[MediaWiki:Gadget-PermissionOTRS.js|Script]]) [[COM:OTRS|OTRS]] ',
	url = mw.config.get('wgScriptPath') + '/api.php',
	page = mw.config.get('wgPageName'),
	ticket = null,
	edittoken = null,
	content = null,
	timestamp = null;

function _prompt(text, OTRS) {
	var type = 'errorbox',
		title = 'OTRS',
		$ticket = $('<input>').attr({
			type : 'text',
			id : 'OTRSdialog',
			maxlength : 16,
			size : 16,
			'class' : 'numbersOnly'
		});
	if (!text) {
		title = 'INVALID ID';
		text = 'You must enter a valid 16-digit ticket number.';
	} else if (text === 'FAIL') {
		title = text;
		text = 'No suitable place found to insert template! Please add the template by hand.';
		$ticket = '';
	} else type = '';
	text = $('<label>').attr({'for': 'OTRSdialog','class': type}).text(text);
	$('<div>').append([text, $ticket]).dialog({
		width : 400,
		dialogClass : "wikiEditor-toolbar-dialog",
		resizable : false,
		modal : true,
		title : title,
		close : function() {
				$(this).dialog("destroy");
				$(this).remove();
		},
		buttons : [{
			text : "OK",
			click : function () {
				ticket = ($ticket)? $.trim($ticket.val()) : '';
				$(this).dialog("close");
				if (ticket) {
					// Check if ticket is valid
					if (!ticket.match(/^\d{16}$/)) return _prompt('', OTRS);
					_getPage(OTRS);
				}
			}
		}]
	});
}

function _addPermission(template, summary) {
	var text = content,
		permTester = /\|\s*[Pp]ermission\s*\=/;
	if (!permTester.test(text)) return _prompt('FAIL');
	text = _cleanUp(text);
	var replLicence = text.match(/\|\s*[Pp]ermission\s*\=\s*([^\n]*)\s*\n/);
	if (replLicence && replLicence[1])
		template += "\n" + replLicence[1];
	text = text.replace(/\|\s*[Pp]ermission(\s*)\=\s*[^\n]*/, "|Permission$1= " + template);
	content = text;
	_savePage(sLink + summary);
}

window.PermissionOTRS = function PermissionOTRS() {
	_addPermission('{{PermissionOTRS|id=' + ticket + "|user=" + user + "}}",
	'permission added');
};

window.OTRSreceived = function OTRSreceived() {
	_addPermission('{{OTRS received|id=' + ticket +
			'|year={{subst:'+
			'#time:Y}}|month={{subst:'+
			'#time:F}}|day={{subst:'+
			'#time:j}}|user=' + user + '}}',
	'email received but permission not yet confirmed');
};

function _getPage(OTRS) {
	$.getJSON(url, {
		action : "query",
		format : "json",
		prop : "info|revisions",
		intoken : "edit",
		rvprop : "content|timestamp",
		titles : page
	}, function (r) {
		var pages = r.query.pages;
		for (var id in pages) {
			if (pages.hasOwnProperty(id)) {
				pages = pages[id];
				edittoken = pages.edittoken;
				content = pages.revisions[0]['*'];
				timestamp = pages.revisions[0].timestamp;
				return OTRS();
			}
		}
	});
}

function _cleanUp(text) {
	return text
	.replace(/\=\= ?Summary ?\=\=/, '=={{int:filedesc}}==')
	.replace(/\=\= ?Licensing ?\=\=/, '=={{int:license-header}}==')
	.replace(/\{\{otrs[ _\-]pending[^\}\n]*\}\}/ig, '')
	.replace(/\{\{[Nn]o[ _]permission[^\}\n]*\}\}/g, '')
	.replace(/\{\{[Nn]o[ _]OTRS[ _]permission[^\}\n]*\}\}/g, '')
	.replace(/\{\{otrs[ _]received[^\}\n]*\}\}/ig, '')
	.replace(/\{\{[Dd]elete[^\n]*\(CES?T\)\s*/g, '')
	.replace(/\{\{[Dd]elete[^\}\n]*\}\}\b/g, '');
}

function _savePage(summary) {
	var params = {
		action : 'edit',
		summary : summary,
		watchlist : 'preferences',
		title : page,
		token : edittoken,
		text : content,
		format : 'json'
	};
	$.ajax({
		url : url,
		cache : false,
		dataType : 'json',
		data : params,
		basetimestamp : timestamp,
		type : 'POST',
		success : function () {
			window.location.reload();
		}
	});
}

window.dialogOTRS = function dialogOTRS(OTRS) {
	_prompt('Ticket ID (16 digit number):', OTRS);
};

mw.loader.using(['mediawiki.util', 'jquery.ui'], function () {
	mw.util.addPortletLink('p-tb', 'javascript:{dialogOTRS(PermissionOTRS);void(0)}', 'Permission OTRS');
	mw.util.addPortletLink('p-tb', 'javascript:{dialogOTRS(OTRSreceived);void(0)}', 'OTRS received');
});
}(jQuery, mediaWiki));
// </nowiki>