29 parent::__construct( $query, $moduleName,
'rn' );
38 $this->
run( $resultPageSet );
50 protected function runQuery( $resultPageSet, $limit, $start, $startId, $end ) {
55 $this->
addFields( [
'page_id',
'page_random' ] );
56 if ( $resultPageSet ===
null ) {
57 $this->
addFields( [
'page_title',
'page_namespace' ] );
59 $this->
addFields( $resultPageSet->getPageTableFields() );
61 $this->
addWhereFld(
'page_namespace', $params[
'namespace'] );
62 if ( $params[
'redirect'] || $params[
'filterredir'] ===
'redirects' ) {
64 } elseif ( $params[
'filterredir'] ===
'nonredirects' ) {
66 } elseif ( $resultPageSet ===
null ) {
67 $this->
addFields( [
'page_is_redirect' ] );
71 if ( isset( $params[
'minsize'] ) ) {
72 $this->
addWhere( $db->expr(
'page_len',
'>=', (
int)$params[
'minsize'] ) );
74 if ( isset( $params[
'maxsize'] ) ) {
75 $this->
addWhere( $db->expr(
'page_len',
'<=', (
int)$params[
'maxsize'] ) );
78 if ( isset( $params[
'contentmodel'] ) ) {
79 $this->
addWhereFld(
'page_content_model', $params[
'contentmodel'] );
84 if ( $start !==
null ) {
86 $this->
addWhere( $db->buildComparison(
'>=', [
87 'page_random' => $start,
88 'page_id' => $startId,
91 $this->
addWhere( $db->buildComparison(
'>=', [
92 'page_random' => $start,
96 if ( $end !==
null ) {
97 $this->
addWhere( $db->expr(
'page_random',
'<', $end ) );
99 $this->
addOption(
'ORDER BY', [
'page_random',
'page_id' ] );
104 $res = $this->
select( __METHOD__ );
106 if ( $resultPageSet ===
null ) {
111 foreach ( $res as $row ) {
112 if ( $count++ >= $limit ) {
113 return [ 0,
"{$row->page_random}|{$row->page_id}" ];
115 if ( $resultPageSet ===
null ) {
118 'id' => (int)$row->page_id,
121 if ( isset( $row->page_is_redirect ) ) {
122 $page[
'redirect'] = (bool)$row->page_is_redirect;
124 $fit = $result->addValue(
$path,
null, $page );
126 return [ 0,
"{$row->page_random}|{$row->page_id}" ];
129 $resultPageSet->processDbRow( $row );
133 return [ $limit - $count, null ];
139 public function run( $resultPageSet =
null ) {
145 if (
$request->getCheck( $this->encodeParamName(
'filterredir' ) ) ) {
149 if ( isset( $params[
'continue'] ) ) {
154 $end = $cont[3] ? $rand :
null;
167 if ( $resultPageSet !==
null ) {
168 $endFlag = $end ===
null ? 0 : 1;
170 $this,
'continue',
"$rand|$start|$startId|$endFlag"
174 [ $left, $continue ] =
175 $this->
runQuery( $resultPageSet, $params[
'limit'], $start, $startId, $end );
176 if ( $end ===
null && $continue ===
null ) {
181 [ , $continue ] = $this->
runQuery( $resultPageSet, $left,
null,
null, $end );
184 if ( $continue !==
null ) {
185 $endFlag = $end ===
null ? 0 : 1;
189 if ( $resultPageSet ===
null ) {
203 ParamValidator::PARAM_TYPE =>
'namespace',
204 ParamValidator::PARAM_ISMULTI => true
207 ParamValidator::PARAM_TYPE => [
'all',
'redirects',
'nonredirects' ],
208 ParamValidator::PARAM_DEFAULT =>
'nonredirects',
211 ParamValidator::PARAM_TYPE =>
'integer',
214 ParamValidator::PARAM_TYPE =>
'integer',
217 ParamValidator::PARAM_TYPE => $this->contentHandlerFactory->getContentModels(),
220 ParamValidator::PARAM_DEPRECATED =>
true,
221 ParamValidator::PARAM_DEFAULT =>
false,
224 ParamValidator::PARAM_TYPE =>
'limit',
225 ParamValidator::PARAM_DEFAULT => 1,
226 IntegerDef::PARAM_MIN => 1,
239 'action=query&list=random&rnnamespace=0&rnlimit=2'
240 =>
'apihelp-query+random-example-simple',
241 'action=query&generator=random&grnnamespace=0&grnlimit=2&prop=info'
242 =>
'apihelp-query+random-example-generator',
243 'action=query&list=random&rnnamespace=0&rnlimit=1&minsize=500'
244 =>
'apihelp-query+random-example-minsize',
250 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Random';
255class_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 ...