Add the revert links for diff pages
Diff revert links: load immediately at the top of the page
Line 61: Line 61:


BM.register( 'revert', {
BM.register( 'revert', {
/* On a diff page, add two links next to the rollback links Twinkle
/* On a diff page, add two links of our own at the top of the page
  adds there: a red "revert and warn", which opens the Revert / Undo
  content as soon as this module loads, without waiting for any
  dialog with the automatic warning ticked, and a plain "revert
  other gadget: a red "revert and warn", which opens the Revert /
  only", which opens the same dialog with the warning unticked.
  Undo dialog with the automatic warning ticked, and a plain
  If Twinkle's links never appear, both links are put on their own
  "revert only", which opens the same dialog with the warning
  at the top of the page content after about 10 seconds. */
  unticked. */
addDiffRevertLink: function () {
addDiffRevertLink: function () {
if ( document.querySelector( '.bm-revert-warn-link' ) ) { return; }
if ( document.querySelector( '.bm-revert-warn-link' ) ) { return; }
Line 86: Line 86:
BM.modules.revert.open( { warn: false } );
BM.modules.revert.open( { warn: false } );
} );
} );
function placeNextToTwinkle() {
function place() {
var links = document.querySelectorAll( 'a' );
var host = document.querySelector( '#mw-content-text' );
for ( var i = 0; i < links.length; i++ ) {
if ( !host ) { return false; }
if ( links[ i ] === a ) { continue; }
var span = document.createElement( 'span' );
if ( links[ i ].textContent.replace( /\s+/g, ' ' ).trim() === 'vandalism' &&
span.appendChild( document.createTextNode( '[' ) );
links[ i ].parentNode && /rollback/i.test( links[ i ].parentNode.textContent || '' ) ) {
span.appendChild( a );
var span = document.createElement( 'span' );
span.appendChild( document.createTextNode( '] || [' ) );
span.appendChild( document.createTextNode( ' || [' ) );
span.appendChild( b );
span.appendChild( a );
span.appendChild( document.createTextNode( '] ' ) );
span.appendChild( document.createTextNode( '] || [' ) );
host.insertBefore( span, host.firstChild );
span.appendChild( b );
return true;
span.appendChild( document.createTextNode( ']' ) );
links[ i ].parentNode.appendChild( span );
return true;
}
}
return false;
}
}
if ( placeNextToTwinkle() ) { return; }
if ( place() ) { return; }
var tries = 0;
var tries = 0;
var timer = setInterval( function () {
var timer = setInterval( function () {
tries++;
tries++;
if ( placeNextToTwinkle() || tries > 40 ) {
if ( place() || tries > 20 ) {
clearInterval( timer );
clearInterval( timer );
if ( !a.isConnected ) {
var host = document.querySelector( '#mw-content-text' );
if ( host ) {
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 );
}
}
}
}
}, 250 );
}, 100 );
},
},
addRestoreLink: function ( revid ) {
addRestoreLink: function ( revid ) {

Revision as of 19:24, 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 ) || [];
		} );
	}
	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, 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. With the warning box ticked, a warning is sent to that editor automatically after the revert, with the level chosen from the warnings already on their talk page. 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 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: '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' }
					],
					preview: function ( v ) {
						var out;
						if ( v.mode === 'rollback' ) {
							out = '\'\'\'Rollback:\'\'\' the consecutive edits of ' + last.user + ' will be reverted.';
						} 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' };
							out += '\n\n\'\'\'Automatic warning:\'\'\' a ' + ( labels[ v.warnType ] || 'Vandalism' ) +
								' warning will be sent to [[User talk:' + last.user + ']] for this page. 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' ) {
							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 () {
							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 );
									}
									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 );
							} );
						} );
					}
				} );
			} ).catch( function () {
				mw.notify( 'The revision list could not be loaded.', { type: 'error' } );
			} );
		}
	} );
}() );
/* </nowiki> */