Jump to content

MediaWiki:RecentRelated.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
var recentRelated = {
	init : function () {
		if ( mw.config.get('wgAction') != 'view' ) return ;
		var self = this ;
		var portletLink = mw.util.addPortletLink( 'p-navigation', '#', 'recentRelated','t-trcent-related');
		$(portletLink).click ( function () {
			self.run() ;
			return false ;
		} ) ;
	} ,
	
	run : function () {
		$('#firstHeading').text ( 'Recent changes related to edits by ' + mw.user.getName() ) ;
		$('#bodyContent')
			.html ( '<i>Checking edits...</i>' )
			.css ( 'background-color' , '#EEEEEE' ) ;
		var self = this ;
		var url = mw.util.wikiScript('api');
		$.getJSON ( url , {
			action : 'query' ,
			list : 'usercontribs' ,
			ucuser : mw.user.getName() ,
			uclimit : 100 ,
			ucshow : '!minor' ,
			format : 'json'
		} , function ( d ) {
		
			var cont = {} ;
			self.left = 0 ;
			$.each ( d.query.usercontribs , function ( k , v ) {
				if ( undefined === cont[v.title] ) {
					cont[v.title] = v ;
					self.left++ ;
				} else {
					if ( cont[v.title].timestamp < v.timestamp ) cont[v.title] = v ;
				}
			} ) ;
		
			self.list = {} ;
			$.each ( cont , function ( k , v ) {
				self.processMyEdits ( v ) ;
			} ) ;
		} ) ;
	} ,
	
	processMyEdits : function ( o ) {
		var self = this ;
		var user = mw.user.getName() ;
		var url = mw.util.wikiScript('api') ;
		$.getJSON ( url , {
			action : 'query' ,
			prop : 'revisions' ,
			rvendid : o.revid ,
			titles : o.title ,
			rvprop : 'ids|timestamp|flags|comment|user' ,
			rvlimit : 50 ,
			format : 'json'
		} , function ( d ) {
			if ( undefined !== d.query.pages ) {
				$.each ( d.query.pages , function ( k1 , v1 ) {
					if ( undefined === self.list[v1.title] ) self.list[v1.title] = [] ;
					$.each ( v1.revisions , function ( k2, v2 ) {
						self.list[v1.title].push ( v2 ) ;
						if ( v2.user == user ) return false ;
					} ) ;
				} ) ;
			self.left-- ;
			if ( self.left == 0 ) self.showResults() ;
			}
		} ) ;
	} ,
	
	showResults : function () {
		var self = this ;
		var user = mw.user.getName() ;
		var parts = {} ;
		var keys = [] ;
		$.each ( self.list , function ( page , pd ) {
			if ( pd.length == 1 && pd[0].user == user ) return ; // Only the user's own edits, skip
			pd.sort(function(a,b){return b.timestamp.localeCompare(a.timestamp);});
			
			var ts ;
			var h = '' ;
			h += "<h2><a href='./" + encodeURIComponent(page) + "'>" + page + "</a>" ;
			h += " [<a href='/w/index.php?title=" + encodeURIComponent(page) + "&diff=" + pd[0].revid + "&oldid=" + pd[pd.length-1].revid ;
			h += "' title='Diff to previous version'>&Delta;</a>]" ;
			h += "</h2><table border=1 cellspacing=0 cellpadding=3>" ;
			$.each ( pd , function ( k2 , v2 ) {
				if ( undefined === ts ) ts = v2.timestamp ;
				h += "<tr><td>" ;
				h += $.trim(v2.timestamp.replace(/[A-Z]/g,' ')) ;
				h += "</td><td>" ;
				if ( k2+1 < pd.length ) {
					h += "<a href='/w/index.php?title=" + encodeURIComponent(page) + "&diff=" + v2.revid + "&oldid=" + v2.parentid ;
					h += "' title='Diff to previous version'>&Delta;</a>" ;
				}
				h += "</td><td>" ;
				h += " <a href='./User:" + encodeURIComponent(v2.user) + "'>" + v2.user + "</a>" ;
				h += "</td><td>" ;
				h += v2.comment ;
				h += "</td></tr>" ;
			} ) ;
			h += "</table>" ;
			var k = ts ;
			while ( undefined !== parts[k] ) k += 'b' ;
			parts[k] = h ;
			keys.push ( k ) ;
		} ) ;
		
		keys.sort ( function ( a , b ) { return b.localeCompare(a); } ) ;
		
		var h = '' ;
		$.each ( keys , function ( dummy , key ) {
			h += parts[key] ;
		} ) ;
		
		$('#bodyContent').html ( h ) ;
	}
	
} ;

$(recentRelated.init);