User:Ilmari Karonen/fixarxivlinks.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
This user script seems to have a documentation page at User:Ilmari Karonen/fixarxivlinks. |
// 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...
});