Jump to content

User:Enterprisey/link-deleted-revs.js

From Wikipedia, the free encyclopedia
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
$( 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 = $( "<p>" )
            .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( $( "<p>" )
                    .append( $( "<strong>" )
                        .append( $( "<a>" )
                            .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 = $( "<p>" )
            .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( $( "<p>" )
                    .append( $( "<strong>" )
                        .append( $( "<a>" )
                            .attr( "href", mw.util.getUrl( "Special:Undelete", {
                                target: data.title,
                                timestamp: data.timestamps[oldid],
                            } ) )
                            .text( "You can view it here" ) )
                        .append( "." ) ) );
            } )
        } );
    }
} );