async function getLinksOnPageUnd() {
var params = {
action: 'query',
titles: mw.config.get('wgPageName'),
generator: 'links',
gpllimit: 'max',
format: 'json'
};
var links;
var ret = [];
var api = new mw.Api();
let data = await api.get(params);
links = Object.values(data.query.pages);
links.forEach(i => {
if(i.hasOwnProperty("missing")) {
ret.push(i);
}
});
return ret.map(i => i.title);
}
if (mw.config.get("wgArticleId") > 0) {
(async function() {
if(await getLinksOnPageUnd()) {
const undBatchLink = mw.util.addPortletLink('luna-actions', '#', 'Und-Batch', 'luna-undbatch', 'Undelete all pages linked from this page');
$(undBatchLink).click(() => {
(async function () {
await undeleteBatch();
})();
});
}
})();
}
async function undeleteBatch() {
var list = await getLinksOnPageUnd();
newList = list.map(item => `<li><a class="new" href="/wiki/${item.replace(/ /g, "_")}">${item}</a></li>`);
if (newList.length == 0) {
alert("Luna: There are no pages to undelete.");
return;
}
$("#bodyContent").append(`
<div id="undBatchDialog">
You are about to undelete the following ${newList.length} pages:\n<ol>${newList.join("\n")}</ol>Are you sure you want to do this?<br/>
<textarea id="undBatchReason" name="undBatchReason" placeholder="Reason for undeletion ('$title' will be replaced by the respective page's title)"></textarea>
<input type="checkbox" id="undeleteTalk" name="undeleteTalk" /><label for="undeleteTalk">Undelete associated talk pages</label><br/>
<button type="button" id="undBatchConfirm">Undelete all pages</button>
</div>`);
$("#undBatchDialog").dialog({ title: "Batch undelete", width: "auto", height: "auto" });
$("#undBatchConfirm").click(async () => {
var reason = document.getElementById("undBatchReason").value;
if (!reason) {
alert("You must provide a reason.");
contin = false;
return;
}
let deletePromises = [];
for (const p of list) {
let params = {
action: 'undelete',
undeletetalk: $("#undeleteTalk").prop("checked"),
title: p,
reason: reason.replace(/\$[Tt]itle/gm, p),
tags: "Luna",
format: 'json'
},
api = new mw.Api();
console.log(`Undeleting page: ${p}`);
deletePromises.push(api.postWithToken('csrf', params));
}
await Promise.all(deletePromises);
// (async function() {
// var newerList = await getLinksOnPageUnd();
// if(newerList.length > 0) {
// alert("It looks like not all of the pages were undeleted. This is most likely because you hit the rate limit. Please try again later.");
// }
// })();
mw.notify("Luna:\nThe batch undeletion has been completed. Reloading...");
console.log("All undeletions completed.");
setTimeout(() => window.location.reload(), 2000);
});
}