Jump to content

User:Anne drew/SetupAutoArchive.js

From Wikipedia, the free encyclopedia
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
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.
function getArchiveCounter(current) {
    var url = "https://en.wikipedia.org/w/api.php?action=query&titles=" + mw.config.get('wgPageName') + "/Archive_" + current + "&format=json&formatversion=2"
    var cont = true
    $.ajax({
	     async: false,
	     type: 'GET',
	     url: url,
	     success: function(data) {
	            if (data.query.pages["0"].missing) {
	                cont = false
	            }
	        }
	});
	if(cont){
		return getArchiveCounter(current+1)
	}
    return current>1 ? current-1 : current
}

function addAutoArchive(talkheader, archives, age) {
    age = age ? "|age=" + age : "";
    talkHeader = document.editform.wpTextbox1.value.toLowerCase().indexOf("talk header") === -1 &&
        talkheader === "true" ? "yes" : "no";
    archives = talkHeader === "no" && archives === "true" ? "yes" : "no";
    if ((document.editform.wpTextbox1.value.indexOf("User:MiszaBot/config") === -1 && document.editform.wpTextbox1.value.indexOf("User:ClueBot III/ArchiveThis") === -1) ||
        confirm("It looks like this page is already being automatically archived. Are you sure you want to continue?")) {
        var content = document.editform.wpTextbox1.value;
        contents = [content.substr(0, content.indexOf('==')), content.substr(content.indexOf('=='))];
        document.editform.wpTextbox1.value = (contents[0] + "{{subst:" + "Setup auto archiving|small=yes|notice=yes|counter=" + getArchiveCounter(1) + "|talk=" + talkHeader + "|archives=" + archives + age + "}}\n\n" + contents[1])
            .replace(/{{archiveme}}|{{longtalk}}|{{long talk}}|{{Tpcleanup}}/ig, '');
        document.editform.wpSummary.value = "Set up automatic archiving using [[User:Anne drew Andrew and Drew/SetupAutoArchive|SetupAutoArchive.js]]";
        document.getElementById('wpSave').click();
    } else window.history.back();
}

function showModal() {
    var actionrequired = "";
    while (actionrequired.toLowerCase() !== "yes" && actionrequired.toLowerCase() !== "no") {
        actionrequired = prompt("Is there already a {{talk header}} on the page? (yes/no)");
    }
    actionrequired = actionrequired === "no";
    if (actionrequired) {
        var talkheader = "";
        while (talkheader.toLowerCase() !== "yes" && talkheader.toLowerCase() !== "no") {
            talkheader = prompt("Prepend standard {{talk header}} to page? (yes/no)");
        }
        talkheader = talkheader === "yes";
        var archives = "";
        if (!talkheader) {
            while (archives.toLowerCase() !== "yes" && archives.toLowerCase() !== "no") {
                archives = prompt("Prepend {{archives}} index template to page instead? (yes/no)");
            }
            archives = archives === "yes";
        } else archives = false;
    } else {
        talkheader = false;
        archives = false;
    }
    var age;
    while (age === undefined || (!(/[0-9]*/.test(age)) && age !== "")) {
        age = prompt("How many days should a thread be inactive before archiving?")
    }
    age = age ? "&age=" + age : "";
    var editLink = window.location.protocol + "//" + window.location.host + "/w/index.php?title=" +
        encodeURIComponent(mw.config.get('wgPageName')) + "&action=edit&setupArchive=" + Date.now() +
        "&talkheader=" + talkheader + "&archives=" + archives + age;
    window.location.href = editLink;
}

jQuery(document).ready(function($) {
  if (["talk", "wikipedia"].some(function (str) {
    return mw.config.get('wgCanonicalNamespace').toLowerCase().indexOf(str) > -1
  })) {
    if (document.editform) {
            var params = new URLSearchParams(window.location.search);
            var shouldContinue = params.get("setupArchive");
            if (shouldContinue) {
                shouldContinue = Date.now() - shouldContinue < 10000;
                if (shouldContinue) {
                    var talkheader = params.get("talkheader");
                    var archives = params.get("archives");
                    var age = params.get("age");
                    addAutoArchive(talkheader, archives, age);
                } else {
                    throw "Operation timed out. [SetupAutoArchive.js]";
                }
            }
        } else {
            mw.util.addPortletLink("p-cactions", "javascript:showModal()", "Setup Archive", "autoArchive", "Add auto archiving", "");
        }
    }
});