Jump to content

User:JJPMaster/und-batch.js

From Wikibooks, open books for an open world
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.
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(() => {
			var reason = document.getElementById("undBatchReason").value;
			if (!reason) {
				alert("You must provide a reason.");
				contin = false;
				return;
			}
			
			list.forEach(p => {
				params = {
					action: 'undelete',
					undeletetalk: $("#undeleteTalk").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() {
		// 	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...");
		setTimeout(() => window.location.reload(), 2000);
	});
}