38 parent::__construct( $query, $moduleName,
'rn' );
46 $this->
run( $resultPageSet );
58 protected function runQuery( $resultPageSet, $limit, $start, $startId, $end ) {
63 $this->
addFields( [
'page_id',
'page_random' ] );
64 if ( $resultPageSet ===
null ) {
65 $this->
addFields( [
'page_title',
'page_namespace' ] );
67 $this->
addFields( $resultPageSet->getPageTableFields() );
70 if (
$params[
'redirect'] ||
$params[
'filterredir'] ===
'redirects' ) {
72 } elseif (
$params[
'filterredir'] ===
'nonredirects' ) {
74 } elseif ( $resultPageSet ===
null ) {
75 $this->
addFields( [
'page_is_redirect' ] );
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' ] );
100 $res = $this->
select( __METHOD__ );
102 if ( $resultPageSet ===
null ) {
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,
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 ];
135 public function run( $resultPageSet =
null ) {
140 $request = $this->
getMain()->getRequest();
141 if ( $request->getCheck( $this->encodeParamName(
'filterredir' ) ) ) {
145 if ( isset(
$params[
'continue'] ) ) {
150 $end = $cont[3] ? $rand :
null;
163 if ( $resultPageSet !==
null ) {
164 $endFlag = $end ===
null ? 0 : 1;
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 ) {
177 [ , $continue ] = $this->
runQuery( $resultPageSet, $left,
null,
null, $end );
180 if ( $continue !==
null ) {
181 $endFlag = $end ===
null ? 0 : 1;
185 if ( $resultPageSet ===
null ) {
197 ParamValidator::PARAM_TYPE =>
'namespace',
198 ParamValidator::PARAM_ISMULTI => true
201 ParamValidator::PARAM_TYPE => [
'all',
'redirects',
'nonredirects' ],
202 ParamValidator::PARAM_DEFAULT =>
'nonredirects',
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,
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',
231 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Random';
236class_alias( ApiQueryRandom::class,
'ApiQueryRandom' );
wfRandom()
Get a random decimal value in the domain of [0, 1), in a way not likely to give duplicate values for ...
array $params
The job parameters.