Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 135
0.00% covered (danger)
0.00%
0 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiQueryRandom
0.00% covered (danger)
0.00%
0 / 134
0.00% covered (danger)
0.00%
0 / 9
1406
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 executeGenerator
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 runQuery
0.00% covered (danger)
0.00%
0 / 56
0.00% covered (danger)
0.00%
0 / 1
342
 run
0.00% covered (danger)
0.00%
0 / 32
0.00% covered (danger)
0.00%
0 / 1
156
 getCacheMode
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getAllowedParams
0.00% covered (danger)
0.00%
0 / 33
0.00% covered (danger)
0.00%
0 / 1
2
 getExamplesMessages
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
 getHelpUrls
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3/**
4 * Copyright © 2008 Brent Garber
5 *
6 * @license GPL-2.0-or-later
7 * @file
8 */
9
10namespace MediaWiki\Api;
11
12use MediaWiki\Content\ContentHandlerFactory;
13use MediaWiki\Title\Title;
14use Wikimedia\ParamValidator\ParamValidator;
15use Wikimedia\ParamValidator\TypeDef\IntegerDef;
16
17/**
18 * Query module to get list of random pages
19 *
20 * @ingroup API
21 */
22class ApiQueryRandom extends ApiQueryGeneratorBase {
23
24    public function __construct(
25        ApiQuery $query,
26        string $moduleName,
27        private readonly ContentHandlerFactory $contentHandlerFactory,
28    ) {
29        parent::__construct( $query, $moduleName, 'rn' );
30    }
31
32    public function execute() {
33        $this->run();
34    }
35
36    /** @inheritDoc */
37    public function executeGenerator( $resultPageSet ) {
38        $this->run( $resultPageSet );
39    }
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|null $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
53        $this->resetQueryParams();
54        $this->addTables( 'page' );
55        $this->addFields( [ 'page_id', 'page_random' ] );
56        if ( $resultPageSet === null ) {
57            $this->addFields( [ 'page_title', 'page_namespace' ] );
58        } else {
59            $this->addFields( $resultPageSet->getPageTableFields() );
60        }
61        $this->addWhereFld( 'page_namespace', $params['namespace'] );
62        if ( $params['redirect'] || $params['filterredir'] === 'redirects' ) {
63            $this->addWhereFld( 'page_is_redirect', 1 );
64        } elseif ( $params['filterredir'] === 'nonredirects' ) {
65            $this->addWhereFld( 'page_is_redirect', 0 );
66        } elseif ( $resultPageSet === null ) {
67            $this->addFields( [ 'page_is_redirect' ] );
68        }
69
70        $db = $this->getDB();
71        if ( isset( $params['minsize'] ) ) {
72            $this->addWhere( $db->expr( 'page_len', '>=', (int)$params['minsize'] ) );
73        }
74        if ( isset( $params['maxsize'] ) ) {
75            $this->addWhere( $db->expr( 'page_len', '<=', (int)$params['maxsize'] ) );
76        }
77
78        if ( isset( $params['contentmodel'] ) ) {
79            $this->addWhereFld( 'page_content_model', $params['contentmodel'] );
80        }
81
82        $this->addOption( 'LIMIT', $limit + 1 );
83
84        if ( $start !== null ) {
85            if ( $startId > 0 ) {
86                $this->addWhere( $db->buildComparison( '>=', [
87                    'page_random' => $start,
88                    'page_id' => $startId,
89                ] ) );
90            } else {
91                $this->addWhere( $db->buildComparison( '>=', [
92                    'page_random' => $start,
93                ] ) );
94            }
95        }
96        if ( $end !== null ) {
97            $this->addWhere( $db->expr( 'page_random', '<', $end ) );
98        }
99        $this->addOption( 'ORDER BY', [ 'page_random', 'page_id' ] );
100
101        $result = $this->getResult();
102        $path = [ 'query', $this->getModuleName() ];
103
104        $res = $this->select( __METHOD__ );
105
106        if ( $resultPageSet === null ) {
107            $this->executeGenderCacheFromResultWrapper( $res, __METHOD__ );
108        }
109
110        $count = 0;
111        foreach ( $res as $row ) {
112            if ( $count++ >= $limit ) {
113                return [ 0, "{$row->page_random}|{$row->page_id}" ];
114            }
115            if ( $resultPageSet === null ) {
116                $title = Title::makeTitle( $row->page_namespace, $row->page_title );
117                $page = [
118                    'id' => (int)$row->page_id,
119                ];
120                ApiQueryBase::addTitleInfo( $page, $title );
121                if ( isset( $row->page_is_redirect ) ) {
122                    $page['redirect'] = (bool)$row->page_is_redirect;
123                }
124                $fit = $result->addValue( $path, null, $page );
125                if ( !$fit ) {
126                    return [ 0, "{$row->page_random}|{$row->page_id}" ];
127                }
128            } else {
129                $resultPageSet->processDbRow( $row );
130            }
131        }
132
133        return [ $limit - $count, null ];
134    }
135
136    /**
137     * @param ApiPageSet|null $resultPageSet
138     */
139    public function run( $resultPageSet = null ) {
140        $params = $this->extractRequestParams();
141
142        // Since 'filterredir' will always be set in $params, we have to dig
143        // into the WebRequest to see if it was actually passed.
144        $request = $this->getMain()->getRequest();
145        if ( $request->getCheck( $this->encodeParamName( 'filterredir' ) ) ) {
146            $this->requireMaxOneParameter( $params, 'filterredir', 'redirect' );
147        }
148
149        if ( isset( $params['continue'] ) ) {
150            $cont = $this->parseContinueParamOrDie( $params['continue'], [ 'string', 'string', 'int', 'string' ] );
151            $rand = $cont[0];
152            $start = $cont[1];
153            $startId = $cont[2];
154            $end = $cont[3] ? $rand : null;
155            $this->dieContinueUsageIf( !preg_match( '/^0\.\d+$/', $rand ) );
156            $this->dieContinueUsageIf( !preg_match( '/^0\.\d+$/', $start ) );
157            $this->dieContinueUsageIf( $cont[3] !== '0' && $cont[3] !== '1' );
158        } else {
159            $rand = wfRandom();
160            $start = $rand;
161            $startId = 0;
162            $end = null;
163        }
164
165        // Set the non-continue if this is being used as a generator
166        // (as a list it doesn't matter because lists never non-continue)
167        if ( $resultPageSet !== null ) {
168            $endFlag = $end === null ? 0 : 1;
169            $this->getContinuationManager()->addGeneratorNonContinueParam(
170                $this, 'continue', "$rand|$start|$startId|$endFlag"
171            );
172        }
173
174        [ $left, $continue ] =
175            $this->runQuery( $resultPageSet, $params['limit'], $start, $startId, $end );
176        if ( $end === null && $continue === null ) {
177            // Wrap around. We do this even if $left === 0 for continuation
178            // (saving a DB query in this rare case probably isn't worth the
179            // added code complexity it would require).
180            $end = $rand;
181            [ , $continue ] = $this->runQuery( $resultPageSet, $left, null, null, $end );
182        }
183
184        if ( $continue !== null ) {
185            $endFlag = $end === null ? 0 : 1;
186            $this->setContinueEnumParameter( 'continue', "$rand|$continue|$endFlag" );
187        }
188
189        if ( $resultPageSet === null ) {
190            $this->getResult()->addIndexedTagName( [ 'query', $this->getModuleName() ], 'page' );
191        }
192    }
193
194    /** @inheritDoc */
195    public function getCacheMode( $params ) {
196        return 'public';
197    }
198
199    /** @inheritDoc */
200    public function getAllowedParams() {
201        return [
202            'namespace' => [
203                ParamValidator::PARAM_TYPE => 'namespace',
204                ParamValidator::PARAM_ISMULTI => true
205            ],
206            'filterredir' => [
207                ParamValidator::PARAM_TYPE => [ 'all', 'redirects', 'nonredirects' ],
208                ParamValidator::PARAM_DEFAULT => 'nonredirects', // for BC
209            ],
210            'minsize' => [
211                ParamValidator::PARAM_TYPE => 'integer',
212            ],
213            'maxsize' => [
214                ParamValidator::PARAM_TYPE => 'integer',
215            ],
216            'contentmodel' => [
217                ParamValidator::PARAM_TYPE => $this->contentHandlerFactory->getContentModels(),
218            ],
219            'redirect' => [
220                ParamValidator::PARAM_DEPRECATED => true,
221                ParamValidator::PARAM_DEFAULT => false,
222            ],
223            'limit' => [
224                ParamValidator::PARAM_TYPE => 'limit',
225                ParamValidator::PARAM_DEFAULT => 1,
226                IntegerDef::PARAM_MIN => 1,
227                IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1,
228                IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2
229            ],
230            'continue' => [
231                ApiBase::PARAM_HELP_MSG => 'api-help-param-continue'
232            ],
233        ];
234    }
235
236    /** @inheritDoc */
237    protected function getExamplesMessages() {
238        return [
239            'action=query&list=random&rnnamespace=0&rnlimit=2'
240                => 'apihelp-query+random-example-simple',
241            'action=query&generator=random&grnnamespace=0&grnlimit=2&prop=info'
242                => 'apihelp-query+random-example-generator',
243            'action=query&list=random&rnnamespace=0&rnlimit=1&minsize=500'
244                => 'apihelp-query+random-example-minsize',
245        ];
246    }
247
248    /** @inheritDoc */
249    public function getHelpUrls() {
250        return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Random';
251    }
252}
253
254/** @deprecated class alias since 1.43 */
255class_alias( ApiQueryRandom::class, 'ApiQueryRandom' );