MediaWiki:Gadget-FlagMovedPageForDeletion.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.

// <nowiki>
// Idea: set a local storage item to remember which page got moved. 
// Then, on the "other side", make an automated edit which replaces the redirect with {{d}}.

// Setup. Don't add the button if the user already has permissions to delete the page immediately.
if (document.querySelector(".movepage-wrapper") && !document.querySelector("#wpLeaveRedirect")) {
	let firstCheckbox = document.querySelector(".movepage-wrapper .oo-ui-fieldLayout-align-inline");
	firstCheckbox.insertAdjacentHTML("afterEnd", `<div id="flag-delete-field" class="oo-ui-layout oo-ui-labelElement oo-ui-fieldLayout oo-ui-fieldLayout-align-inline"><div class="oo-ui-fieldLayout-body"><span class="oo-ui-fieldLayout-field"><span id="flag-delete" class="oo-ui-widget oo-ui-widget-enabled oo-ui-inputWidget oo-ui-checkboxInputWidget"><input type="checkbox" tabindex="0" id="flag-delete-checkbox" class="oo-ui-inputWidget-input"><span class="oo-ui-checkboxInputWidget-checkIcon oo-ui-widget oo-ui-widget-enabled oo-ui-iconElement-icon oo-ui-icon-check oo-ui-iconElement oo-ui-labelElement-invisible oo-ui-iconWidget oo-ui-image-invert"></span></span></span><span class="oo-ui-fieldLayout-header"><label for="flag-delete-checkbox" class="oo-ui-labelElement-label">Flag the source page for deletion</label></span></div></div>`);
	
	let flagDeleteCheckbox = document.querySelector("#flag-delete-checkbox");
	let submitButton = document.querySelector(".movepage-wrapper button");

	submitButton.addEventListener("click", () => {
		if (flagDeleteCheckbox.checked) {
			let oldPageTitle = document.querySelector(`[name="wpOldTitle"]`).value;
			localStorage.setItem("flagDelete", oldPageTitle);
		}
	});
}

// Make the edit after the move has completed.
let sourcePageElem = document.querySelector("#movepage-oldlink");
let pageToFlagDelete = localStorage.getItem("flagDelete");

if (pageToFlagDelete && sourcePageElem && sourcePageElem.textContent === localStorage.getItem("flagDelete")) {
	localStorage.removeItem("flagDelete");

	let actionAPI = new mw.Api();
	let targetPage = document.querySelector("#movepage-newlink").textContent;

	// Find out the edit summary through the logevents API.
	actionAPI.get({
		action: "query",
		letitle: pageToFlagDelete,
		list: "logevents",
		letype: "move",
		format: "json"
	}).then(moveLogResponse => {
		let reason = "(no reason provided)";
		if (moveLogResponse.query.logevents[0] && moveLogResponse.query.logevents[0].comment)
			reason = moveLogResponse.query.logevents[0].comment;
		console.log(moveLogResponse);
		console.log("Reason: " + reason);
		return reason;
	}).then(reason => {
		// Make the edit.
		return actionAPI.postWithToken("csrf", {
			action: "edit",
			title: pageToFlagDelete,
			summary: "Flagging for deletion after moving the page to [[:" + targetPage + "]]: " + reason + " ([[MediaWiki:Gadget-FlagMovedPageForDeletion.js|automated]])",
			text: "{{d|Page moved to [[:" + targetPage + "]]: " + reason + "}}"
		});
	}).then(() => {
		let a = document.createElement("a");
		a.href = mw.util.getUrl(pageToFlagDelete);
		a.textContent = pageToFlagDelete;

		let message = document.createElement("span");
		message.append(a, " has been successfully flagged for deletion.");

		mw.notify(message);
	}, error => {
		console.error("Edit failed with error: " + error);
	});
}
// </nowiki>