User:NonvocalScream/monobook.js

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
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.
// <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]]
 
PermissionOTRS = {
	usersignature: "\~\~\~",
	prompt: function () {
		this.ticket = prompt('Ticket link or Number:');
		this.ticket = $.trim(this.ticket);
		if (this.ticket) this.getPage();
	},
	getPage: function () {
		$.getJSON(wgScriptPath + '/api.php', {
			action: "query",
			format: "json",
			prop: "info|revisions",
			intoken: "edit",
			rvprop: "content|timestamp",
			titles: wgPageName
		}, function(r) {PermissionOTRS.addPermission(r);});
	},
	addPermission: function (result) {
		var pages = result.query.pages;
		for (var id in pages) {
			var page = pages[id];
			this.edittoken = page.edittoken;
			this.content = page.revisions[0]['*'];
			this.timestamp = page.revisions[0].timestamp;
 
			//Check whether numerical
			if (this.ticket.match(/^\d*$/)) {
				var type = "id";
			} else {
				var 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 = 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, '');
 
			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();
		}
	},
	savePage: function () {
		var params = {
			action: 'edit',
			summary: 'Adding [[Commons:OTRS|OTRS]] permissions.',
			watchlist: 'preferences',
			title: wgPageName,
			token: this.edittoken,
			text: this.content,
			format:'json'
		};
		$.ajax({
			url: wgScriptPath + '/api.php',
			cache: false,
			dataType: 'json',
			data: params,
			basetimestamp: this.timestamp,
			type: 'POST',
			success: function () {
				window.location.reload();
			}
		});
	}
};
if (wgNamespaceNumber == 6) $(document).ready(function () {
	mw.util.addPortletLink('p-tb', 'javascript:void(PermissionOTRS.prompt())', 'PermissionOTRS');
});
 
// </nowiki>