Jump to content

User:Anne drew/SetupAutoArchive.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Anne drew (talk | contribs) at 22:42, 23 February 2018 (var). 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.
function getArchiveCounter(){
	var count=-1;
	var exists=false;
	var url;
	while(!exists && (count<30||confirm("continue?"))){
		count++;
		url = "https://en.wikipedia.org/w/api.php?action=query&titles="+mw.config.get('wgPageName')+"/Archive_"+count+"&format=json&formatversion=2"
		$.get(
		    url,
		    {},
		    function(data) {
		       exists = !data.query.pages["0"].missing;
		    }
		);
	}
	return count;
}

function addAutoArchive(talkheader, archives, algo) {
	algo = algo ? "|age=" + algo : "";
    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|notice=yes|counter=" + getArchiveCounter() + "|talk=" + talkHeader + "|archives=" + archives + algo + "}}\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 algo;
    while(algo === undefined || (!(/[0-9]*/.test(algo)) && algo !== "")){
    	algo=prompt("How many days should a thread be inactive before archiving? (default 31 days)")
    }
    algo = algo ? "&algo=" + algo : "";
    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 + algo;
    window.location.href = editLink;
}

jQuery(document).ready(function($) {
    if (mw.config.get('wgCanonicalNamespace').toLowerCase().indexOf("talk") > -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 algo = params.get("algo");
                    addAutoArchive(talkheader, archives, algo);
                } else {
                    throw "Operation timed out. [SetupAutoArchive.js]";
                }
            }
        } else {
            mw.util.addPortletLink("p-cactions", "javascript:showModal()", "Setup Archive", "autoArchive", "Add auto archiving", "");
        }
    }
});