User:Path slopu/scripts/RBK-assist.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
//User script provide a edit summary for rollback
//By Path slopu, 2019
//Attribution:Nageh, Eizzen
//This script is a combined version of userscripts created by Nageh and Eizzen

//Original code-User:Nageh/rollbackSum.js
$(function(){
 
   var promptSummary = function () {
        var summary = prompt("Enter summary for rollback:", "[[Help:Reverting|Reverted]] edits by [[Special:Contributions/$2|$2]] ([[User talk:$2|talk]]) to last version by $1");
        if (summary == null || summary == "") prompt("Summary is mandatory (or rollback will be cancelled):", "[[Help:Reverting|Reverted]] edits by [[Special:Contributions/$2|$2]] ([[User talk:$2|talk]]) to last version by $1");
        if (summary == null || summary == "") return false;
        if (summary == "[[Help:Reverting|Reverted]] edits by [[Special:Contributions/$2|$2]] ([[User talk:$2|talk]]) to last version by $1") return true;
        this.href = this.href.replace("?", "?summary=" + encodeURIComponent(summary) + "&"); 
    };
 
   $('.mw-rollback-link a').click( promptSummary );
 
})

//Original code-User:Eizzen/PageCreator.js
mw.loader.using(["mediawiki.util", "mediawiki.api"]).then(function () {
    "use strict";

var PageCreator = {
        conf: mw.config.get([
            "skin",
            "wgPageName",
            "wgArticleId",
            "wgUserLanguage",
            "wgNamespaceNumber"
        ]),
        lang: {
            main: "Created by $1",
            on: "on $2",
            talk: "talk",
            contribs: "contribs"
        },

getData: function (callback) {
            var that = this;

            this.api.get({
                action: "query",
                prop: "revisions",
                titles: this.conf.wgPageName,
                rvprop: "ids|timestamp|user|userid",
                rvlimit: "1",
                rvdir: "newer",
                format: "json"
            }).done(function ($data) {
                if (!$data.error) {
                    callback(that, $data);
                }
            });
        },

handleData: function (that, $result) {
            var $data = $result.query.pages[that.conf.wgArticleId].revisions[0];
            var $divElement = mw.html.element("div", {id: "page-creator"});

            var $userNameLink =
                    "<a href='/wiki/User:" + $data.user + "'>" + $data.user +
                    "</a> (<a href='/wiki/User_talk:" + $data.user + "'>" +
                    that.lang.talk + "</a> | <a href='/wiki/Special:Contributions/" +
                    $data.user + "'>" + that.lang.contribs + "</a>)";

            if (jQuery("#last-editor").length) {
                jQuery($divElement).insertBefore("#last-editor");
            } else {
                switch (that.conf.skin) {
                case "vector":
                    jQuery($divElement).insertBefore("#siteSub");
                    break;
                case "monobook":
                    jQuery($divElement).prependTo("#bodyContent");
                    break;
                }
            }

            if (that.options.useTimestamp === true) {
                that.handleTimestamps(that, $data, $userNameLink);
            } else {
                jQuery("#page-creator").html(that.lang.main.replace(/\$1/g, $userNameLink));
            }
        },

 handleTimestamps: function (that, $data, $link) {
            var $time;
            var $formattedCreationDate;
            var $creationDateLink;

            if (that.options.useUTC === true) {
                $time = new Date($data.timestamp).toUTCString();
                $formattedCreationDate = $time.slice(0, 3) + ", " +
                        $time.slice(4, 16) + ", " + $time.slice(17, 25) +
                        " (" + $time.slice(26) + ")";
            } else {
                $time = new Date($data.timestamp).toString();
                $formattedCreationDate = $time.slice(0, 3) + ", " +
                        $time.slice(4, 15) + ", " + $time.slice(16, 24) +
                        " " + $time.slice(34);
            }

            $creationDateLink = "<a href='/?oldid=" + $data.revid + "' target='_blank'>" +
                    $formattedCreationDate + "</a>";

            jQuery("#page-creator")
                .html(
                    that.lang.main.replace(/\$1/g, $link) + " "
                    + that.lang.on.replace(/\$2/g, $creationDateLink)
                );
        },

init: function () {
            if (jQuery("#page-creator").length || window.isPageCreatorLoaded) {
                return;
            }
            window.isPageCreatorLoaded = true;

            this.api = new mw.Api();
            this.options = {
                namespaces: [0, 4, 8, 10],
                excluded: [],
                useTimestamp: true,
                useUTC: true
            };

            jQuery.extend(this.options, window.PageCreatorOptions);

            mw.util.addCSS(
                "#page-creator {" +
                    "line-height: normal;" +
                    "font-size: 12px;" +
                    "font-weight: normal;" +
                "}"
            );

            if (
                jQuery.inArray(this.conf.wgNamespaceNumber, this.options.namespaces) !== -1 &&
                jQuery.inArray(this.conf.wgPageName, this.options.excluded) === -1
            ) {
                this.getData(this.handleData);
            }
        }
    };

    jQuery(document).ready(function () {
        PageCreator.init();
    });
});

