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.' ); } | ||
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' } | ||
], | ], | ||
preview: function () { | |||
return '\'\'\'Revision to be restored:\'\'\' ' + revid + ' - ' + rev.user + ' (' + when + ')'; | return '\'\'\'Revision to be restored:\'\'\' ' + revid + ' - ' + rev.user + ' (' + when + ')'; | ||
}, | }, | ||
onSubmit: function ( | previewTitle: function () { return title; }, | ||
return BM.core. | submitLabel: 'Restore', | ||
success: 'The old revision has been restored.', | |||
onSubmit: function ( v ) { | |||
} ).then( function ( | 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 () { | |||
setTimeout( function () { location. | // 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 () { | ||
if ( document.querySelector( '.bm-revert-warn-link' ) ) { return; } | |||
var a = document.createElement( 'a' ); | |||
a.href = '#'; | |||
a.className = 'bm-revert-warn-link'; | |||
var | a.setAttribute( 'style', 'color:#ba0000 !important' ); | ||
a.textContent = 'revert and warn'; | |||
a.addEventListener( 'click', function ( e ) { | |||
e.preventDefault(); | e.preventDefault(); | ||
BM.modules.revert.open( | 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 ) { | |||
var | |||
e.preventDefault(); | e.preventDefault(); | ||
BM.modules.revert.open( { warn: false } ); | |||
} ); | } ); | ||
var | 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 ) { | open: function ( opts ) { | ||
opts = | var warnDefault = !( opts && opts.warn === false ); | ||
var title = pageTitle(); | var title = pageTitle(); | ||
getRevisions( title, | getRevisions( title, 11 ).then( function ( revs ) { | ||
if ( | if ( !revs.length ) { mw.notify( 'No revisions were found for this page.', { type: 'error' } ); return; } | ||
var last = revs[ 0 ]; | var last = revs[ 0 ]; | ||
var warnPlans = null; | |||
var warnApi = null; | |||
var warnPlans | |||
var | |||
BM.core.dialog( { | BM.core.dialog( { | ||
title: 'Revert | 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: [ | 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, | ||
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: ' | { name: 'warnType', label: 'Automatic warning type', type: 'select', required: true, options: [ | ||
{ name: ' | [ 'vandalism', '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' ], | ||
[ '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 { | ||
out = '\'\'\'Undo:\'\'\' revision ' + v.revid + ' will be reverted.'; | |||
out = '\'\'\'Undo:\'\'\' revision ' + v.revid | |||
} | } | ||
if ( v.warnUser ) { | if ( v.warnUser ) { | ||
var | 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)'; | |||
} else if ( | |||
} | } | ||
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 | 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' ) { | ||
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 | 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.' ); | throw new Error( 'Undo failed.' ); | ||
} ); | } ); | ||
} | } | ||
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"]' ); | ||
function syncMode() { $rev.prop( 'disabled', $mode.val() !== 'undo' ); } | |||
function syncMode() { | if ( $mode.length ) { $mode.on( 'change', syncMode ); syncMode(); } | ||
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; | ||
} | } | ||
$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 () { | |||
if ( | 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 ) { warnPlans = plans; }, function () {} ); | |||
} | |||
syncLevels(); | syncLevels(); | ||
} | }, 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> */