User:Conrad.Irwin/beerArchiver.js
Note: You may have to bypass your browser’s cache to see the changes. In addition, after saving a sitewide CSS file such as MediaWiki:Common.css, it will take 5-10 minutes before the changes take effect, even if you clear your cache.
- Mozilla / Firefox / Safari: hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (Command-R on a Macintosh);
- Konqueror and Chrome: click Reload or press F5;
- Opera: clear the cache in Tools → Preferences;
- Internet Explorer: hold Ctrl while clicking Refresh, or press Ctrl-F5.
- This script lacks a documentation subpage. Please create it.
- Useful links: root page • root page’s subpages • links • redirects • your own
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<splits.length; i+=2)
{
if (get_time(splits[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(/§ion=([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_title + ']]', 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);
}
}
});