MediaWiki:Gadget-FlagMovedPageForDeletion.js

Hello, you have come here looking for the meaning of the word MediaWiki:Gadget-FlagMovedPageForDeletion.js. In DICTIOUS you will not only get to know all the dictionary meanings for the word MediaWiki:Gadget-FlagMovedPageForDeletion.js, but we will also tell you about its etymology, its characteristics and you will know how to say MediaWiki:Gadget-FlagMovedPageForDeletion.js in singular and plural. Everything you need to know about the word MediaWiki:Gadget-FlagMovedPageForDeletion.js you have here. The definition of the word MediaWiki:Gadget-FlagMovedPageForDeletion.js will help you to be more precise and correct when speaking or writing your texts. Knowing the definition ofMediaWiki:Gadget-FlagMovedPageForDeletion.js, as well as those of other words, enriches your vocabulary and provides you with more and better linguistic resources.
// <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(``).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({ ajax: { headers: { "Api-User-Agent": "Gadget developed by ]" } } });
	let targetPage = document.querySelector("#movepage-newlink").textContent;
	let reason = "(no reason provided)";
	let pageExists = false;

	// 1. Make sure the moved page still exists (sometimes it gets deleted immediately)
	actionAPI.get({
		action: "query",
		titles: pageToFlagDelete,
		format: "json"
	}).then(r => {
		if (r.query.pages === undefined) {
			pageExists = true;
			// 2. Find out the edit summary through the logevents API.
			return actionAPI.get({
				action: "query",
				letitle: pageToFlagDelete,
				list: "logevents",
				letype: "move",
				format: "json"
			}).then(moveLogResponse => {
				if (moveLogResponse.query.logevents && moveLogResponse.query.logevents.comment)
					reason = moveLogResponse.query.logevents.comment;
				
				// 3. Make the edit.
				return actionAPI.postWithToken("csrf", {
					action: "edit",
					title: pageToFlagDelete,
					summary: "Flagging for deletion after moving the page to ]: " + reason + " (])",
					text: "{{d|Page moved to ]: " + reason + "}}"
				});
			})
			// 4. If there's an error, try again on /documentation.
			.catch(error => {
				return actionAPI.postWithToken("csrf", {
					action: "edit",
					title: pageToFlagDelete + "/documentation",
					summary: "Flagging for deletion after moving the page to ]: " + reason + " (])",
					text: "{{d|Page moved to ]: " + reason + "}}"
				});
			})
		}
	})
	// 5. Display a message on success.
	.then(() => {
		let a = document.createElement("a");
		a.href = mw.util.getUrl(pageToFlagDelete);
		a.textContent = pageToFlagDelete;

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

		mw.notify(message);
	});
}
// </nowiki>