Jump to content

User:Equazcion/assesslinks.js

From Wikipedia, the free encyclopedia

This is the current revision of this page, as edited by Ladsgroup (talk | contribs) at 15:00, 6 March 2021 (Maintenance: Replacing legacy global variable (phab:T72470)). The present address (URL) is a permanent link to this version.

(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)
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.
$(document).ready(function () {

/*
if (typeof(unsafeWindow) != 'undefined'){
	console = unsafeWindow.console;
	importStylesheet = unsafeWindow.importStylesheet;
	mw = unsafeWindow.mw;
	sajax_init_object = unsafeWindow.sajax_init_object;
} */

var showOldPeerReviews = false;

$('<li/>', {'id': 't-assess-article-links'}).appendTo('#p-tb-list');
$('<a/>', {
	'title': 'Find the assessment of all articles linked and color the links',
	'href': '#assessLinks',
	'text': 'Assess links'
}).click(function(){assessLinks()}).appendTo('#t-assess-article-links');

assessLinks(); //run on load for debug, instead of waiting for link click

function assessLinks () {
	importStylesheet('User:Pyrospirit/metadata/assesslinks.css');
	
	$('#mw-content-text a').each(function(){
		var pre = location.protocol + '//' + wgPageContentLanguage + '.' + wgNoticeProject + '.org' + '/w/index.php';
		var href = $(this).attr('href');
		var localUrl = mw.config.get('wgServer') + mw.config.get('wgArticlePath').replace('$1', '');
        var titlePre = '';
		if (href.replace(/#.*/, '') != document.location.href && RegExp('^' + localUrl).test(href)) {
			if (!/^[a-z]+([_ ]talk)?:[^_ ]/i.test(href.replace(localUrl, ''))) {
				titlePre = 'Talk:';
			}
		}
		if ( $(this).attr('title').length > 0 ){
			var url1 = ( titlePre + $(this).attr('title') ).replace(/ /g,'_');
			var request1 = {
				title:  encodeURIComponent(url1),
				action: 'raw',
				section: 0
			}
			$.get(pre, request1, function(response1){
				var assess = {rating: 'none', pageLink: [null, null], extra: [], activeReview: null};
				assess.rating = getRating(response1);
				var newClass = talkAssess(assess).newClass;
				$(this).addClass('assess-wikilink').addClass(newClass);
			});
		}
	});
}

function getExtraInfo (extra, pageLink) {
	var info = [];
	var page = encodePageName(mw.config.get('wgPageName'));
	if (extra.indexOf('fac') != -1) {
		info.push(makeInfoString('Currently a', pageLink[0], 'Wikipedia:Featured_article_candidates/'
			+ page, 'featured article candidate', null));
	} else if (extra.indexOf('flc') != -1) {
		info.push(makeInfoString('Currently a', pageLink[0], 'Wikipedia:Featured_list_candidates/'
			+ page, 'featured list candidate', null));
	} else if (extra.indexOf('gan') != -1) {
		info.push(makeInfoString('Currently a', pageLink[0], 'Wikipedia:Good_article_nominations',
			'good article nominee', null));
	} else if (extra.indexOf('far') != -1) {
		info.push(makeInfoString('Currently undergoing', pageLink[0], 'Wikipedia:Featured_article_review/'
			+ page, 'review', 'of its featured status'));
	} else if (extra.indexOf('flrc') != -1) {
		info.push(makeInfoString('Currently a', pageLink[0], 'Wikipedia:Featured_list_removal_candidates/'
			+ page, 'candidate', 'for removal as a featured list'));
	} else if (extra.indexOf('gar') != -1) {
		info.push(makeInfoString('<span id="assess-gar-link">Currently undergoing a', pageLink[0],
			'Wikipedia:Good_article_reassessment', 'good article reassessment', '<\/span>', true));
	}
	if (extra.indexOf('ffa') != -1) {
		info.push(makeInfoString('A', pageLink[1], 'Wikipedia:Featured_article_review/' + page,
			'former', 'featured article'));
	} else if (extra.indexOf('ffl') != -1) {
		info.push(makeInfoString('A', pageLink[1], 'Wikipedia:Featured_list_removal_candidates/'
			+ page, 'former', 'featured list'));
	} else if (extra.indexOf('dga') != -1) {
		info.push(makeInfoString('A', pageLink[1], 'Wikipedia:Good_article_reassessment',
			'delisted', 'good article'));
	} else if (extra.indexOf('ffac') != -1) {
		info.push(makeInfoString('A former', pageLink[1], 'Wikipedia:Featured_article_candidates/'
			+ page, 'featured article candidate', null));
	} else if (extra.indexOf('fflc') != -1) {
		info.push(makeInfoString('A former', pageLink[1], 'Wikipedia:Featured_list_candidates/'
			+ page, 'featured list candidate', null));
	} else if (extra.indexOf('fgan') != -1) {
		info.push(makeInfoString('A former', pageLink[1], 'Wikipedia:Good_article_nominations',
			'good article nominee', null));
	}
	return info;
}

function getRating (text) {
    callHooks('getRating_before');
    var rating = 'none';
    if (text.match(/\|\s*(class|currentstatus)\s*=\s*fa\b/i))
        rating = 'fa';
    else if (text.match(/\|\s*(class|currentstatus)\s*=\s*fl\b/i))
        rating = 'fl';
    else if (text.match(/\|\s*class\s*=\s*a\b/i)) {
        if (text.match(/\|\s*class\s*=\s*ga\b|\|\s*currentstatus\s*=\s*(ffa\/)?ga\b/i))
            rating = 'a/ga'; // A-class articles that are also GA's
        else rating = 'a';
    } else if (text.match(/\|\s*class\s*=\s*ga\b|\|\s*currentstatus\s*=\s*(ffa\/)?ga\b|\{\{\s*ga\s*\|/i)
               && !text.match(/\|\s*currentstatus\s*=\s*dga\b/i))
        rating = 'ga';
    else if (text.match(/\|\s*class\s*=\s*b\b/i))
        rating = 'b';
    else if (text.match(/\|\s*class\s*=\s*bplus\b/i))
        rating = 'bplus'; // used by WP Math
    else if (text.match(/\|\s*class\s*=\s*c\b/i))
        rating = 'c';
    else if (text.match(/\|\s*class\s*=\s*start/i))
        rating = 'start';
    else if (text.match(/\|\s*class\s*=\s*stub/i))
        rating = 'stub';
    else if (text.match(/\|\s*class\s*=\s*list/i))
        rating = 'list';
    else if (text.match(/\|\s*class\s*=\s*sl/i))
        rating = 'sl'; // used by WP Plants
    else if (text.match(/\|\s*class\s*=\s*(dab|disambig)/i))
        rating = 'dab';
    else if (text.match(/\|\s*class\s*=\s*cur(rent)?/i))
        rating = 'cur';
    else if (text.match(/\|\s*class\s*=\s*future/i))
        rating = 'future';
    callHooks('getRating_after');
    return rating;
}

function talkAssess (assess) {
    callHooks('talkAssess_before');
    var path = mw.config.get('wgArticlePath').replace('$1', '');
    var assessLink = path + 'Wikipedia:Version_1.0_Editorial_Team/Assessment';
    if (typeof assess.extra === 'undefined') assess.extra = '';
    var extra = assess.extra, rating = assess.rating;
    var pageLink = assess.pageLink ? [encodePageName(assess.pageLink[0]),
        encodePageName(assess.pageLink[1])] : [null, null];
    var peerReview = encodePageName(assess.review);
    var info = getExtraInfo(extra, pageLink);
    var peerReviewText = addPeerReview(peerReview, assess.activeReview);
    if (peerReviewText)
        info.push(peerReviewText);
    var newClass, slogan;
    if (rating == 'a' || rating == 'a/ga') {
        newClass = 'assess-a-text';
        slogan = 'An <a href="' + assessLink + '">A-class</a> article';
        if (rating == 'a/ga') {
            info.push('Also a <a href="' + path + 'Wikipedia:Good_Articles">good article</a>.');
        }
    } else if (rating == 'ga') {
        newClass = 'assess-ga-text';
        slogan = 'A <a href="' + path + 'Wikipedia:Good_Articles">good article</a>'
    } else if (rating == 'b') {
        newClass = 'assess-b-text';
        slogan = 'A <a href="' + assessLink + '">B-class</a> article';
    } else if (rating == 'bplus') {
        newClass = 'assess-bplus-text';
        slogan = 'A <a href="' + path + 'Wikipedia:WikiProject_Mathematics/Wikipedia_1.0'
            + '/Grading_scheme">B-plus-class</a> article';
    } else if (rating == 'c') {
        newClass = 'assess-c-text';
        slogan = 'A <a href="' + assessLink + '">C-class</a> article';
    } else if (rating == 'start') {
        newClass = 'assess-start-text';
        slogan = 'A <a href="' + assessLink + '">start-class</a> article';
    } else if (rating == 'stub') {
        newClass = 'assess-stub-text';
        slogan = 'A <a href="' + assessLink + '">stub-class</a> article';
    } else if (rating == 'sl') {
        newClass = 'assess-sl-text';
        slogan = 'A <a href="' + assessLink + '">stub-class</a> list';
    } else if (rating == 'list') {
        newClass = 'assess-list-text';
        slogan = 'A <a href="' + path + 'Wikipedia:Lists">list-class</a> article';
    } else if (rating == 'dab') {
        newClass = 'assess-dab-text';
        slogan = 'A <a href="' + path + 'Wikipedia:Disambiguation">disambiguation page</a>';
    } else if (rating == 'setindex') {
        newClass = 'assess-setindex-text';
        slogan = 'A <a href="' + path + 'Wikipedia:Disambiguation#Set_index_articles">'
            + 'set index article</a>';
    } else if (rating == 'redir') {
        newClass = 'assess-redir-text';
        slogan = 'A <a href="' + path + 'Help:Redirect">redirect page</a>';
    } else if (rating == 'fl') {
        newClass = 'assess-fl-text';
        slogan = 'A <a href="' + path + 'Wikipedia:Featured_lists">featured list</a>';
    } else if (rating == 'fa') {
        newClass = 'assess-fa-text';
        slogan = 'A <a href="' + path + 'Wikipedia:Featured_articles">featured article</a>';
    } else if (rating == 'cur') {
        newClass = 'assess-cur-text';
        slogan = 'A <a href="' + path + 'Portal:Current_events">current-class</a> article';
    } else if (rating == 'future') {
        newClass = 'assess-future-text';
        slogan = 'A <a href="' + path + 'Category:Future-Class_articles">future-class</a>'
            + ' article';
    } else {
        newClass = 'assess-unassessed-text';
        slogan = 'An <a href="' + assessLink + '">unassessed</a> article';
    }
    callHooks('talkAssess_after');
    return {newClass: newClass, slogan: slogan, info: info};
}

function makeInfoString (start, pageLink, defLink, linkText, end, noEndSpace) {
    var output;
    var path = mw.config.get('wgArticlePath').replace('$1', '');
    var page = pageLink ? path + pageLink : (defLink ? path + defLink : null);
    start = start ? start.toString() + ' ' : '';
    linkText = linkText ? linkText.toString() : '';
    end = (end ? (noEndSpace ? '' : ' ') + end.toString() + '.' : '.');
    output = start + (page ? '<a href="' + page + '"' + (linkText ? '>' : ' \/>') : '')
        + linkText + ((page && linkText) ? '<\/a>' : '') + end;
    return output;
}

function makeInfoString (start, pageLink, defLink, linkText, end, noEndSpace) {
	var output;
	// path is usually just '/wiki/', but it's different on secure.wikimedia.org
	var path = mw.config.get('wgArticlePath').replace('$1', '');
	var page = pageLink ? path + pageLink : (defLink ? path + defLink : null);
	start = start ? start.toString() + ' ' : '';
	linkText = linkText ? linkText.toString() : '';
	end = (end ? (noEndSpace ? '' : ' ') + end.toString() + '.' : '.');
	output = start + (page ? '<a href="' + page + '"' + (linkText ? '>' : ' \/>') : '')
		+ linkText + ((page && linkText) ? '<\/a>' : '') + end;
	return output;
}

function encodePageName (inputText) {
    if (!inputText) return null;
    var outputText = encodeURIComponent(inputText);
    while (outputText != null && outputText.match(/(\%20|\%2F|\%253A)/i)) {
		outputText = outputText.replace(/\%20/i, '_'); // unescape spaces for readability
		outputText = outputText.replace(/\%2F/i, '\/'); // %2F must be unescaped
		outputText = outputText.replace(/\%253A/i, ':'); // "%253A" for special cases such as [[Metroid: Other M]]
    }
    return outputText;
}

function callHooks (hook) {
    for (funct in this[hook]) {
        this[hook][funct].call(this);
    }
}

function addHook (hook, funct) {
    if (typeof this[hook] === 'undefined')
        this[hook] = [];
    this[hook][this[hook].length] = funct;
    return this;
}

function decodeEntities (str) {
    var t = document.createElement("textarea");
    t.innerHTML = str;
    return t.value;
}

function addPeerReview (peerReview, activeReview) {
	var reviewText = null;
	var path = mw.config.get('wgArticlePath').replace('$1', '');
	if (peerReview) {
		reviewText = (activeReview
			? 'Currently being <a href="' + path + peerReview + '">peer reviewed</a>.'
			: 'Previously <a href="' + path + peerReview + '">peer reviewed</a>.');
		reviewText = '<span class="assess-info-review">' + reviewText + '</span>';
	}
	return reviewText;
}

function getGARLink (articleName, reviewNumber) {
	var communityTitle = 'Wikipedia:Good_article_reassessment\/' + articleName + '\/' + reviewNumber;
	var individualTitle = 'Talk:' + articleName + '\/GA' + reviewNumber;
	var request2 = {
		action: 'query',
		titles: communityTitle + '|' + individualTitle,
		prop: 'info',
		format: 'json'
	}
	$.get(mw.config.get("wgScriptPath")+"/api.php", request2, function(response2){
		var text = response2;
		if (JSON && JSON.parse) {
			var query = JSON.parse(text)['query'];
		} else {
			return;
		}
		var communityTitleNorm = communityTitle;
		var individualTitleNorm = individualTitle;
		var len = query['normalized'].length;
		for (var j = 0; j < len; j++) {
		    switch (query['normalized'][j]['from']) {
		        case altTitles[0]:
		            communityTitleNorm = query['normalized'][j]['to'];
		            break;
		        case altTitles[1]:
		            individualTitleNorm = query['normalized'][j]['to'];
		            break;
		    }
		}
		var noCommunityAssessment = false;
		for (var i = -1; i >= -2; i--) {
			if (query['pages'][i] && typeof query['pages'][i]['missing'] === 'string') {
				if (query['pages'][i]['title'] == individualTitleNorm) {
					return;
				} else if (query['pages'][i]['title'] == communityTitleNorm) {
					noCommunityAssessment = true;
				}
			}
		}
		var garLink = $('#assess-gar-link a:first');
		if (noCommunityAssessment && garLink.length){
			garLink.attr('href', mw.config.get('wgArticlePath').replace('$1', '') + altTitles[1]);
		}
	});
	return communityTitle;
}

});