User:Gary/enhanced random article.js

In this article, the topic of User:Gary/enhanced random article.js will be addressed from a broad and detailed perspective, with the aim of offering the reader a complete and updated vision of this topic of interest. Various aspects related to User:Gary/enhanced random article.js will be analyzed, including its origin, evolution, current impact and possible future perspectives. Likewise, different points of view and opinions from experts on the subject will be presented, in order to provide a comprehensive and enriching vision of User:Gary/enhanced random article.js. In addition, case studies and concrete examples will be presented that exemplify the relevance and importance of User:Gary/enhanced random article.js in different contexts. Through this article, we aim to provide the reader with a complete understanding of User:Gary/enhanced random article.js, so that they can deepen their knowledge and obtain a broad and detailed vision of this interesting topic.
// Generated by CoffeeScript 1.7.1

/*
	ENHANCED RANDOM ARTICLE
	Documentation: ]
	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.
 */
var addEnhancedRandomArticlePortletLink, addPortletLink, checkRandomMatch, eRCheckMatches, eRCreateArticleList, enhancedRandomArticle, enhancedRandomArticleBegin, matchDisambig, mw;

if (typeof enhancedRandomArticle === "undefined" || enhancedRandomArticle === null) {
  enhancedRandomArticle = {};
}

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

enhancedRandomArticleBegin = function() {

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

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

matchDisambig = function(possiblePatterns, content) {
  var matchExpr, pattern, _i, _len;
  for (_i = 0, _len = possiblePatterns.length; _i < _len; _i++) {
    pattern = possiblePatterns;
    matchExpr = new RegExp(pattern, 'i');
    if (content.match(matchExpr)) {
      return true;
    }
  }
  return false;
};

checkRandomMatch = function(userSettings, title, content) {
  var disambiguationMatches, eRA, matched, matchedPattern, pattern, _i, _len, _ref;
  eRA = userSettings;
  disambiguationMatches = ;
  matched = false;
  matchedPattern = '';
  _ref = eRA.patterns;
  for (_i = 0, _len = _ref.length; _i < _len; _i++) {
    pattern = _ref;
    if (title.match(pattern)) {
      matched = true;
      matchedPattern = pattern;
      break;
    }
  }
  if (!eRA.patterns.length && eRA.showDisambiguationPages && eRA.showStubs) {
    console.log(1);
    matched = true;
  }
  if (matched && !eRA.showDisambiguationPages && matchDisambig(disambiguationMatches, content)) {
    matched = false;
  } else if (!eRA.patterns.length && !eRA.showDisambiguationPages && !matchDisambig(disambiguationMatches, content)) {
    console.log(2);
    matched = true;
  }
  if (matched && !eRA.showStubs && content.match(/stub}}/i)) {
    matched = false;
  } else if (!eRA.patterns.length && !eRA.showStubs && !content.match(/stub}}/i)) {
    console.log(3);
    matched = true;
  }
  return matched;
};

eRCheckMatches = function(obj) {
  var article, articles, content, eRA, matched, page, title, value, _i, _len, _ref;
  articles = ;
  _ref = obj.query.pages;
  for (page in _ref) {
    value = _ref;
    articles.push(value);
  }
  eRA = enhancedRandomArticle;
  if (typeof eRA.patterns === 'string') {
    eRA.patterns = ;
  }
  for (_i = 0, _len = articles.length; _i < _len; _i++) {
    article = articles;
    content = article;
    title = article;
    matched = checkRandomMatch(eRA, title, content);
    if (matched) {
      window.location = 'https://wikifreehand.com/en/' + title;
      return;
    }
  }
  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 = function() {
  var node, nodeLink, randomPageLink;
  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', function(event) {
      event.preventDefault();
      return enhancedRandomArticleBegin();
    });
    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');
    return nodeLink.attr('title', nodeLink.attr('title') + ' ');
  }
};

$(addEnhancedRandomArticlePortletLink);

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