User:Gary/enhanced random article.coffee
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.
TODO - Match patterns to NOT show.
enhancedRandomArticle = {} unless enhancedRandomArticle?
if typeof(unsafeWindow) isnt 'undefined' addPortletLink = unsafeWindow.addPortletLink mw = unsafeWindow.mw
enhancedRandomArticleBegin = -> ### DEFAULT SETTINGS
enhancedRandomArticle = { patterns: [], showDisambiguationPages: false, showStubs: false }
enhancedRandomArticle = {} enhancedRandomArticle.patterns = [] enhancedRandomArticle.showDisambiguationPages = false enhancedRandomArticle.showStubs = false ###
enhancedRandomArticle = {} unless enhancedRandomArticle? enhancedRandomArticle.patterns = [] unless enhancedRandomArticle.patterns? enhancedRandomArticle.showDisambiguationPages = false unless enhancedRandomArticle.showDisambiguationPages? enhancedRandomArticle.showStubs = false unless enhancedRandomArticle.showStubs?
$.get(mw.config.get('wgScriptPath') + '/api.php', { action: 'query', list: 'random', rnnamespace: 0, format: 'json', rnlimit: 10 }, eRCreateArticleList)
eRCreateArticleList = (obj) -> articles = obj.query.random articlesArray = []
for article in articles articlesArray.push(article.title)
$.get(mw.config.get('wgScriptPath') + '/api.php', { action: 'query', prop: 'revisions', rvdir: 'older', rvprop: 'content', titles: articlesArray.join('|'), format: 'json' }, eRCheckMatches)
matchDisambig = (possiblePatterns, content) -> for pattern in possiblePatterns matchExpr = new RegExp(pattern, 'i') if content.match(matchExpr) return true false
checkRandomMatch = (userSettings, title, content) -> eRA = userSettings disambiguationMatches = ['disambig}}', 'dis}}', 'disambiguation}}', 'surname}}'] # Primary, and secondary matched = false # TRUE indicates we found an article we want matchedPattern =
for pattern in eRA.patterns # Found a pattern in an article's title if (title.match(pattern)) matched = true matchedPattern = pattern break
# User wants disambig and stubs, has no patterns if not eRA.patterns.length and eRA.showDisambiguationPages and eRA.showStubs console.log(1) matched = true
# User doesn't want disambig, gets disambig if matched and not eRA.showDisambiguationPages and matchDisambig(disambiguationMatches, content) matched = false else if not eRA.patterns.length and not eRA.showDisambiguationPages and not matchDisambig(disambiguationMatches, content) console.log(2) matched = true
# User doesn't want stubs, gets stub if matched and not eRA.showStubs and content.match(/stub}}/i) matched = false else if not eRA.patterns.length and not eRA.showStubs and not content.match(/stub}}/i) console.log(3) matched = true
matched
eRCheckMatches = (obj) -> articles = []
for page, value of obj.query.pages articles.push(value)
# match patterns eRA = enhancedRandomArticle if typeof(eRA.patterns) is 'string' eRA.patterns = [eRA.patterns]
for article in articles content = article['revisions'][0]['*'] title = article['title'] matched = checkRandomMatch(eRA, title, content)
if matched window.location = '/wiki/' + title return
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.")
addEnhancedRandomArticlePortletLink = -> if $('#p-navigation').length mw.util.addPortletLink('p-navigation', '#', 'Enhanced random', 't-enhanced-random-article', 'Show a random article, with enhanced settings', , '#n-randompage')
$('#t-enhanced-random-article').on('click', (event) -> event.preventDefault() enhancedRandomArticleBegin() )
# access key if $('#n-randompage').length randomPageLink = $('#n-randompage').children().eq(0) randomPageLink.attr('accesskey', null)
node = $('#t-enhanced-random-article') nodeLink = node.children().eq(0)
nodeLink.attr('accesskey', 'x') nodeLink.attr('title', nodeLink.attr('title') + ' [ctrl-x]')
$(addEnhancedRandomArticlePortletLink)
if typeof(unsafeWindow) isnt 'undefined' unsafeWindow.checkRandomMatch = checkRandomMatch unsafeWindow.enhancedRandomArticle = enhancedRandomArticle unsafeWindow.enhancedRandomArticleBegin = enhancedRandomArticleBegin unsafeWindow.eRCheckMatches = eRCheckMatches unsafeWindow.eRCreateArticleList = eRCreateArticleList unsafeWindow.matchDisambig = matchDisambig