MediaWiki:Gadget-PermissionOTRS.js

Megjegyzés: közzététel után frissítened kell a böngésződ gyorsítótárát, hogy lásd a változásokat.

  • Firefox / Safari: tartsd lenyomva a Shift gombot és kattints a Frissítés gombra a címsorban, vagy használd a Ctrl–F5 vagy Ctrl–R (Macen ⌘–R) billentyűkombinációt
  • Google Chrome: használd a Ctrl–Shift–R (Macen ⌘–Shift–R) billentyűkombinációt
  • Internet Explorer / Edge: tartsd nyomva a Ctrl-t, és kattints a Frissítés gombra, vagy nyomj Ctrl–F5-öt
  • Opera: Nyomj Ctrl–F5-öt
/**
 * PermissionOTRS.js
 * @description A képengedély sablon hozzáadására szolgáló eszköz.
 * @revision 2017. május 11., 17:50 (CEST)
 * @source https://commons.wikimedia.org/wiki/MediaWiki:Gadget-PermissionOTRS.js
 * @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
 *	Fordította: Bencemac
 * @required modules: mediawiki.util, oojs-ui-core, oojs-ui-windows, mediawiki.api
 * <nowiki>
 */
/*global mediaWiki, jQuery*/
/*jshint curly:false, scripturl:true, forin:false*/
(function ( $, mw ) {
'use strict';

var user = mw.config.get( 'wgUserName' ),
	uGrp = mw.config.get( 'wgUserGroups' ),
	page = mw.config.get( 'wgPageName' ),
	sLink = '([[MediaWiki:Gadget-PermissionOTRS.js|Szkript]]) [[Wikipédia:ÖNÜF|ÖNÜF]] ',
	ticket = null,
	content = null,
	timestamp = null,
	deferred = null,
	editform = ( /^(edit|submit)$/.test( mw.config.get( 'wgAction' ) ) ) ? $( '#wpTextbox1' ) : null;

if ( mw.config.get( 'wgNamespaceNumber' ) !== 6 ||
	$.inArray( 'autoconfirmed', uGrp ) === -1 ||
	!user ) return;

function _prompt( text, OTRS ) {
	/* nincs ilyen a magyar sablonban
	var $otrsField = $( '#field-otrs' );
	if ( $otrsField.length ) {
		ticket = $otrsField.attr( 'title' ).replace( /[^\n]+(\d{16})$/, '$1' );
	} else if ( editform && /\{\{otrs[ _]received\|id=(\d{16})+[^\}\n]*\}\}/i.test( editform.val() ) ) {
		ticket = RegExp.$1;
	}
	*/
	
	var type = 'errorbox',
		noprompt = false; // whether we expect input
	if ( !text ) {
		text = 'Adj meg egy érvényes 16 számjegyű számot.';
	} else if ( text === 'FAIL' ) {
		text = 'Nem található megfelelő hely a sablon beillesztésére. Írd be kézzel.';
		noprompt = true;
	} else {
		type = '';
	}
	if ( type ) {
		text = $( '<span>' )
			.addClass( type )
			.text( text );
	}
	mw.loader.using( [ 'oojs-ui-core', 'oojs-ui-windows' ], function () {
		if ( noprompt ) {
			OO.ui.alert( text );
		} else {
			var config = {
				maxLength: 23, // for possible pre-phrase "Ticket#"
				validate: /^\s*(Ticket)?#?\d{16}\s*$/,
				value: ticket
			};
			OO.ui.prompt( text, { textInput: config } ).done( function ( result ) {
				ticket = $.trim( result ).replace( /^(Ticket)?#/, '' ); // clean up input
				if (ticket) {  // Check ticket validity
					if ( /^\d{16}$/.test( ticket ) ) {
						_getPage( OTRS );
					} else {
						_prompt( '', OTRS );
					}
				}
			} );
		}
	} );
}

function _addPermission( template, summary ) {
	var text = _cleanUp( content ),
		text_before = text,
		regP = /( *\|\s*[Ee]ngedély *\=\s*)([^\n]*\n)/,
		regA = /( )*\|\s*[Ss]zerző( )*\=\s*[^\n]*\n/g; // fallback position, if no permission parameter
	// Remove {{OTRS pending}}
	text = text.replace( /\n*\{\{[Ee]ngedélykérés folyamatban\|[^}]*\}\} */, '' );
	text = text.replace( /\n*\{\{[Üü]gyintézés folyamatban\|[^}]*\}\} */, '' );
	if ( !regP.test( text ) ) {
		if ( !regA.test( text ) ) {
			_prompt( 'FAIL' );
			return;
		}
		var l = regA.lastIndex,
			s1 = RegExp.$1 || '', // leading whitespaces as indent?
			s2 = RegExp.$2 ? ' ' : '', // whitespace on parameter?
			sr = text.substr( l ).search( /^\s*(\||\}\})/m ); // try to got at template end
		if ( sr >= 0 ) {
			sr += l; // insert to the end of information template
			text = text.substr(0, sr) + s1 + '| engedély' + s2 + '=' + s2 + template + '\n' + text.substr(sr);
		} else {
			_prompt( 'FAIL' );
			return;
		}
	} else {
		if ( text.indexOf( template ) > 0 ) {
			mw.notify( 'A jegy már szerepel az oldalon.', { title: 'Hiba…', type: 'warn' } );
			return;
		}
		text = text.replace( regP, function ( match, p1, p2 ) {
			return p1.replace( /\n*$/, '' ) + template + ( p2 ? '\n' + p2 : '' );
		} );
	}
	if ( text_before === text ) {
		_prompt('FAIL');
		return;
	}
	content = text;
	_savePage( sLink + summary );
}

