User:Zackmann08/scripts/SetupAutoArchive.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump.
This code will be executed when previewing this page.
This code will be executed when previewing this page.
This user script seems to have a documentation page at User:Zackmann08/scripts/SetupAutoArchive.
//<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>