User:Nomeata/enhanced random article.js

/*	ENHANCED RANDOM ARTICLE Documentation: Enhanced Random Article Description: Enhances the existing "Random article" feature. Disambiguation pages, redirects, and pages with titles that match a certain pattern can be skipped over. TODO - Match patterns to NOT show.

if (typeof(enhancedRandomArticle) == 'undefined') enhancedRandomArticle = {};

if (typeof(unsafeWindow) != 'undefined') {	var addPortletLink = unsafeWindow.addPortletLink; var mw = unsafeWindow.mw; }

enhancedRandomArticleBegin = function {	/*		DEFAULT SETTINGS enhancedRandomArticle = {}; enhancedRandomArticle.patterns = []; enhancedRandomArticle.showDisambiguationPages = false; enhancedRandomArticle.showStubs = false; */	if (typeof(enhancedRandomArticle) == 'undefined') enhancedRandomArticle = {}; if (typeof(enhancedRandomArticle.patterns) == 'undefined') enhancedRandomArticle.patterns = []; if (typeof(enhancedRandomArticle.showDisambiguationPages) == 'undefined') enhancedRandomArticle.showDisambiguationPages = false; if (typeof(enhancedRandomArticle.showStubs) == 'undefined') enhancedRandomArticle.showStubs = false; $.get(mw.config.get('wgScriptPath') + '/api.php', { action: 'query', list: 'random', rnnamespace: 0, format: 'json', rnlimit: 10 }, eRCreateArticleList); }

eRCreateArticleList = function(obj) {	var articles = obj.query.random; var articlesArray = []; for (var i = 0; i < articles.length; i++) articlesArray.push(articles[i].title); $.get(mw.config.get('wgScriptPath') + '/api.php', { action: 'query', prop: 'revisions', rvdir: 'older', rvprop: 'content', titles: articlesArray.join('|'), format: 'json' }, eRCheckMatches); }

eRCheckMatches = function(obj) {	var articles = []; for (var page in obj.query.pages) articles.push(obj.query.pages[page]); // match patterns var content, pattern, title; var matched = false; var matchedPattern = ''; var eRA = enhancedRandomArticle; if (typeof(eRA.patterns) == 'string') eRA.patterns = [eRA.patterns];

for (var j = 0; j < articles.length; j++) {		content = articles[j]['revisions'][0]['*']; title = articles[j]['title']; matched = true;

// User doesn't want disambig, gets disambig if (!eRA.showDisambiguationPages && (content.match(/disambig}}/i) || content.match(/dis}}/i) || content.match(/disambiguation}}/i))) matched = false; // User doesn't want stubs, gets stub if (!eRA.showStubs && content.indexOf('stub}}') != -1) matched = false; // We do not want certain categories if (content.match(/Category:People/i)) matched = false; if (content.match(/Persondata/i)) matched = false; if (content.match(/Geobox/i)) matched = false; if (matched) {			window.location = '/wiki/' + title; return; }		matched = false; matchedPattern = ''; }	alert("ENHANCED RANDOM ARTICLES\n\nAn article that meets all of your criteria could not be found. Please try again.\nAlso, perhaps consider using less restrictive criteria.\n\nKeep in mind that only 10 articles are retrieved each time this script is activated,\nso there's a good chance that most of them won't match your criteria if it's too strict."); }

function addEnhancedRandomArticlePortletLink {	if ($('#p-navigation').length) {		mw.util.addPortletLink('p-navigation', 'javascript:enhancedRandomArticleBegin;', 'Enhanced random', 't-enhanced-random-article', 'Show a random article, with enhanced settings', '', '#n-randompage'); if (!$('wpDiff').length && !$('.historysubmit').length) {			var node = $('#t-enhanced-random-article'); var nodeLink = node.children.eq(0); nodeLink.attr('accesskey', 'v'); nodeLink.attr('title', nodeLink.attr('title') + ' [ctrl-v]'); }	} }

$(document).ready(function {	addEnhancedRandomArticlePortletLink; });

if (typeof(unsafeWindow) != 'undefined') {	unsafeWindow.enhancedRandomArticle = enhancedRandomArticle; unsafeWindow.enhancedRandomArticleBegin = enhancedRandomArticleBegin; unsafeWindow.eRCheckMatches = eRCheckMatches; unsafeWindow.eRCreateArticleList = eRCreateArticleList; }