var PermissionOTRS = function PermissionOTRS() {
	_addPermission( '{{Képengedély|id=' + ticket + '}}', 'engedély kihelyezése' );
};

/*var 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 _cleanUp( text ) {
	// ToDo: here could be loaded another cleanup module!?
	// text = text
	// 	.replace(/\=\= ?Summary ?\=\=/, '=={{int:filedesc}}==')
	// 	.replace(/\=\= ?Licensing ?\=\=/, '=={{int:license-header}}==')
	// 	.replace(/\{\{OTRS[ _\-](pending|received)[^\}\n]*\}\}\s?/ig, '')
	// 	.replace(/\{\{[Nn]o[ _](OTRS[ _])?permission[^\}\n]*\}\}\s*/g, '\n')
	// 	.replace(/\{\{[Dd]elete[^\}\n]*\}\}\s?/g, '')
	// 	.replace(/\[\[[Cc]ategory\:(u|U)ploaded[ _]with[ _]UploadWizard\]\]\n/g, '')
	// 	.replace(/^\|\s*([A-Z][^=\n{}\|?!.]+\s*\=)/gm,
	// 		function (m, p){ return '|' + p.toLowerCase(); }  // lowercase parameter name default
	// 	);
	return $.trim( text );
}

function _apiFail( code, r ) {
	if ( code === 'http' ) {
		console.log( 'HTTP error: ' + r.textStatus );
	} else if ( code === 'ok-but-empty' ) {
		console.log( 'Üres válasz érkezett a szervertől.' );
	} else {
		console.log( 'API hiba: ' + code );
	}
	mw.notify(
		'Hiba lépett fel a művelet végrehajtása során.\nAPI-hibakód:' + code + '\nKérlek, próbáld újra!',
		{ title: 'Hiba!', autoHide: 0, type: 'error' }
	);
}

function _getPage( OTRS ) {
	if ( !editform ) {
		mw.notify(
			'Az összekapcsolás a(z) ' + ticket + ' számú jeggyel elkezdődött',
			{ title: 'Folyamatban…' }
		);
		mw.loader.using( 'mediawiki.api', function () {
			new mw.Api().edit(
				page,
				function ( revision ) {
					mw.notify( 'Az oldal tartalmának betöltése…', { title: 'Folyamatban…' } );
					content = revision.content;
					timestamp = revision.timestamp;
					deferred = $.Deferred();
					OTRS();
					return deferred.promise();
				}
			)
			.done( function () {
				doneMsg().done( function () {
					location.reload();
				} );
			} )
			.fail( _apiFail );
		} );
	} else {
		content = editform.val();
		return OTRS();
	}
}

function doneMsg() {
	return mw.notify( 'Képengedély sikeresen hozzáadva a laphoz.', { title: 'Kész!' } );
}

function _savePage( summary ) {
	if ( deferred ) {
		deferred.resolve( {
			text: content,
			summary: summary,
			watchlist: 'preferences',
			minor: false
		} );
	} else {
		$( '#wpMinoredit' ).prop( 'checked', 0 );
		editform.val( content );
		$( '#wpSummary' ).val( function ( i, v ) {
			return $.trim( v.replace( summary, '' ) + ' ' + summary );
		} );
		doneMsg();
	}
}

function dialogOTRS( e, OTRS ) {
	e.preventDefault();

	function chkuGrp( b ) {
		if ( b ) {
			_prompt( 'Ticket ID (16 jegyű szám):', OTRS );
		} else {
			mw.notify(
				'Nincs jogosultságod a művelet végrehajtásához!',
				{ title: 'ÖNÜF hiba!', type: 'warn' }
			);
		}
	}
	
	mw.loader.using( [ 'mediawiki.api' ], function () {
		if ( $.inArray( 'vrt-permissions', mw.config.get('wgGlobalGroups') ) >= 0 || $.inArray( 'sysop', uGrp ) >= 0 ) {
			chkuGrp( 1 );
		} else { // run only for non-admins and no global VRT membership found?
			new mw.Api().get( {
				meta : 'globaluserinfo',
				guiuser : user,
				guiprop : 'groups',
			} ).done( function ( r ) {
				chkuGrp( ( $.inArray( 'vrt-permissions', r.query.globaluserinfo.groups ) >= 0 ) );
			} ).fail( function ( code, r ){ _apiFail( code, r ); } );
		}
	} );
}

var bReceive, bConfirm; // booleans; add link only if needed
$.each( mw.config.get( 'wgCategories' ), function ( i, text ) {
	if ( /^Engedéllyel felhasználható művek$/.test( text ) )
		bConfirm = bReceive = true;
//	else if ( /^OTRS received/.test( this.text ) )
//		bReceive = true;
} );

mw.loader.using( 'mediawiki.util', function () {
	if ( !bConfirm ) {
		$( mw.util.addPortletLink('p-tb', '#', 'Képengedély hozzáadása') )
		.click( function ( e ) { dialogOTRS( e, PermissionOTRS ); } );
//		if ( !bReceive )
//			$( mw.util.addPortletLink('p-tb', '#', 'OTRS received') )
//			.click( function ( e ) { dialogOTRS( e, OTRSreceived ); } );
	}
});
}( jQuery, mediaWiki ));
// EOF </nowiki>