MediaWiki:RecentChanges.js

(function {	function mainPageRecentChanges(data) {		var maxRecent = 12;		if (data.query.recentchanges.length > 0) {			var prev = {};			for (var i=0; i < data.query.recentchanges.length;i++) {				if (typeof data.query.recentchanges[i].minor === "undefined") {					prev = data.query.recentchanges[i];					break;				} else {					data.query.recentchanges.splice(i,1);					i--;				}			}			i++;			var userExists = function (arr, user) {				return arr.some(function (obj) { return obj.name === user; });			};			for (i < data.query.recentchanges.length;i++) {				prev.users = (prev.users || [{name: (mw.util.isIPAddress(prev.user) ? "IP editor" : prev.user), count:1}]);				if (typeof data.query.recentchanges[i].minor === "undefined") {					if (prev.ns === data.query.recentchanges[i].ns && prev.title === data.query.recentchanges[i].title) {						prev.oldlen = data.query.recentchanges[i].oldlen;						prev.old_revid = data.query.recentchanges[i].old_revid; var tempUser = (mw.util.isIPAddress(data.query.recentchanges[i].user) ? "IP editor" : data.query.recentchanges[i].user); if (!userExists(prev.users, tempUser)) { prev.users.push({name: tempUser, count:1}); } else { for (var y=0; y < prev.users.length;y++) { if (prev.users[y].name === tempUser) { prev.users[y].count++; break; } }						}						data.query.recentchanges.splice(i,1); i--; data.query.recentchanges[i] = prev; }				} else { data.query.recentchanges.splice(i,1); i--; data.query.recentchanges[i] = prev; }				prev = data.query.recentchanges[i]; }			if (typeof prev.users === "undefined") { prev.users = (prev.users || [{name: (mw.util.isIPAddress(prev.user) ? "IP editor" : prev.user), count:1}]); }			if (data.query.recentchanges.length > maxRecent) { data.query.recentchanges.length = maxRecent; } var changeDate1 = new Date(data.query.recentchanges[0].timestamp); var monthNames = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ]; var getDate = function(date) { return date.getUTCFullYear+'-'+date.getUTCMonth+'-'+date.getUTCDate; };	       var getDateElem = function(date, space) { return mw.html.element( 'tr', {}, new mw.html.Raw ( mw.html.element( 'td', {colspan:"2"}, new mw.html.Raw ( (space ? mw.html.element( 'div', {class: "mp-recent-changes-spacer"}, "" ) : "") + mw.html.element( 'div', {class: "mp-recent-changes-date"}, 					   	new mw.html.Raw(date.getUTCDate + ' ' + monthNames[date.getUTCMonth] + ' ' + date.getUTCFullYear )) ))				));	       };	        var addDateZeroRC = function(date) { if ((date+'').length === 1) { return '0' + date; } else { return date; } };			var output = ""; var outputTemp1 = ""; var outputTemp2 = getDateElem(changeDate1,false); var outputTemp3 = ""; for (var i=0; i < data.query.recentchanges.length;i++) { var change = data.query.recentchanges[i]; var changeDate2 = new Date(change.timestamp); if (getDate(changeDate1) !== getDate(changeDate2)) { changeDate1 = changeDate2; outputTemp2 += getDateElem(changeDate2, true); }				outputTemp1 += outputTemp2; outputTemp2 = ""; outputTemp1 += mw.html.element( 'tr', {}, new mw.html.Raw ( mw.html.element( 'td', {class: "mp-recent-changes-title-row"}, new mw.html.Raw ( (change.type === "new" ? mw.html.element( 'span', {class: "newpage"}, new mw.html.Raw("N")) : "") + mw.html.element( 'span', {class: "mp-recent-changes-time"}, new mw.html.Raw ( addDateZeroRC(changeDate2.getUTCHours) + ':' + addDateZeroRC(changeDate2.getUTCMinutes))					))) + mw.html.element( 'td', {}, new mw.html.Raw ( mw.html.element( 'span', {class: "mw-title"}, new mw.html.Raw ( mw.html.element( 'a', {href: "/wiki/" + mw.util.escapeIdForLink(change.title), title: change.title}, change.title ) ))					))				));				var byteChange = change.newlen - change.oldlen; var byteStrong = (Math.abs(byteChange) > 999 ? true : false); outputTemp3 = mw.html.element( 'span', {}, new mw.html.Raw('[')); for (var y=0; y < change.users.length;y++) { var user = change.users[y]; if (user.name === "IP editor") { outputTemp3 += mw.html.element( 'span', {}, new mw.html.Raw("IP editor" + (user.count > 1 ? (" (" + user.count + "x)") : ""))); } else { outputTemp3 += mw.html.element( 'a', {class: "mw-userlink", 								href: "/wiki/User:" + mw.util.escapeIdForLink(user.name)							}, user.name						); outputTemp3 += (user.count > 1 ? mw.html.element( 'span', {}, new mw.html.Raw(" (" + user.count + "x)")) : ""); }					if (y+1 < change.users.length) { outputTemp3 += mw.html.element( 'span', {}, new mw.html.Raw('; ')); }				}				outputTemp3 += mw.html.element( 'span', {}, new mw.html.Raw(']')); var countChanges = change.users.reduce(function (prev, cur) { return prev + cur.count; }, 0); var reducedComment = change.comment.substring(0,46); if (reducedComment.length === 46) { reducedComment += '...'; } outputTemp1 += mw.html.element( 'tr', {}, new mw.html.Raw ( mw.html.element( 'td', {}, "") + mw.html.element( 'td', {}, new mw.html.Raw ( mw.html.element( 'span', {}, new mw.html.Raw ( mw.html.element( 'a', {class: "mw-changeslist-diff", href: "/w/index.php?title=" + mw.util.escapeIdForLink(change.title) +					   		"&curid=" + change.pageid + "&diff=" + change.revid + 					    		"&oldid=" + change.old_revid					    	}, new mw.html.Raw(countChanges === 1 ? "Diff" : countChanges + " changes")) )) +					   mw.html.element( 'span', {dir: "ltr", title: change.newlen + " bytes after change",					    		class: "mw-diff-bytes " + (byteChange === 0 ? "mw-plusminus-null" : (byteChange > 0) ? "mw-plusminus-pos" : "mw-plusminus-neg")					   	}, new mw.html.Raw ((byteChange === 0 ? "(" : (byteChange > 0) ? "(+" : "(") + 					   		(byteStrong ? mw.html.element('strong',{},new mw.html.Raw (byteChange)) : byteChange) + ")" )) + 					   mw.html.element( 'span', {class: "mp-recent-changes-users"}, new mw.html.Raw ( outputTemp3 )) +					   mw.html.element( 'span', {class: 'comment'}, (change.comment.indexOf('Created page with') === 0 ? 'Created page' : reducedComment)) ))				));			}			output = mw.html.element( 'table', {class: "mp-recent-changes-table"}, new mw.html.Raw ( mw.html.element( 'tbody', {}, new mw.html.Raw ( outputTemp1 ))			));			$("#mp-recent-changes").html(output); }	}	function addDateZero(date) { if (date === 0) { date = 1; } if ((date+'').length === 1) { return '0' + date; } else { return date; } }	function getFloorMinutes (minutes, date) { var ms = 1000 * 60 * minutes; // convert minutes to ms	 var roundedDate = new Date(Math.floor(date.getTime / ms) * ms); return roundedDate; }	var oldDate = new Date(Date.now - 7 * 24 * 60 * 60 * 1000); var dateFormatted = oldDate.getFullYear+'-'+addDateZero(oldDate.getMonth)+'-'+addDateZero(oldDate.getDate)+'T'+ addDateZero(oldDate.getHours)+':'+addDateZero(getFloorMinutes(5, oldDate).getMinutes)+':'+'01'+'Z'; new mw.Api.get( {	   action: 'query',	    list: 'recentchanges',	    rctype: [ 'edit', 'new' ],	    rclimit: 30,	    rcend: dateFormatted,	    rcshow: '!bot',	    rcprop: [ 'ids', 'user', 'comment', 'timestamp', 'title', 'sizes', 'redirect', 'flags' ],	    maxage: 300,	    smaxage: 300,	    uselang: "content",	    rcnamespace: [ -2,0,1,6,7,10,11,14,15,828,829,3000,3001 ]	} ).done( function ( data ) {		if ( document.readyState === 'interactive' || document.readyState === 'complete' ) {			mainPageRecentChanges(data);		} else {			document.addEventListener( 'DOMContentLoaded', function  { mainPageRecentChanges(data); } );		}	}); });