Fix undo of a chosen revision, add a warning level choice, plain error messages
No edit summary
Line 37: Line 37:
} ).then( function ( r ) {
} ).then( function ( r ) {
var p = r.query.pages[ 0 ];
var p = r.query.pages[ 0 ];
if ( !p || !p.revisions || !p.revisions[ 0 ] ) {
if ( !p || !p.revisions || !p.revisions[ 0 ] ) { throw new Error( 'Revision not found.' ); }
throw new Error( 'Revision not found.' );
}
var rev = p.revisions[ 0 ];
var rev = p.revisions[ 0 ];
rev.title = p.title;
rev.title = p.title;
Line 53: Line 51:
help: 'Page: ' + title + '. Revision ' + revid + ' (' + rev.user + ', ' + when + ') will be made the current revision. The current content will remain safe in the history.',
help: 'Page: ' + title + '. Revision ' + revid + ' (' + rev.user + ', ' + when + ') will be made the current revision. The current content will remain safe in the history.',
fields: [
fields: [
{ name: 'reason', label: 'Reason (optional)', type: 'text' },
{ name: 'reason', label: 'Reason (optional)', type: 'text' }
],
],
onPreview: function () {
preview: function () {
return '\'\'\'Revision to be restored:\'\'\' ' + revid + ' - ' + rev.user + ' (' + when + ')';
return '\'\'\'Revision to be restored:\'\'\' ' + revid + ' - ' + rev.user + ' (' + when + ')';
},
},
onSubmit: function ( values ) {
previewTitle: function () { return title; },
return BM.core.api().postWithToken( 'csrf', {
submitLabel: 'Restore',
action: 'edit', title: title, text: rev.slots.main.content, baserevid: revid,
success: 'The old revision has been restored.',
summary: ( values.reason || 'Restored revision ' + revid + ' (using [[Bharatpedia:Bharat Mitra|Bharat Mitra]])' ), formatversion: 2
onSubmit: function ( v ) {
} ).then( function ( r ) {
return BM.core.getPage( title ).then( function ( cur ) {
if ( !( r.edit && r.edit.result === 'Success' ) ) {
var summary = 'Restored an old revision (revision ' + revid + ') (using [[Bharatpedia:Bharat Mitra|Bharat Mitra]])';
throw new Error( 'Restore failed.' );
if ( v.reason ) { summary += ': ' + BM.core.cleanParam( v.reason ); }
}
return BM.core.savePage( title, rev.slots.main.content, summary, cur.revid );
mw.notify( 'Revision restored.' );
} ).then( function () {
setTimeout( function () { location.reload(); }, 600 );
// Go to the plain current page afterwards.
// A plain reload would reopen this old revision view, so the restore bar would be
// drawn again even though the restore succeeded.
setTimeout( function () { location.href = mw.util.getUrl( title ); }, 600 );
} );
} );
}
}
} );
} );
} ).catch( function () {
mw.notify( 'Revision details could not be retrieved.', { type: 'error' } );
} );
} );
}
}
BM.register( 'revert', {
BM.register( 'revert', {
/* On a diff page, add two links of our own at the top of the page
  content as soon as this module loads, without waiting for any
  other gadget: a red "revert and warn", which opens the Revert /
  Undo dialog with the automatic warning ticked, and a plain
  "revert only", which opens the same dialog with the warning
  unticked. */
addDiffRevertLink: function () {
addDiffRevertLink: function () {
var $links = $( '.mw-diff-revision .mw-diff-tool, .mw-diff-v1 .mw-diff-tool, .mw-diff-v2 .mw-diff-tool' );
if ( document.querySelector( '.bm-revert-warn-link' ) ) { return; }
if ( !$links.length ) {
var a = document.createElement( 'a' );
return;
a.href = '#';
}
a.className = 'bm-revert-warn-link';
var $link = $links.first();
a.setAttribute( 'style', 'color:#ba0000 !important' );
var href = $link.find( 'a' ).first().attr( 'href' ) || '';
a.textContent = 'revert and warn';
var m = href.match( /[?&]oldid=(\d+)/ );
a.addEventListener( 'click', function ( e ) {
if ( !m ) {
return;
}
$( '<span>' ).text( ' - ' ).appendTo( $link );
$( '<a>' ).attr( 'href', '#revert' ).text( 'Revert / Undo' ).appendTo( $link ).on( 'click', function ( e ) {
e.preventDefault();
e.preventDefault();
BM.modules.revert.open( { revid: Number( m[ 1 ] ) } );
BM.modules.revert.open();
} );
} );
},
var b = document.createElement( 'a' );
addRestoreLink: function () {
b.href = '#';
if ( !mw.config.get( 'wgRevisionId' ) ) {
b.className = 'bm-revert-only-link';
return;
b.textContent = 'revert only';
}
b.addEventListener( 'click', function ( e ) {
var revid = Number( mw.config.get( 'wgRevisionId' ) );
if ( document.querySelector( '#bm-restore' ) ) {
return;
}
var $p = $( '<p>' ).attr( 'id', 'bm-restore' );
$( '<span>' ).text( 'Restore an old revision: ' ).appendTo( $p );
$( '<a>' ).attr( 'href', '#restore' ).text( 'Restore this revision' ).appendTo( $p ).on( 'click', function ( e ) {
e.preventDefault();
e.preventDefault();
restoreDialog( revid );
BM.modules.revert.open( { warn: false } );
} );
} );
var $tool = $( '#mw-content-text .mw-parser-output' );
function place() {
if ( $tool.length ) {
var host = document.querySelector( '#mw-content-text' );
$tool.before( $p );
if ( !host ) { return false; }
} else {
var span = document.createElement( 'span' );
$( '#mw-content-text' ).prepend( $p );
span.appendChild( document.createTextNode( '[' ) );
span.appendChild( a );
span.appendChild( document.createTextNode( '] || [' ) );
span.appendChild( b );
span.appendChild( document.createTextNode( '] ' ) );
host.insertBefore( span, host.firstChild );
return true;
}
}
if ( place() ) { return; }
var tries = 0;
var timer = setInterval( function () {
tries++;
if ( place() || tries > 20 ) {
clearInterval( timer );
}
}, 100 );
},
addRestoreLink: function ( revid ) {
if ( $( '.bm-restore-bar' ).length ) { return; }
var bar = $( '<div>' ).addClass( 'bm-restore-bar' );
bar.append( $( '<span>' ).text( 'You are viewing an old revision of this page. ' ) );
var btn = $( '<button>' ).attr( 'type', 'button' ).addClass( 'bm-btn bm-primary' ).text( '⟲ Restore this revision' );
btn.on( 'click', function () { restoreDialog( revid ); } );
bar.append( btn );
var host = $( '#mw-content-text' );
if ( host.length ) { host.prepend( bar ); }
else { $( document.body ).append( bar ); }
},
},
open: function ( opts ) {
open: function ( opts ) {
opts = opts || {};
var warnDefault = !( opts && opts.warn === false );
var title = pageTitle();
var title = pageTitle();
getRevisions( title, 20 ).then( function ( revs ) {
getRevisions( title, 11 ).then( function ( revs ) {
if ( !revs || !revs.length ) {
if ( !revs.length ) { mw.notify( 'No revisions were found for this page.', { type: 'error' } ); return; }
throw new Error( 'No revisions were found for this page.' );
}
var last = revs[ 0 ];
var last = revs[ 0 ];
var p = revs.length > 1 && Number( revs[ 1 ].revid ) || Number( last.revid );
var warnPlans = null;
var undoAfter = revs.length > 1 ? Number( revs[ 1 ].revid ) : 0;
var warnApi = null;
var undoAfterId = revs.length > 1 ? Number( revs[ 1 ].parentid ) : 0;
var warnPlans, warnTypes = [];
var labels = BM.modules.warn ? BM.modules.warn.labels : {};
var planKeys = [];
var plansReady = false;
var revWindow = Math.min( revs.length, 10 );
var monthsAgo = 0;
var monthsAgoCutoff = new Date();
monthsAgoCutoff.setMonth( monthsAgoCutoff.getMonth() - 1 );
var cortege = false;
var revidToShow = last.revid;
BM.loadModule( 'warn' ).then( function ( warnApi ) {
if ( BM.modules.warn && BM.modules.warn.TYPES ) {
warnTypes = BM.modules.warn.TYPES.map( function ( t ) { return [ t.key, t.label ]; } );
}
warnApi.planAll( last.user, title ).then( function ( plans ) {
warnPlans = plans;
}, function () {} );
} ).catch( function () {
return BM.loadModule( 'warn' ).then( function ( warnApi ) {
if ( BM.modules.warn && BM.modules.warn.TYPES ) {
warnTypes = BM.modules.warn.TYPES.map( function ( t ) { return [ t.key, t.label ]; } );
}
warnApi.planAll( last.user, title ).then( function ( plans ) {
warnPlans = plans;
}, function () {} );
} );
} );
var $form = $( '<div>' );
BM.core.dialog( {
BM.core.dialog( {
title: 'Revert revision',
title: 'Revert / Undo',
submit: 'Revert',
help: 'Page: ' + title + '. Most recent editor: ' + last.user + '. Rollback reverts all consecutive edits by the most recent editor at once, and the revision list does not apply to it. Undo takes out the one revision you choose. With the warning box ticked, a warning is sent to that editor automatically after the revert, at the level set below. Untick the box for a good faith revert.',
submitClass: 'oo-ui-flaggedElement-destructive',
help: 'Revert the edits made by ' + last.user + ' on ' + title + '. Undo works on one specific revision; rollback reverts all the recent edits by that editor.',
fields: [
fields: [
{ name: 'mode', label: 'Method', type: 'select', required: true, options: [
{ name: 'mode', label: 'Method', type: 'select', required: true, options: [
Line 161: Line 151:
[ 'undo', 'Undo: revert one chosen revision' ]
[ 'undo', 'Undo: revert one chosen revision' ]
], value: 'rollback' },
], value: 'rollback' },
{ name: 'revid', label: 'Revision', type: 'select', required: true, options: revs.slice( 0, 10 ).map( function ( r ) {
{ name: 'revid', label: 'Revision to undo (Undo only)', type: 'select', required: true,
var c = ( r.comment || '' ).replace( /\s+/g, ' ' );
options: revs.slice( 0, 10 ).map( function ( r ) {
if ( c.length > 40 ) {
var c = ( r.comment || '' ).replace( /\s+/g, ' ' );
c = c.slice( 0, 40 ) + '…';
if ( c.length > 40 ) { c = c.slice( 0, 40 ) + '…'; }
}
return [ String( r.revid ), r.revid + ' - ' + r.user + ( c ? ' - ' + c : '' ) ];
return [ String( r.revid ), r.revid + ' - ' + r.user + ( c ? ' - ' + c : '' ) ];
} ), value: String( last.revid ) },
} ), value: String( last.revid ) },
{ name: 'warnUser', label: 'Warn the user automatically after reverting', type: 'checkbox', value: warnDefault },
{ name: 'reason', label: 'Reason (optional)', type: 'text' },
{ name: 'warnType', label: 'Automatic warning type', type: 'select', required: true, options: [
{ name: 'warnUser', label: 'Warn the user', type: 'checkbox' },
[ 'vandalism', 'Vandalism' ],
{ name: 'warnType', label: 'Warning type', type: 'select', options: warnTypes, value: 'vandalism' },
[ 'test', 'Test edits' ],
{ name: 'warnLevel', label: 'Warning level', type: 'select', options: [
[ 'blanking', 'Blanking' ],
[ 'advertising', 'Advertising' ],
[ 'unsourced', 'Unsourced' ],
[ 'npov', 'Point of view' ]
], value: 'vandalism' },
{ name: 'warnLevel', label: 'Warning level', type: 'select', required: true, options: [
[ 'auto', 'Automatic (based on previous warnings)' ],
[ 'auto', 'Automatic (based on previous warnings)' ],
[ '1', 'Level 1' ], [ '2', 'Level 2' ], [ '3', 'Level 3' ], [ '4', 'Level 4' ], [ '4im', 'Level 4im' ]
[ '1', 'Level 1' ],
], value: 'auto' },
[ '2', 'Level 2' ],
{ name: 'tpvpreview', label: 'Talk page preview', type: 'html', value: '' }
[ '3', 'Level 3' ],
[ '4', 'Level 4' ],
[ '4im', 'Level 4im' ]
], value: 'auto' }
],
],
preview: function ( v ) {
preview: function ( v ) {
Line 181: Line 179:
if ( v.mode === 'rollback' ) {
if ( v.mode === 'rollback' ) {
out = '\'\'\'Rollback:\'\'\' the consecutive edits of ' + last.user + ' will be reverted.';
out = '\'\'\'Rollback:\'\'\' the consecutive edits of ' + last.user + ' will be reverted.';
if ( last.user === mw.config.get( 'wgUserName' ) ) {
out += ' The most recent editor is you, so this rollback cannot be submitted.';
}
} else {
} else {
var r = revs.find( function ( x ) { return String( x.revid ) === String( v.revid ); } );
out = '\'\'\'Undo:\'\'\' revision ' + v.revid + ' will be reverted.';
out = '\'\'\'Undo:\'\'\' revision ' + v.revid + ' by ' + ( r ? r.user : last.user ) + ' will be undone.';
}
}
if ( v.warnUser ) {
if ( v.warnUser ) {
var lvl = v.warnLevel;
var labels = { vandalism: 'Vandalism', test: 'Test edits', blanking: 'Blanking', advertising: 'Advertising', unsourced: 'Unsourced', npov: 'Point of view' };
var suffix = '', previewLevel = lvl;
var lvl = '';
if ( !warnPlans || warnPlans.length === 0 ) {
if ( v.warnLevel && v.warnLevel !== 'auto' ) {
lvl = 'auto';
lvl = ', level ' + v.warnLevel;
} else if ( lvl === 'auto' ) {
} else if ( warnPlans && warnPlans[ v.warnType ] ) {
previewLevel = BM.modules.warn ? BM.modules.warn.suggestFor( warnPlans ) : '';
lvl = warnPlans[ v.warnType ].level ? ', level ' + warnPlans[ v.warnType ].level + ' (automatic)' : ' (standard notice)';
} else if ( lvl !== 'single' ) {
previewLevel = BM.modules.warn ? BM.modules.warn.clampLevel( warnPlans[ 0 ].type, lvl ) : '';
}
}
out += '\n\n\'\'\'Automatic warning:\'\'\' a ' + ( labels[ v.warnType ] || 'Vandalism' ) + ' warning' + lvl + ' will be sent to [[User talk:' + last.user + ']] for this page.' + ( lvl ? '' : ' The level is chosen automatically from the warnings already on that talk page.' );
out += '\n\n\'\'\'Automatic warning:\'\'\' a ' + ( labels[ v.warnType ] || 'Vandalism' ) +
' warning' + lvl + ' will be sent to [[User talk:' + last.user + ']] for this page.' +
( lvl ? '' : ' The level is chosen automatically from the warnings already on that talk page.' );
} else {
out += '\n\nNo warning will be sent.';
}
}
return { tpvpreview: out };
return out;
},
},
previewTitle: function () { return title; },
submitLabel: 'Revert',
success: 'The edit has been reverted.',
onSubmit: function ( v ) {
onSubmit: function ( v ) {
var done;
var done;
if ( v.mode === 'rollback' && last.user === mw.config.get( 'wgUserName' ) ) {
throw new Error( 'The most recent editor of this page is you, so there is nothing to roll back. A rollback now would undo your own last edit.' );
}
if ( v.mode === 'rollback' ) {
if ( v.mode === 'rollback' ) {
return BM.core.api().postWithToken( 'rollback', {
done = BM.core.api().postWithToken( 'rollback', {
action: 'rollback', title: title, user: last.user, format: 'json'
action: 'rollback', title: title, user: last.user, format: 'json'
} ).then( function ( r ) {
} ).then( function ( r ) {
if ( !( r.rollback && r.rollback.title ) ) {
if ( r && r.rollback ) { return r; }
throw new Error( 'Rollback failed.' );
throw new Error( 'Rollback failed.' );
}
mw.notify( 'Rollback done.' );
return true;
} );
} );
}
} else {
done = BM.core.api().postWithToken( 'csrf', {
var idx = -1, i;
action: 'edit', title: title, undo: v.revid, undoafter: undoAfter,
for ( i = 0; i < revs.length; i++ ) {
summary: 'Undid one revision (revision ' + v.revid + ') (using [[Bharatpedia:Bharat Mitra|Bharat Mitra]])', format: 'json'
if ( String( revs[ i ].revid ) === String( v.revid ) ) { idx = i; break; }
} ).then( function ( r ) {
}
if ( !( r.edit && r.edit.result === 'Success' ) ) {
var undoAfter = ( idx >= 0 && revs[ idx + 1 ] ) ? revs[ idx + 1 ].revid : 0;
done = BM.core.api().postWithToken( 'edit', {
action: 'edit', title: title, undo: v.revid, undoafter: undoAfter,
summary: 'Undid one revision (revision ' + v.revid + ') (using [[Bharatpedia:Bharat Mitra|Bharat Mitra]])', format: 'json'
} ).then( function ( r ) {
if ( r && r.edit && r.edit.result === 'Success' ) { return r; }
throw new Error( 'Undo failed.' );
throw new Error( 'Undo failed.' );
}
mw.notify( 'Undo done.' );
return true;
} );
done = done.then( null, function ( err ) {
throw friendlyError( err );
} );
if ( v.warnUser ) {
done = done.then( function () {
return BM.loadModule( 'warn' ).then( function () {
return BM.modules.warn.sendAuto( last.user, v.warnType, title, v.warnLevel );
} );
} );
} );
}
}
done = done.then( null, function ( err ) { throw friendlyError( err ); } );
return done.then( function () {
return done.then( function () {
setTimeout( function () { location.reload(); }, 600 );
var warned = $.Deferred().resolve( null ).promise();
if ( v.warnUser && BM.loadModule ) {
warned = BM.loadModule( 'warn' ).then( function () {
if ( BM.modules.warn && BM.modules.warn.sendAuto ) {
return BM.modules.warn.sendAuto( last.user, v.warnType, title, v.warnLevel );
}
return null;
} ).then( function ( plan ) { return plan; }, function () {
mw.notify( 'The revert was done, but the automatic warning could not be sent. Please warn the user manually.', { type: 'error' } );
return null;
} );
}
return warned.then( function () {
setTimeout( function () { location.reload(); }, 600 );
} );
} );
} );
}
}
Line 241: Line 254:
var $wtype = $( 'select[data-bm-field="warnType"]' );
var $wtype = $( 'select[data-bm-field="warnType"]' );
var $wlevel = $( 'select[data-bm-field="warnLevel"]' );
var $wlevel = $( 'select[data-bm-field="warnLevel"]' );
var $wuser = $( 'input[data-bm-field="warnUser"]' );
function syncMode() { $rev.prop( 'disabled', $mode.val() !== 'undo' ); }
function syncMode() {
if ( $mode.length ) { $mode.on( 'change', syncMode ); syncMode(); }
if ( $wlevel.val() === 'auto' ) {
$wlevel.val( 'auto' );
}
$rev.prop( 'disabled', $mode.val() !== 'undo' );
}
function syncLevels() {
function syncLevels() {
if ( !$wlevel.length ) {
if ( !warnApi || !warnApi.levelsFor || !$wlevel.length ) { return; }
var levels = warnApi.levelsFor( $wtype.val() );
var cur = $wlevel.val();
$wlevel.empty();
if ( !levels ) {
$wlevel.append( $( '<option>' ).val( 'auto' ).text( 'Standard notice (no levels)' ) );
return;
return;
}
}
var cur = $wlevel.val();
$wlevel.append( $( '<option>' ).val( 'auto' ).text( 'Automatic (based on previous warnings)' ) );
var levels = [];
levels.forEach( function ( lv ) {
return BM.loadModule( 'warn' ).then( function ( warnApi ) {
$wlevel.append( $( '<option>' ).val( lv ).text( lv === '4im' ? 'Level 4im' : 'Level ' + lv ) );
levels = warnApi.levelsFor( $wtype.val() );
$wlevel.empty();
if ( !levels ) {
$wlevel.append( $( '<option>' ).val( 'auto' ).text( 'Standard notice (no levels)' ) );
} else {
$wlevel.append( $( '<option>' ).val( 'auto' ).text( 'Automatic (based on previous warnings)' ) );
levels.forEach( function ( lv ) {
$wlevel.append( $( '<option>' ).val( lv ).text( lv === '4im' ? 'Level 4im' : 'Level ' + lv ) );
} );
}
if ( cur && ( cur === 'auto' || levels.indexOf( cur ) !== -1 ) ) {
$wlevel.val( cur );
}
} );
} );
if ( cur && ( cur === 'auto' || levels.indexOf( cur ) !== -1 ) ) { $wlevel.val( cur ); }
}
}
$mode.on( 'change', syncMode );
if ( $wtype.length ) { $wtype.on( 'change', syncLevels ); }
$wtype.on( 'change', syncLevels );
if ( BM.loadModule ) {
$wlevel.on( 'change', syncMode );
BM.loadModule( 'warn' ).then( function () {
if ( !warnPlans ) {
warnApi = ( BM.modules && BM.modules.warn ) || null;
BM.loadModule( 'warn' ).then( function ( warnApi ) {
if ( warnApi && warnApi.planAll ) {
warnApi.planAll( last.user, title ).then( function ( plans ) {
warnApi.planAll( last.user, title ).then( function ( plans ) { warnPlans = plans; }, function () {} );
warnPlans = plans;
}
}, function () {} );
syncLevels();
syncLevels();
} ).catch( function () {} );
}, function () {} );
}
}
} ).catch( function () {
} ).catch( function () {

Revision as of 23:52, 11 November 2026

/* <nowiki> */
/* Bharat Mitra: revert module (rollback, undo, restore an old revision) */
( function () {
	'use strict';
	var BM = window.BharatMitra;
	function pageTitle() { return mw.config.get( 'wgPageName' ).replace( /_/g, ' ' ); }
	function getRevisions( title, limit ) {
		return BM.core.api().get( {
			action: 'query', prop: 'revisions', titles: title,
			rvprop: 'ids|user|timestamp|comment', rvlimit: limit || 10, formatversion: 2
		} ).then( function ( r ) {
			var p = r.query.pages[ 0 ];
			return ( p && p.revisions ) || [];
		} );
	}
	/* Turn a raw API error code into a plain message an editor can act on. */
	function friendlyError( err ) {
		var code = err ? String( err.code || err.message || err ) : '';
		if ( code.indexOf( 'onlyauthor' ) !== -1 ) {
			return new Error( 'This cannot be rolled back: every revision of this page is by a single editor, so there is no earlier editor whose edits could be rolled back.' );
		}
		if ( code.indexOf( 'alreadyrolled' ) !== -1 ) {
			return new Error( 'Those edits have already been rolled back.' );
		}
		if ( code.indexOf( 'undofailure' ) !== -1 ) {
			return new Error( 'That revision could not be undone. Later edits have changed the same content, so this one revision cannot be taken out on its own. To go back to an older version of the page as a whole, open that revision from the page history and use Restore this revision.' );
		}
		if ( code.indexOf( 'Rollback failed' ) !== -1 || code.indexOf( 'Undo failed' ) !== -1 ) {
			return new Error( code );
		}
		return new Error( 'The revert failed' + ( code && code.length < 60 ? ' (' + code + ')' : '' ) + '.' );
	}
	function getRevisionContent( revid ) {
		return BM.core.api().get( {
			action: 'query', prop: 'revisions', revids: revid,
			rvprop: 'ids|user|timestamp|comment|content', rvslots: 'main', formatversion: 2
		} ).then( function ( r ) {
			var p = r.query.pages[ 0 ];
			if ( !p || !p.revisions || !p.revisions[ 0 ] ) { throw new Error( 'Revision not found.' ); }
			var rev = p.revisions[ 0 ];
			rev.title = p.title;
			return rev;
		} );
	}
	function restoreDialog( revid ) {
		var title = pageTitle();
		getRevisionContent( revid ).then( function ( rev ) {
			var when = String( rev.timestamp || '' ).replace( 'T', ' ' ).replace( 'Z', '' );
			BM.core.dialog( {
				title: 'Restore an old revision',
				help: 'Page: ' + title + '. Revision ' + revid + ' (' + rev.user + ', ' + when + ') will be made the current revision. The current content will remain safe in the history.',
				fields: [
					{ name: 'reason', label: 'Reason (optional)', type: 'text' }
				],
				preview: function () {
					return '\'\'\'Revision to be restored:\'\'\' ' + revid + ' - ' + rev.user + ' (' + when + ')';
				},
				previewTitle: function () { return title; },
				submitLabel: 'Restore',
				success: 'The old revision has been restored.',
				onSubmit: function ( v ) {
					return BM.core.getPage( title ).then( function ( cur ) {
						var summary = 'Restored an old revision (revision ' + revid + ') (using [[Bharatpedia:Bharat Mitra|Bharat Mitra]])';
						if ( v.reason ) { summary += ': ' + BM.core.cleanParam( v.reason ); }
						return BM.core.savePage( title, rev.slots.main.content, summary, cur.revid );
					} ).then( function () {
						// Go to the plain current page afterwards.
						// A plain reload would reopen this old revision view, so the restore bar would be
						// drawn again even though the restore succeeded.
						setTimeout( function () { location.href = mw.util.getUrl( title ); }, 600 );
					} );
				}
			} );
		} ).catch( function () {
			mw.notify( 'Revision details could not be retrieved.', { type: 'error' } );
		} );
	}

	BM.register( 'revert', {
		/* On a diff page, add two links of our own at the top of the page
		   content as soon as this module loads, without waiting for any
		   other gadget: a red "revert and warn", which opens the Revert /
		   Undo dialog with the automatic warning ticked, and a plain
		   "revert only", which opens the same dialog with the warning
		   unticked. */
		addDiffRevertLink: function () {
			if ( document.querySelector( '.bm-revert-warn-link' ) ) { return; }
			var a = document.createElement( 'a' );
			a.href = '#';
			a.className = 'bm-revert-warn-link';
			a.setAttribute( 'style', 'color:#ba0000 !important' );
			a.textContent = 'revert and warn';
			a.addEventListener( 'click', function ( e ) {
				e.preventDefault();
				BM.modules.revert.open();
			} );
			var b = document.createElement( 'a' );
			b.href = '#';
			b.className = 'bm-revert-only-link';
			b.textContent = 'revert only';
			b.addEventListener( 'click', function ( e ) {
				e.preventDefault();
				BM.modules.revert.open( { warn: false } );
			} );
			function place() {
				var host = document.querySelector( '#mw-content-text' );
				if ( !host ) { return false; }
				var span = document.createElement( 'span' );
				span.appendChild( document.createTextNode( '[' ) );
				span.appendChild( a );
				span.appendChild( document.createTextNode( '] || [' ) );
				span.appendChild( b );
				span.appendChild( document.createTextNode( '] ' ) );
				host.insertBefore( span, host.firstChild );
				return true;
			}
			if ( place() ) { return; }
			var tries = 0;
			var timer = setInterval( function () {
				tries++;
				if ( place() || tries > 20 ) {
					clearInterval( timer );
				}
			}, 100 );
		},
		addRestoreLink: function ( revid ) {
			if ( $( '.bm-restore-bar' ).length ) { return; }
			var bar = $( '<div>' ).addClass( 'bm-restore-bar' );
			bar.append( $( '<span>' ).text( 'You are viewing an old revision of this page. ' ) );
			var btn = $( '<button>' ).attr( 'type', 'button' ).addClass( 'bm-btn bm-primary' ).text( '⟲ Restore this revision' );
			btn.on( 'click', function () { restoreDialog( revid ); } );
			bar.append( btn );
			var host = $( '#mw-content-text' );
			if ( host.length ) { host.prepend( bar ); }
			else { $( document.body ).append( bar ); }
		},
		open: function ( opts ) {
			var warnDefault = !( opts && opts.warn === false );
			var title = pageTitle();
			getRevisions( title, 11 ).then( function ( revs ) {
				if ( !revs.length ) { mw.notify( 'No revisions were found for this page.', { type: 'error' } ); return; }
				var last = revs[ 0 ];
				var warnPlans = null;
				var warnApi = null;
				BM.core.dialog( {
					title: 'Revert / Undo',
					help: 'Page: ' + title + '. Most recent editor: ' + last.user + '. Rollback reverts all consecutive edits by the most recent editor at once, and the revision list does not apply to it. Undo takes out the one revision you choose. With the warning box ticked, a warning is sent to that editor automatically after the revert, at the level set below. Untick the box for a good faith revert.',
					fields: [
						{ name: 'mode', label: 'Method', type: 'select', required: true, options: [
							[ 'rollback', 'Rollback: revert consecutive edits by the last editor' ],
							[ 'undo', 'Undo: revert one chosen revision' ]
						], value: 'rollback' },
						{ name: 'revid', label: 'Revision to undo (Undo only)', type: 'select', required: true,
							options: revs.slice( 0, 10 ).map( function ( r ) {
								var c = ( r.comment || '' ).replace( /\s+/g, ' ' );
								if ( c.length > 40 ) { c = c.slice( 0, 40 ) + '…'; }
								return [ String( r.revid ), r.revid + ' - ' + r.user + ( c ? ' - ' + c : '' ) ];
							} ), value: String( last.revid ) },
						{ name: 'warnUser', label: 'Warn the user automatically after reverting', type: 'checkbox', value: warnDefault },
						{ name: 'warnType', label: 'Automatic warning type', type: 'select', required: true, options: [
							[ 'vandalism', 'Vandalism' ],
							[ 'test', 'Test edits' ],
							[ 'blanking', 'Blanking' ],
							[ 'advertising', 'Advertising' ],
							[ 'unsourced', 'Unsourced' ],
							[ 'npov', 'Point of view' ]
						], value: 'vandalism' },
						{ name: 'warnLevel', label: 'Warning level', type: 'select', required: true, options: [
							[ 'auto', 'Automatic (based on previous warnings)' ],
							[ '1', 'Level 1' ],
							[ '2', 'Level 2' ],
							[ '3', 'Level 3' ],
							[ '4', 'Level 4' ],
							[ '4im', 'Level 4im' ]
						], value: 'auto' }
					],
					preview: function ( v ) {
						var out;
						if ( v.mode === 'rollback' ) {
							out = '\'\'\'Rollback:\'\'\' the consecutive edits of ' + last.user + ' will be reverted.';
							if ( last.user === mw.config.get( 'wgUserName' ) ) {
								out += ' The most recent editor is you, so this rollback cannot be submitted.';
							}
						} else {
							out = '\'\'\'Undo:\'\'\' revision ' + v.revid + ' will be reverted.';
						}
						if ( v.warnUser ) {
							var labels = { vandalism: 'Vandalism', test: 'Test edits', blanking: 'Blanking', advertising: 'Advertising', unsourced: 'Unsourced', npov: 'Point of view' };
							var lvl = '';
							if ( v.warnLevel && v.warnLevel !== 'auto' ) {
								lvl = ', level ' + v.warnLevel;
							} else if ( warnPlans && warnPlans[ v.warnType ] ) {
								lvl = warnPlans[ v.warnType ].level ? ', level ' + warnPlans[ v.warnType ].level + ' (automatic)' : ' (standard notice)';
							}
							out += '\n\n\'\'\'Automatic warning:\'\'\' a ' + ( labels[ v.warnType ] || 'Vandalism' ) +
								' warning' + lvl + ' will be sent to [[User talk:' + last.user + ']] for this page.' +
								( lvl ? '' : ' The level is chosen automatically from the warnings already on that talk page.' );
						} else {
							out += '\n\nNo warning will be sent.';
						}
						return out;
					},
					previewTitle: function () { return title; },
					submitLabel: 'Revert',
					success: 'The edit has been reverted.',
					onSubmit: function ( v ) {
						var done;
						if ( v.mode === 'rollback' && last.user === mw.config.get( 'wgUserName' ) ) {
							throw new Error( 'The most recent editor of this page is you, so there is nothing to roll back. A rollback now would undo your own last edit.' );
						}
						if ( v.mode === 'rollback' ) {
							done = BM.core.api().postWithToken( 'rollback', {
								action: 'rollback', title: title, user: last.user, format: 'json'
							} ).then( function ( r ) {
								if ( r && r.rollback ) { return r; }
								throw new Error( 'Rollback failed.' );
							} );
						} else {
							var idx = -1, i;
							for ( i = 0; i < revs.length; i++ ) {
								if ( String( revs[ i ].revid ) === String( v.revid ) ) { idx = i; break; }
							}
							var undoAfter = ( idx >= 0 && revs[ idx + 1 ] ) ? revs[ idx + 1 ].revid : 0;
							done = BM.core.api().postWithToken( 'edit', {
								action: 'edit', title: title, undo: v.revid, undoafter: undoAfter,
								summary: 'Undid one revision (revision ' + v.revid + ') (using [[Bharatpedia:Bharat Mitra|Bharat Mitra]])', format: 'json'
							} ).then( function ( r ) {
								if ( r && r.edit && r.edit.result === 'Success' ) { return r; }
								throw new Error( 'Undo failed.' );
							} );
						}
						done = done.then( null, function ( err ) { throw friendlyError( err ); } );
						return done.then( function () {
							var warned = $.Deferred().resolve( null ).promise();
							if ( v.warnUser && BM.loadModule ) {
								warned = BM.loadModule( 'warn' ).then( function () {
									if ( BM.modules.warn && BM.modules.warn.sendAuto ) {
										return BM.modules.warn.sendAuto( last.user, v.warnType, title, v.warnLevel );
									}
									return null;
								} ).then( function ( plan ) { return plan; }, function () {
									mw.notify( 'The revert was done, but the automatic warning could not be sent. Please warn the user manually.', { type: 'error' } );
									return null;
								} );
							}
							return warned.then( function () {
								setTimeout( function () { location.reload(); }, 600 );
							} );
						} );
					}
				} );
				var $mode = $( 'select[data-bm-field="mode"]' );
				var $rev = $( 'select[data-bm-field="revid"]' );
				var $wtype = $( 'select[data-bm-field="warnType"]' );
				var $wlevel = $( 'select[data-bm-field="warnLevel"]' );
				function syncMode() { $rev.prop( 'disabled', $mode.val() !== 'undo' ); }
				if ( $mode.length ) { $mode.on( 'change', syncMode ); syncMode(); }
				function syncLevels() {
					if ( !warnApi || !warnApi.levelsFor || !$wlevel.length ) { return; }
					var levels = warnApi.levelsFor( $wtype.val() );
					var cur = $wlevel.val();
					$wlevel.empty();
					if ( !levels ) {
						$wlevel.append( $( '<option>' ).val( 'auto' ).text( 'Standard notice (no levels)' ) );
						return;
					}
					$wlevel.append( $( '<option>' ).val( 'auto' ).text( 'Automatic (based on previous warnings)' ) );
					levels.forEach( function ( lv ) {
						$wlevel.append( $( '<option>' ).val( lv ).text( lv === '4im' ? 'Level 4im' : 'Level ' + lv ) );
					} );
					if ( cur && ( cur === 'auto' || levels.indexOf( cur ) !== -1 ) ) { $wlevel.val( cur ); }
				}
				if ( $wtype.length ) { $wtype.on( 'change', syncLevels ); }
				if ( BM.loadModule ) {
					BM.loadModule( 'warn' ).then( function () {
						warnApi = ( BM.modules && BM.modules.warn ) || null;
						if ( warnApi && warnApi.planAll ) {
							warnApi.planAll( last.user, title ).then( function ( plans ) { warnPlans = plans; }, function () {} );
						}
						syncLevels();
					}, function () {} );
				}
			} ).catch( function () {
				mw.notify( 'The revision list could not be loaded.', { type: 'error' } );
			} );
		}
	} );
}() );
/* </nowiki> */