Jump to content

User:JPxG/Difformatter.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 URLs to oldids and diffs and etc, and replace them with wikilinks to Special:Permalink or Special:Diff.

// Examples which should work:
// [https://en.wikipedia.org/w/index.php?oldid=1239040342 Le epin link text]
// [https://en.wikipedia.org/w/index.php?oldid=1239040342 ]
// [https://en.wikipedia.org/w/index.php?oldid=1239040342]
// [https://en.wikipedia.org/w/index.php?title=Environmental_impact_of_aviation&diff=next&oldid=1239040342 Le epin link text]
// [https://en.wikipedia.org/w/index.php?title=Environmental_impact_of_aviation&diff=cur&oldid=1239040342 Le epin link text]
// [https://en.wikipedia.org/w/index.php?title=Environmental_impact_of_aviation&diff=prev&oldid=1239040342 Le epin link text]
// [https://en.wikipedia.org/w/index.php?title=Main_Page&oldid=prev&diff=1223300368 Le epin link text]
// [https://en.wikipedia.org/w/index.php?title=Main_Page&oldid=next&diff=1223300368 Le epin link text]
// [https://en.wikipedia.org/w/index.php?title=Environmental_impact_of_aviation&oldid=1239040342 Le epin link text]
// [https://en.wikipedia.org/w/index.php?title=Talk:River&oldid=1236201671 Le epin link text]
// [https://en.wikipedia.org/?diff=624372972 Le epin link text]

// These should NOT work because there's no way to make them work with pure regex or by formatting special links:
// [https://en.wikipedia.org/w/index.php?title=Environmental_impact_of_aviation&direction=next&oldid=1239040342 Le epin link text]
// [https://en.wikipedia.org/w/index.php?title=Environmental_impact_of_aviation&direction=prev&oldid=1239040342 Le epin link text]


