MediaWiki:Gadget-FlagMovedPageForDeletion.js
Jump to navigation
Jump to search
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.
- This script lacks a documentation subpage. Please create it.
- This script is a part of the
FlagMovedPageForDeletion
gadget (edit definitions)- Description (edit): Add a checkbox when moving a page to flag the old page for immediate deletion (which would otherwise become a redirect).
- Useful links: subpage list • links • redirects
// <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({ ajax: { headers: { "Api-User-Agent": "Gadget developed by [[User:Ioaxxere]]" } } });
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[-1] === 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[0] && moveLogResponse.query.logevents[0].comment)
reason = moveLogResponse.query.logevents[0].comment;
// 3. 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 + "}}"
});
})
// 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 [[:" + targetPage + "]]: " + reason + " ([[MediaWiki:Gadget-FlagMovedPageForDeletion.js|automated]])",
text: "{{d|Page moved to [[:" + targetPage + "]]: " + 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>