MediaWiki:Gadget-watchlist-add-categorymembers.js

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.
// Autor: Peter Bowman @ plwikt
 
var watchlist_add_categorymembers = mw.libs.watchlistAddCategorymembers = {
	articles_list: null,
	cat_list: null,
	cat_queue: null,
	visited_cats: null,
	cat_nsname: '',
	api: null
};
 
watchlist_add_categorymembers.query = function ( catname, incl_subcats, cont, callback, refresh ) {
	var self = this;
	this.visited_cats[ this.trimCatNamespace( catname ) ] = true;
	
	if ( !cont ) {
		catname = this.trimCatNamespace( catname );
		
		if ( incl_subcats && this.cat_list.length ) {
			this.cat_list.forEach( function ( cat ) {
				if ( catname === cat ) {
					if ( !self.cat_queue.length ) {
						callback( true );
					} else {
						self.query( self.cat_queue.pop(), true, null, callback, refresh );
						return;
					}
				}
			} );
		}
			
		this.cat_list.push( catname );
	}
 
	this.api.get( $.extend( {
		list:     'categorymembers',
		formatversion: 2,
		cmtitle:  'Category:' + catname,
		cmtype:   'page' + ( incl_subcats ? '|subcat' : '' ),
		cmprop:   'title',
		cmlimit:  'max'
	}, {
		'continue': cont || undefined
	} ) ).done( function ( data ) {
		var pages = data.query.categorymembers;
 
		pages.forEach( function ( page ) {
			if ( incl_subcats && page.ns === 14 ) {
				if ( !( self.trimCatNamespace( page.title ) in self.visited_cats ) ) {
					self.cat_queue.push( page.title );
				}
			} else {
				self.articles_list.push( page.title );
			}
		} );
 
		if ( data[ 'continue' ] ) {
			refresh();
			cont = data[ 'continue' ];
			self.query( catname, incl_subcats, cont, callback, refresh );
		} else {
			if ( incl_subcats && self.cat_queue.length ) {
				refresh();
				self.query( self.cat_queue.pop(), true, null, callback, refresh );
			} else {
				callback( !!pages.length );
			}
		}
	} );
};
 
watchlist_add_categorymembers.trimCatNamespace = function ( s ) {
	if ( s.indexOf( this.cat_nsname ) === 0 ) {
		s = s.slice( this.cat_nsname.length + 1 );
	}
	
	return s;
};
 
watchlist_add_categorymembers.init = function () {
	this.cat_nsname = mw.config.get( 'wgFormattedNamespaces' )[ 14 ];
	this.api = new mw.Api();
	
	var $textarea = $( 'textarea' ).first();
 
	var $textbox = null;
	var $checkbox = null;
	var $button = null;
	var $result = null;
	
	var self = this;
 
	$( '#mw-input-wpTitles' ).before(
		$( '<label>' )
			.attr( 'for', 'cat-inputbox' )
			.text( mw.msg( 'watchlist-addcm-addcategory' ) ),
		$textbox = $( '<input>' )
			.attr( {
				'type': 'text',
				'id': 'cat-inputbox',
				'name': 'cat-inputbox',
				'size': '60'
			} ),
		$( '<span>' )
			.css( 'margin', '0 1em' )
			.append(
				$checkbox = $( '<input>' )
					.attr( {
						'type': 'checkbox',
						'id': 'cat-checkbox',
						'name': 'cat-checkbox'
					} ),
				$( '<label>' )
					.attr( 'for', 'cat_checkbox' )
					.text( mw.msg( 'watchlist-addcm-subcat' ) )
					.css( 'margin-left', '5px' )
			),
		$button = $( '<input>' )
			.attr( {
				'type': 'submit',
				'id': 'cat-submit',
				'value': mw.msg( 'watchlist-addcm-search' )
			} )
			.on( 'click', function ( e ) {
				e.preventDefault();

				var catname = $textbox.val();
				
				if ( !catname.length ) {
					return false;
				}

				$button.attr( 'disabled', true );
				var result_text = mw.msg( 'watchlist-addcm-searching' );
				$result.text( result_text );
				var incl_subcats = $checkbox.prop( 'checked' );

				self.articles_list = [];
				self.cat_list = [];
				self.cat_queue = [];
				self.visited_cats = {};

				self.query( catname, incl_subcats, null, function ( op ) {
					if ( op ) {
						var old_text = $textarea.val();
						var new_text = self.articles_list.join( '\n' );
						$result.text( mw.msg( 'watchlist-addcm-added-X-articles', self.articles_list.length )
							+ ( incl_subcats
								? ( ' ' + mw.msg( 'watchlist-addcm-scanned-X-subcats', self.cat_list.length - 1 ) )
								: ''
						) );
						$textarea
							.val( ( old_text + '\n' + new_text ).trim() )
							.textSelection( 'setSelection', {
								start: old_text.length,
								end: old_text.length + 1 + new_text.length
							} )
							.focus();
					} else {
						$result.text( mw.msg( 'watchlist-addcm-nonexistent-cat' ) );
					}
					$button.attr( 'disabled', false );
				}, function () {
					$result.text( result_text + ' ' + self.articles_list.length );
				} );
			} ),
		$( '<br>' ),
		$result = $( '<span>' ).attr( 'id', 'cat-results' )
	);
	
	// z ext.vector.simpleSearch.js
	$textbox.suggestions( {
		fetch: function ( query ) {
			var $this = $( this );
			if ( query.length !== 0 ) {
				var request = self.api.get( {
					action: 'opensearch',
					search: query,
					namespace: 14,
					suggest: ''
				} ).done( function ( data ) {
					if ( Array.isArray( data ) && data.length > 1 ) {
						$this.suggestions( 'suggestions', data[ 1 ].map( function ( name ) {
							return self.trimCatNamespace( name );
						} ) );
					}
				} );
				$this.data( 'request', request );
			}
		},
		cancel: function () {
			var request = $( this ).data( 'request' );
			// If the delay setting has caused the fetch to have not even happend yet, the request object will
			// have never been set
			if ( request && request.abort && typeof request.abort === 'function' ) {
				request.abort();
				$( this ).removeData( 'request' );
			}
		},
		delay: 120,
		positionFromLeft: $( 'body' ).hasClass( 'rtl' ),
		highlightInput: true
	} )
	.on( 'paste cut drop', function ( e ) {
		// make sure paste and cut events from the mouse and drag&drop events
		// trigger the keypress handler and cause the suggestions to update
		$( this ).trigger( 'keypress' );
	} );
};

