From StrategyWiki, the video game walkthrough and strategy guide wiki
Jump to navigation Jump to search

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
// Recent Changes patrol
// to load on your site, add the following to your personal js page:
// mw.loader.load( 'http://strategywiki.org/w/index.php?title=User:Skizzerz/Scripts/RcPatrol.js&action=raw&ctype=text/javascript' );
// to use, browse to Special:RecentChanges or Special:BlankPage and click on the "Patrol" tab there

function RcPatrol() {
	// the constructor adds a portlet link and a click handler to actually launch the setup
	
	// private variables and methods
	var version = '0.1 alpha';
	var that = this;
	var tab = mw.util.addPortletLink( 'p-cactions', '#', 'Patrol', 'ca-rcpatrol', 'Launch RecentChanges Patrol' );
	var rcapi = {
		action: 'query',
		list: 'recentchanges',
		rcdir: 'newer',
		rclimit: 50,
		rcshow: '!patrolled',
		rctoken: 'patrol'
	};
	var data = {};
	var next = 0;
	var onApiComplete = function( d ) {
		data = d;
		next = 0;
		// check if we have more to load later
		if ( 'query-continue' in data ) {
			rcapi.rcstart = data['query-continue'].recentchanges.rcstart;
		} else {
			rcapi = false;
		}
		that.fetchNextDiff();
	};
	
	// privileged variables and methods
	this.api = new mw.Api();
	this.rcid = false;
	this.token = false;
	this.cur = false;
	this.fetchNextDiff = function() {
		that.rcid = false;
		that.token = false;
		that.cur = false;
		$( '#rcpatrol-inner' ).text( 'Loading next diff...' );
		if ( next == data.query.recentchanges.length && rcapi ) {
			that.api.get( rcapi ).done( onApiComplete ).fail( function( error ) {
				$( '#rcpatrol-inner' ).text( 'Error loading diff.' );
			} );
			return;
		} else if ( next == data.query.recentchanges.length ) {
			$( '#rcpatrol-inner' ).text( 'You have viewed every diff.' );
			return; // we are done
		}
		// grab the diffs and a parsed version of the rev being looked at
		var pagedata = data.query.recentchanges[next];
		var parsed = that.api.get( {
			action: 'parse',
			oldid: pagedata.revid,
			prop: 'text|categorieshtml|wikitext'
		} );
		var revisions = that.api.get( {
			action: 'query',
			prop: 'revisions',
			revids: ( pagedata.old_revid !== 0 ? ( pagedata.old_revid + '|' ) : '' ) + pagedata.revid,
			rvprop: 'ids|timestamp|user|parsedcomment'
		} );
		next++;
		if ( pagedata.type == 'new' ) {
			$.when( pagedata, parsed, revisions ).then( that.showDiff );
		} else {
			$.when(	pagedata, parsed, revisions,
				that.api.get( {
					action: 'compare',
					fromrev: pagedata.old_revid,
					torev: pagedata.revid
				} )
			).then( that.showDiff );
		}
	};
	
	// setup
	$( tab ).click( function( e ) {
		e.preventDefault();
		mw.loader.load( 'mediawiki.action.history.diff', 'text/css' );
		$( tab ).addClass( 'selected' );
		$( '#firstHeading' ).text( 'Recent changes patrol' );
		$( '#mw-content-text' ).html(
			'<fieldset>\
				<legend>Patrol Options</legend>\
				<button id="rcpatrol-patrol">Mark as Patrolled</button> <button id="rcpatrol-skip">Skip Diff</button>\
				<!--<button id="rcpatrol-ignorepage">Ignore Page</button> <button id="rcpatrol-ignoreguide">Ignore Guide</button> <button id="rcpatrol-unignore">Unignore Everything</button>-->\
			</fieldset>\
			<div id="rcpatrol-inner"></div>\
			<br /><br /><div id="rcpatrol-poweredby"><small>RcPatrol version ' + version + ' by <a href="http://strategywiki.org/wiki/User:Skizzerz">Skizzerz</a></small></div>'
		);
		$( '#rcpatrol-inner' ).text( 'Loading Recent changes patrol interface...' );
		that.api.get( rcapi ).done( onApiComplete ).fail( function( error ) {
			$( '#rcpatrol-inner' ).text( 'Error loading interface.' );
		} );
		$( tab ).off();
		$( '#rcpatrol-patrol' ).click( that.markPatrolled );
		$( '#rcpatrol-skip' ).click( that.fetchNextDiff );
	} );
}

