Jump to content

User:EpochFail/ArticleQuality.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by EpochFail (talk | contribs) at 19:38, 18 July 2018. 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.
articleQuality = {};
(function($, mw, OO, articleQuality){
	var ORES_HOST = "https://ores.wikimedia.org";
	var CLASS_WEIGHTS = {Stub: 1, Start: 2, C: 3, B: 4, GA: 5, FA: 6};
	var revisionNodes = $('#pagehistory li');
	var computeWeightedSum = function(data, revId){
		var clsProba = data.enwiki.scores[revId].wp10.score.probability;
		var weightedSum = 0;
		for (var cls in clsProba) {
			if (clsProba.hasOwnProperty(cls)) {
				var proba = clsProba[cls];
				weightedSum += proba * CLASS_WEIGHTS[cls];
			}
		}
		return weightedSum;
	};
	var getAndRenderQualityPrediction = function(node){
		var revId = node.attr('data-mw-revid');
		var url = ORES_HOST + "/v3/scores/enwiki/" + revId + "/wp10";
		var qualityPredictionNode = $("<div>").addClass("qualityprediction");
		console.log("Requesting quality for revid=" + revId);
		node.append(qualityPredictionNode);
		var renderQualityPrediction = function(data){
			var weightedSum = computeWeightedSum(data, revId);
			var prediction = data.enwiki.scores[revId].wp10.score.prediction;
			var level = Math.round(weightedSum);
			qualityPredictionNode.addclass("level_" + level);
			qualityPredictionNode.append(it$("<div>").addClass("bar").css("width", (weightedSum/6)*100));
			qualityPredictionNode.attr('title', "ORES prediction " + prediction + " (" + Math.round(weightedSum, 2) + ")");
		};
		$.ajax(url, {success: renderQualityPrediction.bind(this),
		         error: function(jqxmlhr, status, error){console.log(status + ": " + error)}});
	};
	articleQuality.getAndRenderArticleQuality = function(){
		revisionNodes.each(function(i, element){getAndRenderQualityPrediction($(this))});
	};
	
})(jQuery, mediaWiki, OO, articleQuality);