User:Guywan/Scripts/FileMoverHelper-dev.js
// <nowiki>
$(() =>
{
if(mw.config.get("wgCanonicalNamespace") != "User") return;
console.log("FMH is setting up.");
var api = new mw.Api();
var source = mw.config.get("wgPageName");
var link = RegExp(`(\\[\\[)${source}((\\|.*?)?\\]\\])`, "g");
// Add portlet link.
$("#p-cactions ul").append("<li id='ca-fmh'><a href='#' title='Rename this file and redirect all links to the new name'>FMH</a></li>");
$("#ca-fmh").on("click", e =>
{
console.log("FMH is doing something.");
// (1) Get destination.
var destination = prompt("Please enter the destination of this move");
if(!destination) return;
// (2) Move the page.
// api.post(
// {
// "action": "move",
// "from": source,
// "to": `{$destination}`,
// "reason": "More suitable name",
// "movetalk": true,
// "token": mw.user.tokens.get("csrfToken")
// })
// .fail(result => { mw.notify("Failed to move!", {type: "error"}); })
// .done(() =>
// {
// Replace page content with reporting area.
$(".mw-parser-output")[0].innerHTML = "<div id='fmh-reports'></div>";
var reports = document.getElementById("fmh-reports");
reports.insertAdjacentHTML("afterend", `<p style='color:green'>Moved ${source} to ${destination}</p>`);
// (4) Get a list of pages. Currently, only gets the first 500.
api.get(
{
"action": "query",
"list": "backlinks",
"bltitle": source,
"bllimit": "max"
})
.fail(result => { mw.notify("Failed to retrieve links to this file!", {type: "error"}); })
.done(data =>
{
for(var i = 0; i < data.query.backlinks.length; i++)
{
redirectFileLinks(data.query.backlinks[i].title, destination, i, data.query.backlinks.length - 1);
}
});
// });
});
function redirectFileLinks(pagename, dest, number, end)
{
var reports = document.getElementById("fmh-reports");
api.post(
{
"action": "edit",
"title": pagename,
"text": getWikitext(pagename).replace(link, (match, p1, p2) => { return p1 + dest + p2; }),
"summary": "Redirecting links to new name ([[User:Guywan/Scripts/FileMoverHelper-dev.js|Dev test]])",
"token": mw.user.tokens.get("csrfToken")
})
.done(() =>
{
reports.insertAdjacentHTML("afterend", `<p style='color:green'>Finished redirecting links in <a href='/wiki/${pagename}'>${pagename}</a> ...</p>`);
if(number == end)
{
reports.insertAdjacentHTML("afterend", "<p style='color:green;font-size:1.5em'>Operation completed! You may refresh or leave this page</p>");
}
})
.fail(() =>
{
reports.insertAdjacentHTML("afterend", `<p style='color:red'>Failed to redirect links in ${pagename} ...</p>`);
});
}
function getWikitext(page)
{
return $.ajax(
{
url: mw.util.getUrl(page) + "?action=raw",
data: "text",
async: false
}).responseText;
}
});
// </nowiki>
Content Disclaimer
Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.
- The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
- There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
- It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
- Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
- Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.