User:PiRSquared17/thanks.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
Documentation for this user script can be added at User:PiRSquared17/thanks. |
//<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);