User:Opencooper/domainRedirect.js

// Determines whether or not the current article has a redirect from its URL pointing to it

// License: CC0

// FIXME: isn't case insensitive; see The Psycho Ex-Wife // FIXME: Doesn't work with TLDs like .co.uk

function setup { // If we're not reading an article, do nothing if (!(mw.config.get("wgAction") === "view" && mw.config.get("wgIsArticle") && !location.search.split('oldid=')[1] && !mw.config.get("wgIsMainPage"))) { return; }   // Make sure we have somewhere to put result if (!$("#contentSub").length) { return; }   getWikidata; }

// First try to get the official site off Wikidata function getWikidata { var wikidataId = mw.config.get( 'wgWikibaseItemId' ); if (wikidataId === null) { getInfobox; return; }

// API docs: https://www.wikidata.org/w/api.php?action=help&modules=wbgetclaims var url; $.ajax({       url: "https://www.wikidata.org/w/api.php",        data: {            action: "wbgetclaims",            entity: wikidataId,            property: "P856",            languages: "en",            format: "json",            origin: "*"        },        success: function(response) {            if (typeof response.claims.P856 != "undefined") {                wikidataUrl = response.claims.P856[0].mainsnak.datavalue.value;                retrievalMethod = "Wikidata";                checkRedirect(wikidataUrl);            } else {                // console.log("domainRedirect.js: Wikidata failed");                getInfobox;            }        }    }); }

// Then try getting a website in the infobox function getInfobox { var infoboxUrl;

// Get undefined infoboxUrl = $(".infobox .url a").last.attr("href"); if (infoboxUrl) { retrievalMethod = "undefined"; checkRedirect(infoboxUrl); return; } else { // console.log("domainRedirect.js: Infobox undefined failed"); }

// Get |website= $(".infobox tr").each(function {       if ($(this).children("th").text == "Website") {            infoboxUrl = $(this).children("td").children("a").attr("href");            return false;        }    }); if (infoboxUrl) { retrievalMethod = "website="; checkRedirect(infoboxUrl); } else { // console.log("domainRedirect.js: Infobox website= failed"); getExternals; } }

// Finally, try to get it from the external links function getExternals { // First try getting var officialUrl = $(".official-website a").first.attr("href"); if (officialUrl) { retrievalMethod = ""; checkRedirect(officialUrl); return; } else { // console.log("domainRedirect.js: No official site"); }   // Then try the external links section and the first link that says official var externalUrl = $("h2").has("#External_links").nextUntil("h2") .find("li a.external").first; if (externalUrl && /Official/i.test(externalUrl.text)) { var externalHref = externalUrl.attr("href"); retrievalMethod = "§External_links"; checkRedirect(externalHref); } else { // console.log("domainRedirect.js: No external links list found"); console.info("domainRedirect.js: No candidates found"); } }

function checkRedirect(candidateUrl) { var parser = new URL(candidateUrl); var candidateDomain = parser.hostname; candidateDomain = candidateDomain.replace(/^w+\d*\./, ""); // get rid of www // API docs: https://www.mediawiki.org/wiki/API:Query var apiUrl = location.origin + "/w/api.php"; $.ajax({       url: apiUrl,        data: {            action: "query",            format: "json",            titles: candidateDomain,        },        success: function(response) {            if (response.query.pages["-1"]) {                displayCandidate(candidateUrl);            } else {                console.info("domainRedirect.js: Domain redirect already present: " + candidateDomain);               return;            }        }    }); }

function displayCandidate(candidateUrl) { var pageTitle = mw.config.get("wgTitle"); pageTitle = encodeURIComponent(pageTitle).replace(/'/g, "%27");

var parser = new URL(candidateUrl); var candidateDomain = parser.hostname; candidateDomain = candidateDomain.replace(/^w+\d*\./, ""); // get rid of www

$("#contentSub").append(" ");

var domainType = "domain"; if (candidateDomain.split(".").length-1 >= 2) { domainType = "subdomain"; } else if (parser.pathname != "/") { domainType = "subpage"; }

if (domainType != "domain") { console.info("domainRedirect.js: Ignoring " + domainType + " redirect: "                    + candidateDomain); $("#domainRedirect").hide; }

var createUrl = "https://en.wikipedia.org/w/index.php?title=" + candidateDomain + "&action=edit"; var editSummary = "Create redirect (using "                     + "domainRedirect.js)"; var editSummaryEncoded = encodeURIComponent(editSummary); createUrl += "&preload=User:Opencooper/domainRedirectPreloads.js&preloadparams[]=" + pageTitle + "&summary=" + editSummaryEncoded;

var domainMarkup = "" + candidateDomain + ""; var candidateText = "Possible " + domainType + " redirect: " + domainMarkup; var createLink = " " + "+";

var visitLink = ""; if (candidateUrl.replace(/\/$/, "") != parser.origin) { visitLink = " " + "→"; }

$("#domainRedirect").append(" (" + candidateText + createLink + visitLink + ")"); }

var retrievalMethod; $(setup);