ApiQueryRandom.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. /*
  3. * Created on Monday, January 28, 2008
  4. *
  5. * API for MediaWiki 1.8+
  6. *
  7. * Copyright (C) 2008 Brent Garber
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write to the Free Software Foundation, Inc.,
  21. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  22. * http://www.gnu.org/copyleft/gpl.html
  23. */
  24. if (!defined('MEDIAWIKI')) {
  25. // Eclipse helper - will be ignored in production
  26. require_once ('ApiQueryBase.php');
  27. }
  28. /**
  29. * Query module to get list of random pages
  30. *
  31. * @ingroup API
  32. */
  33. class ApiQueryRandom extends ApiQueryGeneratorBase {
  34. public function __construct($query, $moduleName) {
  35. parent :: __construct($query, $moduleName, 'rn');
  36. }
  37. public function execute() {
  38. $this->run();
  39. }
  40. public function executeGenerator($resultPageSet) {
  41. $this->run($resultPageSet);
  42. }
  43. protected function prepareQuery($randstr, $limit, $namespace, &$resultPageSet, $redirect) {
  44. $this->resetQueryParams();
  45. $this->addTables('page');
  46. $this->addOption('LIMIT', $limit);
  47. $this->addWhereFld('page_namespace', $namespace);
  48. $this->addWhereRange('page_random', 'newer', $randstr, null);
  49. $this->addWhereFld('page_is_redirect', $redirect);
  50. $this->addOption('USE INDEX', 'page_random');
  51. if(is_null($resultPageSet))
  52. $this->addFields(array('page_id', 'page_title', 'page_namespace'));
  53. else
  54. $this->addFields($resultPageSet->getPageTableFields());
  55. }
  56. protected function runQuery(&$resultPageSet) {
  57. $db = $this->getDB();
  58. $res = $this->select(__METHOD__);
  59. $count = 0;
  60. while($row = $db->fetchObject($res)) {
  61. $count++;
  62. if(is_null($resultPageSet))
  63. {
  64. // Prevent duplicates
  65. if(!in_array($row->page_id, $this->pageIDs))
  66. {
  67. $fit = $this->getResult()->addValue(
  68. array('query', $this->getModuleName()),
  69. null, $this->extractRowInfo($row));
  70. if(!$fit)
  71. # We can't really query-continue a random list.
  72. # Return an insanely high value so
  73. # $count < $limit is false
  74. return 1E9;
  75. $this->pageIDs[] = $row->page_id;
  76. }
  77. }
  78. else
  79. $resultPageSet->processDbRow($row);
  80. }
  81. $db->freeResult($res);
  82. return $count;
  83. }
  84. public function run($resultPageSet = null) {
  85. $params = $this->extractRequestParams();
  86. $result = $this->getResult();
  87. $this->pageIDs = array();
  88. $this->prepareQuery(wfRandom(), $params['limit'], $params['namespace'], $resultPageSet, $params['redirect']);
  89. $count = $this->runQuery($resultPageSet);
  90. if($count < $params['limit'])
  91. {
  92. /* We got too few pages, we probably picked a high value
  93. * for page_random. We'll just take the lowest ones, see
  94. * also the comment in Title::getRandomTitle()
  95. */
  96. $this->prepareQuery(0, $params['limit'] - $count, $params['namespace'], $resultPageSet, $params['redirect']);
  97. $this->runQuery($resultPageSet);
  98. }
  99. if(is_null($resultPageSet)) {
  100. $result->setIndexedTagName_internal(array('query', $this->getModuleName()), 'page');
  101. }
  102. }
  103. private function extractRowInfo($row) {
  104. $title = Title::makeTitle($row->page_namespace, $row->page_title);
  105. $vals = array();
  106. $vals['id'] = intval($row->page_id);
  107. ApiQueryBase::addTitleInfo($vals, $title);
  108. return $vals;
  109. }
  110. public function getAllowedParams() {
  111. return array (
  112. 'namespace' => array(
  113. ApiBase :: PARAM_TYPE => 'namespace',
  114. ApiBase :: PARAM_ISMULTI => true
  115. ),
  116. 'limit' => array (
  117. ApiBase :: PARAM_TYPE => 'limit',
  118. ApiBase :: PARAM_DFLT => 1,
  119. ApiBase :: PARAM_MIN => 1,
  120. ApiBase :: PARAM_MAX => 10,
  121. ApiBase :: PARAM_MAX2 => 20
  122. ),
  123. 'redirect' => false,
  124. );
  125. }
  126. public function getParamDescription() {
  127. return array (
  128. 'namespace' => 'Return pages in these namespaces only',
  129. 'limit' => 'Limit how many random pages will be returned',
  130. 'redirect' => 'Load a random redirect instead of a random page'
  131. );
  132. }
  133. public function getDescription() {
  134. return array( 'Get a set of random pages',
  135. 'NOTE: Pages are listed in a fixed sequence, only the starting point is random. This means that if, for example, "Main Page" is the first ',
  136. ' random page on your list, "List of fictional monkeys" will *always* be second, "List of people on stamps of Vanuatu" third, etc.',
  137. 'NOTE: If the number of pages in the namespace is lower than rnlimit, you will get fewer pages. You will not get the same page twice.'
  138. );
  139. }
  140. protected function getExamples() {
  141. return 'api.php?action=query&list=random&rnnamespace=0&rnlimit=2';
  142. }
  143. public function getVersion() {
  144. return __CLASS__ . ': $Id: ApiQueryRandom.php overlordq$';
  145. }
  146. }