No edit summary
Send an automatic warning after a revert
Line 79: Line 79:
BM.core.dialog( {
BM.core.dialog( {
title: 'Revert / Undo',
title: 'Revert / Undo',
help: 'Page: ' + title + '. Most recent editor: ' + last.user + '. Rollback reverts all consecutive edits by the most recent editor at once.',
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: [
fields: [
{ name: 'mode', label: 'Method', type: 'select', required: true, options: [
{ name: 'mode', label: 'Method', type: 'select', required: true, options: [
Line 91: Line 91:
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: 'warnAfter', label: 'Open the warning dialog after reverting', type: 'checkbox', value: false }
{ name: 'warnUser', label: 'Warn the user automatically after reverting', type: 'checkbox', value: true },
{ 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 ) {
preview: function ( v ) {
var out;
if ( v.mode === 'rollback' ) {
if ( v.mode === 'rollback' ) {
return '\'\'\'Rollback:\'\'\' the consecutive edits of ' + last.user + ' will be reverted.';
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 '\'\'\'Undo:\'\'\' revision ' + v.revid + ' will be reverted.';
return out;
},
},
previewTitle: function () { return title; },
previewTitle: function () { return title; },
Line 121: Line 139:
}
}
return done.then( function () {
return done.then( function () {
if ( v.warnAfter && BM.loadModule ) {
var warned = $.Deferred().resolve( null ).promise();
BM.loadModule( 'warn' ).then( function () {
if ( v.warnUser && BM.loadModule ) {
if ( BM.modules.warn ) { BM.modules.warn.open(); }
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;
} );
} );
}
}
setTimeout( function () { location.reload(); }, 600 );
return warned.then( function () {
setTimeout( function () { location.reload(); }, 600 );
} );
} );
} );
}
}

Revision as of 11:38, 10 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', {
		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. 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: true },
						{ 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> */