MediaWiki:Gadget-PermissionOTRS.js

From Wikipedia

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
// <nowiki>
// PermissionOTRS.js
//
// Adds OTRS permission template via the API. Other templates such as
// {{OTRS pending}} will be removed.
//
// Maintainer: [[DieBuche]]
// Licensed under the terms of the MIT license
// Authors: Bryan Tong Minh, [[:de:User:DerHexer]], [[User:Guandalug]], [[User:DieBuche]]

/*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: "{{subst:" + "REVISIONUSER}}",
	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 = '{{PermissionOTRS|' + type + '=' + this.ticket + "|" + this.usersignature + "}}";

			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 [[Commons:OTRS|OTRS]] permission using [[MediaWiki:Gadget-PermissionOTRS.js|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 {{subst:#time ...}} 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 = '{{OTRS received|' + type + '=' + this.ticket + this.formatDate("|year=YYYY|month=MON|day=DAY}}");
 
			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('[[Commons:OTRS|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, '== {{int:filedesc}} ==');
	text = text.replace(/\=\= Licensing \=\=/g, '== {{int:license-header}} ==');

	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, '');
	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.util.addPortletLink('p-tb', 'javascript:void(PermissionOTRS.prompt())', 'PermissionOTRS');
	mw.util.addPortletLink('p-tb', 'javascript:void(OTRSreceived.prompt())', 'OTRS received');
});

}(jQuery, mediaWiki));
// </nowiki>