function formatLink(txt) {
    try {
        console.log("trying to format diff");
        let diff = 0;
        if(txt.indexOf("Special%3ADiff") !== -1){     diff = 1;}
        if(txt.indexOf("Special:Diff") !== -1){       diff = 1;}
        if(txt.indexOf("Special%3APermalink") !== -1){diff = 1;}
        if(txt.indexOf("Special:Permalink") !== -1){  diff = 1;}
        if(txt.indexOf("diff=") !== -1){              diff = 1;}
        if(txt.indexOf("oldid=") !== -1){             diff = 1;}
        
        if(diff == 0){return txt;}
        // If we don't recognize it as being a diff/permalink URL, just return it unmodified.
        if(txt.indexOf(":Permalink/") !== -1){return txt;}
        if(txt.indexOf(":Diff/") !== -1){     return txt;}
        // For some reason this script loves to chew up already-formatted diff links.
        
        var url = txt.split(" ")[0];
        // txt is something like:
        // https://en.wikipedia.org/w/index.php?oldid=1239040342 Link text
        // url will be:
        // https://en.wikipedia.org/w/index.php?oldid=1239040342
        
        if(url == txt){txt += " link";}
        // if there's no link text

        console.log(txt);
        console.log(url);

        var output = ""
        var done = 0;
        
        // Extra-special snowflake cases that can't be resolved by formatting (Special:Permalink doesn't support /next or /prev suffices)
        // If we see these we just abort trying to process the link at all.
        // https://en.wikipedia.org/w/index.php?title=Environmental_impact_of_aviation&direction=next&oldid=1239040342
        // Gives revid 1239041264!
        // https://en.wikipedia.org/w/index.php?title=Environmental_impact_of_aviation&direction=prev&oldid=1239040342
        // Gives revid 1237707685!
        if ((done == 0) && (url.indexOf("direction=next&oldid=") !== -1)) {
            console.log("not parsing, weird url");
            return txt;
        }
        if ((done == 0) && (url.indexOf("direction=prev&oldid=") !== -1)) {
            console.log("not parsing, weird url");
            return txt;
        }
        
        // Special snowflake case: diff and oldid with IDs for both.
        // https://en.wikipedia.org/w/index.php?title=Death_from_laughter&diff=1232650458&oldid=1218331300
        // if "diff= [digits]" and "oldid = [digits]" are in the link, then format it like "[[Special:Diff/1218331300/1232650458]]"
        const diffOldidRegex = /[?&]diff=(\d+)&oldid=(\d+)/;
        if ((done == 0) && (diffOldidRegex.test(url))) {
            const matches = url.match(diffOldidRegex);
            const diff  = matches[1];
            const oldid = matches[2];
            output = `Special:Diff/${oldid}/${diff}`;
            console.log("parsed 01");
            done = 1;
        }
        
        const oldidDiffRegex = /[?&]oldid=(\d+)&diff=(\d+)/;
        if ((done == 0) && (oldidDiffRegex.test(url))) {
            const matches = url.match(oldidDiffRegex);
            const diff  = matches[1];
            const oldid = matches[2];
            output = `Special:Diff/${oldid}/${diff}`;
            console.log("parsed 02");
            done = 1;
        }
        
        // https://en.wikipedia.org/w/index.php?title=Environmental_impact_of_aviation&diff=next&oldid=1239040342
        // https://en.wikipedia.org/w/index.php?title=Environmental_impact_of_aviation&diff=cur&oldid=1239040342
        // https://en.wikipedia.org/w/index.php?title=Environmental_impact_of_aviation&diff=prev&oldid=1239040342
        
        if ((done == 0) && (url.indexOf("diff=prev&oldid=") !== -1)) {
            output = `Special:Diff/` + url.split("oldid=")[1] + `/prev`
            console.log("parsed 02");
            done = 1;
        }
        if ((done == 0) && (url.indexOf("diff=next&oldid=") !== -1)) {
            output = `Special:Diff/` + url.split("oldid=")[1] + `/next`
            console.log("parsed 03");
            done = 1;
        }
        if ((done == 0) && (url.indexOf("diff=cur&oldid=") !== -1)) {
            output = `Special:Diff/` + url.split("oldid=")[1] + `/cur`
            console.log("parsed 04");
            done = 1;
        }

        // https://en.wikipedia.org/w/index.php?title=Main_Page&oldid=prev&diff=1223300368
        // https://en.wikipedia.org/w/index.php?title=Main_Page&oldid=next&diff=1223300368
        if ((done == 0) && (url.indexOf("oldid=prev&diff=") !== -1)) {
            output = `Special:Diff/` + url.split("diff=")[1]; done = 1;}
        if ((done == 0) && (url.indexOf("oldid=next&diff=") !== -1)) {
            output = `Special:Diff/` + url.split("diff=")[1]; done = 1;}
            
        if ((done == 0) && (url.indexOf("oldid=next&diff=") !== -1)) {
            output = `Special:Diff/` + url.split("diff=")[1]; done = 1;}
            
        // https://en.wikipedia.org/w/index.php?title=Environmental_impact_of_aviation&oldid=1239040342
        // https://en.wikipedia.org/w/index.php?title=Talk:River&oldid=1236201671
        
        if ((done == 0) && (url.indexOf("oldid=") !== -1)) {
            output = `Special:Permalink/` + url.split("oldid=")[1]; done = 1;}
            
        if ((done == 0) && (url.indexOf("diff=") !== -1)) {
            output = `Special:Diff/` + url.split("diff=")[1]; done = 1;}
            
            
        // Weird link formats that, frankly, shouldn't even work, but do somehow -- usually if someone types in a diff URL manually
        
        // https://en.wikipedia.org/?diff=624372972
        if ((done == 0) && (url.indexOf("https://en.wikipedia.org/?diff=") !== -1)) {
            console.log("weird"); output = `Special:Diff/` + url.split("?diff=")[1]; done = 1;}
        // https://en.wikipedia.org/?oldid=624372972
        if ((done == 0) && (url.indexOf("https://en.wikipedia.org/?oldid=") !== -1)) {
            console.log("weird"); output = `Special:Permalink/` + url.split("?oldid=")[1]; done = 1;}
            
        if ((done == 0) && (url.indexOf("https://en.wikipedia.org/w/index.php?diff=") !== -1)) {
            console.log("weird"); output = `Special:Diff/` + url.split("?diff=")[1]; done = 1;}
        if ((done == 0) && (url.indexOf("https://en.wikipedia.org/w/index.php?oldid=") !== -1)) {
            console.log("weird"); output = `Special:Permalink/` + url.split("?oldid=")[1]; done = 1;}
            
        if ((done == 0) && (url.indexOf("action=view&diff=") !== -1)) {
            console.log("weird"); output = `Special:Diff/` + url.split("?diff=")[1]; done = 1;}
        if ((done == 0) && (url.indexOf("action=view&oldid=") !== -1)) {
            console.log("weird"); output = `Special:Permalink/` + url.split("?oldid=")[1]; done = 1;}
            
        if ((done == 0) && (url.indexOf("type=revision&diff=") !== -1)) {
            console.log("weird"); output = `Special:Diff/` + url.split("?diff=")[1]; done = 1;}
        if ((done == 0) && (url.indexOf("type=revision&oldid=") !== -1)) {
            console.log("weird"); output = `Special:Permalink/` + url.split("?oldid=")[1]; done = 1;}
            
        output = output + "|"
        console.log("returning output");
        
        return "[" + txt.replace(url, output).replace("  ", " ").replace("| ", "|") + "]"
        
    } catch(e) {console.log(e); return txt;}
}


function doDifformat() {
    //alert("This part works");
    tbox = String(document.editform.wpTextbox1.value);
    // Get the text from the edit box and store it as "tbox".
    let startIndex, endIndex, prevIndex;
    console.log("doing stuff 00");

    while ((startIndex = tbox.indexOf("[", prevIndex)) !== -1) {
        endIndex = tbox.indexOf("]", startIndex);
        console.log("doing stuff 01");

        // If there's no closing bracket, exit the loop
        if (endIndex === -1) {
        console.log("doing stuff 02");
            break;
        }
        const substring = tbox.substring(startIndex + 1, endIndex);
        var processed = formatLink(substring);
        console.log("doing stuff 03");

		processed = "[" + processed + "]";
		processed = processed.replace("|]]", "|link]]");
        tbox = tbox.substring(0, startIndex) + processed + tbox.substring(endIndex + 1);
        console.log("doing stuff 04");
        prevIndex = startIndex + 2;
    }

    document.editform.wpTextbox1.value = tbox;
    document.editform.wpSummary.value += " Automatically formatted diff/permalink URLs into wikilinks with [[User:JPxG/Difformatter.js]] V1.0";
} // function to replace the stuff with the other stuff

addOnloadHook(function() {
    if (document.editform) {
        mw.util.addPortletLink("p-cactions", "javascript:doDifformat()", "Difformat", "ca-difformat", "Fix diff/permalinks", "");
    }
}); // onloadhook
// </nowiki>