User:Enterprisey/link-deleted-revs.js

$( function () {

function getTimestamps( revids, getPrev ) {

return new mw.Api().get( {

action: "query",

prop: "deletedrevisions",

revids: revids,

drvprop: "ids|timestamp",

formatversion: "2",

} ).then( function ( data ) {

var timestamps = {};

data.query.pages[0].deletedrevisions.forEach( function ( rev ) {

timestamps[rev.revid] = rev.timestamp;

} );

if( getPrev ) {

var parents = data.query.pages[0].deletedrevisions.map( function ( rev ) {

return rev.parentid;

} ).join( "|" );

return getTimestamps( parents, false ).then( function ( data ) {

for( var revid in data.timestamps ) {

timestamps[revid] = data.timestamps[revid];

}

return {

title: data.title,

timestamps: timestamps,

};

} );

} else {

return $.when( {

title: data.query.pages[0].title,

timestamps: timestamps,

} );

}

} ).catch( function ( e ) { console.error( e ); } );

}

if( window.location.search.indexOf( "diff=" ) >= 0 &&

!document.querySelector( "table.diff" ) ) {

var status = $( "

" )

.text( "Loading diff link..." )

.appendTo( "#mw-content-text" );

mw.loader.using( [ "mediawiki.api", "mediawiki.util" ], function () {

var diff = window.location.search.match( /diff=(\d+|\w+)?/ )[1];

var oldid = window.location.search.match( /oldid=(\d+|\w+)?/ )[1];

var revids = "";

if( Number( oldid ) ) {

revids += oldid;

}

if( Number( diff ) ) {

if( revids ) {

revids += "|";

}

revids += diff;

}

var getPrev = !oldid;

getTimestamps( revids, getPrev ).then( function ( data ) {

var oldidTimestamp = oldid && data.timestamps[oldid];

if( !oldid ) {

for( var revid in data.timestamps ) {

if( revid !== diff ) {

oldidTimestamp = data.timestamps[revid];

}

}

}

status.empty().append( $( "

" )

.append( $( "" )

.append( $( "" )

.attr( "href", mw.util.getUrl( "Special:Undelete", {

target: data.title,

timestamp: oldidTimestamp,

diff: data.timestamps[diff] || diff,

} ) )

.text( "You can view it here" ) )

.append( "." ) ) );

} );

} );

} else if( window.location.search.indexOf( "oldid=" ) >= 0 &&

document.getElementsByClassName( "mw-revision" ).length === 0 &&

!document.querySelector( "table.diff" ) ) {

var status = $( "

" )

.text( "Loading diff link..." )

.appendTo( "#mw-content-text" );

mw.loader.using( [ "mediawiki.api", "mediawiki.util" ], function () {

var oldid = window.location.search.match( /oldid=(\d+)/ )[1];

getTimestamps( oldid ).then( function ( data ) {

status.empty().append( $( "

" )

.append( $( "" )

.append( $( "" )

.attr( "href", mw.util.getUrl( "Special:Undelete", {

target: data.title,

timestamp: data.timestamps[oldid],

} ) )

.text( "You can view it here" ) )

.append( "." ) ) );

} )

} );

}

} );