User:Writ Keeper/Scripts/sockStaleness.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:Writ Keeper/Scripts/sockStaleness. |
//<nowiki>
/*
Checkuser staleness checker, written by Writ Keeper. Checks contribs, deleted contribs, and user creation date, and determines whether the account has activity past the 90-day checkuser staleness mark.
*/
if(typeof maxUsersCUStaleness !== "number" || maxUsersCUStaleness < 1)
{
maxUsersCUStaleness = 10;
}
function insertStalenessIndicator(siblingNode, message, color)
{
var newNode = "<span style='color:" + color + ";'> -- " + message + "</span>";
$(siblingNode).after(newNode);
$(siblingNode).parent("span.cuEntry").removeClass("cuEntry");
}
function performStalenessCheck()
{
//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";
//for each cu entry (as defined by use of the {{checkuser}} template)...
$("div.mw-content-ltr ul a").each(function (index)
{
//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"
};
var sockName = $(this).text();
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 lastTimestamp = -1;
//first search and display creation date
if (index < maxUsersCUStaleness)
{
$.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")
{
insertStalenessIndicator(cuLogLinks, "account does not exist", problemColor);
}
else
{
//check registration date, if it exists.
if (typeof response.query.users[0].registration !== "undefined")
{
var regTime = new Date(response.query.users[0].registration);
//if the account itself is newer than 90 days, we don't have to check anything else, we already know it's not stale
if (currentTimestamp - regTime.getTime() < timestampDifference)
{
insertStalenessIndicator(cuLogLinks, "created " + regTime.getUTCFullYear() + "-" + (regTime.getUTCMonth() + 1) + "-" + regTime.getUTCDate() + ", not stale", notStaleColor);
return;
}
else
{
message = "created " + regTime.getUTCFullYear() + "-" + (regTime.getUTCMonth() + 1) + "-" + regTime.getUTCDate() + ", ";
}
}
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
lastTimestamp = new Date(response.query.usercontribs[0].timestamp);
//a contrib more recent than 90 days, so it's not stale; no need to check the deleted contribs
if (currentTimestamp - lastTimestamp.getTime() < timestampDifference)
{
insertStalenessIndicator(cuLogLinks, message + "last edit " + lastTimestamp.getUTCFullYear() + "-" + (lastTimestamp.getUTCMonth() + 1) + "-" + lastTimestamp.getUTCDate() + ", not stale", notStaleColor);
return;
}
//normal contribs were all stale, so now try deleted (if we can)
else 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;
insertStalenessIndicator(cuLogLinks, message + "last edit " + lastTimestamp.getUTCFullYear() + "-" + (lastTimestamp.getUTCMonth() + 1) + "-" + lastTimestamp.getUTCDate() + ", possibly stale", maybeStaleColor);
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
if (currentTimestamp - deletedTimestamp.getTime() < timestampDifference)
{
insertStalenessIndicator(cuLogLinks, message + "last (deleted) edit " + deletedTimestamp.getUTCFullYear() + "-" + (deletedTimestamp.getUTCMonth() + 1) + "-" + deletedTimestamp.getUTCDate() + ", not stale", notStaleColor);
return;
}
else
{
insertStalenessIndicator(cuLogLinks, message + "last (deleted) edit " + deletedTimestamp.getUTCFullYear() + "-" + (deletedTimestamp.getUTCMonth() + 1) + "-" + deletedTimestamp.getUTCDate() + ", probably stale", maybeStaleColor);
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
{
insertStalenessIndicator(cuLogLinks, message + "last edit " + lastTimestamp.getUTCFullYear() + "-" + (lastTimestamp.getUTCMonth() + 1) + "-" + lastTimestamp.getUTCDate() + ", probably stale", maybeStaleColor);
return;
}
}
else //no deleted contribs, so report the normal contribs and flag as probably stale
{
insertStalenessIndicator(cuLogLinks, message + "last edit " + lastTimestamp.getUTCFullYear() + "-" + (lastTimestamp.getUTCMonth() + 1) + "-" + lastTimestamp.getUTCDate() + ", probably stale", maybeStaleColor);
return;
}
}
);
}
else //we already know we can't see deleted contribs, so just settle for possibly stale from the normal contribs
{
insertStalenessIndicator(cuLogLinks, message + "last edit " + lastTimestamp.getUTCFullYear() + "-" + (lastTimestamp.getUTCMonth() + 1) + "-" + lastTimestamp.getUTCDate() + ", possibly stale", notStaleColor);
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;
insertStalenessIndicator(cuLogLinks, message + "no visible contribs, possibly stale", maybeStaleColor);
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);
if (currentTimestamp - deletedTimestamp.getTime() < timestampDifference)
{
insertStalenessIndicator(cuLogLinks, message + "last (deleted) edit " + deletedTimestamp.getUTCFullYear() + "-" + (deletedTimestamp.getUTCMonth() + 1) + "-" + deletedTimestamp.getUTCDate() + ", not stale", notStaleColor);
return;
}
else
{
insertStalenessIndicator(cuLogLinks, message + "last (deleted) edit " + deletedTimestamp.getUTCFullYear() + "-" + (deletedTimestamp.getUTCMonth() + 1) + "-" + deletedTimestamp.getUTCDate() + ", probably stale", maybeStaleColor);
return;
}
}
else //no deleted contribs, so report the normal contribs and flag as probably stale
{
insertStalenessIndicator(cuLogLinks, message + "no contribs, probably stale", maybeStaleColor);
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(resumeStalenessCheck);
return false;
}
}
);
}
function resumeStalenessCheck()
{
$(".cuStalenessContinue").remove();
performStalenessCheck();
}
mw.hook('wikipage.content').add(function ()
{
var titleRegex = /Category:(Suspected_)?Wikipedia_sockpuppets_of/;
//only use on a SPI page (or my sandbox for testing)
if (titleRegex.test(mw.config.get("wgPageName")) || mw.config.get("wgPageName") === "User:Writ_Keeper/sandbox")
{
performStalenessCheck();
}
}
);
//</nowiki>