MediaWiki:Linksweeper/code.js

// Link Sweeper // Deletes backlinks from pages, works from Special:WantedPages / Special:Whatlinkshere $(function {    function sendMsg(msg) {        if ($("#idReport") .length) {           $("#idReport")                .append(msg + "\n");        } else {            console.log(msg);        }    }

function processAction(type, actionConfig, successMsg, failMsg, runMethod) { var mwApi = (new mw.Api); if (actionConfig) { var promise; if (type === "post") { promise = mwApi.post(actionConfig); } else { promise = mwApi.get(actionConfig); }           promise.then(function (data) {                    if (!runMethod) {                        if (!data.error) {                            sendMsg(successMsg);                        } else {                            failMsg = failMsg || 'Failed : ' + data.error.info + ' ';                            sendMsg(failMsg);                        }                    } else {                        runMethod(data.query, successMsg, failMsg);                    }                }) .fail(function (data) {                   console.log(data);                }); }   }    //Code mostly adapted from http://dev.wikia.com/wiki/MediaWiki:AutoEditPages/code.js    function deleteInternalLinks(linkedPage, page) { $.get(mw.util.wikiScript('api'), {               action: 'query',                prop: 'info|revisions',                intoken: 'edit',                titles: page,                rvprop: 'content',                rvlimit: '1',                indexpageids: 'true',                format: 'json'            }, function (response) {                var pages = response.query.pages[response.query.pageids[0]],                    content = pages.revisions[0]['*'],                    origContent = pages.revisions[0]['*'],                    regEx = new RegExp("(\\[\\[.*?\\]\\])", "g"),                    succMsg,                    arrLinks = content.match(regEx),                    linkParts,                    count = 0,                    i = 0;                if (arrLinks && arrLinks.length) {                    for (i = 0; i < arrLinks.length; i += 1) {                        linkParts = arrLinks[i].split("|"); if (linkParts[0].substring(2, linkedPage.length + 2) === linkedPage) { content = content.replace(arrLinks[i], linkedPage); count += 1; }                   }                } else { sendMsg('Link not found in page: ' + page); return false; }               var config = { minor: true, bot: true, format: 'json', summary: 'Deleted link :' + linkedPage, action: 'edit', title: page, basetimestamp: pages.revisions[0].timestamp, startimestamp: pages.starttimestamp, token: pages.edittoken, text: content };               if (content !== origContent) { succMsg = page + " (" + count + " links removed)"; } else if (content !== origContent) { succMsg = page + " (failed, no change)"; }               processAction("post", config, succMsg); })           .fail(function (data) { console.log(data.error); });   }

function getPageLink($currItem) { var specialPage = mw.config.get("wgCanonicalSpecialPageName"); var pageName; if (specialPage === "Whatlinkshere") { pageName = $currItem.siblings("a") .text; } else if (specialPage === "Wantedpages") { pageName = $currItem.siblings(".new") .text || $currItem.siblings(".newcategory") .text; return pageName; }       return pageName; }

function removeBackLinks { var curPageName = getPageLink($(this)); var taskForm = '' + ' ' + ' Page:' + curPageName + ' ' + ' Report:' + ' ' + ' ' + ' '; $.showCustomModal('Backlinks remover', taskForm, {           id: 'backLinkForm',            width: 320,            buttons: [{                message: 'Cancel',                handler: function  {                    $('#backLinkForm')                        .closeModal;                }            }, {                message: 'Remove back links',                defaultButton: true,                handler: function  {                    // Find out Whatlinkshere -> page                    var config = {                        action: 'query',                        list: "backlinks",                        bltitle: curPageName,                        bot: true,                        bllimit: 500                    };                    sendMsg("Processing ...");                    var runMethod = function (data) {                        if (data.backlinks.length > 0) {                            sendMsg("Found " + data.backlinks.length + " link(s)."); var confirmAction = true; if (data.backlinks.length > 20) { confirmAction = confirm("This action will remove links from more than 20 pages. Are you sure?"); }                           if (confirmAction) { var i;                               for (i = 0; i < data.backlinks.length; i += 1) { deleteInternalLinks(curPageName, data.backlinks[i].title); }                               sendMsg("Processing concluded."); } else { sendMsg("Aborted"); }                       } else { sendMsg("No links were found to the page (Walls and Threads are not supported):\n " + curPageName + ""); }                   };                    processAction("get", config, "", "", runMethod); }           }]        });    }    //Initialize    function main {        var specialPage = mw.config.get("wgCanonicalSpecialPageName");        var Html = mw.html;        var removeLinks = Html.element('input', { type: "button", value: "Remove backlinks", class: "classRemoveLinks " });       if (specialPage === "Whatlinkshere") {            $("#mw-whatlinkshere-list li")                .each(function  { $(this) .append(removeLinks); });       } else if (specialPage === "Wantedpages") {            $(".special li")                .each(function  { $(this) .append(removeLinks); });       } else {            return;        }        $('body')            .on("click", ".classRemoveLinks", removeBackLinks);    }    main; });