Jump to content

User:Terasail/ArticleInfo.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Terasail (talk | contribs) at 15:27, 16 June 2021 (Fix error caused by IP's creating the page (Undefined edit count)). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
//<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> &#183; ';
			if (watchers != null) {
				watchers = addCommas(watchers);
				outStrTwo += "Watchers: " + watchers + " &#183; ";
			}
			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 += " &#183; " + 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 &#183; ";
	}
	if (totalRevisions == 1) {
		outStr.replace("Revisions", "Revision");
	}
	if (totalEditors != null) {
		outStr += addCommas(totalEditors) + " Editors &#183; ";
	}
	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>