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.
/* <nowiki> */
/* Bharat Mitra: redirect special pages. On Special:DoubleRedirects it adds a
one click fix for every double redirect. On Special:BrokenRedirects it adds
batch selection with speedy deletion nomination for all users and batch
deletion for administrators. Shown only when Bharat Mitra is enabled. */
( function () {
'use strict';
var BM = window.BharatMitra;
var started = false;
function isSysop() {
return ( mw.config.get( 'wgUserGroups' ) || [] ).indexOf( 'sysop' ) !== -1;
}
function specialName() {
return mw.config.get( 'wgCanonicalSpecialPageName' ) || '';
}
function queryPageAll( name ) {
var out = [];
function step( cont ) {
var params = {
action: 'query', list: 'querypage', qppage: name,
qplimit: 500, formatversion: 2
};
if ( cont ) { params.qpcontinue = cont; }
return BM.core.api().get( params ).then( function ( r ) {
var qp = ( r.query && r.query.querypage ) || {};
( qp.results || [] ).forEach( function ( x ) { out.push( x ); } );
if ( r.continue && r.continue.qpcontinue ) {
return step( r.continue.qpcontinue );
}
return out;
} );
}
return step( null );
}
function finalTarget( title ) {
return BM.core.api().get( {
action: 'query', titles: title, redirects: 1, formatversion: 2
} ).then( function ( r ) {
var pages = ( r.query && r.query.pages ) || [];
return pages.length ? pages[ 0 ].title : title;
} );
}
function wait( ms ) {
return new $.Deferred( function ( d ) { setTimeout( d.resolve, ms ); } ).promise();
}
function pageLink( title ) {
return $( '<a>' ).attr( 'href', mw.util.getUrl( title ) ).text( title );
}
function makePanel( heading ) {
var host = document.querySelector( '#mw-content-text' );
if ( !host || document.getElementById( 'bm-specialredirects' ) ) { return null; }
var box = $( '<div>' ).attr( 'id', 'bm-specialredirects' )
.attr( 'style', 'border:1px solid #a2a9b1;background:#f8f9fa;padding:10px 14px;margin:0 0 14px 0;font-size:95%;' );
box.append( $( '<div>' ).append( $( '<strong>' ).text( heading ) ) );
var status = $( '<div>' ).attr( 'style', 'margin:6px 0;' ).text( 'Loading…' );
box.append( status );
host.insertBefore( box[ 0 ], host.firstChild );
return { box: box, status: status };
}
function makeTable( headers ) {
var table = $( '<table>' ).addClass( 'wikitable' ).attr( 'style', 'width:100%;background:#fff;' );
var tr = $( '<tr>' );
headers.forEach( function ( h ) { tr.append( $( '<th>' ).text( h ) ); } );
table.append( tr );
return table;
}
/* ---------------- Double redirects ---------------- */
function runDouble( panel ) {
queryPageAll( 'DoubleRedirects' ).then( function ( rows ) {
if ( !rows.length ) {
panel.status.text( 'Double redirects: 0. There is nothing to fix.' );
return;
}
panel.status.text( 'Double redirects: ' + rows.length + '. The final target of each page is found live when the table loads.' );
var table = makeTable( [ 'Page', 'Final target', 'Status' ] );
panel.box.append( table );
var rowInfo = [];
var pending = rows.length;
rows.forEach( function ( r, idx ) {
var tr = $( '<tr>' );
tr.append( $( '<td>' ).append( pageLink( r.title ) ) );
var targetCell = $( '<td>' ).text( '…' );
var statusCell = $( '<td>' ).text( '' );
tr.append( targetCell, statusCell );
table.append( tr );
rowInfo[ idx ] = { title: r.title, target: null, statusCell: statusCell };
finalTarget( r.title ).then( function ( fin ) {
rowInfo[ idx ].target = fin;
targetCell.text( fin );
} ).catch( function () {
targetCell.text( 'Could not be found' );
} ).then( function () {
pending--;
if ( !pending ) { addFixButton( panel, rowInfo ); }
} );
} );
} ).catch( function () {
panel.status.text( 'The double redirect list could not be loaded.' );
} );
}
function addFixButton( panel, rowInfo ) {
var btn = $( '<button>' ).attr( 'type', 'button' ).addClass( 'bm-btn bm-primary' )
.attr( 'style', 'margin-top:10px;' ).text( 'Fix all double redirects (' + rowInfo.length + ')' );
panel.box.append( btn );
var armed = false;
btn.on( 'click', function () {
if ( !armed ) {
armed = true;
btn.text( 'Confirm: fix all ' + rowInfo.length + ' double redirects' );
panel.status.text( 'Each page will be changed to point directly to its final target. Click the button again to confirm.' );
return;
}
btn.prop( 'disabled', true );
var fixed = 0, failed = 0, skipped = 0;
var chain = $.Deferred().resolve().promise();
rowInfo.forEach( function ( info ) {
chain = chain.then( function () {
if ( !info.target || info.target === info.title ) {
skipped++; info.statusCell.text( 'Skipped' ); return null;
}
info.statusCell.text( 'Working…' );
return BM.core.getPage( info.title ).then( function ( p ) {
if ( !p.exists ) { skipped++; info.statusCell.text( 'Skipped: page missing' ); return null; }
var lines = p.content.split( '\n' );
if ( !/^\s*#\s*REDIRECT/i.test( lines[ 0 ] ) ) {
skipped++; info.statusCell.text( 'Skipped: not a redirect' ); return null;
}
lines[ 0 ] = '#REDIRECT [[' + info.target + ']]';
return BM.core.savePage( info.title, lines.join( '\n' ),
'Fixed a double redirect (using [[Bharatpedia:Bharat Mitra|Bharat Mitra]])', p.revid )
.then( function () { fixed++; info.statusCell.text( 'Fixed' ); } );
} ).catch( function () {
failed++; info.statusCell.text( 'Failed' );
} ).then( function () { return wait( 300 ); } );
} );
} );
chain.then( function () {
var msg = 'Done. Fixed: ' + fixed + ', skipped: ' + skipped + ', failed: ' + failed +
'. This special page list is cached by the wiki, so fixed pages can stay listed here for some time.';
panel.status.text( msg );
mw.notify( msg, { type: failed ? 'warn' : 'success' } );
} );
} );
}
/* ---------------- Broken redirects ---------------- */
function targetText( r ) {
var d = r.databaseResult || {};
var t = String( d.rd_title || '' ).replace( /_/g, ' ' );
if ( !t ) { return ''; }
if ( d.rd_fragment ) { t += '#' + String( d.rd_fragment ).replace( /_/g, ' ' ); }
return t;
}
function runBroken( panel ) {
queryPageAll( 'BrokenRedirects' ).then( function ( rows ) {
if ( !rows.length ) {
panel.status.text( 'Broken redirects: 0. There is nothing to do.' );
return;
}
panel.status.text( 'Broken redirects: ' + rows.length + '. Select the pages, then nominate them for speedy deletion' +
( isSysop() ? ' or, as an administrator, delete them directly.' : '.' ) );
var table = makeTable( [ '', 'Page', 'Target (does not exist)', 'Status' ] );
panel.box.append( table );
var items = [];
rows.forEach( function ( r ) {
var cb = $( '<input>' ).attr( 'type', 'checkbox' ).addClass( 'bm-broken-check' );
var statusCell = $( '<td>' ).text( '' );
var tr = $( '<tr>' );
tr.append( $( '<td>' ).append( cb ) );
tr.append( $( '<td>' ).append( pageLink( r.title ) ) );
tr.append( $( '<td>' ).text( targetText( r ) ) );
tr.append( statusCell );
table.append( tr );
items.push( { title: r.title, cb: cb, statusCell: statusCell } );
} );
var controls = $( '<div>' ).attr( 'style', 'margin-top:10px;display:flex;gap:8px;flex-wrap:wrap;align-items:center;' );
var selAll = $( '<button>' ).attr( 'type', 'button' ).addClass( 'bm-btn' ).text( 'Select all' );
var selNone = $( '<button>' ).attr( 'type', 'button' ).addClass( 'bm-btn' ).text( 'Select none' );
selAll.on( 'click', function () { items.forEach( function ( i ) { i.cb.prop( 'checked', true ); } ); } );
selNone.on( 'click', function () { items.forEach( function ( i ) { i.cb.prop( 'checked', false ); } ); } );
controls.append( selAll, selNone );
var notifyLabel = $( '<label>' ).attr( 'style', 'font-size:13px;' );
var notifyCb = $( '<input>' ).attr( 'type', 'checkbox' );
notifyLabel.append( notifyCb, document.createTextNode( ' Notify creators (nomination only)' ) );
controls.append( notifyLabel );
var nomBtn = $( '<button>' ).attr( 'type', 'button' ).addClass( 'bm-btn bm-primary' )
.text( 'Nominate selected for speedy deletion' );
controls.append( nomBtn );
var delBtn = null;
if ( isSysop() ) {
delBtn = $( '<button>' ).attr( 'type', 'button' ).addClass( 'bm-btn' )
.attr( 'style', 'background:#b03a2e;border-color:#b03a2e;color:#fff;font-weight:bold;' )
.text( 'Delete selected (admin)' );
controls.append( delBtn );
}
panel.box.append( controls );
function selected() {
return items.filter( function ( i ) { return i.cb.prop( 'checked' ); } );
}
function setBusy( b ) {
nomBtn.prop( 'disabled', b );
if ( delBtn ) { delBtn.prop( 'disabled', b ); }
selAll.prop( 'disabled', b ); selNone.prop( 'disabled', b );
}
var armedNom = false, armedDel = false;
function disarm() {
armedNom = false; armedDel = false;
nomBtn.text( 'Nominate selected for speedy deletion' );
if ( delBtn ) { delBtn.text( 'Delete selected (admin)' ); }
}
items.forEach( function ( i ) { i.cb.on( 'change', disarm ); } );
nomBtn.on( 'click', function () {
var sel = selected();
if ( !sel.length ) { mw.notify( 'Select at least one page first.', { type: 'warn' } ); return; }
if ( !armedNom ) {
armedNom = true;
nomBtn.text( 'Confirm: nominate ' + sel.length + ' selected' );
panel.status.text( 'These pages will be nominated for speedy deletion (criterion G8). Click the button again to confirm.' );
return;
}
disarm();
setBusy( true );
var done = 0, skipped = 0, failed = 0;
var chain = $.Deferred().resolve().promise();
sel.forEach( function ( item ) {
chain = chain.then( function () {
item.statusCell.text( 'Working…' );
return BM.core.getPage( item.title ).then( function ( p ) {
if ( !p.exists ) { skipped++; item.statusCell.text( 'Skipped: page missing' ); return null; }
if ( /\{\{\s*(?:subst:)?db/i.test( p.content ) ) {
skipped++; item.statusCell.text( 'Skipped: already nominated' ); return null;
}
var tag = '{{subst:db-g8}}';
var nl = p.content.indexOf( '\n' );
var firstLine = nl === -1 ? p.content : p.content.slice( 0, nl );
var newContent;
if ( /^\s*#\s*REDIRECT/i.test( firstLine ) && nl !== -1 ) {
newContent = firstLine + '\n' + tag + p.content.slice( nl );
} else {
newContent = tag + '\n' + p.content;
}
return BM.core.savePage( item.title, newContent,
'Nominated for speedy deletion (G8) (using [[Bharatpedia:Bharat Mitra|Bharat Mitra]])', p.revid )
.then( function () {
done++; item.statusCell.text( 'Nominated' );
if ( !notifyCb.prop( 'checked' ) ) { return null; }
var block = '{{subst:Db-notice|' + BM.core.cleanParam( item.title ) + '}}\n' + BM.SIG;
return BM.core.notifyCreator( item.title, block,
'Speedy deletion notice (using [[Bharatpedia:Bharat Mitra|Bharat Mitra]])' );
} );
} ).catch( function () {
failed++; item.statusCell.text( 'Failed' );
} ).then( function () { return wait( 300 ); } );
} );
} );
chain.then( function () {
setBusy( false );
var msg = 'Done. Nominated: ' + done + ', skipped: ' + skipped + ', failed: ' + failed + '.';
panel.status.text( msg );
mw.notify( msg, { type: failed ? 'warn' : 'success' } );
} );
} );
if ( delBtn ) {
delBtn.on( 'click', function () {
var sel = selected();
if ( !sel.length ) { mw.notify( 'Select at least one page first.', { type: 'warn' } ); return; }
if ( !armedDel ) {
armedDel = true;
delBtn.text( 'Confirm: delete ' + sel.length + ' selected' );
panel.status.text( 'These pages will be deleted. This cannot be undone from this page. Click the button again to confirm.' );
return;
}
disarm();
setBusy( true );
var done = 0, failed = 0;
var chain = $.Deferred().resolve().promise();
sel.forEach( function ( item ) {
chain = chain.then( function () {
item.statusCell.text( 'Working…' );
return BM.core.api().postWithToken( 'delete', {
action: 'delete', title: item.title,
reason: 'Broken redirect to a page that does not exist (using [[Bharatpedia:Bharat Mitra|Bharat Mitra]])',
format: 'json'
} ).then( function () {
done++; item.statusCell.text( 'Deleted' ); item.cb.prop( 'checked', false );
} ).catch( function () {
failed++; item.statusCell.text( 'Failed' );
} ).then( function () { return wait( 700 ); } );
} );
} );
chain.then( function () {
setBusy( false );
var msg = 'Done. Deleted: ' + done + ', failed: ' + failed +
'. This special page list is cached by the wiki, so deleted pages can stay listed here for some time.';
panel.status.text( msg );
mw.notify( msg, { type: failed ? 'warn' : 'success' } );
} );
} );
}
} ).catch( function () {
panel.status.text( 'The broken redirect list could not be loaded.' );
} );
}
function check() {
if ( started ) { return; }
if ( mw.config.get( 'wgNamespaceNumber' ) !== -1 ) { return; }
var name = specialName();
if ( name !== 'BrokenRedirects' && name !== 'DoubleRedirects' ) { return; }
started = true;
$( function () {
if ( name === 'DoubleRedirects' ) {
var p1 = makePanel( 'Bharat Mitra: double redirect fixer' );
if ( p1 ) { runDouble( p1 ); }
} else {
var p2 = makePanel( 'Bharat Mitra: broken redirect tools' );
if ( p2 ) { runBroken( p2 ); }
}
} );
}
BM.register( 'specialredirects', {
open: check,
check: check
} );
}() );
/* </nowiki> */