ApiQueryRandom.php 6.9 KB

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