MediaWiki REL1_37
ApiQueryRandom.php
Go to the documentation of this file.
1<?php
2
30
35 public function __construct( ApiQuery $query, $moduleName ) {
36 parent::__construct( $query, $moduleName, 'rn' );
37 }
38
39 public function execute() {
40 $this->run();
41 }
42
43 public function executeGenerator( $resultPageSet ) {
44 $this->run( $resultPageSet );
45 }
46
56 protected function runQuery( $resultPageSet, $limit, $start, $startId, $end ) {
57 $params = $this->extractRequestParams();
58
59 $this->resetQueryParams();
60 $this->addTables( 'page' );
61 $this->addFields( [ 'page_id', 'page_random' ] );
62 if ( $resultPageSet === null ) {
63 $this->addFields( [ 'page_title', 'page_namespace' ] );
64 } else {
65 $this->addFields( $resultPageSet->getPageTableFields() );
66 }
67 $this->addWhereFld( 'page_namespace', $params['namespace'] );
68 if ( $params['redirect'] || $params['filterredir'] === 'redirects' ) {
69 $this->addWhereFld( 'page_is_redirect', 1 );
70 } elseif ( $params['filterredir'] === 'nonredirects' ) {
71 $this->addWhereFld( 'page_is_redirect', 0 );
72 } elseif ( $resultPageSet === null ) {
73 $this->addFields( [ 'page_is_redirect' ] );
74 }
75 $this->addOption( 'LIMIT', $limit + 1 );
76
77 if ( $start !== null ) {
78 $start = $this->getDB()->addQuotes( $start );
79 if ( $startId > 0 ) {
80 $startId = (int)$startId; // safety
81 $this->addWhere( "page_random = $start AND page_id >= $startId OR page_random > $start" );
82 } else {
83 $this->addWhere( "page_random >= $start" );
84 }
85 }
86 if ( $end !== null ) {
87 $this->addWhere( 'page_random < ' . $this->getDB()->addQuotes( $end ) );
88 }
89 $this->addOption( 'ORDER BY', [ 'page_random', 'page_id' ] );
90
91 $result = $this->getResult();
92 $path = [ 'query', $this->getModuleName() ];
93
94 $res = $this->select( __METHOD__ );
95
96 if ( $resultPageSet === null ) {
97 $this->executeGenderCacheFromResultWrapper( $res, __METHOD__ );
98 }
99
100 $count = 0;
101 foreach ( $res as $row ) {
102 if ( $count++ >= $limit ) {
103 return [ 0, "{$row->page_random}|{$row->page_id}" ];
104 }
105 if ( $resultPageSet === null ) {
106 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
107 $page = [
108 'id' => (int)$row->page_id,
109 ];
111 if ( isset( $row->page_is_redirect ) ) {
112 $page['redirect'] = (bool)$row->page_is_redirect;
113 }
114 $fit = $result->addValue( $path, null, $page );
115 if ( !$fit ) {
116 return [ 0, "{$row->page_random}|{$row->page_id}" ];
117 }
118 } else {
119 $resultPageSet->processDbRow( $row );
120 }
121 }
122
123 return [ $limit - $count, null ];
124 }
125
129 public function run( $resultPageSet = null ) {
130 $params = $this->extractRequestParams();
131
132 // Since 'filterredir" will always be set in $params, we have to dig
133 // into the WebRequest to see if it was actually passed.
134 $request = $this->getMain()->getRequest();
135 if ( $request->getCheck( $this->encodeParamName( 'filterredir' ) ) ) {
136 $this->requireMaxOneParameter( $params, 'filterredir', 'redirect' );
137 }
138
139 if ( isset( $params['continue'] ) ) {
140 $cont = explode( '|', $params['continue'] );
141 $this->dieContinueUsageIf( count( $cont ) != 4 );
142 $rand = $cont[0];
143 $start = $cont[1];
144 $startId = (int)$cont[2];
145 $end = $cont[3] ? $rand : null;
146 $this->dieContinueUsageIf( !preg_match( '/^0\.\d+$/', $rand ) );
147 $this->dieContinueUsageIf( !preg_match( '/^0\.\d+$/', $start ) );
148 $this->dieContinueUsageIf( $cont[2] !== (string)$startId );
149 $this->dieContinueUsageIf( $cont[3] !== '0' && $cont[3] !== '1' );
150 } else {
151 $rand = wfRandom();
152 $start = $rand;
153 $startId = 0;
154 $end = null;
155 }
156
157 // Set the non-continue if this is being used as a generator
158 // (as a list it doesn't matter because lists never non-continue)
159 if ( $resultPageSet !== null ) {
160 $endFlag = $end === null ? 0 : 1;
161 $this->getContinuationManager()->addGeneratorNonContinueParam(
162 $this, 'continue', "$rand|$start|$startId|$endFlag"
163 );
164 }
165
166 list( $left, $continue ) =
167 $this->runQuery( $resultPageSet, $params['limit'], $start, $startId, $end );
168 if ( $end === null && $continue === null ) {
169 // Wrap around. We do this even if $left === 0 for continuation
170 // (saving a DB query in this rare case probably isn't worth the
171 // added code complexity it would require).
172 $end = $rand;
173 list( $left, $continue ) = $this->runQuery( $resultPageSet, $left, null, null, $end );
174 }
175
176 if ( $continue !== null ) {
177 $endFlag = $end === null ? 0 : 1;
178 $this->setContinueEnumParameter( 'continue', "$rand|$continue|$endFlag" );
179 }
180
181 if ( $resultPageSet === null ) {
182 $this->getResult()->addIndexedTagName( [ 'query', $this->getModuleName() ], 'page' );
183 }
184 }
185
186 public function getCacheMode( $params ) {
187 return 'public';
188 }
189
190 public function getAllowedParams() {
191 return [
192 'namespace' => [
193 ApiBase::PARAM_TYPE => 'namespace',
195 ],
196 'filterredir' => [
197 ApiBase::PARAM_TYPE => [ 'all', 'redirects', 'nonredirects' ],
198 ApiBase::PARAM_DFLT => 'nonredirects', // for BC
199 ],
200 'redirect' => [
202 ApiBase::PARAM_DFLT => false,
203 ],
204 'limit' => [
205 ApiBase::PARAM_TYPE => 'limit',
210 ],
211 'continue' => [
212 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue'
213 ],
214 ];
215 }
216
217 protected function getExamplesMessages() {
218 return [
219 'action=query&list=random&rnnamespace=0&rnlimit=2'
220 => 'apihelp-query+random-example-simple',
221 'action=query&generator=random&grnnamespace=0&grnlimit=2&prop=info'
222 => 'apihelp-query+random-example-generator',
223 ];
224 }
225
226 public function getHelpUrls() {
227 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Random';
228 }
229}
wfRandom()
Get a random decimal value in the domain of [0, 1), in a way not likely to give duplicate values for ...
const PARAM_MAX2
Definition ApiBase.php:89
const PARAM_DEPRECATED
Definition ApiBase.php:101
const PARAM_MAX
Definition ApiBase.php:85
dieContinueUsageIf( $condition)
Die with the 'badcontinue' error.
Definition ApiBase.php:1620
getMain()
Get the main module.
Definition ApiBase.php:513
const PARAM_TYPE
Definition ApiBase.php:81
const PARAM_DFLT
Definition ApiBase.php:73
const PARAM_MIN
Definition ApiBase.php:93
const LIMIT_BIG1
Fast query, standard limit.
Definition ApiBase.php:220
requireMaxOneParameter( $params,... $required)
Die if more than one of a certain set of parameters is set and not false.
Definition ApiBase.php:936
getResult()
Get the result object.
Definition ApiBase.php:628
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:764
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition ApiBase.php:162
const LIMIT_BIG2
Fast query, apihighlimits limit.
Definition ApiBase.php:222
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:497
getContinuationManager()
Definition ApiBase.php:662
const PARAM_ISMULTI
Definition ApiBase.php:77
static addTitleInfo(&$arr, $title, $prefix='')
Add information (title and namespace) about a Title object to a result array.
resetQueryParams()
Blank the internal arrays with query parameters.
addFields( $value)
Add a set of fields to select to the internal array.
addOption( $name, $value=null)
Add an option such as LIMIT or USE INDEX.
addTables( $tables, $alias=null)
Add a set of tables to the internal array.
getDB()
Get the Query database connection (read-only)
executeGenderCacheFromResultWrapper(IResultWrapper $res, $fname=__METHOD__, $fieldPrefix='page')
Preprocess the result set to fill the GenderCache with the necessary information before using self::a...
select( $method, $extraQuery=[], array &$hookData=null)
Execute a SELECT query based on the values in the internal arrays.
addWhereFld( $field, $value)
Equivalent to addWhere( [ $field => $value ] )
addWhere( $value)
Add a set of WHERE clauses to the internal array.
setContinueEnumParameter( $paramName, $paramValue)
Overridden to set the generator param if in generator mode.
Query module to get list of random pages.
run( $resultPageSet=null)
getExamplesMessages()
Returns usage examples for this module.
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
__construct(ApiQuery $query, $moduleName)
getCacheMode( $params)
Get the cache mode for the data generated by this module.
executeGenerator( $resultPageSet)
Execute this module as a generator.
getHelpUrls()
Return links to more detailed help pages about the module.
runQuery( $resultPageSet, $limit, $start, $startId, $end)
Actually perform the query and add pages to the result.
This is the main query class.
Definition ApiQuery.php:37
return true
Definition router.php:92