Jump to content

User:Ilmari Karonen/fixarxivlinks.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Ilmari Karonen (talk | contribs) at 23:15, 13 July 2008 (draft; should work but no real improvement, will rv). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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)/(?: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 (wgCanonicalNamespace == "Special" && wgCanonicalSpecialPageName == "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 = 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 (wgAction == "edit" && /[?&]fixarxivlinks=/.test(window.location.search)) addOnloadHook(function () {
    var editForm = document.forms.editform;
    if (!editForm) return;
    var content1 = editForm.wpTextbox1.value;

    var pdfReURL = "\\bhttp://(?:(?:\\w+\\.|)arxiv\\.org|xxx\\.lanl\\.gov)/(?:abs/|)\\1\\4(?:\\5|)\\b";
    var pdfReInterWiki = "\\[\\[[\\s_]*:?[\\s_]*arXiv[\\s_]*:[\\s_]*\\1\\4(?:\\5|)[\\s_]*(?:\\]\\]|\\|)";
    var pdfReTemplate1 = "\\{\\{[\\s_]*arXiv[\\s_]*\\|\\s*(?:id\\s*=\\s*|)\\1\\4(?:\\5|)\\s*(?:\\}\\}|\\|)";
    var pdfReTemplate2 = "\\{\\{[\\s_]*arXiv[\\s_]*\\|\\s*(?:archive\\s*=\\s*|)\\2\\s*\\|\\s*(?:id\\s*=\\s*|)\\4(?:\\5|)\\s*(?:\\}\\}|\\|)";

    var content2 = content1;
    var content2 = content2.replace(new RegExp("\\b" + arXivPSCacheRegex + "\\b(?=[^\\0]*" + pdfReURL + ")", "ig"), "http://arxiv.org/pdf/$1$4$5");
    var content2 = content2.replace(new RegExp("\\b" + arXivPSCacheRegex + "\\b(?=[^\\0]*" + pdfReInterWiki + ")", "ig"), "http://arxiv.org/pdf/$1$4$5");
    var content2 = content2.replace(new RegExp("\\b" + arXivPSCacheRegex + "\\b(?=[^\\0]*" + pdfReTemplate1 + ")", "ig"), "http://arxiv.org/pdf/$1$4$5");
    var content2 = content2.replace(new RegExp("\\b" + arXivPSCacheRegex + "\\b(?=[^\\0]*" + pdfReTemplate2 + ")", "ig"), "http://arxiv.org/pdf/$1$4$5");

    var absRe = "(?:(\\s*\\|[^{}]*|\\s*)\\|\\s*format\\s*=\\s*(?:PDF|\\[\\[PDF\\]\\])\\s*(?=\\\}\\}|\\s*\\|)|(\\s+Preprint on arXiv)\\s*\\(PDF\\)|)";

    var content3 = content2.replace(new RegExp("\\b" + arXivPSCacheRegex + "\\b" + absRe, "ig"), "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...
});