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>
Article Info
Created by: Terasail
*/
// jshint esnext: false, esversion: 8
var wgArticleIdAI = mw.config.get("wgArticleId");
var wgServerNameAI = mw.config.get("wgServerName");
var wgpageNameAI = mw.config.get("wgPageName");
var wgActionAI = mw.config.get("wgAction");
var api = new mw.Api();
if (typeof(mw.config.values.wgRelevantUserName) != "undefined") {
let pageUsr = mw.config.values.wgRelevantUserName;
api.get({
action: "query",
list: "users",
ususers: pageUsr,
usprop: "editcount|groups|implicitgroups",
format: "json"
}).done(function(data) {
let userData = data.query.users[0];
let outStr = '<div id="usrInfoBar" margin:5px 0px">User links: ';
outStr += constructUser(userData, wgServerNameAI, true);
//outStr += ' (<a style="font-size:90%" href="/wiki/Special:ListUsers?limit=1&username=' + pageUsr + '">Rights</a> / ';
//outStr += '<a style="font-size:90%" href="/wiki/Special:Log?type=rights&user=&page=User%3A' + pageUsr + '&wpdate=&tagfilter=&subtype=">Log</a>) ';
//outStr += ' (<a style="font-size:90%" href="/wiki/Special:Contributions/' + pageUsr + '">Contributions</a> / ';
//outStr += '<a style="font-size:90%" href="/wiki/Special:Log/' + pageUsr + '">Logs</a>) ';
outStr += ' (<a style="font-size:90%" href="/wiki/Special:CentralAuth/' + pageUsr + '">Accounts</a>)</div>';
addHtml(outStr, "usrInfoBar", "siteSub");
});
}
if (wgArticleIdAI != 0 && (wgActionAI == "view" || wgActionAI == "edit")) {
let assessments = {NA:["6/6a/Symbol_na_class"], Draft:["0/09/Symbol_draft_class"], Template:["5/5c/Symbol_template_class"], Redirect:["a/a2/Symbol_redirect_class", "c"], Project:["7/72/Symbol_project_class"], Portal:["e/e2/Symbol_portal_class"], File:["9/9c/Symbol_file_class"], Disambig:["2/2a/Symbol_dab_class", "c"], Category:["9/96/Symbol_category_class"], 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", "c"], GA:["9/94/Symbol_support_vote"], FM:["b/bc/Featured_article_star", "c"], FL:["b/bc/Featured_article_star", "c"], FA:["b/bc/Featured_article_star", "c"]};
let 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", "c"], templateeditor:["5/53/Template-protection-shackle"], extendedconfirmed:["8/8c/Extended-protection-shackle"], autoconfirmed:["1/1b/Semi-protection-shackle"]};
api.get({
action: "query",
prop: "pageviews|info|revisions|pageassessments",
pageids: wgArticleIdAI,
pvipdays: "30",
inprop: "protection|watchers",
rvprop: "timestamp|user",
rvlimit: "1",
rvdir: "newer",
palimit: "1",
format: "json"
}).done(function(data) {
data = data.query.pages[wgArticleIdAI];
x = data;
let protection = data.protection;
let pageviews = Object.entries(data.pageviews);
let firstUser = data.revisions[0].user;
let firstTime = new Date(data.revisions[0].timestamp).toUTCString().slice(5, 16);
let watchers = data.watchers;
let totalViews = 0;
let assessment = data.pageassessments;
for (let i = 0; i < pageviews.length; i++) {
if (typeof pageviews[i][1] == "number") {
totalViews += pageviews[i][1];
}
}
api.get({
action: "query",
list: "users",
ususers: firstUser,
usprop: "editcount|groups|implicitgroups",
format: "json"
}).done(function(data) {
let icons = '<div id="apageInfoBar" style="font-size:90%; margin:5px 0px">';
if (assessment != null) {
assessment = createImg(assessments[Object.entries(assessment)[0][1].class], false);
icons += assessment + " ";
}
for (let i = 0; i < protection.length; i++) {
let protType = protection[i].type;
let protLevel = protection[i].level;
if (icons.includes("Cascade") == false) {
if (protType == "move" || protType == "upload") {
icons += createImg(editLocks[protType], true);
} else if (protType == "edit") {
if (icons.includes("protection") == false) {
icons += createImg(editLocks[protLevel], true);
} else {
icons += createImg(editLocks.cascade, true);
}
}
}
}
if (icons.includes("<img")) {
icons += " ";
}
let mainStringAI = ' <abbr title="Pageviews in the last 30 days">Pageviews</abbr> · ';
mainStringAI = addCommas(totalViews) + mainStringAI;
if (watchers != null) {
mainStringAI += "Watchers: " + addCommas(watchers) + " · ";
}
mainStringAI += "Created on " + firstTime + ", by " + constructUser(data.query.users[0], wgServerNameAI, false);
let pageStats = newTab("Xarticleinfo/" + wgServerNameAI + wgpageNameAI, "Page information");
mainStringAI += " · " + pageStats + "</div>";
mainStringAI = mainStringAI.replaceAll("undefined", "0");
concatageInfo(icons, mainStringAI);
});
});
}
function constructUser(userData, wgServerNameAI, longRights) {
let 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": "IPE",
abusefilter: "EFM",
"abusefilter-helper": "EFH",
"massmessage-sender": "MMS",
sysop: "A",
bureaucrat: "B",
steward: "S",
"interface-admin": "IA",
suppress: "O",
checkuser: "CU",
bot: "BOT",
translationadmin: "TA",
"import": "IM",
propertycreator: "PROP",
gwtoolset: "GWT",
transwiki: "TW",
uploader: "U",
centralnoticeadmin: "CNA",
oauthadmin: "OAA",
"global-renamer": "GR",
flood: "F",
"image-reviewer": "IR",
upwizcampeditors: "UWC",
"machinevision-tester": "MVT"};
let usrDtaName = userData.name;
let groups = userData.groups;
let editCount = newTab("Xec/" + wgServerNameAI + usrDtaName, addCommas(userData.editcount));
let usrStr = newTab("U" + usrDtaName, usrDtaName) + " ";
if (groups != null) {
groups = groups.filter(function(usrRight) {
if (userData.implicitgroups.includes(usrRight)) { return false; }
return true;
});
}
if (userData.invalid == null) {
usrStr += "(" + editCount + ") ";
}
if (groups != null && groups.length > 0) {
usrStr += '[<span style="font-size:90%">';
let numRights = 0;
let listRights = "";
for (let j = 0; j < groups.length; j++) {
numRights++;
let rightsStr = userRightsList[groups[j]];
if (rightsStr == null) {
rightsStr = groups[j];
}
listRights += rightsStr + ", ";
}
if (longRights) {
usrStr += listRights;
} else {
usrStr += numRights + " right";
if (numRights != 1) { usrStr += "s"; }
}
usrStr += "</span>]";
usrStr = usrStr.replace(", </", "</");
}
return usrStr;
}
function createImg(data, protection) {
if (data != null) {
if (data.length == 1) {
data[1] = "en";
} else if (data[1] == "c") {
data[1] = "commons";
}
let imageStr = '<a href="/wiki/File:' + data[0].slice(5) + '.svg">';
if (protection) {
imageStr = '<a target="_blank" href="/w/index.php?title=Special:Log&type=protect&page=' + wgpageNameAI + '">';
}
imageStr += '<img src="/media/wikipedia/' + data[1] + '/thumb/';
imageStr += data[0] + ".svg/25px-" + data[0].slice(5);
imageStr += '.svg.png" decoding="async" width="18" height="18"></a>';
return imageStr;
}
}
function newTab(page, msg) {
let target = "<a ";
if (page.slice(0,1) == "X") {
target += 'target="_blank" href="https://xtools.wmflabs.org/';
} else {
target += 'href="/wiki/User:';
}
return target + page.slice(1).replace(/.org(?=\w)/, ".org/") + '">' + msg + "</a>";
}
function addHtml(mainStringAI, id, target) {
let bodyElm = document.getElementById("bodyContent");
$(bodyElm).prepend(mainStringAI);
}
async function concatageInfo(icons, mainStringAI) {
try {
let numUsrs = await fetch("https://" + wgServerNameAI + "/w/rest.php/v1/page/" + wgpageNameAI.replaceAll("/", "%2F") + "/history/counts/editors?");
let numRev = await fetch("https://" + wgServerNameAI + "/w/rest.php/v1/page/" + wgpageNameAI.replaceAll("/", "%2F") + "/history/counts/edits?");
numUsrs = await numUsrs.json();
numRev = await numRev.json();
numUsrs = addCommas(numUsrs.count) + " Editor";
numRev = addCommas(numRev.count) + " Revision";
if (numUsrs != "1 Editor") { numUsrs += "s"; }
if (numRev != "1 Revision") { numRev += "s"; }
mainStringAI = icons + numRev + " · " + numUsrs + " · " + mainStringAI;
mainStringAI = mainStringAI.replaceAll(/30,000(?= (Revision|Editor))/g, "30,000+");
mainStringAI = mainStringAI.replace(/ *undefined */, "");
addHtml(mainStringAI, "apageInfoBar", "usrInfoBar");
} catch( err ) {
console.error(err.message);
}
}
function addCommas(number) {
if (typeof(number) == "number") {
let numString = number.toLocaleString('en-US');
return numString;
}
return number;
}
//</nowiki>[[Category:Wikipedia scripts]]