Jump to content

User:Quarl/directredirect.js

From Wikipedia, the free encyclopedia
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.
// [[User:Quarl/directredirect.js]] - automatically fix double redirects
//   Annotates Special:Whatlinkshere pages with [fix] and [fix all] buttons
//   that asynchronously change the target to a direct redirect.

// quarl 2006-01-31 initial version

// requires: wikipage.js, wikiedit.js, util.js

//<pre><nowiki>

directredirect = new Object();

directredirect.annotatePage = function() {
    var contentDiv = document.getElementById('bodyContent');
    var links = contentDiv.getElementsByTagName('a');
    directredirect.wpTarget = null;
    var redirects = directredirect.redirects = [];

    for (var i in links) {
        var link = links[i];
        if (!link.href) continue;
        if (!link.href.match(/redirect=no/)) continue;

        var wp = new WikiPage(link.href);
        if (!directredirect.wpTarget) {
            // first "redirect=no" link, this is the target
            directredirect.wpTarget = wp;
        } else {
            // redirect page; is it a double (or worse) redirect?

            if (link.parentNode.parentNode.parentNode == contentDiv) {
                // if the parent is contentDiv, then it's a first-level
                // redirect.
            } else {
                // double redirect; add button
                var url = "javascript:directredirect.fix(" + string_quote_escape(wp.page) + ")";
                var button = document.createElement('span');
                button.innerHTML = ' [<a href="'+url+'"><b>fix</b></a>]';
                add_after(link.nextSibling, button);
                var o = { wp: wp, link: link, button: button };
                redirects[wp.page] = o;
                redirects.push(o);
            }
        }
    }

    if (redirects.length) {
        var p = contentDiv.getElementsByTagName('p')[0];
        var button = document.createElement('blockquote');
        button.innerHTML = 'There are '+redirects.length+' indirect redirects. [<a href="javascript:directredirect.fixall()">fix all</a>]';
        add_before(p, button);
    }
}

directredirect.fix = function(pagename) {
    directredirect.fix0( directredirect.redirects[pagename] );
}

directredirect.fixall = function() {
    for (var i in directredirect.redirects) {
        directredirect.fix0( directredirect.redirects[i] );
    }
}

directredirect.fix0 = function(redirect) {
    if (!redirect) { alert ("## internal error 8e747379-406c-4bcf-b85f-770c855d9db1"); return; }
    redirect.button.innerHTML = ' [<b>fixing</b>: downloading...]';
    redirect.wp.getEditorAsync(directredirect.edit, redirect);
}

directredirect.edit = function(editor, redirect) {
    redirect.button.innerHTML = ' [<b>fixing</b>: submitting...]';
    var pagename = directredirect.wpTarget.page;
    if (!pagename) { alert ("## internal error 4cf6e5b6-5ed4-4d97-98d3-6288eb8e4f39"); return; }
    var redir = '#REDIRECT [['+pagename+']]';
    editor.wpTextbox1 = redir;
    editor.wpSummary = 'Direct redirect '+redir;
    editor.wpMinoredit = true;
    editor.submitAsync(null, directredirect.editCompleted, redirect);
}

directredirect.editCompleted = function(req, redirect) {
    if (req.status != 200) {
        alert( "Error submitting new redirect content!" );
        return;
    }

    redirect.button.innerHTML = ' [<b>fixed</b>]';
}

directredirect.load = function() {
    if (wikiPage.page != 'Special:Whatlinkshere') return;
    directredirect.annotatePage();
}

$(directredirect.load);

//</nowiki></pre>