Jump to content

User:PiRSquared17/thanks.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.
//<nowiki>
/**
 * Feel free to use, but doesn't always work (create a better tool than this if you can!).
 **/
/*global mediaWiki */
/*jshint jquery:true */

(function (mw, $) {
    "use strict";
    mw.loader.load("jquery.tablesorter");
    $(function () {
        var isBlankpage = mw.config.get("wgCanonicalSpecialPageName") === "Blankpage";
        var pageNamePath = mw.config.get("wgPageName").split("/");
        if (isBlankpage && pageNamePath[1] === "thanks") {
            var searchThanker = pageNamePath[2];
            var searchThankee = pageNamePath[3];
            var api = new mw.Api();
            var apiResult = null;
            var apiParams = {
                action: 'query',
                list: 'logevents',
                lelimit: '500',
                letype: 'thanks'
            };
            if (searchThanker) {
                apiParams.leuser = searchThanker;
            }
            if (searchThankee) {
                apiParams.letitle = "User:" + searchThankee;
            }
            api.get(apiParams).done(function (data) {
                apiResult = data.query.logevents;
                var resultTitles = {};
                var titles = $.map(apiResult, function (x) {
                    return x.title;
                });
                var val;
                for (var i = 0; i < titles.length; i++) {
                    val = titles[i];
                    resultTitles[val] = resultTitles[val] ? resultTitles[val] + 1 : 1;
                }
                var resultUsers = {};
                var users = $.map(apiResult, function (x) {
                    return x.user;
                });
                for (i = 0; i < users.length; i++) {
                    val = users[i];
                    resultUsers[val] = resultUsers[val] ? resultUsers[val] + 1 : 1;
                }
                $("#mw-content-text").html("Thanker: " + (searchThanker ? searchThanker : "[all]") + ", " + "thankee: " + (searchThankee ? searchThankee : "[all]"));
                $("#mw-content-text").append("<br />Total number of <!--im in ur html, comentin ur comentz-->results (max 500 currently): " + apiResult.length + "<h2>Thankers (thankful)</h2><table class=\"wikitable sortable\" id=\"thanks-table-users\"><thead><tr><th class=\"headerSort\">User</th><th class=\"headerSort\"># of times</th></tr></thead></table>");
                for (var user in resultUsers) {
                    if (resultUsers.hasOwnProperty(user)) {
                        $("#thanks-table-users").append("<tr><td>" + user + "</td>" + "<td>" + resultUsers[user] + "</td></tr>");
                    }
                }
                $("#mw-content-text").append("<h2>Thankees (thanked)</h2><table class=\"wikitable sortable\" id=\"thanks-table-titles\"><thead><tr><th class=\"headerSort\">Title</th><th class=\"headerSort\"># of times</th></tr></thead></table>");
                for (var title in resultTitles) {
                    if (resultTitles.hasOwnProperty(title)) {
                        $("#thanks-table-titles").append("<tr><td>" + title + "</td>" + "<td>" + resultTitles[title] + "</td></tr>");
                    }
                }
                $("#thanks-table-users").tablesorter();
                $("#thanks-table-titles").tablesorter();
            })
                .fail(function (error) {
                    console.error('thanks.js: MediaWiki API failed :(', error);
                    return {
                        "error": error
                    };
                });
        }
    });
})(mediaWiki, jQuery);