User:Mdaniels5757/sockStaleness.js

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
Note: After saving, you have to bypass your browser's cache to see the changes. Internet Explorer: press Ctrl-F5, Mozilla: hold down Shift while clicking Reload (or press Ctrl-Shift-R), Opera/Konqueror: press F5, Safari: hold down Shift + Alt while clicking Reload, Chrome: hold down Shift while clicking Reload.
//<nowiki>
/**
 * @brief Checkuser staleness checker for categories.
 * @desc Checks contribs, deleted contribs, and user creation date, and determines whether the account has activity past the 90-day checkuser staleness mark.
 * @author Originally written by Writ Keeper. Modified by User:mdaniels5757 for Commons.
 * @license CC-BY-SA-3.0
 * @copyright (c) 2017-2020 by the Authors
*/
if(typeof maxUsersCUStaleness !== "number" || maxUsersCUStaleness < 1)
{
    maxUsersCUStaleness = 50;
}
if(typeof lazyCheckCatStaleness !== "boolean" || !(lazyCheckCatStaleness === false))
{
    lazyCheckCatStaleness = true;
}
sockStaleDates = {};
sockStaleDates.newestCreate = null;
sockStaleDates.newestCreateID = null;
sockStaleDates.oldestCreate = null;
sockStaleDates.oldestCreateID = null;
sockStaleDates.newestContrib = null;
sockStaleDates.newestContribID = null;
sockStaleDates.oldestContrib = null;
sockStaleDates.oldestContribID = null;
sockStaleDates.CREATE = 1;
sockStaleDates.CONTRIB = 0;
sockStaleRunCount = 0;

