User:Legoktm/PermissionOTRS.js

// // PermissionOTRS.js // Originally from https://commons.wikimedia.org/wiki/MediaWiki:Gadget-PermissionOTRS.js // // Adds OTRS permission template via the API. Other templates such as // will be removed. // // Maintainer: DieBuche // Licensed under the terms of the MIT license // Authors: Bryan Tong Minh, de:User:DerHexer, User:Guandalug, User:DieBuche // Modified by Legoktm for use on /*global mediaWiki:false, jQuery:false, prompt:false, alert:false */ /*jshint curly:false */ (function($, mw) { 'use strict'; if (mw.config.get('wgNamespaceNumber') !== 6) return; var PermissionOTRS = window.PermissionOTRS = {	usersignature: "",	prompt: function {		this.ticket = prompt('Ticket link or Number:');		this.ticket = $.trim(this.ticket);		if (this.ticket) this.getPage(this, this.addPermission);	},	getPage: null,	cleanUp: null,	savePage: null,	addPermission: function (result) {		var pages = result.query.pages;		for (var id in pages) {		if (pages.hasOwnProperty(id)) {			var page = pages[id];			this.edittoken = page.edittoken;			this.content = page.revisions[0]['*'];			this.timestamp = page.revisions[0].timestamp;			//Check whether numerical			var type;			if (this.ticket.match(/^\d*$/)) {				type = "id";			} else {				type = "ticket";			}			var template = '";			var text = this.content;			var permTester = /\|Permission\s*\=/i;			if (!permTester.test(text)) {				return alert('No suitable place found to insert template!'); }			text = this.cleanUp(text); var replacedlicence = text.match(/\|Permission\s*\=([^\n]*)(\n)/); if (replacedlicence && replacedlicence[1] && replacedlicence[2]) { if ($.trim(replacedlicence[1]) !== "") { replacedlicence = replacedlicence[2] + replacedlicence[1]; text += "\r\n" + replacedlicence; }			}			text = text.replace(/\|Permission(\s*)\=[^\n]*/i, "|Permission$1= " + template); this.content = text; this.savePage('Adding OTRS permission using PermissionOTRS.js'); }		}	} }; var OTRSreceived = window.OTRSreceived = { prompt: function { this.ticket = prompt('Ticket link or Number:'); this.ticket = $.trim(this.ticket); if (this.ticket) this.getPage(this, this.addPermission); },	getPage: null, cleanUp: null, savePage: null, // Rillke: I will never understand why doing so complicated. Just use and you even get the server(UTC)-time. formatDate: function (fmt, date) { var pad0 = function (s) { s = "" + s; return (s.length > 1 ? s : "0" + s); }; // zero-pad to two digits if (!date) date = new Date; fmt = fmt.replace(/YYYY/g, date.getUTCFullYear); fmt = fmt.replace(/MM/g, pad0(date.getUTCMonth + 1)); fmt = fmt.replace(/DD/g, pad0(date.getUTCDate)); fmt = fmt.replace(/MON/g, mw.config.get('wgMonthNames')[date.getUTCMonth+1]); fmt = fmt.replace(/DAY/g, date.getUTCDate); return fmt; },	addPermission: function (result) { var pages = result.query.pages; for (var id in pages) { if (pages.hasOwnProperty(id)) { var page = pages[id]; this.edittoken = page.edittoken; this.content = page.revisions[0]['*']; this.timestamp = page.revisions[0].timestamp; //Check whether numerical var type; if (this.ticket.match(/^\d*$/)) { type = "id"; } else { type = "ticket"; }			var template = '");			var text = this.content;			var permTester = /\|Permission\s*\=/i;			if (!permTester.test(text)) {				return alert('No suitable place found to insert template!');			}			text = this.cleanUp(text);			var replacedlicence = text.match(/\|Permission\s*\=([^\n]*)(\n)/);			if (replacedlicence && replacedlicence[1] && replacedlicence[2]) {				if ($.trim(replacedlicence[1]) !== "") {					replacedlicence = replacedlicence[2] + replacedlicence[1];					text += "\r\n" + replacedlicence;				}			}			text = text.replace(/\|Permission(\s*)\=[^\n]*/i, "|Permission$1= " + template);			this.content = text;			this.savePage('OTRS email received but permission not yet confirmed');		}		}	} }; OTRSreceived.getPage = PermissionOTRS.getPage = function (thisarg, cb) {	$.getJSON(mw.config.get('wgScriptPath') + '/api.php', {		action: "query",		format: "json",		prop: "info|revisions", intoken: "edit", rvprop: "content|timestamp", titles: mw.config.get('wgPageName') }, function(r) {cb.apply(thisarg, [r]);}); }; OTRSreceived.cleanUp = PermissionOTRS.cleanUp = function(text) {	//text = text.replace(/\=\= Summary \=\=/g, '== ==');	//text = text.replace(/\=\= Licensing \=\=/g, '==  ==');	text = text.replace(/\{\{OTRS[ _\-][Pp]ending[^\}\n]*\}\}/g, );	text = text.replace(/\{\{no[ _]license[^\}\n]*\}\}/g, );	text = text.replace(/\{\{no[ _]permission[^\}\n]*\}\}/g, );	text = text.replace(/\{\{no[ _]OTRS[ _]permission[^\}\n]*\}\}/g, );	text = text.replace(/\{\{OTRS[ _][Rr]eceived[^\}\n]*\}\}/g, );	text = text.replace(/\{\{delete[^\n]*\(CE[S]?T\)\s*/g, );	text = text.replace(/\{\{delete[^\}\n]*\}\}\b/g, );	text = text.replace(/\{\{di-no[ _]permission[^\}\n]*\}\}\b/g, ); //Added by Legoktm	return text; }; OTRSreceived.savePage = PermissionOTRS.savePage = function (summary) {	var params = {		action: 'edit',		summary: summary,		watchlist: 'preferences', title: mw.config.get('wgPageName'), token: this.edittoken, text: this.content, format:'json' };	$.ajax({		url: mw.config.get('wgScriptPath') + '/api.php',		cache: false,		dataType: 'json',		data: params,		basetimestamp: this.timestamp,		type: 'POST',		success: function {			window.location.reload;		}	}); }; $(document).ready(function {	mw.loader.using( ['mediawiki.util'], function { mw.util.addPortletLink('p-tb', 'javascript:void(PermissionOTRS.prompt)', 'PermissionOTRS'); mw.util.addPortletLink('p-tb', 'javascript:void(OTRSreceived.prompt)', 'OTRS received'); }); }); }(jQuery, mediaWiki)); //