User:TheresNoTime/Archiver.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 user script seems to have a documentation page at User:TheresNoTime/Archiver. |
$.when(mw.loader.using(['mediawiki.util', 'mediawiki.api']), $.ready).done(function () {
if (mw.config.get("wgNamespaceNumber") % 2 == 0 && mw.config.get("wgNamespaceNumber") != 4)
// not a talk page and not project namespace
return;
if (mw.config.get("wgNamespaceNumber") == -1)
// is a special page
return;
$("head").append("<style>a.arkylink { font-weight:bold } .arkyhighlight { background-color:#D9E9FF }</style>");
let validSections = new Object();
let fromTos = new Object();
let wikiText = "";
let revStamp;
let startArchiveButton = mw.util.addPortletLink("p-cactions", "#", "ØCA", "pt-oeca", "Enter/exit the archival process", null, null);
let overlay = $(document.createElement("button"));
$(startArchiveButton).click(function (e) {
$(".arkylink").click();
$(".arky").toggle();
$("#archivebutton").toggle();
});
overlay.html("archive all the selected threads")
.attr("id", 'archivebutton')
.css("position", 'sticky')
.css("bottom", 0)
.css("width", '100%')
.css("font-size", '200%');
$(document.body).append(overlay);
overlay.toggle();
overlay.click(function (e) {
let numOfThreads, archiveTarget;
let sections, archiveThis;
let cutOffset, revisedPage;
function cut(s, start, end) {
return s.substr(0, start) + s.substring(end);
}
cutOffset = numOfThreads = 0;
revisedPage = wikiText;
sections = $("a.arkylink").map(function () {
return $(this).attr("data-section");
});
if (!(numOfThreads = sections.length))
return alert("No threads selected, aborting");
let reqRevs = {
action: "query",
titles: mw.config.get('wgPageName'),
rvsection: 0,
prop: "revisions|info",
rvprop: "content",
indexpageids: 1,
dataType: "xml",
format: "xml"
};
$.get(mw.config.get("wgScriptPath") + "/api.php", reqRevs, function (respRevs) {
let content = $(respRevs).find('rev').text();
let reCounter = new RegExp(/\| ?counter|numberstart ?= ?(?<counter>\d+)/, 'i');
let rePrefix = new RegExp(/\| ?archiveprefix ?= ?(?<prefix>.+?)(\n|\|)/, 'i');
let counter = reCounter.exec(content);
if ((counter == null) || (typeof counter == 'undefined')) {
console.debug('Could not find counter');
archiveTarget = prompt("Archiving " + numOfThreads + " threads: where should we move them to? (e.g. Wikipedia:Sandbox/Archive 1)", mw.config.get("wgPageName"));
} else {
console.debug('Found counter');
let archiveNum = counter.groups.counter;
let prefix = rePrefix.exec(content);
if ((prefix == null) || (typeof prefix == 'undefined')) {
console.debug('Could not find prefix');
archiveTarget = prompt(
"Archiving " + numOfThreads + " threads.\n" +
"Automatically found counter.\n" +
"Please confirm...",
mw.config.get("wgPageName") + '/Archive ' + archiveNum
);
} else {
console.debug('Found prefix');
let archivePrefix = prefix.groups.prefix;
archiveTarget = prompt(
"Archiving " + numOfThreads + " threads.\n" +
"Automatically found prefix and counter.\n" +
"Please confirm...",
archivePrefix + ' ' + archiveNum
);
}
}
if (!archiveTarget || archiveTarget == mw.config.get("wgPageName"))
return alert("No archive target selected, aborting");
sections.each(function (i, n) {
revisedPage = cut(
revisedPage,
fromTos[n][0] - cutOffset,
fromTos[n][1] - cutOffset
);
cutOffset += fromTos[n][1] - fromTos[n][0];
});
archiveThis = sections.map(function () {
return wikiText.substring(fromTos[this][0], fromTos[this][1]);
}).toArray().join("");
console.log("archive this:" + archiveThis);
console.log("revised page:" + revisedPage);
if (1) new mw.Api().postWithToken("csrf", {
action: 'edit',
title: mw.config.get("wgPageName"),
text: revisedPage,
summary: "Removed " + numOfThreads + " threads to [[" + archiveTarget + "]] using [[User:TheresNoTime/Archiver.js|script]]",
basetimestamp: revStamp,
starttimestamp: revStamp
})
.done(function (res1) {
alert("Successfully removed threads from talk page");
console.log(res1);
new mw.Api().postWithToken("csrf", {
action: 'edit',
title: archiveTarget,
appendtext: "\n" + archiveThis,
summary: "Added threads from [[" + mw.config.get("wgPageName") + "]] using [[User:TheresNoTime/Archiver.js|script]]"
})
.done(function (res2) {
alert("Successfully added threads to archive page");
})
.fail(function (res2) {
alert("failed to add threads to archive page. manual inspection needed.");
})
.always(function (res2) {
console.log(res2);
window.location.reload();
});
})
.fail(function (res1) {
alert("failed to remove threads from talk page. aborting archive process.");
console.log(res1);
window.location.reload();
});
});
});
new mw.Api().get({
action: 'parse',
page: mw.config.get("wgPageName")
}).done(function (dataShit) {
new mw.Api().get({
action: 'query',
pageids: mw.config.get("wgArticleId"),
prop: ['revisions'],
rvprop: ['content', 'timestamp']
}).done(function (shit) {
let rv;
rv = shit.query.pages[mw.config.get("wgArticleId")].revisions[0];
wikiText = rv["*"];
revStamp = rv['timestamp'];
});
$(dataShit.parse.sections)
.filter(function (i, s) {
return s.index == parseInt(s.index)
})
.each(function (i, s) {
validSections[s.index] = s
});
for (let i in validSections) {
i = parseInt(i);
fromTos[i] = [
validSections[i].byteoffset,
validSections.hasOwnProperty(i + 1) ? validSections[i + 1].byteoffset : Infinity
];
}
$("#mw-content-text").find(":header").find("span.mw-headline").each(function (i, title) {
let header, headerNum, editSection, sectionNumber;
header = $(this).parent();
headerNum = header.prop("tagName").substr(1, 1) * 1; // wtf javascript
editSection = header.find(".mw-editsection"); // 1st child
sectionNumber = header.find(".mw-editsection a:last");
if (sectionNumber[0]) {
// Note: href may not be set.
sectionNumber = sectionNumber.attr("href") && sectionNumber.attr("href").match(/§ion=(\d+)/);
if (sectionNumber)
sectionNumber = sectionNumber[1];
} else // eg <h2>not a real section</h2>
sectionNumber = undefined;
if (validSections.hasOwnProperty(sectionNumber))
editSection[0].innerHTML += " <span class=arky style=display:none><span class=mw-editsection-bracket>[</span><a data-header=" + headerNum + " " + "data-section=" + sectionNumber + " " + "onclick='$(this).closest(\":header\").toggleClass(\"arkyhighlight\");$(this).toggleClass(\"arkylink\");let archivable = $(\".arky a\"); for(let i=archivable.index(this); i<archivable.length; ++i) { if (archivable[i] == this) { continue; } if ($(archivable[i]).attr(\"data-header\") > " + headerNum + " && $(archivable[i]).hasClass(\"arkylink\") != $(this).hasClass(\"arkylink\")) { $(archivable[i]).click(); } else { break; } }'>archive</a><span class=mw-editsection-bracket>]</span></span>";
});
});
});