MediaWiki:Gadget-history-hide-bots.js

Z Wikisłownika – wolnego słownika wielojęzycznego

Uwaga: aby zobaczyć zmiany po opublikowaniu, może zajść potrzeba wyczyszczenia pamięci podręcznej przeglądarki.

  • Firefox / Safari: Przytrzymaj Shift podczas klikania Odśwież bieżącą stronę, lub naciśnij klawisze Ctrl+F5, lub Ctrl+R (⌘-R na komputerze Mac)
  • Google Chrome: Naciśnij Ctrl-Shift-R (⌘-Shift-R na komputerze Mac)
  • Internet Explorer / Edge: Przytrzymaj Ctrl, jednocześnie klikając Odśwież, lub naciśnij klawisze Ctrl+F5
  • Opera: Naciśnij klawisze Ctrl+F5.
/**
 * Ukrywanie edycji botów w historii hasła
 * Autor: [[User:Peter Bowman]]
 */

var hhb = mw.libs.historyHideBots = {};

var storageKey = 'ext.gadget.history-hide-bots.state';

var L10n = {
	en: {
		'history-hide-bots-show':       'Show {{PLURAL:$1|$1 bot edit|$1 bot edits}}',
		'history-hide-bots-hide':       'Hide {{PLURAL:$1|$1 bot edit|$1 bot edits}}',
		'history-hide-bots-none':       'No bot edits found',
		'history-hide-bots-show-group': 'Show $1 {{PLURAL:$1|hidden edit|hidden edits}} of the next {{PLURAL:$2|bot|bots}}: $3'
	},
	pl: {
		'history-hide-bots-show':       'Pokaż {{PLURAL:$1|$1 edycję bota|$1 edycje botów|$1 edycji botów}}',
		'history-hide-bots-hide':       'Ukryj {{PLURAL:$1|$1 edycję bota|$1 edycje botów|$1 edycji botów}}',
		'history-hide-bots-none':       'Brak edycji botów',
		'history-hide-bots-show-group': 'Pokaż $1 {{PLURAL:$1|ukrytą edycję|ukryte edycje|ukrytych edycji}} {{PLURAL:$2|bota|botów}}: $3'
	}
};

// [[Wikisłownik:Boty]]
hhb.bots = [
	// Aktywne boty
	'AlkamidBot', 'Olafbot', 'PBbot', 'PiotrekD.bot',
	
	// Aktywne boty bez flagi
	'CommonsDelinker', 'MalarzBOT',
	
	// Nieaktywne boty
	'Beau.bot', 'CompBot', 'DerbethBot', 'EquadusBot', 'Golem',
	'Tawbot~plwiktionary', 'Tsca.bot', 'YarluBot',
	
	// Nieaktywne boty bez flagi
	'AkBot', 'Interwicket', 'KlaudiuBot', 'MonoBot', 'Npkbdbot', 'OsamaKBOT',
	'Uhebot', 'VolkovBot', 'タチコマ robot',
	
	// Boty interwiki, którym odebrano uprawnienia
	'AvocatoBot', 'CarsracBot', 'HydrizBot', 'JAnDbot', 'KamikazeBot',
	'Luckas-bot', 'Makecat-bot', 'MastiBot', 'OctraBot', 'RobotGMwikt',
	'UT-interwiki-Bot', 'YS-Bot',
	
	// Skrypty i boty WMF
	'Conversion script', 'Flow talk page manager', 'Maintenance script',
	'Manager strony dyskusji z Flow', 'MediaWiki default',
	'MediaWiki message delivery', 'Template namespace initialisation script'
];

function processLogs( $pageHistory, button ) {
	var $lis;
	
	$pageHistory.find( '.mw-userlink' ).each( function () {
		var $this = $( this ),
			user = $this.text();
		
		if ( hhb.bots.indexOf( user ) !== -1 ) {
			$this.closest( 'li' )
				.addClass( 'history-is-bot' )
				.data( 'user', user );
			
			hhb.edits++;
		}
	} );
	
	$lis = $pageHistory.find( 'li' );
	
	if ( !hhb.edits || $lis.length < 3 ) {
		return;
	}
	
	$lis
		.filter( function ( i, li ) {
			return (
				!( i === 0 || i === $lis.length - 1 ) &&
				$( li ).hasClass( 'history-is-bot' ) &&
				(
					i === 1 ||
					!$lis.eq( i - 1 ).hasClass( 'history-is-bot' )
				)
			);
		} )
		.each( function () {
			var i, $groupLabel, $li, label,
				$head = $( this ),
				$all = $( [] ),
				bots = [];
			
			for ( i = $lis.index( $head ); i < $lis.length; i++ ) {
				if ( i !== $lis.length - 1 && $lis.eq( i ).hasClass( 'history-is-bot' ) ) {
					$all = $all.add( $lis.eq( i ) );
				} else {
					break;
				}
			}
			
			$all
				.addClass( 'history-hide-bot' )
				.hide()
				.each( function () {
					bots.push( $( this ).data( 'user' ) );
				} );
			
			label = makeLabel( bots );
			
			$groupLabel = $( '<span>' )
				.html( mw.msg(
					'history-hide-bots-show-group',
					bots.length, label.length, label.join( ', ' )
				) );
			
			$li = $( '<li>' )
				.addClass( 'history-bot-group-label' )
				.append( $groupLabel )
				.insertBefore( $head );
			
			$groupLabel.on( 'click', function () {
				$li.hide();
				$all.fadeIn();
				
				if (
					!$pageHistory
						.find( '.history-bot-group-label' )
						.filter( ':visible' )
						.length
				) {
					button.setLabel( mw.msg( 'history-hide-bots-hide', hhb.edits ) );
					hhb.hidden = false;
				}
			} );
		} );
}

