مدیاویکی:Gadget-rollback.js

' 'قئید:' یازدیق‌دان سونرا دییشیک‌لیک‌لری گؤرمک اوچون براوزئرینیزین کئش یادداشیندا تمیزلمه‌نیز لازیم اولا بیلر.

  • 'فایرفاکس / سافاری:Shift دویمه‌سینه باسین "یئنی‌دن یوکله"-ا داخیل اولون یا دا Ctrl-F5 یا Ctrl-R ائدین (ماج اوچون ⌘-R).
  • 'گووگل کروم:'Ctrl+Shift+R باسین. (ماج اوچون ⌘-Shift-R)
  • 'اینترنت ایکسپئلور: کلید Ctrl باسین یئنی‌لئ 'یئ داخیل اولون یا دا Ctrl-F5 ائدین.
  • 'اوپئرا:' آلت‌لری → سئچیملر 'دن اؤنبئللیغی Tools → Preferences تمیزلیین.
jhhg// From: https://hu.wikipedia.org/w/index.php?title=MediaWiki:Gadget-rollback.js&oldid=18322106
( function ( $, mw ) {
	var rollbackDelay = 1000; // ms
	
	/**
	 * Displays success or failure with an icon at the end of each link.
	 * @param {jQuery} $link
	 * @param {String} status One of 'working', 'success', 'error'
	 * @param {String} [errorCode] Error code, if status is 'error'
	 * @param {String} [errorMessage] Error message, if status is 'error'
	 */
	function updateLinkStatus( $link, status, errorCode, errorMessage ) {
		$link.removeClass( 'api-working api-success api-error' );
		if ( status in { working: 1, success: 1, error: 1 } ) {
			$link.addClass( 'api-' + status );
			if ( status === 'error' ) {
				mw.log( 'gadget-rollback: ' + errorCode );
				errorMessage = $.parseHTML( errorMessage );
				errorMessage = $( '<span>' ).append( errorMessage );
				errorText = errorMessage.text();
				errorMessage.prepend( 'ارور: ' );
				mw.notify( errorMessage );
				$link.attr( 'title', errorText );
			}
		} else {
			mw.log( 'gadget-rollback: invalid link status' );
		}
	}
	
	/**
	 * Given some rollback links, sends an API request which does the same thing
	 * the links would have done. Displays success or failure with an icon
	 * at the end of each link.
	 * @param {jQuery} $links
	 */
	function submitRollbackLink( $links ) {
		var deferreds = [];
		
		$links.each( function ( i, el ) {
			var errorMessage,
				api = new mw.Api(),
				deferred = new $.Deferred(),
				uri = new mw.Uri( el.href ),
				$el = $( el ),
				title = uri.query.title,
				user = uri.query.from,
				bot = uri.query.bot;

			deferreds.push( deferred );
			
			if ( !title || ! user ) { // Rollback info not found in link
				errorMessage = 'Belső hiba: nem sikerült értelmezni a linket';
				updateLinkStatus( $el, 'error', 'rollback-info-not-found-in-link', errorMessage );
				deferred.reject( errorMessage );
				return;
			}
			
			updateLinkStatus( $el, 'working' );

			api.postWithToken( 'rollback', {
				action: 'rollback',
				title: title,
				user: user,
				markbot: bot,
				formatversion: 2,
				errorformat: 'html',
				errorsuselocal: true,
				uselang: mw.config.get( 'wgUserLanguage' )
			} ).done( function ( result, jqXHR ) {
				updateLinkStatus( $el, 'success' );
				deferred.resolve();
			} ).fail( function ( code, result ) {
				if ( result.errors && result.errors[0] ) {
					errorMessage = result.errors[0].html;
				} else {
					errorMessage = 'خطای API: ' +  code;
				}
				updateLinkStatus( $el, 'error', code, errorMessage );
				deferred.reject( errorMessage );
			} );
		} );
		
		return $.when.apply( $, deferreds );
	}

	function rollbackAll() {
		var $links = $( '.mw-rollback-link a.rollback' ),
			$button = $( '#rollback-all' ),
			oldButtonText = $button.text();

		if ( !confirm( 'هامیسینی قایتاریرسینیز؟' ) ) {
			return false;
		}
		
		// Preload the notification module for mw.notify
		mw.loader.load( 'mediawiki.notification' );

		$button
			.attr( 'disabled', true )
			.text( 'قایتاریلیر' );
		submitRollbackLink( $links ).always( function () {
			$button
				.attr( 'disabled', false )
				.text( oldButtonText );
		} );
	}
	
	/**
	 * Remove NavPopups from a link
	 * Copied from en:MediaWiki:Gadget-popups.js
	 * @param {jQuery} $link
	 */
	function removeTooltip( $link ) {
		var a = $link.get( 0 );
		if ( !a ) {
			return;
		}
		a.onmouseover = null;
		a.onmouseout = null;
		if ( a.originalTitle ) {
			a.title = a.originalTitle;
		}
		a.hasPopup = false;
	}
	
	/**
	 * Sets up individual rollback links
	 */
	function installLinks() {
		var $botLink,
			isAdmin = ( $.inArray( 'sysop', mw.config.get( 'wgUserGroups' ) ) !== -1 ),
			$rollbackSpans = $( '.mw-rollback-link' ),
			clickHandler = function( e ) {
				// Preload the notification module for mw.notify
				mw.loader.load( 'mediawiki.notification' );
				submitRollbackLink( $( this ) );
				e.preventDefault();
			};
		
		$rollbackSpans.each( function ( i, el ) {
			var $span = $( el ),
				$link = $span.find( 'a:eq(0)' );
				
			$span.off( 'click', 'a[data-mw="interface"]' ); // stop MediaWiki's own rollback.js from interfering @TODO when that script becomes more intelligent, a lot of this code will be unnecessary

			$link
				.addClass( 'rollback' )
				.attr( 'nopopup', 1 ) // stop NavPopups from interfering
				.attr( 'data-mw', false ) // stop MW rollback.js even if it's loaded late
				.click( clickHandler );
				
			// if NavPopups was loaded first, the link is already processed and nopopup is ignored, we need to unprocess it
			removeTooltip( $link );
			
			if ( isAdmin ) {
				$botLink = $( '<a>' ).attr( {
					title: 'روبات بایراغی ایله قایتارما',
					href: $link.attr( 'href' ).replace( '&action=rollback', '&action=rollback&bot=1' ),
					class: 'bot-rollback',
					nopopup: 1
				} );
				
				$botLink
					.text( 'روبات' )
					.click( clickHandler );
				$link
					.after( $botLink )
					.after( ' | ' );
			}
		} );
	}
		
	/**
	 * Sets up a "rollback all" button
	 */
	function installButton() {
		var $rollbackAllDiv = $( '<div>' ).css( 'float', 'right' );

		$rollbackAllDiv.appendTo( '.firstHeading' ).append(
			$( '<button>' )
				.attr( 'id', 'rollback-all' )
				.text( 'بوتون دییشدیرمه‌لری قایتار' )
				.click( rollbackAll )
		);
	}
	
	/**
	 * Main setup function
	 */
	function install() {
		if ( mw.config.get( 'wgAction' ) === 'history' || mw.config.get( 'wgCanonicalNamespace' ) === 'Special' ) {
			installLinks();
			if ( mw.config.get( 'wgCanonicalSpecialPageName' ) === 'Contributions' ) {
				installButton();
			}
		}
	}
	
	$( install );
} ) ( jQuery, mediaWiki );