Jump to content

User:Ilmari Karonen/fixarxivlinks.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.
// FIX LINKS TO TEMPORARY arxiv.org/PS_cache URLS TO PERMANENT ONES:
// SEE ALSO: http://arxiv.org/help/arxiv_identifier_for_services

// To use, go to Special:Linksearch, search for "arxiv.org/PS_cache" or "arxiv.org/ftp" (or same with "www." prefix) and click the "FIX" links.

var arXivPSCacheRegex = "http://(?:(?:\\w+\\.|)arxiv\\.org|xxx\\.lanl\\.gov|arxiv\\d+\\.library\\.cornell\\.edu)/(?:PS_cache|ftp)/(?:arxiv/|(([\\-.\\w]*)/))(?:pdf|ps|papers)/(\\d{4})/(\\3\\.?\\d+)(v\\d+|)\\.(?:ps|pdf)";
// contains 5 capturing parens:
//   $1 = old-style group (if any)
//   $2 = old-style group without trailing slash (if any)
//   $3 = year and month
//   $4 = (within-group) article ID
//   $5 = version, if any
// replace with "$1$4$5" to get the full arXiv ID

if (mw.config.get('wgCanonicalNamespace') == "Special" && mw.config.get('wgCanonicalSpecialPageName').toLowerCase() == "linksearch") addOnloadHook(function () {
    // add quick edit links to Special:Linksearch
    var ol = document.getElementsByTagName("ol")[0];
    if (!ol) return;

    var re = new RegExp("^" + arXivPSCacheRegex + "$", "i");
    var seen = new Object();

    for (var li = ol.firstChild; li; li = li.nextSibling) {
        if (li.nodeType != 1 || li.tagName.toLowerCase() != "li") continue;

        var links = li.getElementsByTagName("a");
        if (links.length != 2) continue;
        if (!re.test(links[0].href)) continue;

        var title = links[1].title;
        if (seen[title]) continue;
        seen[title] = true;

        var editlink = document.createElement("a");
        editlink.href = mw.config.get('wgScript') + "?title=" + encodeURIComponent(title) + "&action=edit&fixarxivlinks=1";
        editlink.title = "Fix arXiv/PS_cache links in " + title;
        editlink.appendChild(document.createTextNode("FIX"));

        li.insertBefore(editlink, li.firstChild); 
        li.insertBefore(document.createTextNode(" ("), editlink); 
        li.insertBefore(document.createTextNode(") "), editlink.nextSibling); 
    }
});

if (mw.config.get('wgAction') == "edit" && /[?&]fixarxivlinks=/.test(window.location.search)) addOnloadHook(function () {
    var editForm = document.forms.editform;
    if (!editForm) return;
    var pdfRe = new RegExp("\\b" + arXivPSCacheRegex + "\\b(?=[^\\0]*\\bhttp://(?:(?:\\w+\\.|)arxiv\\.org|xxx\\.lanl\\.gov|arxiv\\d+\\.library\\.cornell\\.edu)/(?:abs/|)\\1\\4(?:\\5|)\\b|[^\\0]*\\[\\[[\\s_]*:?[\\s_]*arXiv[\\s_]*:[\\s_]*\\1\\4(?:\\5|)[\\s_]*(?:\\]\\]|\\|)|[^\\0]*\\{\\{(?:\\s*msg:|)[\\s_]*(?:Template[\\s_]*:[\\s_]*|)arXiv[\\s_]*\\|\\s*(?:(?:archive\\s*=\\s*|)\\2\\s*\\|\\s*(?:id\\s*=\\s*|)\\4(?:\\5|)|(?:id\\s*=\\s*|)\\1\\4(?:\\5|))\\s*(?:\\}\\}|\\|))", "ig");
    var absRe = new RegExp("\\b" + arXivPSCacheRegex + "\\b(?:(\\s*\\|[^{}]*|\\s*)\\|\\s*format\\s*=\\s*(?:PDF|\\[\\[PDF\\]\\])\\s*(?=\\\}\\}|\\s*\\|)|(\\s+Preprint on arXiv)\\s*\\(PDF\\)|)", "ig");
    var content1 = editForm.wpTextbox1.value;
    var content2 = content1.replace(pdfRe, "http://arxiv.org/pdf/$1$4$5");
    var content3 = content2.replace(absRe, "http://arxiv.org/abs/$1$4$5$6$7");
    if (content1 == content3) return;
    editForm.wpTextbox1.value = content3;
    var summaryType = (content1 == content2 ? "abstract page with download links" : content2 == content3 ? "stable URLs" : "stable URL and/or abstract page");
    editForm.wpSummary.value = "Fixing temporary \"arxiv.org/PS_cache\" and obsolete \"arxiv.org/ftp\" URLs to link to " + summaryType + " instead (with [[User talk:Ilmari Karonen/fixarxivlinks.js|script assistance]])";
    editForm.wpMinoredit.checked = true;
    editForm.wpDiff.click();  // Not feeling lucky yet...
});