ApiQueryRandom.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php
  2. /**
  3. * Copyright © 2008 Brent Garber
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. */
  22. /**
  23. * Query module to get list of random pages
  24. *
  25. * @ingroup API
  26. */
  27. class ApiQueryRandom extends ApiQueryGeneratorBase {
  28. public function __construct( ApiQuery $query, $moduleName ) {
  29. parent::__construct( $query, $moduleName, 'rn' );
  30. }
  31. public function execute() {
  32. $this->run();
  33. }
  34. public function executeGenerator( $resultPageSet ) {
  35. $this->run( $resultPageSet );
  36. }
  37. /**
  38. * Actually perform the query and add pages to the result.
  39. * @param ApiPageSet|null $resultPageSet
  40. * @param int $limit Number of pages to fetch
  41. * @param string|null $start Starting page_random
  42. * @param int $startId Starting page_id
  43. * @param string|null $end Ending page_random
  44. * @return array (int, string|null) Number of pages left to query and continuation string
  45. */
  46. protected function runQuery( $resultPageSet, $limit, $start, $startId, $end ) {
  47. $params = $this->extractRequestParams();
  48. $this->resetQueryParams();
  49. $this->addTables( 'page' );
  50. $this->addFields( [ 'page_id', 'page_random' ] );
  51. if ( is_null( $resultPageSet ) ) {
  52. $this->addFields( [ 'page_title', 'page_namespace' ] );
  53. } else {
  54. $this->addFields( $resultPageSet->getPageTableFields() );
  55. }
  56. $this->addWhereFld( 'page_namespace', $params['namespace'] );
  57. if ( $params['redirect'] || $params['filterredir'] === 'redirects' ) {
  58. $this->addWhereFld( 'page_is_redirect', 1 );
  59. } elseif ( $params['filterredir'] === 'nonredirects' ) {
  60. $this->addWhereFld( 'page_is_redirect', 0 );
  61. } elseif ( is_null( $resultPageSet ) ) {
  62. $this->addFields( [ 'page_is_redirect' ] );
  63. }
  64. $this->addOption( 'LIMIT', $limit + 1 );
  65. if ( $start !== null ) {
  66. $start = $this->getDB()->addQuotes( $start );
  67. if ( $startId > 0 ) {
  68. $startId = (int)$startId; // safety
  69. $this->addWhere( "page_random = $start AND page_id >= $startId OR page_random > $start" );
  70. } else {
  71. $this->addWhere( "page_random >= $start" );
  72. }
  73. }
  74. if ( $end !== null ) {
  75. $this->addWhere( 'page_random < ' . $this->getDB()->addQuotes( $end ) );
  76. }
  77. $this->addOption( 'ORDER BY', [ 'page_random', 'page_id' ] );
  78. $result = $this->getResult();
  79. $path = [ 'query', $this->getModuleName() ];
  80. $res = $this->select( __METHOD__ );
  81. $count = 0;
  82. foreach ( $res as $row ) {
  83. if ( $count++ >= $limit ) {
  84. return [ 0, "{$row->page_random}|{$row->page_id}" ];
  85. }
  86. if ( is_null( $resultPageSet ) ) {
  87. $title = Title::makeTitle( $row->page_namespace, $row->page_title );
  88. $page = [
  89. 'id' => (int)$row->page_id,
  90. ];
  91. ApiQueryBase::addTitleInfo( $page, $title );
  92. if ( isset( $row->page_is_redirect ) ) {
  93. $page['redirect'] = (bool)$row->page_is_redirect;
  94. }
  95. $fit = $result->addValue( $path, null, $page );
  96. if ( !$fit ) {
  97. return [ 0, "{$row->page_random}|{$row->page_id}" ];
  98. }
  99. } else {
  100. $resultPageSet->processDbRow( $row );
  101. }
  102. }
  103. return [ $limit - $count, null ];
  104. }
  105. /**
  106. * @param ApiPageSet|null $resultPageSet
  107. */
  108. public function run( $resultPageSet = null ) {
  109. $params = $this->extractRequestParams();
  110. // Since 'filterredir" will always be set in $params, we have to dig
  111. // into the WebRequest to see if it was actually passed.
  112. $request = $this->getMain()->getRequest();
  113. if ( $request->getCheck( $this->encodeParamName( 'filterredir' ) ) ) {
  114. $this->requireMaxOneParameter( $params, 'filterredir', 'redirect' );
  115. }
  116. if ( isset( $params['continue'] ) ) {
  117. $cont = explode( '|', $params['continue'] );
  118. $this->dieContinueUsageIf( count( $cont ) != 4 );
  119. $rand = $cont[0];
  120. $start = $cont[1];
  121. $startId = (int)$cont[2];
  122. $end = $cont[3] ? $rand : null;
  123. $this->dieContinueUsageIf( !preg_match( '/^0\.\d+$/', $rand ) );
  124. $this->dieContinueUsageIf( !preg_match( '/^0\.\d+$/', $start ) );
  125. $this->dieContinueUsageIf( $cont[2] !== (string)$startId );
  126. $this->dieContinueUsageIf( $cont[3] !== '0' && $cont[3] !== '1' );
  127. } else {
  128. $rand = wfRandom();
  129. $start = $rand;
  130. $startId = 0;
  131. $end = null;
  132. }
  133. // Set the non-continue if this is being used as a generator
  134. // (as a list it doesn't matter because lists never non-continue)
  135. if ( $resultPageSet !== null ) {
  136. $endFlag = $end === null ? 0 : 1;
  137. $this->getContinuationManager()->addGeneratorNonContinueParam(
  138. $this, 'continue', "$rand|$start|$startId|$endFlag"
  139. );
  140. }
  141. list( $left, $continue ) =
  142. $this->runQuery( $resultPageSet, $params['limit'], $start, $startId, $end );
  143. if ( $end === null && $continue === null ) {
  144. // Wrap around. We do this even if $left === 0 for continuation
  145. // (saving a DB query in this rare case probably isn't worth the
  146. // added code complexity it would require).
  147. $end = $rand;
  148. list( $left, $continue ) = $this->runQuery( $resultPageSet, $left, null, null, $end );
  149. }
  150. if ( $continue !== null ) {
  151. $endFlag = $end === null ? 0 : 1;
  152. $this->setContinueEnumParameter( 'continue', "$rand|$continue|$endFlag" );
  153. }
  154. if ( is_null( $resultPageSet ) ) {
  155. $this->getResult()->addIndexedTagName( [ 'query', $this->getModuleName() ], 'page' );
  156. }
  157. }
  158. public function getCacheMode( $params ) {
  159. return 'public';
  160. }
  161. public function getAllowedParams() {
  162. return [
  163. 'namespace' => [
  164. ApiBase::PARAM_TYPE => 'namespace',
  165. ApiBase::PARAM_ISMULTI => true
  166. ],
  167. 'filterredir' => [
  168. ApiBase::PARAM_TYPE => [ 'all', 'redirects', 'nonredirects' ],
  169. ApiBase::PARAM_DFLT => 'nonredirects', // for BC
  170. ],
  171. 'redirect' => [
  172. ApiBase::PARAM_DEPRECATED => true,
  173. ApiBase::PARAM_DFLT => false,
  174. ],
  175. 'limit' => [
  176. ApiBase::PARAM_TYPE => 'limit',
  177. ApiBase::PARAM_DFLT => 1,
  178. ApiBase::PARAM_MIN => 1,
  179. ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
  180. ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
  181. ],
  182. 'continue' => [
  183. ApiBase::PARAM_HELP_MSG => 'api-help-param-continue'
  184. ],
  185. ];
  186. }
  187. protected function getExamplesMessages() {
  188. return [
  189. 'action=query&list=random&rnnamespace=0&rnlimit=2'
  190. => 'apihelp-query+random-example-simple',
  191. 'action=query&generator=random&grnnamespace=0&grnlimit=2&prop=info'
  192. => 'apihelp-query+random-example-generator',
  193. ];
  194. }
  195. public function getHelpUrls() {
  196. return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Random';
  197. }
  198. }