User:Samwilson/MarkProofread.js

From Wikisource
Jump to navigation Jump to search
Note: After saving, changes may not occur immediately. Click here to learn how to bypass your browser's cache.
  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (Cmd-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (Cmd-Shift-R on a Mac)
  • Internet Explorer: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Clear the cache in Tools → Preferences

For details and instructions about other browsers, see Wikipedia:Bypass your cache.

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' );
	}
};

$.when( mw.loader.using( [ 'mediawiki.util', 'mediawiki.api' ] ), $.ready )
	.then( function () {
		proofreadIndicatorsGadget.init();
	} );