MediaWiki  master
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',
206  ApiBase::PARAM_DFLT => 1,
207  ApiBase::PARAM_MIN => 1,
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 }
ApiQueryBase\addFields
addFields( $value)
Add a set of fields to select to the internal array.
Definition: ApiQueryBase.php:212
ApiQuery
This is the main query class.
Definition: ApiQuery.php:39
ApiQueryRandom\getHelpUrls
getHelpUrls()
Return links to more detailed help pages about the module.
Definition: ApiQueryRandom.php:226
ApiQueryBase\resetQueryParams
resetQueryParams()
Blank the internal arrays with query parameters.
Definition: ApiQueryBase.php:156
ApiBase\PARAM_HELP_MSG
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition: ApiBase.php:162
true
return true
Definition: router.php:90
ApiBase\PARAM_TYPE
const PARAM_TYPE
Definition: ApiBase.php:81
ApiBase\getResult
getResult()
Get the result object.
Definition: ApiBase.php:628
ApiQueryRandom\run
run( $resultPageSet=null)
Definition: ApiQueryRandom.php:129
ApiQueryRandom\executeGenerator
executeGenerator( $resultPageSet)
Execute this module as a generator.
Definition: ApiQueryRandom.php:43
ApiQueryBase\addOption
addOption( $name, $value=null)
Add an option such as LIMIT or USE INDEX.
Definition: ApiQueryBase.php:378
$res
$res
Definition: testCompression.php:57
ApiQueryGeneratorBase\setContinueEnumParameter
setContinueEnumParameter( $paramName, $paramValue)
Overridden to set the generator param if in generator mode.
Definition: ApiQueryGeneratorBase.php:83
ApiBase\PARAM_DEPRECATED
const PARAM_DEPRECATED
Definition: ApiBase.php:101
ApiBase\PARAM_MIN
const PARAM_MIN
Definition: ApiBase.php:93
ApiQueryBase\executeGenderCacheFromResultWrapper
executeGenderCacheFromResultWrapper(IResultWrapper $res, $fname=__METHOD__, $fieldPrefix='page')
Preprocess the result set to fill the GenderCache with the necessary information before using self::a...
Definition: ApiQueryBase.php:626
ApiQueryRandom\getAllowedParams
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
Definition: ApiQueryRandom.php:190
ApiBase\LIMIT_BIG1
const LIMIT_BIG1
Fast query, standard limit.
Definition: ApiBase.php:220
ApiQueryBase\getDB
getDB()
Get the Query database connection (read-only)
Definition: ApiQueryBase.php:117
ApiBase\PARAM_MAX
const PARAM_MAX
Definition: ApiBase.php:85
ApiQueryRandom\__construct
__construct(ApiQuery $query, $moduleName)
Definition: ApiQueryRandom.php:35
ApiQueryBase\addTables
addTables( $tables, $alias=null)
Add a set of tables to the internal array.
Definition: ApiQueryBase.php:182
ApiQueryBase\select
select( $method, $extraQuery=[], array &$hookData=null)
Execute a SELECT query based on the values in the internal arrays.
Definition: ApiQueryBase.php:399
ApiBase\extractRequestParams
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:764
$title
$title
Definition: testCompression.php:38
Title\makeTitle
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:666
ApiQueryRandom\getExamplesMessages
getExamplesMessages()
Returns usage examples for this module.
Definition: ApiQueryRandom.php:217
ApiBase\getContinuationManager
getContinuationManager()
Definition: ApiBase.php:662
ApiBase\requireMaxOneParameter
requireMaxOneParameter( $params,... $required)
Die if more than one of a certain set of parameters is set and not false.
Definition: ApiBase.php:936
ApiBase\dieContinueUsageIf
dieContinueUsageIf( $condition)
Die with the 'badcontinue' error.
Definition: ApiBase.php:1626
ApiQueryRandom\getCacheMode
getCacheMode( $params)
Get the cache mode for the data generated by this module.
Definition: ApiQueryRandom.php:186
ApiQueryBase\addWhereFld
addWhereFld( $field, $value)
Equivalent to addWhere( [ $field => $value ] )
Definition: ApiQueryBase.php:282
ApiQueryGeneratorBase
Definition: ApiQueryGeneratorBase.php:28
wfRandom
wfRandom()
Get a random decimal value in the domain of [0, 1), in a way not likely to give duplicate values for ...
Definition: GlobalFunctions.php:239
ApiBase\LIMIT_BIG2
const LIMIT_BIG2
Fast query, apihighlimits limit.
Definition: ApiBase.php:222
$path
$path
Definition: NoLocalSettings.php:25
ApiBase\PARAM_DFLT
const PARAM_DFLT
Definition: ApiBase.php:73
ApiQueryRandom
Query module to get list of random pages.
Definition: ApiQueryRandom.php:29
ApiBase\getModuleName
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:497
ApiBase\PARAM_ISMULTI
const PARAM_ISMULTI
Definition: ApiBase.php:77
ApiQueryRandom\runQuery
runQuery( $resultPageSet, $limit, $start, $startId, $end)
Actually perform the query and add pages to the result.
Definition: ApiQueryRandom.php:56
ApiBase\PARAM_MAX2
const PARAM_MAX2
Definition: ApiBase.php:89
ApiBase\getMain
getMain()
Get the main module.
Definition: ApiBase.php:513
ApiQueryBase\addWhere
addWhere( $value)
Add a set of WHERE clauses to the internal array.
Definition: ApiQueryBase.php:245
ApiQueryRandom\execute
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
Definition: ApiQueryRandom.php:39
ApiQueryBase\addTitleInfo
static addTitleInfo(&$arr, $title, $prefix='')
Add information (title and namespace) about a Title object to a result array.
Definition: ApiQueryBase.php:466