User:Terasail/ArticleInfo.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. |
![]() | This user script seems to have a documentation page at User:Terasail/ArticleInfo and an accompanying .css page at User:Terasail/ArticleInfo.css. |
//<nowiki>
// jshint esnext: false, esversion: 8
var mwconfig = mw.config.values;
var pageid = mwconfig.wgArticleId;
var totalEditors, totalRevisions = null;
var curServer = mwconfig.wgServerName;
var pageName = mwconfig.wgPageName;
if (pageid != 0 && (mwconfig.wgAction == "view" || mwconfig.wgAction == "edit")) {
var assessments = {
"": ["e/e0", "Symbol_question"],
NA: ["6/6a", "Symbol_na_class"],
Draft: ["0/09", "Symbol_draft_class"],
Template: ["5/5c", "Symbol_template_class"],
Redirect: ["8/89", "Symbol redirect vote2"],
Project: ["7/72", "Symbol_project_class"],
Portal: ["e/e2", "Symbol_portal_class"],
File: ["9/9c", "Symbol_file_class"],
Disambig: ["2/2a", "Symbol_dab_class", "commons"],
Category: ["9/96", "Symbol_category_class"],
Book: ["8/89", "Symbol_book_class2"],
List: ["d/db", "Symbol_list_class"],
Stub: ["f/f5", "Symbol_stub_class"],
Start: ["a/a4", "Symbol_start_class"],
C: ["e/e6", "Symbol_c_class"],
B: ["5/5f", "Symbol_b_class"],
A: ["2/25", "Symbol_a_class", "commons"],
GA: ["9/94", "Symbol_support_vote"],
FM: ["b/bc", "Featured_article_star", "commons"],
FL: ["b/bc", "Featured_article_star", "commons"],
FA: ["b/bc", "Featured_article_star", "commons"]
};
var userRightsList = {
extendedconfirmed: "EC",
reviewer: "PCR",
rollbacker: "R",
autopatrolled: "AP",
autoreviewer: "AP",
patroller: "NPR",
filemover: "FM",
extendedmover: "PM",
accountcreator: "AC",
eventcoordinator: "EvCo",
templateeditor: "TE",
"ipblock-exempt": "ip-E",
abusefilter: "EFM",
"abusefilter-helper": "EFH",
"massmessage-sender": "MMS",
sysop: "A",
bureaucrat: "B",
"interface-admin": "IA",
translationadmin: "TA",
oversight: "O",
checkuser: "CU",
bot: "BOT"
};
var editLocks = {
move: ["4/44", "Move-protection-shackle"],
upload: ["d/d7", "Upload-protection-shackle"],
cascade: ["0/0f", "Cascade-protection-shackle"],
sysop: ["1/12", "Full-protection-shackle-block", "commons"],
templateeditor: ["5/53", "Template-protection-shackle"],
extendedconfirmed: ["8/8c", "Extended-protection-shackle"],
autoconfirmed: ["1/1b", "Semi-protection-shackle"]
};
var api = new mw.Api();
api.get({
action: "query",
prop: "pageviews|info|revisions|pageassessments",
pageids: pageid,
pvipdays: "30",
inprop: "protection|watchers",
rvprop: "timestamp|user",
rvlimit: "1",
rvdir: "newer",
palimit: "1",
format: "json"
}).done(function(data) {
data = data.query.pages[pageid];
x = data;
var protection = data.protection;
var pageviews = Object.entries(data.pageviews);
var firstUser = data.revisions[0].user;
var firstTime = new Date(data.revisions[0].timestamp).toUTCString().slice(5, 16);
var watchers = data.watchers;
var totalViews = 0;
var assessment = data.pageassessments;
for (var i = 0; i < pageviews.length; i++) {
if (typeof pageviews[i][1] == "number") {
totalViews += pageviews[i][1];
}
}
totalViews = totalViews.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
api.get({
action: "query",
list: "users",
ususers: firstUser,
usprop: "editcount|groups|implicitgroups",
format: "json"
}).done(function(data) {
var userData = data.query.users[0];
var editCount = newTab("xtools.wmflabs.org/ec/" + curServer, firstUser, addCommas(userData.editcount));
var groups = userData.groups;
if (groups != null) {
groups = groups.filter(function(usrRight) {
if (userData.implicitgroups.includes(usrRight)) {
return false;
}
return true;
});
}
var outStrOne = '<div id="apageInfoBar" style="font-size:85%; margin:5px 0px">';
if (assessment != null) {
assessment = createImg(assessments[Object.entries(assessment)[0][1].class]);
outStrOne += assessment + " ";
}
for (var i = 0; i < protection.length; i++) {
var protType = protection[i].type;
var protLevel = protection[i].level;
if (outStrOne.includes("Cascade") == false) {
if (protType == "move" || protType == "upload") {
outStrOne += createImg(editLocks[protType]);
} else if (protType == "edit") {
if (outStrOne.includes("protection") == false) {
outStrOne += createImg(editLocks[protLevel]);
} else {
outStrOne += createImg(editLocks.cascade);
}
}
}
}
if (outStrOne.includes("<img")) {
outStrOne += " ";
}
var outStrTwo = totalViews + ' <abbr title="Pageviews in the last 30 days">Pageviews</abbr> · ';
if (watchers != null) {
watchers = addCommas(watchers);
outStrTwo += "Watchers: " + watchers + " · ";
}
outStrTwo += "Created on " + firstTime + ", ";
outStrTwo += "by " + newTab(curServer + "/wiki", "User:" + firstUser, firstUser) + " ";
if (userData.invalid == null) {
//editCount = addCommas(editCount);
outStrTwo += "(" + editCount + ") ";
}
if (groups != null && groups.length > 0) {
outStrTwo += '[<span style="font-size:90%">';
for (var j = 0; j < groups.length; j++) {
var rightsStr = userRightsList[groups[j]];
if (rightsStr == null) {
rightsStr = groups[j];
}
outStrTwo += rightsStr + ", ";
}
outStrTwo += "</span>]";
outStrTwo = outStrTwo.replace(", </", "</");
}
var pageStats = newTab("xtools.wmflabs.org/articleinfo/" + curServer, pageName, "Page information");
outStrTwo += " · " + pageStats + "</div>";
outStrTwo = outStrTwo.replaceAll("undefined", "0");
outputTopBar(outStrOne, outStrTwo);
});
});
}
function createImg(data) {
if (data != null) {
if (data.length == 2) {
data[2] = "en";
}
var imageStr = '<img src="/media/wikipedia/' + data[2] + '/thumb/';
imageStr += data[0] + "/" + data[1] + ".svg/25px-" + data[1];
imageStr += '.svg.png" width="18" height="18">';
return imageStr;
}
}
function addCommas(integerVal) {
if(typeof(integerVal != undefined)) {
integerVal = integerVal.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
} else {
integerVal = 0
}
return integerVal;
}
function newTab(server, page, text) {
var newLink = '<a target="_blank" href="https://' + server + '/' + page + '">';
newLink += text + '</a>';
return newLink;
}
async function fetchPageCount() {
try {
var numEditors = await fetch("https://" + curServer + "/w/rest.php/v1/page/" + pageName.replaceAll("/", "%2F") + "/history/counts/editors?");
var numRevisions = await fetch("https://" + curServer + "/w/rest.php/v1/page/" + pageName.replaceAll("/", "%2F") + "/history/counts/edits?");
totalEditors = await numEditors.json();
totalRevisions = await numRevisions.json();
totalEditors = totalEditors.count;
totalRevisions = totalRevisions.count;
} catch( err ) {
console.error( err.message );
}
}
async function outputTopBar(outStrOne, outStrTwo) {
await fetchPageCount();
var outStr = outStrOne;
if (totalRevisions != null) {
outStr += addCommas(totalRevisions) + " Revisions · ";
}
if (totalRevisions == 1) {
outStr.replace("Revisions", "Revision");
}
if (totalEditors != null) {
outStr += addCommas(totalEditors) + " Editors · ";
}
if (totalEditors == 1) {
outStr.replace("Editors", "Editor");
}
outStr = outStr.replace("30,000 Revisions", "30,000+ Revisions");
outStr = outStr.replace("25,000 Editors", "25,000+ Editors");
outStr += outStrTwo;
var bodyContent = document.getElementsByClassName("mw-body-header");
if (bodyContent.length > 0) {
outStr = outStr.replace("85%", "75%");
$(bodyContent[0]).append(outStr);
} else {
var bodyContent = document.getElementById("bodyContent");
var beforeElement = bodyContent.firstElementChild;
if (document.getElementById("siteSub") != null) {
beforeElement = beforeElement.nextElementSibling;
}
$(bodyContent).append(outStr);
bodyContent.insertBefore(document.getElementById("apageInfoBar"), beforeElement);
}
}
//</nowiki>