Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / api / ApiQueryRandom.php
blob3b0056d9de75f50db1d736fa9e5bfde5a9a0bb62
1 <?php
3 /**
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
21 * @file
24 namespace MediaWiki\Api;
26 use MediaWiki\Title\Title;
27 use Wikimedia\ParamValidator\ParamValidator;
28 use Wikimedia\ParamValidator\TypeDef\IntegerDef;
30 /**
31 * Query module to get list of random pages
33 * @ingroup API
35 class ApiQueryRandom extends ApiQueryGeneratorBase {
37 public function __construct( ApiQuery $query, string $moduleName ) {
38 parent::__construct( $query, $moduleName, 'rn' );
41 public function execute() {
42 $this->run();
45 public function executeGenerator( $resultPageSet ) {
46 $this->run( $resultPageSet );
49 /**
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' ] );
66 } else {
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 ) {
80 $db = $this->getDB();
81 if ( $startId > 0 ) {
82 $this->addWhere( $db->buildComparison( '>=', [
83 'page_random' => $start,
84 'page_id' => $startId,
85 ] ) );
86 } else {
87 $this->addWhere( $db->buildComparison( '>=', [
88 'page_random' => $start,
89 ] ) );
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__ );
106 $count = 0;
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 );
113 $page = [
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 );
121 if ( !$fit ) {
122 return [ 0, "{$row->page_random}|{$row->page_id}" ];
124 } else {
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' ] );
147 $rand = $cont[0];
148 $start = $cont[1];
149 $startId = $cont[2];
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' );
154 } else {
155 $rand = wfRandom();
156 $start = $rand;
157 $startId = 0;
158 $end = null;
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).
176 $end = $rand;
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 ) {
191 return 'public';
194 public function getAllowedParams() {
195 return [
196 'namespace' => [
197 ParamValidator::PARAM_TYPE => 'namespace',
198 ParamValidator::PARAM_ISMULTI => true
200 'filterredir' => [
201 ParamValidator::PARAM_TYPE => [ 'all', 'redirects', 'nonredirects' ],
202 ParamValidator::PARAM_DEFAULT => 'nonredirects', // for BC
204 'redirect' => [
205 ParamValidator::PARAM_DEPRECATED => true,
206 ParamValidator::PARAM_DEFAULT => false,
208 'limit' => [
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
215 'continue' => [
216 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue'
221 protected function getExamplesMessages() {
222 return [
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' );