//Original code-User:Eizzen/LastEditor.js
mw.loader.using(["mediawiki.util", "mediawiki.api"]).then(function () {
    "use strict";

var LastEditor = {
        conf: mw.config.get([
            "skin",
            "wgPageName",
            "wgArticleId",
            "wgUserLangu$age",
            "wgNamespaceNumber"
        ]),
        lang: {
            main: "Last edited by $1",
            talk: "talk",
            contribs: "contribs",
            diff: "diff",
            minor: "m",
            summary: "Summary",
            ago: "ago",
            second: "second",
            seconds: "seconds",
            minute: "minute",
            minutes: "minutes",
            hour: "hour",
            hours: "hours",
            day: "day",
            days: "days",
            week: "week",
            weeks: "weeks",
            month: "month",
            months: "months",
            year: "year",
            years: "years"
        },

getData: function (callback) {
            var that = this;

            this.api.get({
                action: "query",
                prop: "revisions",
                titles: this.conf.wgPageName,
                rvprop: "timestamp|user|userid|size|parsedcomment|flags",
                rvlimit: "2",
                rvdiffto: "prev",
                format: "json"
            }).done(function ($data) {
                if (!$data.error) {
                    callback(that, $data);
                }
            });
        },

handleData: function (that, $result) {
            var $data = $result.query.pages[that.conf.wgArticleId].revisions[0];
            var $divElement = mw.html.element("div", {id: "last-editor"});

            var $html =
                    "<a href='/wiki/User:" + $data.user + "'>" + $data.user +
                    "</a> (<a href='/wiki/User_talk:" + $data.user + "'>" +
                    that.lang.talk + "</a> | <a href='/wiki/Special:Contributions/" +
                    $data.user + "'>" + that.lang.contribs + "</a>)";

            if (jQuery("#page-creator").length) {
                jQuery($divElement).insertAfter("#page-creator");
            } else {
                switch (that.conf.skin) {
                case "vector":
                    jQuery($divElement).insertBefore("#siteSub");
                    break;
                case "monobook":
                    jQuery($divElement).prependTo("#bodyContent");
                    break;
                }
            }

            if (that.options.showTime) {
                $html += " " + "<span id='last-editor-time'>" +
                        that.convertDate(that, new Date($data.timestamp)) + "</span>";
            }

            if (that.options.showDiff) {
                $html += " " + "<a id='last-editor-diff' href='/?diff=" + $data.diff.to +
                        "' target='_blank'</a>(" + that.lang.diff + ")</a>";

                if ($data.minor === "") {
                    $html += " " + "<span id='last-editor-minor'>[" + that.lang.minor + "]</span>";
                }
            }

            if (that.options.showSummary && $data.parsedcomment) {
                $html += "<br/>" + that.lang.summary + ": " + $data.parsedcomment;
            }

            jQuery("#last-editor").html(that.lang.main.replace(/\$1/g, $html));
        },

convertDate: function (that, $lastRev) {
            var $age;
            var $now;
            var $ageNumber;
            var $ageRemainder;
            var $ageWords;

            $now = new Date();
            $age = $now.getTime() - $lastRev.getTime();

            if ($age < 60000) {
                $ageNumber = Math.floor($age / 1000);
                $ageWords = that.formatDate(that, $ageNumber, that.lang.second, that.lang.seconds);
            } else if ($age < 3600000) {
                $ageNumber = Math.floor($age / 60000);
                $ageWords = that.formatDate(that, $ageNumber, that.lang.minute, that.lang.minutes);
            } else if ($age < 86400000) {
                $ageNumber = Math.floor($age / 3600000);
                $ageWords = that.formatDate(that, $ageNumber, that.lang.hour, that.lang.hours);
                $ageRemainder = Math.floor(($age - $ageNumber * 3600000) / 60000);
            } else if ($age < 604800000) {
                $ageNumber = Math.floor($age / 86400000);
                $ageWords = that.formatDate(that, $ageNumber, that.lang.day, that.lang.days);
            } else if ($age < 2592000000) {
                $ageNumber = Math.floor($age / 604800000);
                $ageWords = that.formatDate(that, $ageNumber, that.lang.week, that.lang.weeks);
            } else if ($age < 31536000000) {
                $ageNumber = Math.floor($age / 2592000000);
                $ageWords = that.formatDate(that, $ageNumber, that.lang.month, that.lang.months);
            } else {

                $ageNumber = Math.floor($age / 31536000000);
                $ageWords = that.formatDate(that, $ageNumber, that.lang.year, that.lang.years);
                $ageRemainder = Math.floor(($age - $ageNumber * 31536000000) / 2592000000);
                if ($ageRemainder) {
                    $ageWords += " " +
                            that.formatDate(that, $ageRemainder, that.lang.month, that.lang.months);
                }
            }

            return $ageWords;
        },

formatDate: function (that, $number, $singular, $plural) {
            return String($number).replace(/\d{1,3}(?=(\d{3})+(?!\d))/g, "$&,") + "\u00a0" +
                    ($number === 1
                ? $singular
                : $plural) + " " + that.lang.ago;
        },

init: function () {
            if (jQuery("#last-editor").length || window.isLastEditorLoaded) {
                return;
            }
            window.isLastEditorLoaded = true;

            this.api = new mw.Api();
            this.options = {
                namespaces: [0, 4, 8, 10],
                excluded: [],
                showTime: true,
                showDiff: true,
                showSummary: true
            };

            jQuery.extend(this.options, window.LastEditorOptions);

            mw.util.addCSS(
                "#last-editor {" +
                    "line-height: normal;" +
                    "font-size: 12px;" +
                    "font-weight: normal;" +
                "}" +
                "#last-editor-minor {" +
                    "font-weight: bold;" +
                "}"
            );

            if (
                jQuery.inArray(this.conf.wgNamespaceNumber, this.options.namespaces) !== -1 &&
                jQuery.inArray(this.conf.wgPageName, this.options.excluded) === -1
            ) {
                this.getData(this.handleData);
            }
        }
    };

    jQuery(document).ready(function () {
        LastEditor.init();
    });
});

