User:Skizzerz/Scripts/RcPatrol.js

// 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( ' \				 Patrol Options \ Mark as Patrolled Skip Diff \ \			 \			 \

RcPatrol version ' + version + ' by Skizzerz ' );		$( '#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( ' Viewing ' + rcdata.title + ' (history) ' ); 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(			'[Collapse Diff]\			 \				 \			 '		); $( '#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(			' \				 Revision as of ' + rev.timestamp + ' \				 ' + rev.user + '\					 (block) \				 \				 (' + rev.parsedcomment + ')  \			 '		); }	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( ' Revision as of ' + rcdata.timestamp + ' ' + 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; } );	} } );