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 ) || [];
} );
}
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', {
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 () {
var title = pageTitle();
getRevisions( title, 10 ).then( function ( revs ) {
if ( !revs.length ) { mw.notify( 'No revisions were found for this page.', { type: 'error' } ); return; }
var last = revs[ 0 ];
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.',
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 revert (for Undo only)', type: 'select', required: true,
options: revs.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: 'warnAfter', label: 'Open the warning dialog after reverting', type: 'checkbox', value: false }
],
preview: function ( v ) {
if ( v.mode === 'rollback' ) {
return '\'\'\'Rollback:\'\'\' the consecutive edits of ' + last.user + ' will be reverted.';
}
return '\'\'\'Undo:\'\'\' revision ' + v.revid + ' will be reverted.';
},
previewTitle: function () { return title; },
submitLabel: 'Revert',
success: 'The edit has been reverted.',
onSubmit: function ( v ) {
var done;
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 {
done = BM.core.api().postWithToken( 'edit', {
action: 'edit', title: title, undo: v.revid,
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.' );
} );
}
return done.then( function () {
if ( v.warnAfter && BM.loadModule ) {
BM.loadModule( 'warn' ).then( function () {
if ( BM.modules.warn ) { BM.modules.warn.open(); }
} );
}
setTimeout( function () { location.reload(); }, 600 );
} );
}
} );
} ).catch( function () {
mw.notify( 'The revision list could not be loaded.', { type: 'error' } );
} );
}
} );
}() );
/* </nowiki> */