Note: After publishing, you may have to bypass your browser's cache to see the changes.
- Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
- Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
- Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
/* <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( 'nosuchrevid' ) !== -1 ) {
return new Error( 'That revision could not be found. The page history may have changed since this dialog was opened; please close it and try again.' );
}
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;
if ( !undoAfter ) {
throw new Error( 'This is the first revision of the page, so it cannot be undone: there is no earlier revision to undo it against. If you want the page to go back to exactly this revision, open it from the page history and use Restore this revision.' );
}
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 ( plan ) {
setTimeout( function () {
// When a warning was sent, go to that editor's talk
// page so the warning can be seen there. A plain
// reload would only reopen this same page or diff.
if ( plan ) { location.href = mw.util.getUrl( 'User talk:' + last.user ); }
else { 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> */