User:The JoTS/wikia.js

// TopicBlockLog (v0.9.0 - testing) // by User:The JoTS

var TBL_GROUP_DICT = { "roblox-en": [ "roblox-apocalypse-rising", "arashi-games", "roblox-arcane-adventures", "roblox-atla", "azuremines", "basewars", "blox-cards", "boss-fighting-stages-rebirth", "bribbleco", "build-a-boat-for-treasure", "cityroblox", "deathhunt", "dreadrblx", "roblox-eclipsis", "roblox-elemental-battlegrounds", "fantastic-frontier-roblox", "rblx-flood-escape-2", "roblox-galaxy-official", "hexaria", "in-plain-sight", "rblx-jailbreak", "jkrf", "lumber-tycoon-2", "madstudios", "mechacubes", "meepcity", "roblox-medieval-warfare-reforged", "minershaven", "monsterislands", "murder-mystery-2", "roblox-phantom-forces", "pfs", "brickbronze", "pokemon-fighters-ex-roblox", "polyguns-roblox", "prisonroyale", "project-pokemon", "r2da", "r2dremastered", "super-adventure-rpg", "rolve", "rblx-snow-shoveling-simulator", "rblx-snowball-fighting-simulator", "space-mining-tycoon", "strife-official", "survive-the-disasters-2-wiki", "swordburst2", "saob", "theofficialconquerorswikia", "tpt2", "vehicle-simulator-roblox", "welcome-to-bloxburg", "wfyb", "w-r", "roblox-windows-error-simulator", "roblox-zombie-attack" ] };

// Run only on Special:Block if (mw.config.get('wgCanonicalSpecialPageName') !== 'Block'		|| $(".permissions-errors").length) throw "Page is not Special:Block or user does not have permission to view it.";

