MediaWiki:Rollback/code.js

/*
 * Rollback
 * Perform rollbacks without needing to be in the usergroup
 * @author Ozuzanna


 * (function($, mw) {

var main = { init: function { if ($('.mw-rollback-link a').length || (mw.config.get('wgAction') != "history" && mw.config.get('wgCanonicalSpecialPageName') != "Contributions")) return; switch (mw.config.get('wgAction')) { case "history": if ($('#pagehistory li').length > 1) $('#pagehistory li:first .mw-history-undo a').before(' rollback | '); break; default: $('#mw-content-text ul').find('li').each(function {					$(this).append(' [rollback] ');				}); break; }		$('.mw-custom-rollback-link a').click(function {			main.getRevisionIdAndContent($(this).attr('data-id'));		}); },	getRevisionIdAndContent: function(title) { var API = new mw.Api; API.get({			action: 'query',			prop: 'revisions',			titles: title,			rvprop: 'user|ids',			rvlimit: 500		}) .done(function(d) {			if (!d.error) {				var revisions;				for (var i in d.query.pages) {					revisions = d.query.pages[i].revisions;				}				var currentUser = revisions[0].user, //current user rollbacking from				lastUser,				lastRevId;				for (var index in revisions) {					if (revisions[index].user != currentUser) {						lastUser = revisions[index].user; //get user to revert to						lastRevId = revisions[index].revid; //get revision to revert to						break;					}				}				if (lastUser) {					API.get({ action: 'query', prop: 'revisions', rvprop: 'content', revids: lastRevId })					.done(function(d) { if (!d.error) { var content = ""; //can be no content on page so initialise empty as failsafe for (var i in d.query.pages) { if (d.query.pages[i].revisions) content = d.query.pages[i].revisions[0]["*"]; }							main.performRollback(title,content,currentUser,lastUser); }						else new BannerNotification('Unable to rollback (failed to get page content): ' + d.error.code,'error').show; })					.fail(function { new BannerNotification('Unable to rollback: failed to get page content!','error').show; });				}				else					new BannerNotification('Unable to rollback: no different editor found!','error').show;			}			else				new BannerNotification('Unable to rollback (failed to get revisions): ' + d.error.code,'error').show;		}) .fail(function {			new BannerNotification('Unable to rollback: failed to get revisions!','error').show;		}); },	performRollback: function(page,text,user,user2) { var API = new mw.Api; API.post({		action: 'edit',		title: page,		text: text,		summary: 'Reverted edits by ' + user + ' (talk | block) to last version by ' + user2 + '',		token: mw.user.tokens.values.editToken		}) .done(function(d) {			if (!d.error) {				new BannerNotification('Rollback successful!','confirm').show;							}			else				new BannerNotification('Unable to rollback (failed to publish edit): ' + d.error.code,'error').show;		}) .fail(function {			new BannerNotification('Unable to rollback: failed to publish edit!','error').show;		}); } };

main.init; }) (this.jQuery, this.mediaWiki);