Jump to content

User:Opencooper/domainRedirect.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Opencooper (talk | contribs) at 06:26, 3 December 2018 (add). 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.
/* Determine whether or not the current article has a redirect from its URL pointing to it */

function setup() {
    // If we're not reading an article, do nothing
    if (!(mw.config.get("wgAction") === "view"
          && mw.config.get("wgIsArticle")
          && mw.config.get("wgRevisionId") === mw.config.get("wgCurRevisionId")
          && mw.config.get("wgPageName") !== "Main_Page")) {
        return;
    }
    
    getWikidata();
}

// First try to get the official site off Wikidata
function getWikidata() {
	var wikidataId = mw.config.get( 'wgWikibaseItemId' );
	if (wikidataId === null) {
        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;
        	    console.log("domainRedirect.js: Found Url via wikidata: " + wikidataUrl);
        	    getRedirects(wikidataUrl);
            } else {
            	console.log("domainRedirect.js: Wikidata failed");
            	getInfobox();
            }
        }
    });
}

// Then try getting a website in the infobox
function getInfobox() {
	var infoboxUrl;
	$(".infobox tr").each(function() {
        if ($(this).children("th").text() == "Website") {
            infoboxUrl = $(this).children("td").children("a").attr("href");
            return false;
        }
    });
    
    if (infoboxUrl) {
        console.log("domainRedirect.js: Found Url via infobox: " + infoboxUrl);
        getRedirects(infoboxUrl);
    } else {
        console.log("domainRedirect.js: Infobox failed");
        getExternals();
    }
}

// Finally, try to get it from the external links
function getExternals() {
	// First try getting {{official website}}
	var officialUrl = $(".official-website a").attr("href");
    if (officialUrl) {
        console.log("domainRedirect.js: Found Url via {{official site}}: " + officialUrl);
        getRedirects(officialUrl);
    } else {
        console.log("domainRedirect.js: No official site");
        console.log("domainRedirect.js: No candidates found");
    }
    
    // Then try the external links section and grab the first link we find
    /* var externalUrl = $("h2").has("#External_links").nextUntil("h2").find("li a.external").first().attr("href");
    if (externalUrl) {
        console.log("domainRedirect.js: Found Url via External links section: " + externalUrl);
        getRedirects(externalUrl);
    } else {
        console.log("domainRedirect.js: No external links list found");
    } */
}

function getRedirects(candidateUrl) {
	// API docs: https://www.mediawiki.org/wiki/API:Redirects
    $.ajax({
        url: "https://en.wikipedia.org/w/api.php",
        data: {
            action: "query",
            prop: "redirects",
            format: "json",
            titles: mw.config.get("wgPageName"),
            rdprop: "title",
            rdshow: "!fragment",
            rdlimit: "max"
        },
        success: function(response) {
        	var pageId = mw.config.get("wgArticleId");
        	var redirectObject = response.query.pages[pageId];
        	if (typeof redirectObject.redirects != "undefined") {
        	    redirects = redirectObject.redirects;
        	    checkRedirectExistence(redirects, candidateUrl);
        	} else {
        		console.log("domainRedirect.js: No inbound redirects");
        		displayCandidate(candidateUrl);
        	}
        }
    });
}

function checkRedirectExistence(redirects, candidateUrl) {
	for (var r of redirects) {
		var title = r.title;
		if (!/\./.test(title)) {
			continue;
		}

		console.log("domainRedirect.js: Looking for " + title + " in " + candidateUrl);

		var titleEscaped = mw.RegExp.escape(title);
        var re = new RegExp(titleEscaped, "i");

		if (re.test(candidateUrl)) {
			console.log("domainRedirect.js: Domain redirect present: " + title);
			return;
		}
	}
	
	displayCandidate(candidateUrl);
}

function displayCandidate(candidateUrl) {
	var parser = new URL(candidateUrl);
	candidateDomain = parser.hostname;
	
	console.log("domainRedirect.js: Proposing redirect for Domain: " + candidateDomain);
	$("#contentSub").after("<span class='mw-redirectedfrom'>&nbsp;&nbsp;(Possible domain redirect: " + candidateDomain + ")</span>");
}

$(setup);