function makeLabel( nicks ) {
	var obj, users,
		formatString = '<span style="color:#0645AD;">$1</span>';
	
	if ( nicks.length === 1 ) {
		return [ mw.format( formatString, nicks[ 0 ] ) ];
	}
	
	obj = {};
	users = 0;
	
	$.each( nicks, function ( i, nick ) {
		if ( obj[ nick ] ) {
			obj[ nick ]++;
		} else {
			obj[ nick ] = 1;
			users++;
		}
	} );
	
	if ( users === 1 ) {
		return [ mw.format( formatString, nicks[ 0 ] ) ];
	}
	
	return $.map( obj, function ( count, nick ) {
		var span = mw.format( formatString, nick );
		
		return ( count === 1 )
			? span
			: mw.format( '$1 (x$2)', span, count );
	} );
}

function checkSelected( $lis ) {
	var i, child,
		$selected = $lis.filter( '.selected' ),
		$first = $selected.first(),
		$last  = $selected.last(),
		lis = $lis.get();
	
	if ( $first.css( 'display' ) === 'none' ) {
		$first
			.removeClass( 'selected' )
			.find( 'input[name="diff"]' )
			.prop( 'checked', false )
			.end()
			.find( 'input[name="oldid"]' )
			.css( 'visibility', 'visible' );
		
		for ( i = $first.index() - 1; i >= 0; i-- ) {
			child = lis[ i ];
			
			if ( child.className === 'history-bot-group-label' ) {
				continue;
			}
			
			if ( child.style.display !== 'none' ) {
				$( child )
					.addClass( 'selected' )
					.find( 'input[name="diff"]' )
					.prop( 'checked', true );
				break;
			} else {
				$( child )
					.find( 'input[name="oldid"]' )
					.css( 'visibility', 'visible' );
			}
		}
	}
	
	if ( $last.css( 'display' ) === 'none' ) {
		$last
			.removeClass( 'selected' )
			.find( 'input[name="oldid"]' )
			.prop( 'checked', false )
			.end()
			.find( 'input[name="diff"]' )
			.css( 'visibility', 'visible' );
		
		for ( i = $last.index() + 1; i < lis.length; i++ ) {
			child = lis[ i ];
			
			if ( child.className === 'history-bot-group-label' ) {
				continue;
			}
			
			if ( child.style.display != 'none' ) {
				$( child )
					.addClass( 'selected' )
					.find( 'input[name="oldid"]' )
					.prop( 'checked', true );
				break;
			} else {
				$( child )
					.find( 'input[name="diff"]' )
					.css( 'visibility', 'visible' );
			}
		}
	}
}

mw.messages.set( L10n[ mw.config.get( 'wgUserLanguage' ) ] || L10n.en );

mw.hook( 'wikipage.content' ).add( function ( $content ) {
	var button, $labels,
		$edits = $( [] ),
		$pageHistory = $content.find( '#pagehistory' );
	
	hhb.edits = 0;
	hhb.hidden = mw.storage.get( storageKey ) !== 'shown';
	
	button = new OO.ui.ButtonWidget( {
		id: 'history-hide-bots-button'
	} );
	
	if ( $pageHistory.children().length > 1 ) {
		processLogs( $pageHistory, button );
		$edits = $pageHistory.find( '.history-hide-bot' );
	}
	
	if ( !$edits.length ) {
		button.setDisabled( true ).setLabel( mw.msg( 'history-hide-bots-none' ) );
	} else {
		button.setLabel( mw.msg( hhb.hidden ? 'history-hide-bots-show' : 'history-hide-bots-hide', hhb.edits ) );
		$labels = $pageHistory.find( '.history-bot-group-label' );
		
		if ( hhb.hidden ) {
			checkSelected( $pageHistory.children() );
		} else {
			$labels.hide();
			$edits.show();
		}
		
		button.on( 'click', function ( evt ) {
			hhb.hidden = !hhb.hidden;
			
			if ( hhb.hidden ) {
				button.setLabel( mw.msg( 'history-hide-bots-show', hhb.edits ) );
				
				$edits.fadeOut( function () {
					$labels.show();
					checkSelected( $pageHistory.children() );
				} );
			} else {
				button.setLabel( mw.msg( 'history-hide-bots-hide', hhb.edits ) );
				$labels.hide();
				$edits.fadeIn();
			}
			
			mw.storage.set( storageKey, hhb.hidden ? 'hidden' : 'shown' );
		} );
	}
	
	$content.find( '#mw-history-search .mw-htmlform-submit-buttons' ).first().append( button.$element );
} );