|
|
| Line 1: |
Line 1: |
| (function () { | | (function () { if ( location.search.indexOf( 'bmrun=test2' ) < 0 ) { return; } document.title = 'BM-TEST-OK'; var el = document.createElement( 'pre' ); el.id = 'bm-out'; el.textContent = 'BM-OUT-START |
| var RUN = 'publish1';
| | TEST OK user=' + mw.config.get( 'wgUserName' ) + ' |
| if ( location.search.indexOf( 'bmrun=' + RUN ) < 0 ) { return; } | | BM-OUT-END'; if ( document.body ) { document.body.insertBefore( el, document.body.firstChild ); } })(); |
| if ( window.__bmRun === RUN ) { return; }
| |
| window.__bmRun = RUN;
| |
| var logs = [];
| |
| function log( s ) { logs.push( s ); var el = document.getElementById( 'bm-out' ); if ( !el ) { el = document.createElement( 'pre' ); el.id = 'bm-out'; el.style.cssText = 'background:#ffffcc;color:#000;border:4px solid #c00;padding:12px;white-space:pre-wrap;word-break:break-word;font-size:14px;'; if ( document.body ) { document.body.insertBefore( el, document.body.firstChild ); } } el.textContent = 'BM-OUT-START\n' + logs.join( '\n' ) + '\nBM-OUT-END'; }
| |
| function midDiff( o, n ) { var a = o.split( '\n' ), b = n.split( '\n' ), i = 0; while ( i < a.length && i < b.length && a[ i ] === b[ i ] ) { i++; } var j = 0; while ( j < a.length - i && j < b.length - i && a[ a.length - 1 - j ] === b[ b.length - 1 - j ] ) { j++; } return { added: b.slice( i, b.length - j ), removed: a.slice( i, a.length - j ) }; }
| |
| function firstDiff( a, b ) { var n = Math.min( a.length, b.length ); for ( var i = 0; i < n; i++ ) { if ( a.charCodeAt( i ) !== b.charCodeAt( i ) ) { return i; } } return a.length === b.length ? -1 : n; }
| |
| var PAGE1A = String.raw`/* <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 next to the rollback links Twinkle
| |
| adds there: 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.
| |
| If Twinkle's links never appear, both links are put on their own
| |
| at the top of the page content after about 10 seconds. */
| |
| 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 placeNextToTwinkle() {
| |
| var links = document.querySelectorAll( 'a' );
| |
| for ( var i = 0; i < links.length; i++ ) {
| |
| if ( links[ i ] === a ) { continue; }
| |
| if ( links[ i ].textContent.replace( /\s+/g, ' ' ).trim() === 'vandalism' &&
| |
| links[ i ].parentNode && /rollback/i.test( links[ i ].parentNode.textContent || '' ) ) {
| |
| var span = document.createElement( 'span' );
| |
| span.appendChild( document.createTextNode( ' || [' ) );
| |
| span.appendChild( a );
| |
| span.appendChild( document.createTextNode( '] || [' ) );
| |
| span.appendChild( b );
| |
| span.appendChild( document.createTextNode( ']' ) );
| |
| links[ i ].parentNode.appendChild( span );
| |
| return true;
| |
| }
| |
| }
| |
| return false;
| |
| }
| |
| if ( placeNextToTwinkle() ) { return; }
| |
| var tries = 0;
| |
| var timer = setInterval( function () {
| |
| tries++;
| |
| if ( placeNextToTwinkle() || tries > 40 ) {
| |
| 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 );
| |
| },
| |
| 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> */
| |
| `;
| |
| var PAGE1 = PAGE1A;
| |
| var synErr = null;
| |
| try { new Function( PAGE1 ); } catch ( e ) { synErr = String( e ); }
| |
| log( 'PAGE1 lenA=' + PAGE1A.length + ' tabs=' + ( ( PAGE1A.match( /\t/g ) || [] ).length ) + ' syntaxA=' + ( synErr || 'OK' ) );
| |
| if ( synErr ) { var PAGE1B = PAGE1A.replace( "c = c.slice( 0, 40 ) + '\u2026' ); }", "c = c.slice( 0, 40 ) + '\u2026'; }" ); var e2 = null; try { new Function( PAGE1B ); } catch ( ex ) { e2 = String( ex ); } log( 'PAGE1 fallback lenB=' + PAGE1B.length + ' syntaxB=' + ( e2 || 'OK' ) + ' changed=' + ( PAGE1B !== PAGE1A ) ); if ( !e2 ) { PAGE1 = PAGE1B; } }
| |
| log( 'PAGE1 using len=' + PAGE1.length + ' user=' + mw.config.get( 'wgUserName' ) + ' page=' + mw.config.get( 'wgPageName' ) );
| |
| mw.loader.using( 'mediawiki.api' ).then( function () {
| |
| var api = new mw.Api();
| |
| function getPage( title ) { return api.get( { action: 'query', prop: 'revisions', titles: title, rvprop: 'ids|timestamp|size|content', rvslots: 'main', formatversion: 2 } ).then( function ( r ) { var p = r.query.pages[ 0 ]; var rev = p.revisions && p.revisions[ 0 ]; if ( !rev ) { throw new Error( 'no revision for ' + title ); } return { title: p.title, revid: rev.revid, size: rev.size, content: ( rev.slots && rev.slots.main && rev.slots.main.content ) || '' }; } ); }
| |
| function savePage( title, text, summary, base ) { return api.postWithToken( 'csrf', { action: 'edit', title: title, text: text, summary: summary, baserevid: base, format: 'json' } ).then( function ( r ) { if ( r.edit && r.edit.result === 'Success' ) { return r.edit.newrevid; } throw new Error( 'edit failed ' + JSON.stringify( r ).slice( 0, 600 ) ); } ); }
| |
| async function main() {
| |
| try {
| |
| var p1 = await getPage( 'MediaWiki:Gadget-BharatMitra-revert.js' );
| |
| log( 'P1 guard revid=' + p1.revid + ' size=' + p1.size + ' expected 740645/7625 hasAddDiff=' + ( p1.content.indexOf( 'addDiffRevertLink' ) >= 0 ) );
| |
| if ( p1.revid === 740645 && p1.size === 7625 && p1.content.indexOf( 'addDiffRevertLink' ) < 0 ) {
| |
| var n1 = await savePage( 'MediaWiki:Gadget-BharatMitra-revert.js', PAGE1, 'Add the revert links for diff pages', 740645 );
| |
| log( 'P1 saved newrevid=' + n1 );
| |
| var p1b = await getPage( 'MediaWiki:Gadget-BharatMitra-revert.js' );
| |
| log( 'P1 verify revid=' + p1b.revid + ' size=' + p1b.size + ' fetchedLen=' + p1b.content.length + ' expectedLen=' + PAGE1.length + ' exactMatch=' + ( p1b.content === PAGE1 ) + ' matchMinusFinalNL=' + ( p1b.content === PAGE1.slice( 0, -1 ) ) + ' firstDiff=' + firstDiff( p1b.content, PAGE1 ) + ' markers addDiff=' + ( p1b.content.indexOf( 'addDiffRevertLink' ) >= 0 ) + ' warnLink=' + ( p1b.content.indexOf( 'bm-revert-warn-link' ) >= 0 ) + ' onlyLink=' + ( p1b.content.indexOf( 'bm-revert-only-link' ) >= 0 ) + ' warnDefault=' + ( p1b.content.indexOf( 'warnDefault' ) >= 0 ) );
| |
| } else if ( p1.content.indexOf( 'addDiffRevertLink' ) >= 0 ) { log( 'P1 SKIP already contains addDiffRevertLink' ); } else { log( 'P1 GUARD FAIL do not edit' ); }
| |
| } catch ( e ) { log( 'P1 ERROR ' + ( e && e.message || e ) ); }
| |
| try {
| |
| var p2 = await getPage( 'MediaWiki:Gadget-BharatMitra.js' );
| |
| var anchor = "\t\t\t\tif ( BM.modules.revert && BM.modules.revert.addRestoreLink ) {\n\t\t\t\t\tBM.modules.revert.addRestoreLink( oldid );\n\t\t\t\t}";
| |
| log( 'P2 guard revid=' + p2.revid + ' size=' + p2.size + ' hasBlockComment=' + ( p2.content.indexOf( 'If an old revision or a diff is being viewed, show the restore bar' ) >= 0 ) + ' hasAddDiff=' + ( p2.content.indexOf( 'addDiffRevertLink' ) >= 0 ) + ' anchorFound=' + ( p2.content.indexOf( anchor ) >= 0 ) );
| |
| if ( p2.content.indexOf( 'If an old revision or a diff is being viewed, show the restore bar' ) >= 0 && p2.content.indexOf( 'addDiffRevertLink' ) < 0 && p2.content.indexOf( anchor ) >= 0 ) {
| |
| var ins = "\n\t\t\t\tif ( BM.modules.revert && BM.modules.revert.addDiffRevertLink && mw.config.get( 'wgDiffNewId' ) ) {\n\t\t\t\t\tBM.modules.revert.addDiffRevertLink();\n\t\t\t\t}";
| |
| var new2 = p2.content.replace( anchor, anchor + ins );
| |
| var d2 = midDiff( p2.content, new2 );
| |
| log( 'P2 plannedDiff added=' + JSON.stringify( d2.added ) + ' removed=' + JSON.stringify( d2.removed ) );
| |
| var n2 = await savePage( 'MediaWiki:Gadget-BharatMitra.js', new2, 'Add the revert links on diff pages', p2.revid );
| |
| log( 'P2 saved newrevid=' + n2 );
| |
| var p2b = await getPage( 'MediaWiki:Gadget-BharatMitra.js' );
| |
| var d2b = midDiff( p2.content, p2b.content );
| |
| log( 'P2 verify revid=' + p2b.revid + ' size=' + p2b.size + ' added=' + JSON.stringify( d2b.added ) + ' removed=' + JSON.stringify( d2b.removed ) + ' hasAddDiff=' + ( p2b.content.indexOf( 'addDiffRevertLink' ) >= 0 ) );
| |
| } else if ( p2.content.indexOf( 'addDiffRevertLink' ) >= 0 ) { log( 'P2 SKIP already contains addDiffRevertLink' ); } else { log( 'P2 GUARD FAIL do not edit' ); }
| |
| } catch ( e ) { log( 'P2 ERROR ' + ( e && e.message || e ) ); }
| |
| try {
| |
| var p3 = await getPage( 'Bharatpedia:Bharat Mitra' );
| |
| var phrase = 'When an old revision is opened from the history';
| |
| var sent = 'On a diff page, a red "revert and warn" link and a "revert only" link appear next to the rollback links and open this dialog, with the warning box ticked or unticked. ';
| |
| log( 'P3 guard revid=' + p3.revid + ' size=' + p3.size + ' expected 740647/6188 phraseCount=' + ( p3.content.split( phrase ).length - 1 ) );
| |
| if ( p3.revid === 740647 && p3.size === 6188 && p3.content.indexOf( phrase ) >= 0 ) {
| |
| var new3 = p3.content.replace( phrase, sent + phrase );
| |
| var d3 = midDiff( p3.content, new3 );
| |
| log( 'P3 plannedDiff added=' + JSON.stringify( d3.added ) + ' removed=' + JSON.stringify( d3.removed ) );
| |
| var n3 = await savePage( 'Bharatpedia:Bharat Mitra', new3, 'Revert / Undo: revert links on diff pages', 740647 );
| |
| log( 'P3 saved newrevid=' + n3 );
| |
| var p3b = await getPage( 'Bharatpedia:Bharat Mitra' );
| |
| var d3b = midDiff( p3.content, p3b.content );
| |
| log( 'P3 verify revid=' + p3b.revid + ' size=' + p3b.size + ' added=' + JSON.stringify( d3b.added ) + ' removed=' + JSON.stringify( d3b.removed ) + ' hasSentence=' + ( p3b.content.indexOf( sent ) >= 0 ) );
| |
| } else { log( 'P3 GUARD FAIL do not edit' ); }
| |
| } catch ( e ) { log( 'P3 ERROR ' + ( e && e.message || e ) ); }
| |
| try {
| |
| var f1 = await getPage( 'MediaWiki:Gadget-BharatMitra-revert.js' );
| |
| var f2 = await getPage( 'MediaWiki:Gadget-BharatMitra.js' );
| |
| var f3 = await getPage( 'Bharatpedia:Bharat Mitra' );
| |
| log( 'FINAL P1 revid=' + f1.revid + ' size=' + f1.size );
| |
| log( 'FINAL P2 revid=' + f2.revid + ' size=' + f2.size );
| |
| log( 'FINAL P3 revid=' + f3.revid + ' size=' + f3.size );
| |
| var kr = await api.get( { action: 'query', prop: 'revisions', titles: 'Konkani language', rvprop: 'ids|timestamp', rvlimit: 2, formatversion: 2 } );
| |
| var krp = kr.query.pages[ 0 ]; log( 'KONKANI revs=' + JSON.stringify( krp.revisions ) );
| |
| } catch ( e ) { log( 'FINAL ERROR ' + ( e && e.message || e ) ); }
| |
| log( 'BM DONE' );
| |
| }
| |
| main().catch( function ( e ) { log( 'FATAL ' + ( e && e.stack || e ) ); log( 'BM DONE' ); } );
| |
| } );
| |
| } )(); | |