User:Gary/enhanced random article.js

// Generated by CoffeeScript 1.7.1

/*
	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.
 */
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[_i];
    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[_i];
    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 = ['disambig}}', 'dis}}', 'disambiguation}}', 'surname}}'];
  matched = false;
  matchedPattern = '';
  _ref = eRA.patterns;
  for (_i = 0, _len = _ref.length; _i < _len; _i++) {
    pattern = _ref[_i];
    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[page];
    articles.push(value);
  }
  eRA = enhancedRandomArticle;
  if (typeof eRA.patterns === 'string') {
    eRA.patterns = [eRA.patterns];
  }
  for (_i = 0, _len = articles.length; _i < _len; _i++) {
    article = articles[_i];
    content = article['revisions'][0]['*'];
    title = article['title'];
    matched = checkRandomMatch(eRA, title, content);
    if (matched) {
      window.location = '/wiki/' + 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') + ' [ctrl-x]');
  }
};

$(addEnhancedRandomArticlePortletLink);

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

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.