User:Machatjkala/common.js

/** * This script shows a popup message when an article has been edited, giving the * editor an indication of how the edit changed the quality of the article. * * ORES (https://www.mediawiki.org/wiki/ORES) is used to automatically assess * the quality of the article. The popup gives the editor the new quality and * the quality before the edit along with a symbol indicating how the quality * changed. * * Localization strings can be found at the begining of the script along with * configuration parameters. * * This script uses the Article Quality System developed by Aaron Halfaker * (https://meta.wikimedia.org/wiki/User:EpochFail). */

$(function { var i18n = {	close: "Stäng",	more: "Mer...",	previousVersionSuffix: "(föregående version)",	whatIsThis: "Vad är det här?",	apiResponse: "API-svar",	currentVersion: "Nuvarande version",	previousVersion: "Föregående version" }; // I18n strings that need to be parsed from wikitext. If a string contains no // wikitext syntax, add it to `i18n` instead. var i18nWikitext = {	helpText: "Värdena ovan är en automatisk bedömning av artikelns kvalitet, mellan 0 och 10. Bedömningen kan skilja sig från den kvalitet som tilldelats artikeln manuellt. Bedömningen har genererats med hjälp av ORES.",	predictionUp: "",	predictionDown: "",	predictionEqual: "" }; var config = {	ores_host: "https://ores.wikimedia.org",	weights: {s: 0, r: 6, b: 8, u: 10},	names: {		u: "20 px Utmärkt",		b: "20 px Bra",		r: "20 px Rekommenderad", s: "20 px Stub" },	assessment_system: "Kvalitetsnivåer", dbname: mw.config.get("wgDBname") };

var articleQuality; var hideTimeout; var $popup; var $newPrediction; var $oldPrediction; var $newRawData; var $oldRawDataContainer; var $oldRawData; var $predictionSymbol;

function prepareI18n { var promises = []; for(var key in i18nWikitext) { promises.push(parseI18nString(key)); }	return $.when.apply($, promises); }

function parseI18nString(key) { return articleQuality.parseText(i18nWikitext[key]) .then(function(html) {			i18n[key] = html;		}); }

function createPopup { $popup = $(" ").addClass("quality-assessment-popup"); var $popupContent = $(" ") .addClass("quality-assessment-content mw-notification") .appendTo($popup); var $predictions = $(" ") .addClass("quality-assessment-predictions") .appendTo($popupContent); var $predictionScores = $(" ") .appendTo($predictions); $newPrediction = $(" ") .addClass("quality-assessment-new-prediction") .appendTo($predictionScores); $oldPrediction = $(" ") .addClass("quality-assessment-old-prediction") .appendTo($predictionScores); $predictionSymbol = $(" ").appendTo($predictions); $(" ")		.html(i18n.previousVersionSuffix) .appendTo($oldPrediction); var $moreInformation = $(" ") .hide .appendTo($popupContent); $(" ").html(i18n.whatIsThis).appendTo($moreInformation); $(" ").html(i18n.helpText).appendTo($moreInformation); $(" ").html(i18n.apiResponse).appendTo($moreInformation); var $rawData = $(" ") .addClass("quality-assessment-raw-data") .appendTo($moreInformation); $oldRawDataContainer = $(" ").appendTo($rawData); $(" ")		.html(i18n.previousVersion) .appendTo($oldRawDataContainer); $oldRawData = $(" ").appendTo($oldRawDataContainer); var $newRawDataContainer = $(" ").appendTo($rawData); $(" ")		.html(i18n.currentVersion) .appendTo($newRawDataContainer); $newRawData = $(" ").appendTo($newRawDataContainer); $(" ")		.text(i18n.close) .click(function {			$popup.hide;		}) .appendTo($moreInformation); $(" ")		.text(i18n.more) .click(function(event) {			$moreInformation.show;			$(event.target).hide;			clearTimeout(hideTimeout);		}) .appendTo($popupContent); $popup.hide; $("body").prepend($popup); }

function addScoresToPopup { var title = mw.config.get("wgPageName"); var revId = mw.config.get("wgCurRevisionId"); return $.when(		getPreviousRevId(title)			.then(function(id) { if(id) { return articleQuality.oresScore(id); } else { return $.Deferred.resolve; }			})			.then(function(score) { if(score) { return addScore($oldPrediction, $oldRawData, score); } else { $oldPrediction.hide; $oldRawDataContainer.hide; return $.Deferred.resolve; }			}),		articleQuality.oresScore(revId)			.then(function(score) { return addScore($newPrediction, $newRawData, score); })	); }

function getPreviousRevId(title){ return articleQuality.mwApi.get({		action: "query",		prop: "revisions",		titles: title,		rvprop: "ids",		formatversion: 2	}) .then(function(data) {			return data.query.pages[0].revisions[0].parentid;		}); }

function addScore($prediction, $rawData, score) { $rawData.text(JSON.stringify(score, null, 2)); return renderScore($prediction,	score); }

function renderScore($element, score) { var prediction = articleQuality.extractPrediction(score); var weightedSum = Math.round(articleQuality.computeWeightedSum(score)*100)/100; var rawText = articleQuality.names[prediction] + " (" + weightedSum + ")"; return articleQuality.parseText(rawText) .then(function(html) {			$element.html(html + $element.html);			return weightedSum;		}); }

function addPredictionSymbol(oldScore, newScore) { var html; if(newScore > oldScore) { html = i18n.predictionUp; } else if(newScore < oldScore) { html = i18n.predictionDown; } else { html = i18n.predictionEqual; }	$predictionSymbol.html(html); }

mw.hook("postEdit").add(function(data) {	$.getScript( "//meta.wikimedia.org/w/index.php?title=User:EpochFail/ArticleQuality-system.js&action=raw&ctype=text/javascript", function { articleQuality = new ArticleQuality(config); prepareI18n .then(function {					createPopup;					return addScoresToPopup;				}) .then(function(oldScore, newScore) {					if(oldScore) {						// Show symbol only if there is an old score to compare						// with.						addPredictionSymbol(oldScore, newScore);					}					$popup.show;					hideTimeout = setTimeout(function { $popup.hide; }, 5000);				});		}	); }); }); importStylesheetURI("//sv.wikipedia.org/w/index.php?title=Anv%C3%A4ndare:Sebastian_Berlin_(WMSE)/articleQuality.css&action=raw&ctype=text/css");