var DELAY = 10, REQS_BEFORE_PAUSE = 25, // tbh, I don't know how many requests per sec is too many. PAUSE_DELAY = 20; (function($, mw, window) {       ///********/	//* Data *//	/********///    var    config = mw.config.get([ "wgPageName", "wgUserLanguage", "wgDBname" //		"wgServer" ]),   requestMessages = [		// general		"pipe-separator",		"word-separator",		// user		"wall-message-wall-shorten",		"contribslink",		// block		"blocklog-showlog",		"blocklogentry",		"unblocklogentry"	];    config = $.extend(config, { usingLang: mw.util.getParamValue("uselang") || config.wgUserLanguage.split('-')[0], targetUser: config.wgPageName.split("/")[1] });   // No target (no /Username nor wpTarget=Username)    if (!(config.targetUser = config.targetUser        || mw.util.getParamValue("wpTarget"))) return;	/* Load system messages */    mw.loader.using('mediawiki.api').done(function { function loadMessages( messages ) { return (new mw.Api).get( {               action:     'query',                meta:       'allmessages',                ammessages:  messages.join('|'),                amlang:      config.usingLang            } ).then( function (data) {                $.each( data.query.allmessages, function ( i, message ) { if ( message.missing !== '' ) { mw.messages.set( message.name, message['*'] ); }               } );            } );        }        loadMessages(requestMessages).then(function {            $(blockedNotice).text(mw.message("blocklog-showlog").text);        }); });	///*******************/	//* Element Factory *//	/*******************///	class Make /*extends HTMLElement*/ {		// Extending DOM elements is experimental. Whoops.		// https://developer.mozilla.org/en-US/docs/Web/Web_Components/Custom_Elements/Custom_Elements_with_Classes		// Constructor & Factories		constructor(tag, classes) { //			super; // call constructor of parent class			this.e = document.createElement(tag); // indefinite alternative			if (typeof classes !== "undefined")				this.setClass(classes);		}		static link(index, page, ltext) {			var a = new Make('a');			a.e.setAttribute("href", index+encodeURIComponent(page));			a.e.setAttribute("title", page);			if (ltext) a.e.innerHTML = ltext; // I could not for the life of me get this to work in a ternary op, for some reason.			else a.e.innerHTML = page;			return a;		}		static toolLinks(mlinks) {			var tools = new Make('span', 'mw-usertoollinks') .contains('(');			for (i = 0; i < mlinks.length;)				tools.append(mlinks[i] + (++i !== mlinks.length ? (' '+mw.message("pipe-separator")+' ') : ')')); return tools; }		// Object methods setClass(classes) { this.e.setAttribute("class", classes); return this; }		contains(content) { this.e.innerHTML = content; return this; }		append(content) { this.e.innerHTML += content; return this; }		// @override toString { return this.e.outerHTML; } }   ///********************/	//* Report Container *// /********************///   var $blockLog = $(".mw-warning-with-logexcerpt"), blockedNotice; if($blockLog.length) { // Target user had been blocked on local wiki. Pre-existing log. $blockLog.append(document.createElement("hr")); } else { // Target user has never been blocked on local wiki. Create log. $("#mw-content-text").append(           $blockLog = $(document.createElement("div"))                .addClass("mw-warning-with-logexcerpt")                .append(blockedNotice = document.createElement("p"))        ); }	$loading = $blockLog.clone .appendTo("#mw-content-text") .text("Loading"); // temp. use a loading animation instead. $blockLog.hide; // while loading ///*****************/	//* Log Retrieval *// /*****************///   function getInterwikiLog(wiki, callback) { // e.g. http://campcamp.wikia.com/api.php?action=query&list=logevents&letype=block&letitle=User:LuaKey&format=json if (wiki == config.wgDBname) return; $.get("http://" + wiki + ".wikia.com/api.php", {           action: "query",            format: "json",            // Block logs            list:   "logevents",            letype: "block",            letitle: "User:" + config.targetUser,            // SiteInfo            meta:   "siteinfo",            siprop: "general"        }, function(result) {            if (result !== null && result.query.logevents.length !== 0)				callback(result.query);       }, "jsonp"); }	///********************/	//* Make log entries *// /********************///	function makeWikiEntries(result) { var $localLogs, indexURL = result.general.server + "/index.php?title="; // New wiki section $blockLog.append($(document.createElement("div"))           .prepend($localLogs = $(document.createElement("ul")))            .prepend(new Make("h4") .contains( Make.link(result.general.server, '', result.general.sitename) ).e			)       ); // Log entry result.logevents.reverse.forEach(function(entry) {           var liEntry = new Make("li"),            blocked = entry.action === "block",            blockedOn  = new Date(entry.timestamp);            blockedOn = $.extend(blockedOn, { // this would've been a *great* time for toLocaleFormat to be supported... hour:  String(blockedOn.getHours).padStart(2,'0'), minute: String(blockedOn.getMinutes).padStart(2,'0'), date:  blockedOn.getDate, month: mw.config.get("wgMonthNames")[blockedOn.getMonth + 1], // the lazy way, may differ for uselang year:  blockedOn.getFullYear });			console.log			//			var target = entry.title.replace(/^.+?:/,''),			targetUserLinks = Make.link(indexURL, entry.title, target) + ' '				+ Make.toolLinks([ Make.link(indexURL, "Message Wall:"+target, mw.message("wall-message-wall-shorten")), Make.link(indexURL, "Special:Contributions/"+target, mw.message("contribslink")) ]);			liEntry.contains( blockedOn.hour + ':' + blockedOn.minute + ', ' + blockedOn.month + ' ' + blockedOn.date + ", " + blockedOn.year + ' ' + Make.link(indexURL, "User:"+entry.user, entry.user) + ' ' + Make.toolLinks([					Make.link(indexURL, "Message Wall:"+entry.user, mw.message("wall-message-wall-shorten")),					Make.link(indexURL, "Special:Contributions/"+entry.user, mw.message("contribslink"))				]) + ' ' + (blocked					? mw.message("blocklogentry", targetUserLinks, entry.block.duration, )						.plain.replace(/\[|\]/g,)					: mw.message("unblocklogentry", targetUserLinks)				) + ' ' + new Make("i").contains('('+entry.comment+')') );           $(liEntry.e)                .addClass("mw-logline-block")                //.text(entry.comment)                .prependTo($localLogs);        }); }	///****************/	//* Perform task *// /****************///	function getAndParseLogs(wikiIter) { getAndParseLogF(wikiIter); }	function getAndParseLogF(wikiIter) { return function { var reqSent = 0, reqIntv = setInterval(function {				var wiki;				if (( wiki = wikiIter.next ).done) {					clearInterval(reqIntv);					$blockLog.show;					return;				}				getInterwikiLog(wiki.value, makeWikiEntries);				if (++reqSent % 25 === 0) {					clearInterval(reqIntv);					setTimeout(getAndParseLogF(wikiIter), PAUSE_DELAY);				}			}, DELAY); }	}   if (TBL_WIKIS) //		wikis = TBL_WIKIS.values; getAndParseLogs( TBL_WIKIS[Symbol.iterator] ); else if (TBL_GROUP && TBL_GROUP_DICT[TBL_GROUP]) getAndParseLogs( TBL_GROUP_DICT[TBL_GROUP][Symbol.iterator] ); else throw "No wiki group has been defined (or invalid). An interwiki block report cannot be generated."; })(jQuery, mediaWiki, this);