User:Ingenuity/VandalismScanner.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:Ingenuity/VandalismScanner. |
(function() {
const usersFetched = {};
let lastFetched = "2000-01-01T00:00:00Z";
const api = new mw.Api();
async function scanLogs() {
const abuseHits = (await api.get({
"action": "query",
"list": "abuselog",
"afllimit": 100,
"aflend": lastFetched,
"format": "json"
})).query.abuselog;
const abuseDisallows = abuseHits.filter(e => e.result === "disallow");
const userAbuseLog = {};
const totalHits = {};
abuseDisallows.forEach(e => {
if (!userAbuseLog[e.user]) {
userAbuseLog[e.user] = [];
}
userAbuseLog[e.user].push(e.timestamp);
});
for (const user in userAbuseLog) {
let lastAbuseHit = "2000-01-01T00:00:00Z";
for (const hit of userAbuseLog[user]) {
if (Math.abs(new Date(hit).getTime() - new Date(lastAbuseHit).getTime()) > 10000) {
totalHits[user] = (totalHits[user] || 0) + 1;
}
lastAbuseHit = hit;
}
}
const tags = ["mw-rollback", "mw-undo"];
const reverts = [];
for (const tag of tags) {
const edits = (await api.get({
"action": "query",
"list": "recentchanges",
"rctag": tag,
"rcprop": "comment",
"rclimit": 50,
"rcend": lastFetched,
"format": "json"
})).query.recentchanges;
reverts.push(...edits);
}
const rollbackUsers = reverts.map(edit => {
const match = edit.comment.match(/special:contributions\/([^\]\|]+)/i);
return match ? match[1] : null;
}).filter(e => e);
for (const user in totalHits) {
createUser(usersFetched, user);
usersFetched[user].abuseHits += totalHits[user];
usersFetched[user].lastHit = new Date().toISOString();
}
for (const user of rollbackUsers) {
createUser(usersFetched, user);
usersFetched[user].rollbacks++;
usersFetched[user].lastHit = new Date().toISOString();
}
lastFetched = new Date().toISOString();
for (let user in usersFetched) {
if (new Date().getTime() - new Date(user.lastHit).getTime() > 3600000) {
delete usersFetched[user];
}
}
renderUsers();
window.setTimeout(scanLogs, 30000);
}
function createUser(dict, username) {
if (!dict[username]) {
dict[username] = {
"abuseHits": 0,
"rollbacks": 0,
"lastHit": new Date().toISOString(),
"hideUntil": "2000-01-01T00:00:00Z"
};
}
}
function renderUsers() {
const container = document.getElementById("mw-content-text");
container.innerHTML = "";
const userArray = Object.keys(usersFetched).map(user => {
return {
"user": user,
"abuseHits": usersFetched[user].abuseHits,
"rollbacks": usersFetched[user].rollbacks,
"lastHit": usersFetched[user].lastHit,
"hideUntil": usersFetched[user].hideUntil
};
}).sort((a, b) => {
return new Date(b.lastHit) - new Date(a.lastHit);
});
userArray.forEach(user => {
if (user.abuseHits + user.rollbacks >= 3) {
const minsAgo = Math.floor((new Date().getTime() - new Date(user.lastHit).getTime()) / 60000);
container.innerHTML += `
<div>
<a href="/wiki/Special:Contributions/${user.user}" target="_blank">${user.user}</a><br>
<span>Abuse hits: ${user.abuseHits}</span> •
<span>Rollbacks: ${user.rollbacks}</span> •
<span>Last hit: ${minsAgo} minutes ago</span>
</div>
`;
}
})
}
if (mw.config.get("wgPageName") === "Special:BlankPage/VandalismScanner") {
document.getElementById("firstHeading").textContent = "Vandalism Scanner";
scanLogs();
} else {
mw.util.addPortletLink(
'p-navigation',
mw.util.getUrl('Special:BlankPage/VandalismScanner'),
'vscan',
'pt-vscan',
'vscan',
null,
'#pt-preferences'
);
}
})();