45 parent::__construct( $query, $moduleName,
'rn' );
46 $this->contentHandlerFactory = $contentHandlerFactory;
54 $this->
run( $resultPageSet );
66 protected function runQuery( $resultPageSet, $limit, $start, $startId, $end ) {
71 $this->
addFields( [
'page_id',
'page_random' ] );
72 if ( $resultPageSet ===
null ) {
73 $this->
addFields( [
'page_title',
'page_namespace' ] );
75 $this->
addFields( $resultPageSet->getPageTableFields() );
77 $this->
addWhereFld(
'page_namespace', $params[
'namespace'] );
78 if ( $params[
'redirect'] || $params[
'filterredir'] ===
'redirects' ) {
80 } elseif ( $params[
'filterredir'] ===
'nonredirects' ) {
82 } elseif ( $resultPageSet ===
null ) {
83 $this->
addFields( [
'page_is_redirect' ] );
87 if ( isset( $params[
'minsize'] ) ) {
88 $this->
addWhere( $db->expr(
'page_len',
'>=', (
int)$params[
'minsize'] ) );
90 if ( isset( $params[
'maxsize'] ) ) {
91 $this->
addWhere( $db->expr(
'page_len',
'<=', (
int)$params[
'maxsize'] ) );
94 if ( isset( $params[
'contentmodel'] ) ) {
95 $this->
addWhereFld(
'page_content_model', $params[
'contentmodel'] );
100 if ( $start !==
null ) {
101 if ( $startId > 0 ) {
102 $this->
addWhere( $db->buildComparison(
'>=', [
103 'page_random' => $start,
104 'page_id' => $startId,
107 $this->
addWhere( $db->buildComparison(
'>=', [
108 'page_random' => $start,
112 if ( $end !==
null ) {
113 $this->
addWhere( $db->expr(
'page_random',
'<', $end ) );
115 $this->
addOption(
'ORDER BY', [
'page_random',
'page_id' ] );
120 $res = $this->
select( __METHOD__ );
122 if ( $resultPageSet ===
null ) {
127 foreach ( $res as $row ) {
128 if ( $count++ >= $limit ) {
129 return [ 0,
"{$row->page_random}|{$row->page_id}" ];
131 if ( $resultPageSet ===
null ) {
132 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
134 'id' => (int)$row->page_id,
137 if ( isset( $row->page_is_redirect ) ) {
138 $page[
'redirect'] = (bool)$row->page_is_redirect;
140 $fit = $result->addValue(
$path,
null, $page );
142 return [ 0,
"{$row->page_random}|{$row->page_id}" ];
145 $resultPageSet->processDbRow( $row );
149 return [ $limit - $count, null ];
155 public function run( $resultPageSet =
null ) {
160 $request = $this->
getMain()->getRequest();
161 if ( $request->getCheck( $this->encodeParamName(
'filterredir' ) ) ) {
165 if ( isset( $params[
'continue'] ) ) {
170 $end = $cont[3] ? $rand :
null;
183 if ( $resultPageSet !==
null ) {
184 $endFlag = $end ===
null ? 0 : 1;
186 $this,
'continue',
"$rand|$start|$startId|$endFlag"
190 [ $left, $continue ] =
191 $this->
runQuery( $resultPageSet, $params[
'limit'], $start, $startId, $end );
192 if ( $end ===
null && $continue ===
null ) {
197 [ , $continue ] = $this->
runQuery( $resultPageSet, $left,
null,
null, $end );
200 if ( $continue !==
null ) {
201 $endFlag = $end ===
null ? 0 : 1;
205 if ( $resultPageSet ===
null ) {
217 ParamValidator::PARAM_TYPE =>
'namespace',
218 ParamValidator::PARAM_ISMULTI => true
221 ParamValidator::PARAM_TYPE => [
'all',
'redirects',
'nonredirects' ],
222 ParamValidator::PARAM_DEFAULT =>
'nonredirects',
225 ParamValidator::PARAM_TYPE =>
'integer',
228 ParamValidator::PARAM_TYPE =>
'integer',
231 ParamValidator::PARAM_TYPE => $this->contentHandlerFactory->getContentModels(),
234 ParamValidator::PARAM_DEPRECATED =>
true,
235 ParamValidator::PARAM_DEFAULT =>
false,
238 ParamValidator::PARAM_TYPE =>
'limit',
239 ParamValidator::PARAM_DEFAULT => 1,
240 IntegerDef::PARAM_MIN => 1,
252 'action=query&list=random&rnnamespace=0&rnlimit=2'
253 =>
'apihelp-query+random-example-simple',
254 'action=query&generator=random&grnnamespace=0&grnlimit=2&prop=info'
255 =>
'apihelp-query+random-example-generator',
256 'action=query&list=random&rnnamespace=0&rnlimit=1&minsize=500'
257 =>
'apihelp-query+random-example-minsize',
262 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Random';
267class_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 ...