MediaWiki:Gadget-mark-proofread.js

Dari Wikisumber bahasa Indonesia, perpustakaan bebas

Catatan: Setelah disimpan, Anda mungkin perlu melewati tembolok peramban web untuk melihat perubahan.

  • Firefox/Safari: Tekan dan tahan Shift sembari mengeklik Reload, atau tekan Ctrl-F5 atau Ctrl-R (⌘-R di Mac)
  • Google Chrome: Tekan Ctrl-Shift-R (⌘-Shift-R di Mac)
  • Internet Explorer / Edge: Tahan Ctrl sembari mengeklik Refresh, atau tekan Ctrl-F5
  • Opera: Tekan Ctrl-F5.
mw.config.set( 'mark-proofread-index-ns', 106 );

var proofreadIndicatorsGadget = {
	version: 5,
	queue: [],

	init: function() {
		var $tocheck, titleBatch,
			gadget = this;
		if ( mw.config.get( 'wgNamespaceNumber' ) !== mw.config.get( 'mark-proofread-index-ns', 102 ) || ( mw.config.get( 'wgAction' ) !== 'view' && mw.config.get( 'wgAction' ) !== 'purge' ) ) {
			return;
		}
		if ( mw.config.get( 'wgUserName' ) == null ) {
			// Anonymous users are not supported
			return;
		}

		// Validated and without text are done
		// ... but there's no need to flag that
		// COMMENTED OUT: mw.util.$content.find( 'a.quality0, a.quality4' ).addClass( 'ppi-done' );
		
		// Problematic and not proofread are to do
		mw.util.$content.find( 'a.quality1, a.quality2' ).addClass( 'ppi-todo' );

		// Redlinks are to do, but there's no need to flag that.

		// Proofread needs to be checked
		$tocheck = mw.util.$content.find( 'a.quality3' );
		$tocheck.addClass( 'ppi-tocheck' );
		titleBatch = [];
		$tocheck.each( function ( i, $link ) {
			// Strip leading '/wiki/'.
			var title = $( this ).attr( 'href' ).substring( '/wiki/'.length );
			titleBatch.push( decodeURIComponent( title ) );
			if ( titleBatch.length === 50 || i === ( $tocheck.length - 1 ) ) {
				// If we've got a full batch or are at the end of the queue.
				gadget.fetchRevisions( titleBatch );
				titleBatch = [];
			}
		} );
	},

	/**
	 * @param {String[]} titles The page titles to check.
	 */
	fetchRevisions: function( titles ) {
		if ( !titles ) {
			return;
		}
		var request = {
			action: 'query',
			titles: titles.join( '|' ),
			prop: 'revisions',
			rvprop: [ 'content', 'user' ],
		};
		new mw.Api()
			.get( request )
			.done( this.processRevisions.bind( this ) );
	},

	processRevisions: function( result ) {
		if ( result && result.query && result.query.pages ) {
			for ( var pageid in result.query.pages ) {
				this.processPage( result.query.pages[pageid] );
			}
		}
	},

	/**
	 * Check the latest revision of the given page, and if the revision content
	 * contains " user=<wgUserName>" then mark this page as done (by this user).
	 * @param {mixed[]} page
	 */
	processPage: function( page ) {
		if ( page.missing !== undefined || page.revisions.length < 1 ) {
			return;
		}

		var modified = false;

		var revision = page.revisions.shift();
		if ( revision['*'] ) {
			var m = revision['*'].match( / user="([^"]+)" / );
			if ( m ) {
				if ( m[1] == mw.config.get( 'wgUserName' ) ) {
					modified = true;
				}
			}
		}

		// Remove the 'tocheck' class and add either 'done' or 'todo'
		// (look up the page via the title attribute).
		$( 'a[title="' + page.title + '"]' )
			.removeClass( 'ppi-tocheck' )
			.addClass( modified ? 'ppi-done' : 'ppi-todo' );
	}
};

jQuery( document ).ready( function() {
	proofreadIndicatorsGadget.init();
} );