4 * Copyright © 2008 Brent Garber
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
24 namespace MediaWiki\Api
;
26 use MediaWiki\Title\Title
;
27 use Wikimedia\ParamValidator\ParamValidator
;
28 use Wikimedia\ParamValidator\TypeDef\IntegerDef
;
31 * Query module to get list of random pages
35 class ApiQueryRandom
extends ApiQueryGeneratorBase
{
37 public function __construct( ApiQuery
$query, string $moduleName ) {
38 parent
::__construct( $query, $moduleName, 'rn' );
41 public function execute() {
45 public function executeGenerator( $resultPageSet ) {
46 $this->run( $resultPageSet );
50 * Actually perform the query and add pages to the result.
51 * @param ApiPageSet|null $resultPageSet
52 * @param int $limit Number of pages to fetch
53 * @param string|null $start Starting page_random
54 * @param int|null $startId Starting page_id
55 * @param string|null $end Ending page_random
56 * @return array (int, string|null) Number of pages left to query and continuation string
58 protected function runQuery( $resultPageSet, $limit, $start, $startId, $end ) {
59 $params = $this->extractRequestParams();
61 $this->resetQueryParams();
62 $this->addTables( 'page' );
63 $this->addFields( [ 'page_id', 'page_random' ] );
64 if ( $resultPageSet === null ) {
65 $this->addFields( [ 'page_title', 'page_namespace' ] );
67 $this->addFields( $resultPageSet->getPageTableFields() );
69 $this->addWhereFld( 'page_namespace', $params['namespace'] );
70 if ( $params['redirect'] ||
$params['filterredir'] === 'redirects' ) {
71 $this->addWhereFld( 'page_is_redirect', 1 );
72 } elseif ( $params['filterredir'] === 'nonredirects' ) {
73 $this->addWhereFld( 'page_is_redirect', 0 );
74 } elseif ( $resultPageSet === null ) {
75 $this->addFields( [ 'page_is_redirect' ] );
77 $this->addOption( 'LIMIT', $limit +
1 );
79 if ( $start !== null ) {
82 $this->addWhere( $db->buildComparison( '>=', [
83 'page_random' => $start,
84 'page_id' => $startId,
87 $this->addWhere( $db->buildComparison( '>=', [
88 'page_random' => $start,
92 if ( $end !== null ) {
93 $this->addWhere( $this->getDB()->expr( 'page_random', '<', $end ) );
95 $this->addOption( 'ORDER BY', [ 'page_random', 'page_id' ] );
97 $result = $this->getResult();
98 $path = [ 'query', $this->getModuleName() ];
100 $res = $this->select( __METHOD__
);
102 if ( $resultPageSet === null ) {
103 $this->executeGenderCacheFromResultWrapper( $res, __METHOD__
);
107 foreach ( $res as $row ) {
108 if ( $count++
>= $limit ) {
109 return [ 0, "{$row->page_random}|{$row->page_id}" ];
111 if ( $resultPageSet === null ) {
112 $title = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
114 'id' => (int)$row->page_id
,
116 ApiQueryBase
::addTitleInfo( $page, $title );
117 if ( isset( $row->page_is_redirect
) ) {
118 $page['redirect'] = (bool)$row->page_is_redirect
;
120 $fit = $result->addValue( $path, null, $page );
122 return [ 0, "{$row->page_random}|{$row->page_id}" ];
125 $resultPageSet->processDbRow( $row );
129 return [ $limit - $count, null ];
133 * @param ApiPageSet|null $resultPageSet
135 public function run( $resultPageSet = null ) {
136 $params = $this->extractRequestParams();
138 // Since 'filterredir" will always be set in $params, we have to dig
139 // into the WebRequest to see if it was actually passed.
140 $request = $this->getMain()->getRequest();
141 if ( $request->getCheck( $this->encodeParamName( 'filterredir' ) ) ) {
142 $this->requireMaxOneParameter( $params, 'filterredir', 'redirect' );
145 if ( isset( $params['continue'] ) ) {
146 $cont = $this->parseContinueParamOrDie( $params['continue'], [ 'string', 'string', 'int', 'string' ] );
150 $end = $cont[3] ?
$rand : null;
151 $this->dieContinueUsageIf( !preg_match( '/^0\.\d+$/', $rand ) );
152 $this->dieContinueUsageIf( !preg_match( '/^0\.\d+$/', $start ) );
153 $this->dieContinueUsageIf( $cont[3] !== '0' && $cont[3] !== '1' );
161 // Set the non-continue if this is being used as a generator
162 // (as a list it doesn't matter because lists never non-continue)
163 if ( $resultPageSet !== null ) {
164 $endFlag = $end === null ?
0 : 1;
165 $this->getContinuationManager()->addGeneratorNonContinueParam(
166 $this, 'continue', "$rand|$start|$startId|$endFlag"
170 [ $left, $continue ] =
171 $this->runQuery( $resultPageSet, $params['limit'], $start, $startId, $end );
172 if ( $end === null && $continue === null ) {
173 // Wrap around. We do this even if $left === 0 for continuation
174 // (saving a DB query in this rare case probably isn't worth the
175 // added code complexity it would require).
177 [ , $continue ] = $this->runQuery( $resultPageSet, $left, null, null, $end );
180 if ( $continue !== null ) {
181 $endFlag = $end === null ?
0 : 1;
182 $this->setContinueEnumParameter( 'continue', "$rand|$continue|$endFlag" );
185 if ( $resultPageSet === null ) {
186 $this->getResult()->addIndexedTagName( [ 'query', $this->getModuleName() ], 'page' );
190 public function getCacheMode( $params ) {
194 public function getAllowedParams() {
197 ParamValidator
::PARAM_TYPE
=> 'namespace',
198 ParamValidator
::PARAM_ISMULTI
=> true
201 ParamValidator
::PARAM_TYPE
=> [ 'all', 'redirects', 'nonredirects' ],
202 ParamValidator
::PARAM_DEFAULT
=> 'nonredirects', // for BC
205 ParamValidator
::PARAM_DEPRECATED
=> true,
206 ParamValidator
::PARAM_DEFAULT
=> false,
209 ParamValidator
::PARAM_TYPE
=> 'limit',
210 ParamValidator
::PARAM_DEFAULT
=> 1,
211 IntegerDef
::PARAM_MIN
=> 1,
212 IntegerDef
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
213 IntegerDef
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
216 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue'
221 protected function getExamplesMessages() {
223 'action=query&list=random&rnnamespace=0&rnlimit=2'
224 => 'apihelp-query+random-example-simple',
225 'action=query&generator=random&grnnamespace=0&grnlimit=2&prop=info'
226 => 'apihelp-query+random-example-generator',
230 public function getHelpUrls() {
231 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Random';
235 /** @deprecated class alias since 1.43 */
236 class_alias( ApiQueryRandom
::class, 'ApiQueryRandom' );