Note: After publishing, you may have to bypass your browser's cache to see the changes.
- Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
- Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
- Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
(function () {
'use strict';
if ( window.__dcf22 ) { return; }
window.__dcf22 = true;
var TITLE = 'MediaWiki:Gadget-DoubleCatFixer.js';
var EXP_REV = 770934;
var EXP_LEN = 17779;
var EXP_FINAL_LEN = 18415;
var old1 = '\t\t\t}\n\t\t\telse if ( P && P.fixed ) {';
var new1 = '\t\t\t} else if ( P && P.fixed ) {';
var old2 = '\t\tmini.appendChild( miniStop );\n' +
'\t\tvar host = hostElement();\n' +
'\t\tif ( host && host.firstChild ) { host.insertBefore( mini, host.firstChild ); }\n' +
'\t\telse if ( host ) { host.appendChild( mini ); }';
var nb = [];
nb.push( '\t\tmini.appendChild( miniStop );' );
nb.push( '\t\t/* Place the bar where it stays visible on every page type: after' );
nb.push( '\t\t the site notice if there is one, else after the page heading,' );
nb.push( '\t\t else at the top of the content. (Inside the content alone was' );
nb.push( '\t\t not visible on the Main Page.) */' );
nb.push( '\t\tvar placed = false;' );
nb.push( "\t\tvar sn = document.querySelector( '#siteNotice' );" );
nb.push( '\t\tif ( sn && sn.parentNode ) {' );
nb.push( '\t\t\tsn.parentNode.insertBefore( mini, sn.nextSibling );' );
nb.push( '\t\t\tplaced = true;' );
nb.push( '\t\t}' );
nb.push( '\t\tif ( !placed ) {' );
nb.push( "\t\t\tvar fh = document.querySelector( '#firstHeading' );" );
nb.push( '\t\t\tif ( fh && fh.parentNode ) {' );
nb.push( '\t\t\t\tfh.parentNode.insertBefore( mini, fh.nextSibling );' );
nb.push( '\t\t\t\tplaced = true;' );
nb.push( '\t\t\t}' );
nb.push( '\t\t}' );
nb.push( '\t\tif ( !placed ) {' );
nb.push( '\t\t\tvar host = hostElement();' );
nb.push( '\t\t\tif ( host && host.firstChild ) { host.insertBefore( mini, host.firstChild ); }' );
nb.push( '\t\t\telse if ( host ) { host.appendChild( mini ); }' );
nb.push( '\t\t}' );
var new2 = nb.join( '\n' );
function countOcc( text, pat ) { return text.split( pat ).length - 1; }
function out( obj ) {
var d = document.getElementById( 'dcf22' );
if ( !d ) {
d = document.createElement( 'div' );
d.id = 'dcf22';
d.style.cssText = 'border:2px solid #00f;background:#fff;padding:10px;margin:10px 0;white-space:pre-wrap;font-family:monospace;';
document.body.appendChild( d );
}
d.textContent = ( typeof obj === 'string' ) ? obj : JSON.stringify( obj, null, 2 );
}
function done( res ) { out( res ); return res; }
function getPage( api ) {
return api.get( {
action: 'query', prop: 'revisions', titles: TITLE,
rvprop: 'ids|content', rvslots: 'main', formatversion: 2
} ).then( function ( r ) {
var pages = ( r.query && r.query.pages ) || [];
var p = pages[ 0 ] || {};
var rev = ( p.revisions && p.revisions[ 0 ] ) || {};
var content = '';
if ( rev.slots && rev.slots.main && typeof rev.slots.main.content === 'string' ) {
content = rev.slots.main.content;
} else if ( typeof rev.content === 'string' ) {
content = rev.content;
}
return { revid: rev.revid, len: content.length, text: content };
} );
}
function run() {
var res = {
ok: false, alreadyDone: false, error: null,
beforeRevid: null, beforeLen: null,
old1len: old1.length, new1len: new1.length,
old2len: old2.length, new2len: new2.length, new2lines: nb.length,
countOld1: null, countOld2: null,
edit1Net: null, edit1Revid: null,
afterEdit1Revid: null, afterEdit1Len: null, afterEdit1TextMatch: null,
edit2Net: null, computedFinalLen: null, finalLenIs18415: null,
edit2Revid: null, finalRevid: null, finalLen: null,
countNew2InFinal: null, countNew1InFinal: null, countOld1InFinal: null, countOld2InFinal: null,
undoReproduces770934: null, undoLen: null
};
out( 'dcf22 bootstrap started...' );
var api = new mw.Api();
var text0 = '';
return getPage( api ).then( function ( got ) {
res.beforeRevid = got.revid;
res.beforeLen = got.len;
text0 = got.text;
if ( countOcc( text0, new2 ) === 1 && countOcc( text0, new1 ) === 1 && countOcc( text0, old1 ) === 0 && countOcc( text0, old2 ) === 0 ) {
res.alreadyDone = true;
return done( res );
}
if ( got.revid !== EXP_REV ) { res.error = 'before-revid-not-770934'; return done( res ); }
if ( got.len !== EXP_LEN ) { res.error = 'before-len-not-17779'; return done( res ); }
res.countOld1 = countOcc( text0, old1 );
res.countOld2 = countOcc( text0, old2 );
if ( res.countOld1 !== 1 ) { res.error = 'old1-count-not-1'; return done( res ); }
if ( res.countOld2 !== 1 ) { res.error = 'old2-count-not-1'; return done( res ); }
var text1 = text0.split( old1 ).join( new1 );
res.edit1Net = text1.length - text0.length;
if ( res.edit1Net !== -3 ) { res.error = 'edit1-net-not-minus-3'; return done( res ); }
if ( text1.split( new1 ).join( old1 ) !== text0 ) { res.error = 'edit1-not-reversible'; return done( res ); }
return api.postWithToken( 'csrf', {
action: 'edit', title: TITLE, text: text1,
summary: 'Join split line', baserevid: EXP_REV
} ).then( function ( er ) {
res.edit1Revid = er.edit && er.edit.newrevid;
if ( !res.edit1Revid ) { res.error = 'edit1-no-newrevid'; return done( res ); }
return getPage( api ).then( function ( got1 ) {
res.afterEdit1Revid = got1.revid;
res.afterEdit1Len = got1.len;
if ( got1.revid !== res.edit1Revid ) { res.error = 'edit1-revid-mismatch'; return done( res ); }
if ( got1.len !== text1.length ) { res.error = 'edit1-len-mismatch'; return done( res ); }
if ( got1.text !== text1 ) { res.error = 'edit1-text-mismatch'; return done( res ); }
res.afterEdit1TextMatch = true;
if ( countOcc( got1.text, old2 ) !== 1 ) { res.error = 'old2-not-1-after-edit1'; return done( res ); }
var text2 = got1.text.split( old2 ).join( new2 );
res.edit2Net = text2.length - got1.len;
res.computedFinalLen = text2.length;
res.finalLenIs18415 = ( text2.length === EXP_FINAL_LEN );
if ( text2.split( new2 ).join( old2 ) !== got1.text ) { res.error = 'edit2-not-reversible'; return done( res ); }
return api.postWithToken( 'csrf', {
action: 'edit', title: TITLE, text: text2,
summary: 'Place status bar after site notice or heading', baserevid: res.edit1Revid
} ).then( function ( er2 ) {
res.edit2Revid = er2.edit && er2.edit.newrevid;
if ( !res.edit2Revid ) { res.error = 'edit2-no-newrevid'; return done( res ); }
return getPage( api ).then( function ( got2 ) {
res.finalRevid = got2.revid;
res.finalLen = got2.len;
if ( got2.revid !== res.edit2Revid ) { res.error = 'edit2-revid-mismatch'; return done( res ); }
if ( got2.len !== text2.length ) { res.error = 'edit2-len-mismatch'; return done( res ); }
if ( got2.text !== text2 ) { res.error = 'edit2-text-mismatch'; return done( res ); }
res.countNew2InFinal = countOcc( got2.text, new2 );
res.countNew1InFinal = countOcc( got2.text, new1 );
res.countOld1InFinal = countOcc( got2.text, old1 );
res.countOld2InFinal = countOcc( got2.text, old2 );
var undone = got2.text.split( new2 ).join( old2 ).split( new1 ).join( old1 );
res.undoReproduces770934 = ( undone === text0 );
res.undoLen = undone.length;
res.ok = res.undoReproduces770934 && res.countNew2InFinal === 1 && res.countNew1InFinal === 1 && res.countOld1InFinal === 0 && res.countOld2InFinal === 0;
if ( !res.ok ) { res.error = 'final-checks-failed'; }
return done( res );
} );
} );
} );
} );
} ).catch( function ( e ) {
res.error = 'exception: ' + ( e && e.message ? e.message : String( e ) );
return done( res );
} );
}
$( function () { run(); } );
}() );