Jump to content

User:Zackmann08/scripts/SetupAutoArchive.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.
//<nowiki>
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 parse_text(text) {
	//Check if already configured for auto-archiving
    if (text.search("User:MiszaBot\/config") >=0 || text.search("User:ClueBot III\/ArchiveThis") >= 0 ) {
		alert('Auto archiving already added, aborting.');
		return null;
    }
	// Insert the template at the top of the page.
	var new_text = ("{{subst:" 
		+ "Setup auto archiving|small=yes|notice=yes|counter="
		+ getArchiveCounter(1)
		+ "|talk=yes}}\n" 
		+ text).replace(/{{archive\s*me}}|{{long\s*talk}}|{{Tpcleanup}}|{{talk\s*header}}|{{talk\s*page}}|{{tph}}|{{archive\s*request}}/ig, '');
	return new_text;
}

function callApi() {
    mw.loader.using( "mediawiki.api", function () {
		var api = new mw.Api();
        api.get( {
            prop: 'revisions',
            rvprop: 'content',
            rvlimit: 1,
            titles: mw.config.get( "wgPageName" )
        } ).done( function ( data ) {
            if ( !data.query || !data.query.pages ) return;
            var pageid = Object.getOwnPropertyNames( data.query.pages )[0],
                text = data.query.pages[pageid].revisions[0]["*"];
            var new_text = parse_text(text);
			if (new_text == null) {
				return;
            }
            var summary = '';
            if (mw.config.get('wgNamespaceNumber') == 3) {
				summary =  " Helping you out by setting up [[WP:ARCHIVE|auto-archiving]] using [[User:Zackmann08/scripts/SetupAutoArchive|SetupAutoArchive.js]]. " +
					"This is not required, but is helpful, particularly for [[WP:TALKSIZE|large talk pages]]. You are free to revert if not desired.";	
			} else {
				summary = "Setting up automatic archiving using [[User:Zackmann08/scripts/SetupAutoArchive|SetupAutoArchive.js]]";
			}
            api.postWithEditToken( {
                action: "edit",
                title: mw.config.get( "wgPageName" ),
                summary: summary,
                text: new_text
            } ).done ( function ( data ) {
                if ( data && data.edit && data.edit.result && data.edit.result == 'Success' ) {
                    mw.notify( "Archiving successfully setup! Reloading in 2 seconds...", {type: 'success'}  );
                    window.setTimeout( function () {
                        document.location.reload( true );
                    }, 2000 );
                }
            } );
        } );
    } );
}

function startAutoArchive() {
	var common_list = window.DoNotArchive || [];
	var do_not_archive = ['ElijahPepe', 'EEng'];
	do_not_archive.push(...common_list);
	var pagename = mw.config.get('wgTitle');
	
	if (do_not_archive.includes(pagename)) {
		alert(pagename + ' is on the reject list. Aborting.');
	} else {
		mw.notify( "Setting up archive", {type: 'notice'} );
		callApi();
	}
}


jQuery(document).ready(function($) {
	if (["talk", "wikipedia"].some(function (str) {
		return mw.config.get('wgCanonicalNamespace').toLowerCase().indexOf(str) > -1;
	})) {
		mw.util.addPortletLink(
		"p-cactions", 
		"javascript:startAutoArchive()", 
		"Setup Archive", 
		"autoArchive", 
		"Add auto archiving", 
		"");
    }
});
//</nowiki>