User:Nx/Scripts/Autoarchive.js

/* Doesn't work since the upgrade, DON'T USE A script to automate archiving of old threads. THIS IS STILL UNDER DEVELOPMENT AND HAS BUGS. You have been warned.

importScript("User:Nx/Scripts/Shared.js");

if (typeof rkive_maxsize == "undefined") rkive_maxsize = 30000;

var rkive_newnum = 0; var rkive_links = []; var rkive_working = false;

function rkive_setup { var rtitle = ""; //try to find using template:talkpage var as = document.getElementById("content").getElementsByTagName("a"); for (var i=0;i<as.length;++i) {   if (as[i].className == "new" && as[i].title.indexOf(wgTitle) != -1 && as[i].title.indexOf("Archive") != -1) {     rtitle = urlencode(as[i].title); } }  var num = parseInt(rtitle.substr(rtitle.indexOf("Archive")+"Archive".length,rtitle.length),10); //if not found, try api if (rtitle == "" || isNaN(num)) {   var subs; var req = sajax_init_object; req.open("GET",wgServer+wgScriptPath+"/api.php?action=query&format=json&list=allpages&apprefix=" + urlencode(wgTitle) + "&apnamespace=" + wgNamespaceNumber + "&aplimit=500",false) req.send(null); if (req.status == 200) {     var response = eval('(' + req.responseText + ')'); subs = response["query"]["allpages"]; } else { alert("Error " + req.status + " occurred while retrieving list of subpages."); }   //find greatest number var maxnum = 0; var maxi = 0; for (var i=0;i maxnum) {       maxi = i;        maxnum = tmp; }   }    num = maxnum+1; rtitle = urlencode(wgPageName + "/Archive" + num); }

rkive_newnum = num; }

function rkive_checksize { //if there's no Archive yet, skip the size checking var num = rkive_newnum; if (num > 1) {  var last = urlencode(wgPageName + "/Archive" + (num-1)); var e = new Autoedit(last); var lastcont = e.getPage; //if the latest Archive is not big enough yet, use it   if (lastcont.length < rkive_maxsize) {     return (num-1); } else { rkive_newnum = num+1; return num; } }  rkive_newnum = num+1; return num; }

function rkive_section(sect) { if (rkive_working) return; rkive_working = true; var targetarch = rkive_checksize; var ethis = new Autoedit(wgPageName); var earch = new Autoedit(wgPageName + "/Archive" + targetarch); var thisraw = ethis.getPage;

var splitted = thisraw.split(/(==.*==)/);

var sections = []; var sectionheaders = []; var thissections = [];

var j=0; sections[0] = splitted[0]; thissections[0] = splitted[0]; sectionheaders[0] = ""; for (var i=1;i thissections[thissections.length-1].lastIndexOf(" ") || thissections[thissections.length-1].lastIndexOf(""))) {       sections[j] += splitted[i]; thissections[j] += splitted[i]; } else { sections.push(""); sectionheaders.push(splitted[i]); thissections.push(splitted[i]); ++j }   } else { sections[j] += splitted[i]; thissections[j] += splitted[i]; } }

var sectionheader = sectionheaders[sect]; var section = sections[sect]; if (section[0] == "\n") { section = section.substring(1); } if (section[0] == "\n") { section = section.substring(1); } thissections.splice(sect,1); var newcont = thissections.join(""); var sectionheader = sectionheader.match(/(==)(.*)\1/)[2]; ethis.setOncomplete(function {   earch.setOncomplete(function { rkive_links[sect-1].style.display = "none"; rkive_working = false; window.location.reload; });   earch.replaceSection("new",section,sectionheader);  }); ethis.replacePage(newcont,'Archiving section "'+ sectionheader +'" using Autoarchiver'); }

function onc(stuff,i) { stuff.onclick = function {rkive_section(i);} }

function rkive_init { if (!(document.getElementById("ca-addsection"))) return; var bodyContent = document.getElementById("bodyContent"); var es = getElementsByClassName(bodyContent,"*","editsection"); var rkivesection; var j=0; for (var i=0;i<es.length;++i) {   if (es[i].parentNode.tagName == "H2") {     rkivesection = document.createElement("span"); rkivesection.className = "editsection"; rkivesection.style.cursor = "pointer"; onc(rkivesection,j+1); ++j; rkivesection.innerHTML = "[archive]"; es[i].parentNode.insertBefore(rkivesection,es[i]); rkive_links.push(rkivesection); } }  rkive_setup; }

//addOnloadHook(rkive_init);