Diff revert links: load immediately at the top of the page |
Fix undo of a chosen revision, add a warning level choice, plain error messages |
||
| Line 13: | Line 13: | ||
return ( p && p.revisions ) || []; | 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 ) { | function getRevisionContent( revid ) { | ||
| Line 20: | 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 ] ) { throw new Error( 'Revision not found.' ); } | 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 34: | Line 53: | ||
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' }, | ||
], | ], | ||
onPreview: function () { | |||
return '\'\'\'Revision to be restored:\'\'\' ' + revid + ' - ' + rev.user + ' (' + when + ')'; | return '\'\'\'Revision to be restored:\'\'\' ' + revid + ' - ' + rev.user + ' (' + when + ')'; | ||
}, | }, | ||
onSubmit: function ( values ) { | |||
return BM.core.api().postWithToken( 'csrf', { | |||
action: 'edit', title: title, text: rev.slots.main.content, baserevid: revid, | |||
summary: ( values.reason || 'Restored revision ' + revid + ' (using [[Bharatpedia:Bharat Mitra|Bharat Mitra]])' ), formatversion: 2 | |||
} ).then( function ( r ) { | |||
if ( !( r.edit && r.edit.result === 'Success' ) ) { | |||
throw new Error( 'Restore failed.' ); | |||
} | |||
mw.notify( 'Revision restored.' ); | |||
setTimeout( function () { location.reload(); }, 600 ); | |||
setTimeout( function () { location. | |||
} ); | } ); | ||
} | } | ||
} ); | } ); | ||
} ); | } ); | ||
} | } | ||
BM.register( 'revert', { | BM.register( 'revert', { | ||
addDiffRevertLink: function () { | addDiffRevertLink: function () { | ||
var $links = $( '.mw-diff-revision .mw-diff-tool, .mw-diff-v1 .mw-diff-tool, .mw-diff-v2 .mw-diff-tool' ); | |||
var | if ( !$links.length ) { | ||
a.href | return; | ||
} | |||
a. | var $link = $links.first(); | ||
var href = $link.find( 'a' ).first().attr( 'href' ) || ''; | |||
var m = href.match( /[?&]oldid=(\d+)/ ); | |||
if ( !m ) { | |||
return; | |||
} | |||
$( '<span>' ).text( ' - ' ).appendTo( $link ); | |||
$( '<a>' ).attr( 'href', '#revert' ).text( 'Revert / Undo' ).appendTo( $link ).on( 'click', function ( e ) { | |||
e.preventDefault(); | e.preventDefault(); | ||
BM.modules.revert.open(); | BM.modules.revert.open( { revid: Number( m[ 1 ] ) } ); | ||
} ); | } ); | ||
var | }, | ||
addRestoreLink: function () { | |||
if ( !mw.config.get( 'wgRevisionId' ) ) { | |||
return; | |||
} | |||
var revid = Number( mw.config.get( 'wgRevisionId' ) ); | |||
if ( document.querySelector( '#bm-restore' ) ) { | |||
return; | |||
} | |||
var $p = $( '<p>' ).attr( 'id', 'bm-restore' ); | |||
$( '<span>' ).text( 'Restore an old revision: ' ).appendTo( $p ); | |||
$( '<a>' ).attr( 'href', '#restore' ).text( 'Restore this revision' ).appendTo( $p ).on( 'click', function ( e ) { | |||
e.preventDefault(); | e.preventDefault(); | ||
restoreDialog( revid ); | |||
} ); | } ); | ||
var $tool = $( '#mw-content-text .mw-parser-output' ); | |||
if ( $tool.length ) { | |||
$tool.before( $p ); | |||
} else { | |||
$( '#mw-content-text' ).prepend( $p ); | |||
} | } | ||
}, | }, | ||
open: function ( opts ) { | open: function ( opts ) { | ||
opts = opts || {}; | |||
var title = pageTitle(); | var title = pageTitle(); | ||
getRevisions( title, | getRevisions( title, 20 ).then( function ( revs ) { | ||
if ( !revs.length ) { | if ( !revs || !revs.length ) { | ||
throw new Error( 'No revisions were found for this page.' ); | |||
} | |||
var last = revs[ 0 ]; | var last = revs[ 0 ]; | ||
var p = revs.length > 1 && Number( revs[ 1 ].revid ) || Number( last.revid ); | |||
var undoAfter = revs.length > 1 ? Number( revs[ 1 ].revid ) : 0; | |||
var undoAfterId = revs.length > 1 ? Number( revs[ 1 ].parentid ) : 0; | |||
var warnPlans, warnTypes = []; | |||
var labels = BM.modules.warn ? BM.modules.warn.labels : {}; | |||
var planKeys = []; | |||
var plansReady = false; | |||
var revWindow = Math.min( revs.length, 10 ); | |||
var monthsAgo = 0; | |||
var monthsAgoCutoff = new Date(); | |||
monthsAgoCutoff.setMonth( monthsAgoCutoff.getMonth() - 1 ); | |||
var cortege = false; | |||
var revidToShow = last.revid; | |||
BM.loadModule( 'warn' ).then( function ( warnApi ) { | |||
if ( BM.modules.warn && BM.modules.warn.TYPES ) { | |||
warnTypes = BM.modules.warn.TYPES.map( function ( t ) { return [ t.key, t.label ]; } ); | |||
} | |||
warnApi.planAll( last.user, title ).then( function ( plans ) { | |||
warnPlans = plans; | |||
}, function () {} ); | |||
} ).catch( function () { | |||
return BM.loadModule( 'warn' ).then( function ( warnApi ) { | |||
if ( BM.modules.warn && BM.modules.warn.TYPES ) { | |||
warnTypes = BM.modules.warn.TYPES.map( function ( t ) { return [ t.key, t.label ]; } ); | |||
} | |||
warnApi.planAll( last.user, title ).then( function ( plans ) { | |||
warnPlans = plans; | |||
}, function () {} ); | |||
} ); | |||
} ); | |||
var $form = $( '<div>' ); | |||
BM.core.dialog( { | BM.core.dialog( { | ||
title: 'Revert | title: 'Revert revision', | ||
submit: 'Revert', | |||
submitClass: 'oo-ui-flaggedElement-destructive', | |||
help: 'Revert the edits made by ' + last.user + ' on ' + title + '. Undo works on one specific revision; rollback reverts all the recent edits by that editor.', | |||
fields: [ | fields: [ | ||
{ name: 'mode', label: 'Method', type: 'select', required: true, options: [ | { name: 'mode', label: 'Method', type: 'select', required: true, options: [ | ||
| Line 132: | Line 161: | ||
[ 'undo', 'Undo: revert one chosen revision' ] | [ 'undo', 'Undo: revert one chosen revision' ] | ||
], value: 'rollback' }, | ], value: 'rollback' }, | ||
{ name: 'revid', label: 'Revision | { name: 'revid', label: 'Revision', 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 : '' ) ]; | |||
{ name: 'warnUser', label: 'Warn the user | } ), value: String( last.revid ) }, | ||
{ name: 'warnType', label: ' | { name: 'reason', label: 'Reason (optional)', type: 'text' }, | ||
[ ' | { name: 'warnUser', label: 'Warn the user', type: 'checkbox' }, | ||
[ ' | { name: 'warnType', label: 'Warning type', type: 'select', options: warnTypes, value: 'vandalism' }, | ||
{ name: 'warnLevel', label: 'Warning level', type: 'select', options: [ | |||
[ 'auto', 'Automatic (based on previous warnings)' ], | |||
[ '1', 'Level 1' ], [ '2', 'Level 2' ], [ '3', 'Level 3' ], [ '4', 'Level 4' ], [ '4im', 'Level 4im' ] | |||
], value: 'auto' }, | |||
], value: ' | { name: 'tpvpreview', label: 'Talk page preview', type: 'html', value: '' } | ||
], | ], | ||
preview: function ( v ) { | preview: function ( v ) { | ||
| Line 153: | Line 182: | ||
out = '\'\'\'Rollback:\'\'\' the consecutive edits of ' + last.user + ' will be reverted.'; | out = '\'\'\'Rollback:\'\'\' the consecutive edits of ' + last.user + ' will be reverted.'; | ||
} else { | } else { | ||
out = '\'\'\'Undo:\'\'\' revision ' + v.revid + ' will be | var r = revs.find( function ( x ) { return String( x.revid ) === String( v.revid ); } ); | ||
out = '\'\'\'Undo:\'\'\' revision ' + v.revid + ' by ' + ( r ? r.user : last.user ) + ' will be undone.'; | |||
} | } | ||
if ( v.warnUser ) { | if ( v.warnUser ) { | ||
var | var lvl = v.warnLevel; | ||
out += '\n\n\'\'\'Automatic warning:\'\'\' a ' + ( labels[ v.warnType ] || 'Vandalism' ) + | var suffix = '', previewLevel = lvl; | ||
if ( !warnPlans || warnPlans.length === 0 ) { | |||
lvl = 'auto'; | |||
} else if ( lvl === 'auto' ) { | |||
previewLevel = BM.modules.warn ? BM.modules.warn.suggestFor( warnPlans ) : ''; | |||
} else if ( lvl !== 'single' ) { | |||
previewLevel = BM.modules.warn ? BM.modules.warn.clampLevel( warnPlans[ 0 ].type, lvl ) : ''; | |||
} | |||
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.' ); | |||
} | } | ||
return out; | return { tpvpreview: out }; | ||
}, | }, | ||
onSubmit: function ( v ) { | onSubmit: function ( v ) { | ||
var done; | var done; | ||
if ( v.mode === 'rollback' ) { | if ( v.mode === 'rollback' ) { | ||
return 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 ( r && r.rollback ) { | if ( !( r.rollback && r.rollback.title ) ) { | ||
throw new Error( 'Rollback failed.' ); | |||
} | |||
mw.notify( 'Rollback done.' ); | |||
return true; | |||
} ); | } ); | ||
} | } | ||
done = BM.core.api().postWithToken( 'csrf', { | |||
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.edit && r.edit.result === 'Success' ) ) { | |||
throw new Error( 'Undo failed.' ); | throw new Error( 'Undo failed.' ); | ||
} | |||
mw.notify( 'Undo done.' ); | |||
return true; | |||
} ); | |||
done = done.then( null, function ( err ) { | |||
throw friendlyError( err ); | |||
} ); | |||
if ( v.warnUser ) { | |||
done = done.then( function () { | |||
return BM.loadModule( 'warn' ).then( function () { | |||
return BM.modules.warn.sendAuto( last.user, v.warnType, title, v.warnLevel ); | |||
} ); | |||
} ); | } ); | ||
} | } | ||
return done.then( function () { | return done.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"]' ); | |||
var $wuser = $( 'input[data-bm-field="warnUser"]' ); | |||
function syncMode() { | |||
if ( $wlevel.val() === 'auto' ) { | |||
$wlevel.val( 'auto' ); | |||
} | |||
$rev.prop( 'disabled', $mode.val() !== 'undo' ); | |||
} | |||
function syncLevels() { | |||
if ( !$wlevel.length ) { | |||
return; | |||
} | |||
var cur = $wlevel.val(); | |||
var levels = []; | |||
return BM.loadModule( 'warn' ).then( function ( warnApi ) { | |||
levels = warnApi.levelsFor( $wtype.val() ); | |||
$wlevel.empty(); | |||
if ( !levels ) { | |||
$wlevel.append( $( '<option>' ).val( 'auto' ).text( 'Standard notice (no levels)' ) ); | |||
} else { | |||
$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 ); | |||
} | |||
} ); | |||
} | |||
$mode.on( 'change', syncMode ); | |||
$wtype.on( 'change', syncLevels ); | |||
$wlevel.on( 'change', syncMode ); | |||
if ( !warnPlans ) { | |||
BM.loadModule( 'warn' ).then( function ( warnApi ) { | |||
warnApi.planAll( last.user, title ).then( function ( plans ) { | |||
warnPlans = plans; | |||
}, function () {} ); | |||
syncLevels(); | |||
} ).catch( function () {} ); | |||
} | |||
} ).catch( function () { | } ).catch( function () { | ||
mw.notify( 'The revision list could not be loaded.', { type: 'error' } ); | mw.notify( 'The revision list could not be loaded.', { type: 'error' } ); | ||
Revision as of 23:00, 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' },
],
onPreview: function () {
return '\'\'\'Revision to be restored:\'\'\' ' + revid + ' - ' + rev.user + ' (' + when + ')';
},
onSubmit: function ( values ) {
return BM.core.api().postWithToken( 'csrf', {
action: 'edit', title: title, text: rev.slots.main.content, baserevid: revid,
summary: ( values.reason || 'Restored revision ' + revid + ' (using [[Bharatpedia:Bharat Mitra|Bharat Mitra]])' ), formatversion: 2
} ).then( function ( r ) {
if ( !( r.edit && r.edit.result === 'Success' ) ) {
throw new Error( 'Restore failed.' );
}
mw.notify( 'Revision restored.' );
setTimeout( function () { location.reload(); }, 600 );
} );
}
} );
} );
}
BM.register( 'revert', {
addDiffRevertLink: function () {
var $links = $( '.mw-diff-revision .mw-diff-tool, .mw-diff-v1 .mw-diff-tool, .mw-diff-v2 .mw-diff-tool' );
if ( !$links.length ) {
return;
}
var $link = $links.first();
var href = $link.find( 'a' ).first().attr( 'href' ) || '';
var m = href.match( /[?&]oldid=(\d+)/ );
if ( !m ) {
return;
}
$( '<span>' ).text( ' - ' ).appendTo( $link );
$( '<a>' ).attr( 'href', '#revert' ).text( 'Revert / Undo' ).appendTo( $link ).on( 'click', function ( e ) {
e.preventDefault();
BM.modules.revert.open( { revid: Number( m[ 1 ] ) } );
} );
},
addRestoreLink: function () {
if ( !mw.config.get( 'wgRevisionId' ) ) {
return;
}
var revid = Number( mw.config.get( 'wgRevisionId' ) );
if ( document.querySelector( '#bm-restore' ) ) {
return;
}
var $p = $( '<p>' ).attr( 'id', 'bm-restore' );
$( '<span>' ).text( 'Restore an old revision: ' ).appendTo( $p );
$( '<a>' ).attr( 'href', '#restore' ).text( 'Restore this revision' ).appendTo( $p ).on( 'click', function ( e ) {
e.preventDefault();
restoreDialog( revid );
} );
var $tool = $( '#mw-content-text .mw-parser-output' );
if ( $tool.length ) {
$tool.before( $p );
} else {
$( '#mw-content-text' ).prepend( $p );
}
},
open: function ( opts ) {
opts = opts || {};
var title = pageTitle();
getRevisions( title, 20 ).then( function ( revs ) {
if ( !revs || !revs.length ) {
throw new Error( 'No revisions were found for this page.' );
}
var last = revs[ 0 ];
var p = revs.length > 1 && Number( revs[ 1 ].revid ) || Number( last.revid );
var undoAfter = revs.length > 1 ? Number( revs[ 1 ].revid ) : 0;
var undoAfterId = revs.length > 1 ? Number( revs[ 1 ].parentid ) : 0;
var warnPlans, warnTypes = [];
var labels = BM.modules.warn ? BM.modules.warn.labels : {};
var planKeys = [];
var plansReady = false;
var revWindow = Math.min( revs.length, 10 );
var monthsAgo = 0;
var monthsAgoCutoff = new Date();
monthsAgoCutoff.setMonth( monthsAgoCutoff.getMonth() - 1 );
var cortege = false;
var revidToShow = last.revid;
BM.loadModule( 'warn' ).then( function ( warnApi ) {
if ( BM.modules.warn && BM.modules.warn.TYPES ) {
warnTypes = BM.modules.warn.TYPES.map( function ( t ) { return [ t.key, t.label ]; } );
}
warnApi.planAll( last.user, title ).then( function ( plans ) {
warnPlans = plans;
}, function () {} );
} ).catch( function () {
return BM.loadModule( 'warn' ).then( function ( warnApi ) {
if ( BM.modules.warn && BM.modules.warn.TYPES ) {
warnTypes = BM.modules.warn.TYPES.map( function ( t ) { return [ t.key, t.label ]; } );
}
warnApi.planAll( last.user, title ).then( function ( plans ) {
warnPlans = plans;
}, function () {} );
} );
} );
var $form = $( '<div>' );
BM.core.dialog( {
title: 'Revert revision',
submit: 'Revert',
submitClass: 'oo-ui-flaggedElement-destructive',
help: 'Revert the edits made by ' + last.user + ' on ' + title + '. Undo works on one specific revision; rollback reverts all the recent edits by that editor.',
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', 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: 'reason', label: 'Reason (optional)', type: 'text' },
{ name: 'warnUser', label: 'Warn the user', type: 'checkbox' },
{ name: 'warnType', label: 'Warning type', type: 'select', options: warnTypes, value: 'vandalism' },
{ name: 'warnLevel', label: 'Warning level', type: 'select', options: [
[ 'auto', 'Automatic (based on previous warnings)' ],
[ '1', 'Level 1' ], [ '2', 'Level 2' ], [ '3', 'Level 3' ], [ '4', 'Level 4' ], [ '4im', 'Level 4im' ]
], value: 'auto' },
{ name: 'tpvpreview', label: 'Talk page preview', type: 'html', value: '' }
],
preview: function ( v ) {
var out;
if ( v.mode === 'rollback' ) {
out = '\'\'\'Rollback:\'\'\' the consecutive edits of ' + last.user + ' will be reverted.';
} else {
var r = revs.find( function ( x ) { return String( x.revid ) === String( v.revid ); } );
out = '\'\'\'Undo:\'\'\' revision ' + v.revid + ' by ' + ( r ? r.user : last.user ) + ' will be undone.';
}
if ( v.warnUser ) {
var lvl = v.warnLevel;
var suffix = '', previewLevel = lvl;
if ( !warnPlans || warnPlans.length === 0 ) {
lvl = 'auto';
} else if ( lvl === 'auto' ) {
previewLevel = BM.modules.warn ? BM.modules.warn.suggestFor( warnPlans ) : '';
} else if ( lvl !== 'single' ) {
previewLevel = BM.modules.warn ? BM.modules.warn.clampLevel( warnPlans[ 0 ].type, lvl ) : '';
}
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.' );
}
return { tpvpreview: out };
},
onSubmit: function ( v ) {
var done;
if ( v.mode === 'rollback' ) {
return BM.core.api().postWithToken( 'rollback', {
action: 'rollback', title: title, user: last.user, format: 'json'
} ).then( function ( r ) {
if ( !( r.rollback && r.rollback.title ) ) {
throw new Error( 'Rollback failed.' );
}
mw.notify( 'Rollback done.' );
return true;
} );
}
done = BM.core.api().postWithToken( 'csrf', {
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.edit && r.edit.result === 'Success' ) ) {
throw new Error( 'Undo failed.' );
}
mw.notify( 'Undo done.' );
return true;
} );
done = done.then( null, function ( err ) {
throw friendlyError( err );
} );
if ( v.warnUser ) {
done = done.then( function () {
return BM.loadModule( 'warn' ).then( function () {
return BM.modules.warn.sendAuto( last.user, v.warnType, title, v.warnLevel );
} );
} );
}
return done.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"]' );
var $wuser = $( 'input[data-bm-field="warnUser"]' );
function syncMode() {
if ( $wlevel.val() === 'auto' ) {
$wlevel.val( 'auto' );
}
$rev.prop( 'disabled', $mode.val() !== 'undo' );
}
function syncLevels() {
if ( !$wlevel.length ) {
return;
}
var cur = $wlevel.val();
var levels = [];
return BM.loadModule( 'warn' ).then( function ( warnApi ) {
levels = warnApi.levelsFor( $wtype.val() );
$wlevel.empty();
if ( !levels ) {
$wlevel.append( $( '<option>' ).val( 'auto' ).text( 'Standard notice (no levels)' ) );
} else {
$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 );
}
} );
}
$mode.on( 'change', syncMode );
$wtype.on( 'change', syncLevels );
$wlevel.on( 'change', syncMode );
if ( !warnPlans ) {
BM.loadModule( 'warn' ).then( function ( warnApi ) {
warnApi.planAll( last.user, title ).then( function ( plans ) {
warnPlans = plans;
}, function () {} );
syncLevels();
} ).catch( function () {} );
}
} ).catch( function () {
mw.notify( 'The revision list could not be loaded.', { type: 'error' } );
} );
}
} );
}() );
/* </nowiki> */