User:JJPMaster/d-batch.js
Appearance
Note: After saving, changes may not occur immediately. Click here to learn how to bypass your browser's cache.
- Mozilla / Firefox / Safari: hold down Shift while clicking Reload, or press Ctrl-Shift-R (Cmd-Shift-R on Apple Mac);
- Internet Explorer: hold Ctrl while clicking Refresh, or press Ctrl-F5;
- Konqueror: simply click the Reload button, or press F5;
- Opera users may need to completely clear their cache in Tools→Preferences.
Warning: Malicious code can compromise your account. Page preview will cause your web browser to execute this page's content as code under some skins, including Monobook. If you have any questions about any code you plan to add, you can ask at the appropriate reading room. |
Documentation for this user script can be added at User:JJPMaster/d-batch. |
importScript( 'User:JJPMaster/d-batch/links.js' ); // Backlink: [[User:JJPMaster/d-batch/links.js]]
importScript( 'User:JJPMaster/d-batch/cat.js' ); // Backlink: [[User:JJPMaster/d-batch/cat.js]]
importScript( 'User:JJPMaster/d-batch/prefix.js' ); // Backlink: [[User:JJPMaster/d-batch/prefix.js]]
async function getSubpages(page) {
var params = {
action: 'query',
list: 'allpages',
apprefix: `${new mw.Title(page).getMain()}/`,
apnamespace: new mw.Title(page).getNamespaceId(),
aplimit: 'max',
format: 'json'
},
api = new mw.Api();
try {
const data = await api.get(params);
return data.query.allpages.map(i => i.title);
}
catch {
return "No subpages of " + page;
}
}
async function getRedirects(page) {
var params = {
action: 'query',
prop: 'redirects',
titles: page,
rdprop: 'title',
rdlimit: 'max',
format: 'json'
},
api = new mw.Api();
try {
const data = await api.get(params);
return Object.values(data.query.pages)[0].redirects.map(i => i.title);
}
catch {
return "No redirects to " + page;
}
}
async function deleteBatch(arr) {
newList = arr.map(item => `<li><a href="/wiki/${item.replace(/ /g, "_")}">${item}</a></li>`);
if (newList.length == 0) {
alert("Luna: There are no pages to delete.");
return;
}
$("#bodyContent").append(`
<div id="dBatchDialog">
You are about to delete the following ${newList.length} pages:\n<ol>${newList.join("\n")}</ol>Are you sure you want to do this?<br/>
<textarea id="dBatchReason" name="dBatchReason" placeholder="Reason for deletion ('$title' will be replaced by the respective page's title)"></textarea>
<input type="checkbox" id="deleteTalk" name="deleteTalk" /><label for="deleteTalk">Delete associated talk pages</label><br/>
<input type="checkbox" id="deleteSubs" name="deleteSubs" /><label for="deleteSubs">Delete subpages</label><br/>
<input type="checkbox" id="deleteReds" name="deleteReds" /><label for="deleteReds">Delete redirects</label><br/>
<button type="button" id="dBatchConfirm">Delete all pages</button>
</div>`);
$("#dBatchDialog").dialog({ title: "Batch delete", width: "auto", height: "auto" });
$("#dBatchConfirm").click(() => {
var reason = document.getElementById("dBatchReason").value;
if (!reason) {
alert("You must provide a reason.");
return;
}
arr.forEach(p => {
params = {
action: 'delete',
deletetalk: $("#deleteTalk").prop("checked"),
title: p,
reason: reason.replace(/\$[Tt]itle/gm, p),
tags: "Luna",
format: 'json'
},
api = new mw.Api();
api.postWithToken( 'csrf', params ).done( function ( data ) {
console.log( data );
});
(async function () {
if ($("#deleteSubs").prop("checked")) {
var subpages = await getSubpages(p);
try {
subpages.forEach(i => {
params = {
action: 'delete',
deletetalk: $("#deleteTalk").prop("checked"),
title: i,
reason: "Deleted together with the parent page with reason: " + reason.replace(/\$[Tt]itle/gm, p),
tags: "Luna",
format: 'json'
},
api = new mw.Api();
api.postWithToken( 'csrf', params ).done( function ( data ) {
console.log( data );
});
});
}
catch (e) {
if (e instanceof TypeError) {
console.log("There are no subpages of " + p);
}
}
}
if ($("#deleteReds").prop("checked")) {
var redirects = await getRedirects(p);
try {
redirects.forEach(i => {
params = {
action: 'delete',
deletetalk: $("#deleteTalk").prop("checked"),
title: i,
reason: "Deleted together with the redirect target with reason: " + reason.replace(/\$[Tt]itle/gm, p),
tags: "Luna",
format: 'json'
},
api = new mw.Api();
api.postWithToken( 'csrf', params ).done( function ( data ) {
console.log( data );
});
});
}
catch (e) {
if (e instanceof TypeError) {
console.log("There are no redirects to " + p);
}
}
}
})();
});
mw.notify("Luna:\nThe batch deletion has been completed. Reloading...");
setTimeout(() => window.location.reload(), 2000);
});
// if(arr.length > 0) {
// alert("It looks like not all of the pages were deleted. This is most likely because you hit the rate limit. Please try again later.");
// }
}