User:Guywan/Scripts/FileMoverHelper-dev.js

// $( => {   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("FMH");    $("#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 = " ";            var reports = document.getElementById("fmh-reports");            reports.insertAdjacentHTML("afterend", `Moved ${source} to ${destination} `);            // (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 (Dev test)", "token": mw.user.tokens.get("csrfToken") })       .done( => {           reports.insertAdjacentHTML("afterend", `Finished redirecting links in ${pagename} ... `); if(number == end) {               reports.insertAdjacentHTML("afterend", "Operation completed! You may refresh or leave this page "); }       })        .fail( => {       	reports.insertAdjacentHTML("afterend", `Failed to redirect links in ${pagename} ... `); });   }

function getWikitext(page) {		return $.ajax(		{			url: mw.util.getUrl(page) + "?action=raw",			data: "text",			async: false		}).responseText; } }); //