function insertCatStalenessIndicator(siblingNode, message, color, finished)
{
	var newNode = "<span style='color:" + color + ";'> -- " + message + "</span>";
	$(siblingNode).after(newNode);
	$(siblingNode).addClass("cuProcessed");
	finished.resolve();
}
function updateDatesRefs(inputDate, refDateType, index)
{
	if(refDateType == sockStaleDates.CREATE)
	{
		if(sockStaleDates.oldestCreate == null || inputDate < sockStaleDates.oldestCreate)
		{
			sockStaleDates.oldestCreate = inputDate;
			sockStaleDates.oldestCreateID = "" + index;
		}
		if(sockStaleDates.newestCreate == null || inputDate > sockStaleDates.newestCreate)
		{
			sockStaleDates.newestCreate = inputDate;
			sockStaleDates.newestCreateID = "" + index;
		}
	}
	else
	{
		if(sockStaleDates.oldestContrib == null || inputDate < sockStaleDates.oldestContrib)
		{
			sockStaleDates.oldestContrib = inputDate;
			sockStaleDates.oldestContribID = "" + index;
		}
		if(sockStaleDates.newestContrib == null || inputDate > sockStaleDates.newestContrib)
		{
			sockStaleDates.newestContrib = inputDate;
			sockStaleDates.newestContribID = "" + index;
		}
	}
}
function dateFormatter(dateString, index, type)
{
	var message = "<span id='" + type + index + "'>" + dateString + "</span>";
	return message;
}
function performCatStalenessCheck()
{
	sockStaleRunCount++;

	//initialize some variables
	var currentTimestamp = new Date().getTime();
	var canViewDeleted = true; //assume this is true, update if the first call fails
	var timestampDifference = 90 * 24 * 60 * 60 * 1000; //90 days in milliseconds
	var notStaleColor = "#339900";
	var problemColor = "red";
	var maybeStaleColor = "#e6ac00";
	var index = 0;
	var promises = [];
	if(sockStaleDates.newestCreateID != null)
	{
		$("#Create" + sockStaleDates.newestCreateID).css("font-style","normal");
	}
	if(sockStaleDates.newestContribID != null)
	{
		$("#Contrib" + sockStaleDates.newestContribID).css("font-style","normal");
	}
	if(sockStaleDates.oldestCreateID != null)
	{
		$("#Create" + sockStaleDates.oldestCreateID).css("font-weight","normal");
	}
	if(sockStaleDates.oldestContribID != null)
	{
		$("#Contrib" + sockStaleDates.oldestContribID).css("font-weight","normal");
	}
	//for each cu entry (as defined by use of the {{checkuser}} template)...
	$("div.mw-content-ltr ul a").not("[class~='cuProcessed']").each(function ()
	{
		//first, retrieve the current iteration's user name from the text of the template
		var contribsData =
		{
			"action": "query",
			"list": "usercontribs",
			"ucprop": "timestamp",
			"ucuser": "",
			"uclimit": "1",
			"ucdir": "older",
			"format": "json"
		};
		var deletedData =
		{
			"action": "query",
			"list": "alldeletedrevisions",
			"adrprop": "timestamp",
			"adruser": "",
			"adrlimit": "1",
			"adrdir": "older",
			"format": "json"
		};
		var createdData =
		{
			"action": "query",
			"list": "users",
			"ususers": "",
			"usprop": "registration",
			"format": "json"
		};

		if(!(/User([ _]talk)?:([^\/]+)/.test($(this).text())))
		{
			return true;
		}
		index += 1;
		var sockName = /User([ _]talk)?:([^\/]+)/.exec($(this).text())[2];
		var cuLogLinks = $(this);
		var message = "";
		//insert it into the API data packages
		contribsData.ucuser = sockName;
		deletedData.adruser = sockName;
		createdData.ususers = sockName;
		//initialize values
		var lastWasDeleted = false;
		var notStale = false;
		//first search and display creation date
		if (index < maxUsersCUStaleness)
		{
			var finished = $.Deferred();
			promises.push(finished.promise());
			var indexString = "" + sockStaleRunCount + "-"+ index;
			$.post("/w/api.php", createdData, function (response)
			{
				//if this, the account doesn't exist; don't bother to run any more queries
				if (typeof response.query.users[0].missing !== "undefined")
				{
					insertCatStalenessIndicator(cuLogLinks, "account does not exist", problemColor, finished);
				}
				else
				{
					//check registration date, if it exists.
					if (typeof response.query.users[0].registration !== "undefined")
					{

						//API-accessible user creation logs started on December 22, 2005; anything before that would display the epoch date without special handling
						if (response.query.users[0].registration == null)
						{
							message = "created <span style='font-weight:bold;'>on or before 2005-12-22</span>, ";
							cuStaleOldestCreate = new Date(0);
							cuStaleOldestCreateID = null;
						}
						else
						{
							var regTime = new Date(response.query.users[0].registration);
							updateDatesRefs(regTime, sockStaleDates.CREATE, indexString);
							var formattedDate = dateFormatter(regTime.getUTCFullYear() + "-" + (regTime.getUTCMonth() + 1) + "-" + regTime.getUTCDate(), indexString, "Create");
							//if the account itself is newer than 90 days, and we're being lazy, we don't have to check anything else, we already know it's not stale
							if (currentTimestamp - regTime.getTime() < timestampDifference)
							{
								if(lazyCheckCatStaleness)
								{
									insertCatStalenessIndicator(cuLogLinks, "created " + formattedDate + ", not stale", notStaleColor, finished);
									return;
								}
								else
								{
									notStale = true;
								}
							}
							message = "created " + formattedDate + ", ";
						}
					}
					else //it's an IP, make note of the fact and move on
					{
						message = "IP address, ";
					}
					//look up contribs

					$.post("/w/api.php", contribsData, function (response)
					{
						if (typeof response.query !== "undefined" && response.query.usercontribs.length > 0)
						{
							//retrieve the current stamp
							var lastTimestamp = new Date(response.query.usercontribs[0].timestamp);
							updateDatesRefs(lastTimestamp, sockStaleDates.CONTRIB, indexString);
							var formattedDate = dateFormatter(lastTimestamp.getUTCFullYear() + "-" + (lastTimestamp.getUTCMonth() + 1) + "-" + lastTimestamp.getUTCDate(), indexString, "Contrib");
							//a contrib more recent than 90 days, so it's not stale; no need to check the deleted contribs if we're lazy
							if (currentTimestamp - lastTimestamp.getTime() < timestampDifference)
							{
								if(lazyCheckCatStaleness)
								{
									insertCatStalenessIndicator(cuLogLinks, message + "last edit " + formattedDate + ", not stale", notStaleColor, finished);
									return;
								}
								else
								{
									notStale = true;
								}
							}
							//normal contribs were all stale, so now try deleted (if we can)
							if (canViewDeleted)
							{

								$.post("/w/api.php", deletedData, function (response)
								{
									//if we can't see deleted edits due to not being an admin, flag as possibly stale based on the normal contribs and flip the bit so that we don't waste time on deleted contribs in the future
									if (typeof response.error !== "undefined" && response.error.code == "permissiondenied")
									{
										canViewDeleted = false;
										if(notStale)
										{
											insertCatStalenessIndicator(cuLogLinks, message + "last edit " + formattedDate + ", not stale", notStaleColor, finished);
										}
										else
										{
											insertCatStalenessIndicator(cuLogLinks, message + "last edit " + formattedDate + ", possibly stale", maybeStaleColor, finished);
										}
										return;
									}
									//otherwise, check the to see if there are any
									else if (response.query.alldeletedrevisions.length > 0)
									{
										var deletedTimestamp = new Date(response.query.alldeletedrevisions[0].revisions[0].timestamp);
										//there are deleted contribs, so compare the timestamp to see if they're more recent than the normal contrib
										if (deletedTimestamp.getTime() > lastTimestamp)
										{
											//deleted contrib is newer, so display it and use it to decide whether the account is possibly stale or not
											updateDatesRefs(deletedTimestamp, sockStaleDates.CONTRIB, indexString);
											formattedDate = dateFormatter(deletedTimestamp.getUTCFullYear() + "-" + (deletedTimestamp.getUTCMonth() + 1) + "-" + deletedTimestamp.getUTCDate(), indexString, "Contrib");
											if (currentTimestamp - deletedTimestamp.getTime() < timestampDifference)
											{
												insertCatStalenessIndicator(cuLogLinks, message + "last (deleted) edit " + formattedDate + ", not stale", notStaleColor, finished);
												return;
											}
											else
											{
												insertCatStalenessIndicator(cuLogLinks, message + "last (deleted) edit " + formattedDate + ", probably stale", maybeStaleColor, finished);
												return;
											}
										}
										else //the deleted contribs existed but aren't any newer than the normal contribs, so report the normal contribs and flag as probably stale
										{
											if(notStale)
											{
												insertCatStalenessIndicator(cuLogLinks, message + "last edit " + formattedDate + ", not stale", notStaleColor, finished);
											}
											else
											{
												insertCatStalenessIndicator(cuLogLinks, message + "last edit " + formattedDate + ", probably stale", maybeStaleColor, finished);
											}
											return;
										}
									}
									else //no deleted contribs, so report the normal contribs and flag as probably stale
									{
										if(notStale)
										{
											insertCatStalenessIndicator(cuLogLinks, message + "last edit " + formattedDate + ", not stale", notStaleColor, finished);
										}
										else
										{
											insertCatStalenessIndicator(cuLogLinks, message + "last edit " + formattedDate + ", probably stale", maybeStaleColor, finished);
										}
										return;
									}
								}
								);
							}
							else //we already know we can't see deleted contribs, so just settle for possibly stale from the normal contribs
							{
								if(notStale)
								{
									insertCatStalenessIndicator(cuLogLinks, message + "last edit " + formattedDate + ", not stale", notStaleColor, finished);
								}
								else
								{
									insertCatStalenessIndicator(cuLogLinks, message + "last edit " + formattedDate + ", possibly stale", maybeStaleColor, finished);
								}
								return;
							}
						}
						else //no normal contribs, so check deleted contribs if we can
						{
							if (canViewDeleted)
							{

								$.post("/w/api.php", deletedData, function (response)
								{
									//if we can't see deleted edits due to not being an admin, flag as possibly stale based on the normal contribs and flip the bit so that we don't waste time on deleted contribs in the future
									if (typeof response.error !== "undefined" && response.error.code == "permissiondenied")
									{
										canViewDeleted = false;
										if(notStale)
										{
											insertCatStalenessIndicator(cuLogLinks, message + "no visible contribs, not stale", notStaleColor, finished);
										}
										else
										{
											insertCatStalenessIndicator(cuLogLinks, message + "no visible contribs, possibly stale", maybeStaleColor, finished);
										}
										return;
									}
									//otherwise, check the to see if there are any
									else if (response.query.alldeletedrevisions.length > 0)
									{
										var deletedTimestamp = new Date(response.query.alldeletedrevisions[0].revisions[0].timestamp);
										updateDatesRefs(deletedTimestamp, sockStaleDates.CONTRIB, indexString);
										var formattedDate = dateFormatter(deletedTimestamp.getUTCFullYear() + "-" + (deletedTimestamp.getUTCMonth() + 1) + "-" + deletedTimestamp.getUTCDate(), indexString, "Contrib");
										if (currentTimestamp - deletedTimestamp.getTime() < timestampDifference)
										{
											insertCatStalenessIndicator(cuLogLinks, message + "last (deleted) edit " + formattedDate + ", not stale", notStaleColor, finished);
											return;
										}
										else
										{
											if(notStale)
											{
												insertCatStalenessIndicator(cuLogLinks, message + "last (deleted) edit " + formattedDate + ", not stale", notStaleColor, finished);
											}
											else
											{
												insertCatStalenessIndicator(cuLogLinks, message + "last (deleted) edit " + formattedDate + ", probably stale", maybeStaleColor, finished);
											}
											return;
										}
									}
									else //no deleted contribs, so report the normal contribs and flag as probably stale
									{
										if(notStale)
										{
											insertCatStalenessIndicator(cuLogLinks, message + "no contribs, not stale", notStaleColor, finished);
										}
										else
										{
											insertCatStalenessIndicator(cuLogLinks, message + "no contribs, probably stale", maybeStaleColor, finished);
										}
										return;
									}
								}
								);
							}
						}
					}
					);
				}
			}
			);
		}
		else
		{
			var newNode = "<a class='cuStalenessContinue' style='color:" + problemColor + ";'> -- too many users to process at once; this user not checked. click to continue...</a>";
			$(cuLogLinks).after(newNode);
			$(".cuStalenessContinue").click(resumeCatStalenessCheck);
			return false;
		}
	}
	);
	$.when.apply($, promises).then(function()
	{
		if(sockStaleDates.newestCreateID != null)
		{
			$("#Create" + sockStaleDates.newestCreateID).css("font-style","italic");
		}
		if(sockStaleDates.newestContribID != null)
		{
			$("#Contrib" + sockStaleDates.newestContribID).css("font-style","italic");
		}
		if(sockStaleDates.oldestCreateID != null)
		{
			$("#Create" + sockStaleDates.oldestCreateID).css("font-weight","bold");
		}
		if(sockStaleDates.oldestContribID != null)
		{
			$("#Contrib" + sockStaleDates.oldestContribID).css("font-weight","bold");
		}
	});
}
function resumeCatStalenessCheck()
{
	$(".cuStalenessContinue").remove();
	performCatStalenessCheck();
}
mw.hook('wikipage.content').add(function ()
{
	var titleRegex = /Category:Sockpuppets_of_/;
	//only use on a sock category
	if (titleRegex.test(mw.config.get("wgPageName")))
	{
		performCatStalenessCheck();
	}
}
);
//</nowiki>