//Adding links in tool bar

function countervandalismlinks() {
  mw.util.addPortletLink("p-tb", "https://en.wikipedia.org/wiki/Wikipedia:Administrator_intervention_against_vandalism", "AIV", "tb-AIV","Go to AIV");
  mw.util.addPortletLink("p-tb", "https://en.wikipedia.org/wiki/Wikipedia:Administrators%27_noticeboard/Incidents", "ANI", "tb-ANI","Go to ANI");
  mw.util.addPortletLink("p-tb", "https://en.wikipedia.org/wiki/Wikipedia:Administrators%27_noticeboard/Edit_warring", "AN3", "tb-AN3","Go to AN3");
  mw.util.addPortletLink("p-tb", "https://en.wikipedia.org/wiki/Wikipedia:Requests_for_page_protection", "RPP", "tb-RPP","Go to RPP");
  mw.util.addPortletLink("p-tb", "https://en.wikipedia.org/wiki/Wikipedia:Usernames_for_administrator_attention", "UAA", "tb-UAA","Go to UAA");
  mw.util.addPortletLink("p-tb", "https://en.wikipedia.org/wiki/Wikipedia:Biographies_of_living_persons/Noticeboard", "BLPN", "tb-BLPN","Go to BLPN");
}

$(countervandalismlinks);