User:Conrad.Irwin/beerArchiver.js

if (wgPageName == 'Wiktionary:Beer_parlour') addOnloadHook(function {

function archive_page(text) {       var page;

text.replace(/([0-9]+) *(January|Febuary|March|April|May|June|July|August|September|October|November|December) *(20[0-9][0-9]) *\(UTC\)/, function(m, day, month, year)       {            page = "Wiktionary:Beer_parlour_archive/"+year+"/"+month;        });

return page; }

function get_time(text) {       var time = [0,0,0,0,0]; var months = ['', 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];

text.replace(/([0-9]{2}):([0-9]{2}), ([0-9]+) *(January|Febuary|March|April|May|June|July|August|September|October|November|December) *(20[0-9][0-9]) *\(UTC\)/, function(match, hour, minute, day, month, year) {

time = [year, months.indexOf(month), day, hour, minute].join(",");

});       return time;    }

function new_archive_text(destination, text) {       return "{" + "{archive notice}}\n" + "{" + "{Wiktionary:Beer parlour/timeline}}\n\n" + "=" + destination.replace(/.*\/(\d+)\/(.*)/,function(m, year, month) { return month + " " + year; }) + "=\n" + text; }

function get_title(text) {       return text.split("==")[1]; }

function modify_archive(original, new_text) {       new_text = "\n\n" + new_text; var splits = original.split(/(\n==[^=][^\n=]*[^=]==\n)/); var seek_date = get_time(new_text); for (var i=2; i seek_date) {               splits.splice(i-1,0,new_text) return splits.join(""); }       }        splits.push(new_text); return splits.join(""); }

function post_save(heading, section) {       if (arguments.callee['called' + section]) {           do            { var next = heading.nextSibling; heading.parentNode.removeChild(heading); heading = next; }           while (heading && !{'h1':1,'h2':1}[heading.nodeName.toLowerCase]); }       arguments.callee['called' + section] = true; }

function archive(heading, spans) { return function {

var editlink = heading.getElementsByTagName('a')[0];

editlink.href.replace(/&section=([0-9]+)/, function (match, section)       {            heading.appendChild(newNode('Loading...'));            JsMwApi.page(wgPageName).edit({rvsection: section}, function(text, save_callback) {

var destination = archive_page(text); var section_title = get_title(text);

if (confirm("Archive section to " + destination + "\n\nWait for 'Archive Saved' and 'Beer Parlour Saved' after pressing OK.")) {                   JsMwApi.page(archive_page(text)).edit(function (archive_text, archive_save) {

var archive_to_save; if (archive_text == "") archive_to_save = new_archive_text(destination, text); else archive_to_save = modify_archive(archive_text, text);

save_callback("", {summary: "Archiving to " + destination + '', section: section}, function { alert('Beer Parlour Saved'); post_save(heading,section); });

archive_save(archive_to_save, {summary: "Archiving" + section_title}, function {alert('Archive Saved'); post_save(heading,section);}); });               }            });        });

} }

var headings = document.getElementById('bodyContent').getElementsByTagName('h2');

for (var i=0; i<headings.length; i++) {       var heading = headings[i]; var spans = heading.getElementsByTagName('span'); var span = spans[0]; if (span && span.className == "editsection") {           var id = spans[spans.length-1].id; span.insertBefore(               newNode('span', " · ", newNode('a', {href: '#'+id, click: archive(heading)}, 'archive')),            span.lastChild); }   } });