watchlist_add_categorymembers.deleteHandler = function ( node, checknew, checkall ) {
	return function ( evt ) {
		var $node = $( node );
		
		$node.find( 'input[type="checkbox"]' ).each( function () {
			var $this = $( this );
			
			if ( !checknew || $node.find( mw.format( 'label[for="$1"]', this.id ) ).find( 'a' ).first().hasClass( 'new' ) ) {
				$this.prop( 'checked', ( checknew || checkall ) ? true : false );
			}
		} );
		
		return false;
	};
};

watchlist_add_categorymembers.checkDeleted = function () {
	var self = this;
	$( 'fieldset fieldset' ).each( function () {
		$( this ).find( 'legend' ).first().next().prepend( [
			$( '<input>' )
				.attr( {
					'type': 'submit',
					'value': mw.msg( 'watchlist-addcm-check-deleted' )
				} )
				.on( 'click', self.deleteHandler( this, true, false ) ),
			$( '<input>' )
				.attr( {
					'type': 'submit',
					'value': mw.msg( 'watchlist-addcm-check-all' )
				} )
				.on( 'click', self.deleteHandler( this, false, true ) ),
			$( '<input>' )
				.attr( {
					'type': 'submit',
					'value': mw.msg( 'watchlist-addcm-uncheck-all' )
				} )
				.on( 'click', self.deleteHandler( this, false, false ) )
		] );
	} );
};
 
if (
	mw.config.get( 'wgCanonicalSpecialPageName' ) === 'EditWatchlist'
) {
	var title = mw.config.get( 'wgTitle' );
	
	if ( title.slice( title.length - 3 ) === 'raw' ) {
		if ( mw.config.get( 'wgUserLanguage' ) === 'pl' ) {
			mw.messages.set( {
				'watchlist-addcm-addcategory':       'Dodaj kategorię:',
				'watchlist-addcm-subcat':            'Uwzględnij podkategorie',
				'watchlist-addcm-search':            'Szukaj',
				'watchlist-addcm-searching':         'Wyszukiwanie trwa, proszę poczekać...',
				'watchlist-addcm-added-X-articles':  'Dodane strony do listy: $1.',
				'watchlist-addcm-scanned-X-subcats': 'Przeskanowane podkategorie: $1.',
				'watchlist-addcm-nonexistent-cat':   'Podana kategoria jest pusta lub nie istnieje.'
			} );
		}
 
		$.when(
			mw.loader.using( [ 'mediawiki.api', 'jquery.suggestions', 'jquery.textSelection' ] ),
			$.ready
		).done( function () {
			watchlist_add_categorymembers.init();
		} );
	} else {
		if ( mw.config.get( 'wgUserLanguage' ) === 'pl' ) {
			mw.messages.set( {
				'watchlist-addcm-check-deleted': 'Zaznacz usunięte',
				'watchlist-addcm-check-all':     'Zaznacz wszystkie',
				'watchlist-addcm-uncheck-all':   'Wyczyść wszystkie'
			} );
		}
		
		$( function () {
			watchlist_add_categorymembers.checkDeleted();
		} );
	}
}