User:Technical 13/SandBox/Gadget-UserRights.js

if(mw.config.get("wgCanonicalSpecialPageName") === "Userrights"){ //INITIALIZE VARIABLES // Define the api check variables var apiAllusersDone = false; var apiAllusersFailed = false; var apiUsercontribsDone = false; var apiUsercontribsFailed = false; // Get the username from the page title from mw.config variables var urUsername = mw.config.get("wgTitle").substr(mw.config.get("wgTitle").indexOf("/")+1, mw.config.get("wgTitle").length); // Check to see if the VIP checkbox is checked and if it is, they are a VIP already && set isVIP to false for viewing user var urVIP = $("#wpGroup-ddowikivip").is(":checked"); var isVIP = false; // Check to see if the SU checkbox is checked and if it is, they are a SU already && set isSU to false for viewing user var urSU = $("#wpGroup-superuser").is(":checked"); var isfSU = false; // Check to see if the admin checkbox is checked and if it is, they are an admin already && set isAdmin to false for viewing user var urAdmin = $("#wpGroup-sysop").is(":checked"); var isAdmin = false; // Check to see if the crat checkbox is checked and if it is, they are a crat already && set isCrat to false for viewing user var urCrat = $("#wpGroup-bureaucrat").is(":checked"); var isCrat = false; // Check to see if the steward checkbox is checked and if it is, they are a steward already && set isSteward to false for viewing user var urSteward = $("#wpGroup-steward").is(":checked"); var isSteward = false; // Check to see if the bot checkbox is checked and if it is, they are a bot already && set isBot to false for viewing user var urBot = $("#wpGroup-bot").is(":checked"); var isBot = false; // Get viewing user's groups for (var group in wgUserGroups){ switch (wgUserGroups[group]){ case "ddowikivip": isVIP = true; break; case "superuser": isSU = true; break; case "sysop": isAdmin = true; break; case "bureaucrat": isCrat = true; break; case "steward": isSteward = true; break; case "bot": isBot = true; break; default: break; }	}	// Define edit count as -1 to start var urEditCount = -1; // Define registration time as empty to start, may modify this to epoch later and return a message warning about created before reg date existed var urRegistration = ""; // Define user's age in months since Jan 2000 as -1 to start, may need to go back further at some point var urAgeInMonths = -1; // Define longest gap in editing in the last 6 months var urGapLong = -1; // Define number of gaps over 30 days in the last 6 months var urLongGaps = -1; // Define if active in the last year as false var urActive = false; // Define a new date constructor for right now var currDate = new Date; // Calculate the current number of months since Jan 2000 var currDateInMonths = currDate.getFullYear * 12 + currDate.getMonth; // function to format numbers with commas function numformat(nStr){ nStr += ''; x = nStr.split('.'); x1 = x[0]; x2 = x.length > 1 ? '.' + x[1] : ''; var rgx = /(\d+)(\d{3})/; while (rgx.test(x1)) { x1 = x1.replace(rgx, '$1' + ',' + '$2'); }		return x1 + x2; }	if(urAdmin === true){ $(".is-admin").css({"color": "#0F0", "font-size": "12pt"}).html("&#x2714;"); }	// If they are a VIP but not a crat or steward, then show the message about what the * means $.ajax({		url: mw.config.get('wgScriptPath') + '/api.php?action=query&format=json&list=allusers&auprop=editcount|registration&aulimit=1&aufrom=' + encodeURIComponent(urUsername),		dataType: 'json',		success: function(data){			urEditCount = data.query.allusers[0].editcount;			urRegistration = data.query.allusers[0].registration;			if(typeof(urRegistration) != "undefined"){				urRegistration = new Date(urRegistration);			} else {				urRegistration = new Date(1999, 11, 30);			}			urAgeInMonths = ((urRegistration.getFullYear * 12) + urRegistration.getMonth);			urAgeInMonths = (currDateInMonths - urAgeInMonths);			if(urEditCount >= 0){							if(urEditCount >= 100){					$(".100").css({"color": "#0F0", "font-size": "12pt"}).html("&#x2714;");					if(urEditCount >= 250){						$(".250").css({"color": "#0F0", "font-size": "12pt"}).html("&#x2714;");						if(urEditCount >= 1000){							$(".1k").css({"color": "#0F0", "font-size": "12pt"}).html("&#x2714;"); }					}				}			} else { $(".100").html("?") $(".250").html("?") $(".1k").html("?") }			// If account is 6+ months old, tick box in green. If account is between 3-6 months old, tick it in yellow if(urAgeInMonths >= 6){ $(".age").css({"color": "#0F0", "font-size": "12pt"}).html("&#x2714;"); }			else if(urAgeInMonths >= 3 && urAgeInMonths < 6){ $(".age").css({"color": "#AA0", "font-size": "12pt"}).html("&#x2714;"); } else { $(".age").css({"color": "#0F0", "font-size": "12pt"}).html("&#x2714;"); }			// Add the registration date and edit count to the bottom left cell in the table // Create text string var textUserStats = "This "; if (urAgeInMonths < 0 || isNaN(urAgeInMonths)){ textUserStats += "account is too old "; }			else if (urAgeInMonths === 0){ textUserStats += "is a new account "; }			else if(urAgeInMonths === 1){ textUserStats += "account is a month old "; } else { textUserStats += "account is " + urAgeInMonths + " months old "; }			textUserStats += "and has made "; if (urEditCount < 0 || isNaN(urEditCount)){ textUserStats += "an undefined number of edits."; }			else if (urEditCount === 0){ textUserStats += "no edits "; }			else if(urEditCount === 1){ textUserStats += "an edit "; } else { textUserStats += numformat(urEditCount) + " edits."; }			$("#groups-userstats-js").html( textUserStats ); apiAllusersDone = true; },		fail: function(reason){ $(".100").html("??") $(".250").html("???") $(".1k").html("???") $(".age").css({"color": "#AA0", "font-size": "12pt"}).html("???"); console.error("apiAllusers failed: %o", reason); apiAllusersFailed = true; }	}); /*	var next = "";	while (next != 'stop') {		$.ajax({ url: mw.config.get('wgScriptPath') + '/api.php?action=query&format=json&list=usercontribs&uclimit=max&ucprop=timestamp&ucuser=' + encodeURIComponent(urUsername) + next, dataType: 'json', success: function(data){ /*				if(urGapLong <= 45){*//* $(".mo-gap").html("?</a>"); /*					$(".mo-gap").css({"color": "#0F0", "font-size": "12pt"}).html("&#x2714;"); /*				}*//* /*				if(urLongGaps <= 3){*//* $(".no-gaps").html("?</a>"); /*					$(".no-gaps").css({"color": "#0F0", "font-size": "12pt"}).html("&#x2714;"); /*				}*//* /*				if(urActive === true){*//* $(".active").html("?</a>"); /*					$(".active").css({"color": "#0F0", "font-size": "12pt"}).html("&#x2714;"); /*				}*//*				if (data["query-continue"]) { next = '&ucstart=' + data["query-continue"].usercontribs.ucstart; } else { next = 'stop'; }				if(next === "stop"){ apiUsercontribsDone = true; } },			fail: function(reason){ console.error("apiUsercontribs failed: %o", reason); apiUsercontribsFailed = true; }		});	}*/	/* Only show when table is completely updated. */	var tableUpdated = false;	var apiWaitTime = 0;	var apiWaitMore = 0;	var apiWaitInterval = 5;// In seconds	var apiTimeOutLimit = 30;// In seconds	function timer(milliseconds) {		apiWaitTime += milliseconds;	}	while(tableUpdated === false && apiWaitTime <= 5000){		if(apiAllusersDone === true && apiUsercontribsDone === true){			// Change the exclamation mark to a semicolon			$("#groups-qualified-js").html(":");			// Unhide completed table			$("#groups-add-table-js").removeClass("hidden");			tableUpdated = true;		}		else if(apiAllusersFailed === true || apiUsercontribsFailed === true){			var missingInfo = "";			if(apiAllusersFailed === true && apiUsercontribsFailed === true){				missingInfo = "any";			}			else if(apiAllusersFailed === true){				missingInfo = "user";			}			else if(apiUsercontribsFailed === true){				missingInfo = "contribution"; }			$("#groups-userstats-js").html("<span style=\"font-size: 14pt; color: #F00;\">Failed to retrieve " + missingInfo + " information from the API: <span style=\"font-size: 10pt;\"> Please try again</a> in a few minutes or  report error to ShoeMaker</a> "); tableUpdated = true; } else { /* API STUFF HAS NOT COMPLETED, RETURN A TIMEOUT FAILURE MESSAGE */ setTimeout(timer(100), 100); if (apiWaitMore === apiTimeOutLimit - apiWaitInterval) { alert("This is taking too long; I quit!"); break; }			if (apiWaitTime >= (apiWaitInterval * 1000)) { giveUp = confirm("I've been trying for " + (apiWaitMore + (apiWaitTime / 1000)) + " seconds:\n\tPlease press \"Okay\" to keep trying or \"Cancel\" to give up."); if (!giveUp) { alert("This is taking too long; I quit!"); break; } else { apiWaitTime = 0; apiWaitMore = (apiWaitMore + apiWaitInterval); }			}		}		// If the user is ONLY a VIP, display the note that once they add or remove they can't change their mind if (isVIP === true && !(isCrat === true || isSteward === true)){ $("#groups-add-noremove").removeClass("hidden"); }	} }