İçeriğe atla

MediaWiki:Gadget-PermissionOTRS.js

Vikipedi, özgür ansiklopedi

Not: Sayfayı kaydettikten sonra değişiklikleri görebilmek için tarayıcınızın önbelleğinizi temizlemeniz gerekir. Google Chrome, Firefox, Microsoft Edge ve Safari: ⇧ Shift tuşuna basılı tutun ve Yeniden Yükle araç çubuğu düğmesine tıklayın. Ayrıntılar ve diğer tarayıcılara yönelik yönergeler için Vikipedi:Önbelleğinizi atlayın sayfasını inceleyin.

/* ========================================================================== *\
*                                                                              *
*   MediaWiki:PermissionOTRS.js                                                *
*   Maintainer: [[User:Bryan]]                                                 *
*   Copyright (c) 2007 Bryan Tong Minh.                                        *
*   Licensed under the terms of the MIT license                                *
*                                                                              *
*  ==========================================================================  *
*                                                                              *
*   This scripts allows you to add OTRS permission links in an easy manner.    *
*   It will replace {{OTRS pending}} by the correct permission template.       *
*   If no occurrence of {{OTRS pending}} can be found, it will OVERWRITE       *
*   the permission field of the information template. If the information       *
*   template is not available, the script will fail.                           *
*                                                                              *
*  ==========================================================================  *
*                                                                              *
*   Tested with: Mozilla Firefox 2.0.0.6                                       *
*   Install this script by adding the following code to your monobook.js:      *
*    // [[MediaWiki:PermissionOTRS.js]]                                        *
*    includePage('MediaWiki:PermissionOTRS.js');                               *
*                                                                              *
\* ========================================================================== */

function addPermission(ticket)
{
	mw.loader.using( 'mediawiki.api', function () {
		var api = new mw.Api;
		api.get( {
			action: 'query',
			prop: 'info|revisions',
			intoken: 'edit',
			rvprop: 'content|timestamp',
			titles: mw.config.get('wgPageName')
		} ).done( function ( info ) {
			for (var key in info['query']['pages'])
			{
				var page = info['query']['pages'][key];
				var token = page['edittoken'];
				var content = page['revisions'][0]['*'];

				var rOTRS = new RegExp('\\{\\{Otrs[_ ]pending\\}\\}', 'i');
				if (rOTRS.test(content))
				{
					content = content.replace(rOTRS, '{{PermissionOTRS|ticket=' + ticket + '}}');
				}
				else
				{
					var rPermission = new RegExp('\\n\\|Permission[ \\t]*=.*', 'i');
					if (rPermission.test(content))
					{
						content = content.replace(rPermission, '\n|Permission={{PermissionOTRS|ticket=' +
							ticket + '}}');
					}
					else
					{
						alert('No suitable place found to insert template!');
						return;
					}
				}
				api.post( {
					action: 'edit',
					title: mw.config.get('wgPageName'),
					token: token,
					summary: 'Adding [[Commons:OTRS|OTRS]] permission using [[MediaWiki:Gadget-PermissionOTRS.js]]',
					text: content
				} ).done( function () {
					document.location.href = '/wiki/' + mw.config.get('wgPageName');
				} );
			}	
		} );
	} );
}
function OTRS()
{
	var ticket = prompt('Ticket link?');
	if (ticket) addPermission(ticket);
}

$(function () { 
	if (mw.config.get('wgNamespaceNumber') == 6) { //NS_IMAGE
		var t = document.getElementById('t-whatlinkshere');
        	if (!t) return;
		var li = document.createElement('li');
		var a = document.createElement('a');
		a.setAttribute('href', 'javascript:void(OTRS())');
		a.appendChild(document.createTextNode('PermissionOTRS'));
		li.appendChild(a);
		t.parentNode.appendChild(li);
	}
});