User:PiRSquared17/thanks.js

// /** * 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(" Total number of results (max 500 currently): " + apiResult.length + " Thankers (thankful) ");                for (var user in resultUsers) {                    if (resultUsers.hasOwnProperty(user)) {                        $("#thanks-table-users").append("  " + user + " " + " " + resultUsers[user] + "  ");                    }                }                $("#mw-content-text").append(" Thankees (thanked) ");                for (var title in resultTitles) {                    if (resultTitles.hasOwnProperty(title)) { $("#thanks-table-titles").append(" " + title + " " + " " + resultTitles[title] + "  "); }               }                $("#thanks-table-users").tablesorter; $("#thanks-table-titles").tablesorter; })               .fail(function (error) { console.error('thanks.js: MediaWiki API failed :(', error);                   return {                        "error": error                    };                }); }   }); })(mediaWiki, jQuery);