31 parent::__construct( $query, $moduleName,
'rn' );
32 $this->contentHandlerFactory = $contentHandlerFactory;
41 $this->
run( $resultPageSet );
53 protected function runQuery( $resultPageSet, $limit, $start, $startId, $end ) {
58 $this->
addFields( [
'page_id',
'page_random' ] );
59 if ( $resultPageSet ===
null ) {
60 $this->
addFields( [
'page_title',
'page_namespace' ] );
62 $this->
addFields( $resultPageSet->getPageTableFields() );
64 $this->
addWhereFld(
'page_namespace', $params[
'namespace'] );
65 if ( $params[
'redirect'] || $params[
'filterredir'] ===
'redirects' ) {
67 } elseif ( $params[
'filterredir'] ===
'nonredirects' ) {
69 } elseif ( $resultPageSet ===
null ) {
70 $this->
addFields( [
'page_is_redirect' ] );
74 if ( isset( $params[
'minsize'] ) ) {
75 $this->
addWhere( $db->expr(
'page_len',
'>=', (
int)$params[
'minsize'] ) );
77 if ( isset( $params[
'maxsize'] ) ) {
78 $this->
addWhere( $db->expr(
'page_len',
'<=', (
int)$params[
'maxsize'] ) );
81 if ( isset( $params[
'contentmodel'] ) ) {
82 $this->
addWhereFld(
'page_content_model', $params[
'contentmodel'] );
87 if ( $start !==
null ) {
89 $this->
addWhere( $db->buildComparison(
'>=', [
90 'page_random' => $start,
91 'page_id' => $startId,
94 $this->
addWhere( $db->buildComparison(
'>=', [
95 'page_random' => $start,
99 if ( $end !==
null ) {
100 $this->
addWhere( $db->expr(
'page_random',
'<', $end ) );
102 $this->
addOption(
'ORDER BY', [
'page_random',
'page_id' ] );
107 $res = $this->
select( __METHOD__ );
109 if ( $resultPageSet ===
null ) {
114 foreach ( $res as $row ) {
115 if ( $count++ >= $limit ) {
116 return [ 0,
"{$row->page_random}|{$row->page_id}" ];
118 if ( $resultPageSet ===
null ) {
121 'id' => (int)$row->page_id,
124 if ( isset( $row->page_is_redirect ) ) {
125 $page[
'redirect'] = (bool)$row->page_is_redirect;
127 $fit = $result->addValue(
$path,
null, $page );
129 return [ 0,
"{$row->page_random}|{$row->page_id}" ];
132 $resultPageSet->processDbRow( $row );
136 return [ $limit - $count, null ];
142 public function run( $resultPageSet =
null ) {
147 $request = $this->
getMain()->getRequest();
148 if ( $request->getCheck( $this->encodeParamName(
'filterredir' ) ) ) {
152 if ( isset( $params[
'continue'] ) ) {
157 $end = $cont[3] ? $rand :
null;
170 if ( $resultPageSet !==
null ) {
171 $endFlag = $end ===
null ? 0 : 1;
173 $this,
'continue',
"$rand|$start|$startId|$endFlag"
177 [ $left, $continue ] =
178 $this->
runQuery( $resultPageSet, $params[
'limit'], $start, $startId, $end );
179 if ( $end ===
null && $continue ===
null ) {
184 [ , $continue ] = $this->
runQuery( $resultPageSet, $left,
null,
null, $end );
187 if ( $continue !==
null ) {
188 $endFlag = $end ===
null ? 0 : 1;
192 if ( $resultPageSet ===
null ) {
206 ParamValidator::PARAM_TYPE =>
'namespace',
207 ParamValidator::PARAM_ISMULTI => true
210 ParamValidator::PARAM_TYPE => [
'all',
'redirects',
'nonredirects' ],
211 ParamValidator::PARAM_DEFAULT =>
'nonredirects',
214 ParamValidator::PARAM_TYPE =>
'integer',
217 ParamValidator::PARAM_TYPE =>
'integer',
220 ParamValidator::PARAM_TYPE => $this->contentHandlerFactory->getContentModels(),
223 ParamValidator::PARAM_DEPRECATED =>
true,
224 ParamValidator::PARAM_DEFAULT =>
false,
227 ParamValidator::PARAM_TYPE =>
'limit',
228 ParamValidator::PARAM_DEFAULT => 1,
229 IntegerDef::PARAM_MIN => 1,
242 'action=query&list=random&rnnamespace=0&rnlimit=2'
243 =>
'apihelp-query+random-example-simple',
244 'action=query&generator=random&grnnamespace=0&grnlimit=2&prop=info'
245 =>
'apihelp-query+random-example-generator',
246 'action=query&list=random&rnnamespace=0&rnlimit=1&minsize=500'
247 =>
'apihelp-query+random-example-minsize',
253 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Random';
258class_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 ...