Jump to content

User:Gary/enhanced random article.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Gary (talk | contribs) at 22:42, 11 March 2011 (jquery update). 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.
/*
	ENHANCED RANDOM ARTICLE
	Documentation: [[Wikipedia: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.
*/

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'];
		
		for (var i = 0; i < eRA.patterns.length; i++) 
		{
			// Found a pattern in an article's title
			if (title.search(eRA.patterns[i]) != -1)
			{
				matched = true;
				matchedPattern = eRA.patterns[i];
				break;
			}
		}
		
		// User wants disambig and stubs, has no patterns
		if (!eRA.patterns.length && eRA.showDisambiguationPages && eRA.showStubs)
			matched = true;
		
		// User doesn't want disambig, gets disambig
		if (matched && !eRA.showDisambiguationPages && content.indexOf('{{disambig}}') != -1)
			matched = false;
		else if (!eRA.patterns.length && !eRA.showDisambiguationPages && content.indexOf('{{disambig}}') == -1)
			matched = true;
		
		// User doesn't want stubs, gets stub
		if (matched && !eRA.showStubs && content.indexOf('stub}}') != -1)
			matched = false;
		else if (!eRA.patterns.length && !eRA.showStubs && content.indexOf('stub}}') == -1)
			matched = true;
		
		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).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;
}