// public functions
RcPatrol.prototype.showDiff = function( rcdata, parsed, revdata, diff ) {
	// get our object
	var that = mw._rcpatrol;
	// reset the buttons
	$( '#rcpatrol-patrol' ).removeAttr( 'disabled' ).text( 'Mark as Patrolled' );
	$( '#rcpatrol-skip' ).removeAttr( 'disabled' ).text( 'Skip Diff' );
	$( '#rcpatrol-inner' ).empty();
	$( '#rcpatrol-inner' ).append( '<h2>Viewing <a href="' + mw.util.wikiGetlink( rcdata.title ) + '">' + rcdata.title + '</a> (<a href="' + mw.config.get( 'wgScript' ) + '?title=' + mw.util.wikiUrlencode( rcdata.title ) + '&action=history">history</a>)</h2>' );
	if ( rcdata.type == 'edit' ) {
		var oldrev = revdata.query.pages[rcdata.pageid].revisions[0];
		var newrev = revdata.query.pages[rcdata.pageid].revisions[1];
		$( '#rcpatrol-inner' ).append(
			'[<a id="rcpatrol-toggle-prev" href="#">Collapse Diff</a>]\
			<div id="rcpatrol-diff-prev">\
				<table class="diff diff-contentalign-left">\
					<col class="diff-marker" /><col class="diff-content" /><col class="diff-marker" /><col class="diff-content" />\
					<tr style="vertical-align: top"><td colspan="2" class="diff-otitle">\
						<div id="mw-diff-otitle1"><strong>Revision as of ' + oldrev.timestamp + '</strong></div>\
						<div id="mw-diff-otitle2"><a href="' + mw.util.wikiGetlink( 'Special:Contributions/' + oldrev.user ) + '">' + oldrev.user + '</a>\
							<span class="mw-usertoollinks">(<a href="' + mw.util.wikiGetlink( 'Special:Block/' + oldrev.user ) + '">block</a>)</span> \
						</div>\
						<div id="mw-diff-otitle3"><span class="comment">(' + oldrev.parsedcomment + ')</span></div></td><td colspan="2" class="diff-ntitle">\
						<div id="mw-diff-ntitle1"><strong>Revision as of ' + newrev.timestamp + '</strong></div>\
						<div id="mw-diff-ntitle2"><a href="' + mw.util.wikiGetlink( 'Special:Contributions/' + newrev.user ) + '">' + newrev.user + '</a>\
							<span class="mw-usertoollinks">(<a href="' + mw.util.wikiGetlink( 'Special:Block/' + newrev.user ) + '">block</a>)</span> \
						</div>\
						<div id="mw-diff-ntitle3"><span class="comment">(' + newrev.parsedcomment + ')</span></div></td>\
					</tr>' + diff.compare['*'] + '\
				</table>\
			</div>'
		);
		$( '#rcpatrol-toggle-prev' ).click( function() {
			$( this ).text( $( this ).text() == 'Collapse Diff' ? 'Expand Diff' : 'Collapse Diff' );
			$( '#rcpatrol-diff-prev' ).slideToggle();
		} );
	} else {
		var rev = revdata.query.pages[rcdata.pageid].revisions[0];
		$( '#rcpatrol-inner' ).append(
			'<div id="rcpatrol-newpage-info" class="diff-ntitle" style="text-align: center">\
				<div id="mw-diff-ntitle1"><strong>Revision as of ' + rev.timestamp + '</strong></div>\
				<div id="mw-diff-ntitle2"><a href="' + mw.util.wikiGetlink( 'Special:Contributions/' + rev.user ) + '">' + rev.user + '</a>\
					<span class="mw-usertoollinks">(<a href="' + mw.util.wikiGetlink( 'Special:Block/' + rev.user ) + '">block</a>)</span> \
				</div>\
				<div id="mw-diff-ntitle3"><span class="comment">(' + rev.parsedcomment + ')</span></div>\
			</div>'
		);
	}
	
	that.rcid = rcdata.rcid;
	that.token = rcdata.patroltoken;
	that.cur = {
		ns: rcdata.ns,
		title: rcdata.title,
		guide: rcdata.title.split( '/' )[0] //messes up on .hack, but meh. Eventually I'll fix it
	};
	$( '#rcpatrol-inner' ).append( '<h2>Revision as of ' + rcdata.timestamp + '</h2>' + parsed.parse.text['*'] );
};

RcPatrol.prototype.markPatrolled = function() {
	that = mw._rcpatrol;
	if ( !that.rcid || !that.token ) {
		return false;
	}
	that.api.post( {
		action: 'patrol',
		rcid: that.rcid,
		token: that.token
	} ).done( function() {
		that.fetchNextDiff();
		$( '#rcpatrol-patrol' ).text( 'Done' );
	} ).fail( function( error ) {
		$( '#rcpatrol-patrol' ).removeAttr( 'disabled' ).text( 'Mark as Patrolled' );
		console.log( error );
	} );
	$( '#rcpatrol-patrol' ).attr( 'disabled', 'disabled' ).text( 'Patrolling...' );
	that.rcid = false;
	that.token = false;
	return false;
};

$( function() {
	if ( mw.config.get( 'wgCanonicalSpecialPageName' ) == 'Recentchanges' || mw.config.get( 'wgCanonicalSpecialPageName' ) == 'Blankpage' ) {
		mw.loader.using( 'mediawiki.api', function () {
			mw._rcpatrol = new RcPatrol();
		} );
	}
} );