User:Dogad75/MediaWiki:Gadget-PermissionOTRS.js
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.
Documentation for this user script can be added at User:Dogad75/MediaWiki:Gadget-PermissionOTRS. |
/** PermissionOTRS.js
// Adds OTRS permission template via the API. Other templates such as {OTRS pending} will be removed.
* @revision 05:47, 19 February 2016 (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 = _cleanUp(content),
regP = /\|\s*[Pp]ermission\s*\=\s*([^\n]*)\s*\n/,
regA = /\|\s*[Aa]uthor\s*\=\s*[^\n]*\s*\n/g; // fallback position, if no permission parameter
if (!regP.test(text)) {
if (!regA.test(text)) return _prompt('FAIL');
var l = regA.lastIndex,
sr = text.substr(l).search(/^\s*(\||\}\})/m);
if (sr >= 0) {
sr += l;
text = text.substr(0, sr) + "|permission=" + template + "\n" + text.substr(sr);
} else return _prompt('FAIL');
} else {
regP = text.match(regP);
if (regP && regP[1])
template += "\n" + regP[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>