39 parent::__construct( $query, $moduleName,
'rn' );
47 $this->
run( $resultPageSet );
59 protected function runQuery( $resultPageSet, $limit, $start, $startId, $end ) {
64 $this->
addFields( [
'page_id',
'page_random' ] );
65 if ( $resultPageSet ===
null ) {
66 $this->
addFields( [
'page_title',
'page_namespace' ] );
68 $this->
addFields( $resultPageSet->getPageTableFields() );
70 $this->
addWhereFld(
'page_namespace', $params[
'namespace'] );
71 if ( $params[
'redirect'] || $params[
'filterredir'] ===
'redirects' ) {
73 } elseif ( $params[
'filterredir'] ===
'nonredirects' ) {
75 } elseif ( $resultPageSet ===
null ) {
76 $this->
addFields( [
'page_is_redirect' ] );
80 if ( $start !==
null ) {
81 $start = $this->
getDB()->addQuotes( $start );
83 $startId = (int)$startId;
84 $this->
addWhere(
"page_random = $start AND page_id >= $startId OR page_random > $start" );
86 $this->
addWhere(
"page_random >= $start" );
89 if ( $end !==
null ) {
90 $this->
addWhere(
'page_random < ' . $this->
getDB()->addQuotes( $end ) );
92 $this->
addOption(
'ORDER BY', [
'page_random',
'page_id' ] );
99 if ( $resultPageSet ===
null ) {
104 foreach (
$res as $row ) {
105 if ( $count++ >= $limit ) {
106 return [ 0,
"{$row->page_random}|{$row->page_id}" ];
108 if ( $resultPageSet ===
null ) {
109 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
111 'id' => (int)$row->page_id,
114 if ( isset( $row->page_is_redirect ) ) {
115 $page[
'redirect'] = (bool)$row->page_is_redirect;
117 $fit = $result->addValue(
$path,
null, $page );
119 return [ 0,
"{$row->page_random}|{$row->page_id}" ];
122 $resultPageSet->processDbRow( $row );
126 return [ $limit - $count, null ];
132 public function run( $resultPageSet =
null ) {
137 $request = $this->
getMain()->getRequest();
138 if ( $request->getCheck( $this->encodeParamName(
'filterredir' ) ) ) {
142 if ( isset( $params[
'continue'] ) ) {
143 $cont = explode(
'|', $params[
'continue'] );
147 $startId = (int)$cont[2];
148 $end = $cont[3] ? $rand :
null;
162 if ( $resultPageSet !==
null ) {
163 $endFlag = $end ===
null ? 0 : 1;
165 $this,
'continue',
"$rand|$start|$startId|$endFlag"
169 list( $left, $continue ) =
170 $this->
runQuery( $resultPageSet, $params[
'limit'], $start, $startId, $end );
171 if ( $end ===
null && $continue ===
null ) {
176 list( $left, $continue ) = $this->
runQuery( $resultPageSet, $left,
null,
null, $end );
179 if ( $continue !==
null ) {
180 $endFlag = $end ===
null ? 0 : 1;
184 if ( $resultPageSet ===
null ) {
196 ParamValidator::PARAM_TYPE =>
'namespace',
197 ParamValidator::PARAM_ISMULTI =>
true
200 ParamValidator::PARAM_TYPE => [
'all',
'redirects',
'nonredirects' ],
201 ParamValidator::PARAM_DEFAULT =>
'nonredirects',
204 ParamValidator::PARAM_DEPRECATED =>
true,
205 ParamValidator::PARAM_DEFAULT =>
false,
208 ParamValidator::PARAM_TYPE =>
'limit',
209 ParamValidator::PARAM_DEFAULT => 1,
210 IntegerDef::PARAM_MIN => 1,
222 'action=query&list=random&rnnamespace=0&rnlimit=2'
223 =>
'apihelp-query+random-example-simple',
224 'action=query&generator=random&grnnamespace=0&grnlimit=2&prop=info'
225 =>
'apihelp-query+random-example-generator',
230 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Random';
wfRandom()
Get a random decimal value in the domain of [0, 1), in a way not likely to give duplicate values for ...
dieContinueUsageIf( $condition)
Die with the 'badcontinue' error.
getMain()
Get the main module.
const LIMIT_BIG1
Fast query, standard limit.
requireMaxOneParameter( $params,... $required)
Die if more than one of a certain set of parameters is set and not false.
getResult()
Get the result object.
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
const LIMIT_BIG2
Fast query, apihighlimits limit.
getModuleName()
Get the name of the module being executed by this instance.
static addTitleInfo(&$arr, $title, $prefix='')
Add information (title and namespace) about a Title object to a result array.
resetQueryParams()
Blank the internal arrays with query parameters.
addFields( $value)
Add a set of fields to select to the internal array.
addOption( $name, $value=null)
Add an option such as LIMIT or USE INDEX.
addTables( $tables, $alias=null)
Add a set of tables to the internal array.
getDB()
Get the Query database connection (read-only)
executeGenderCacheFromResultWrapper(IResultWrapper $res, $fname=__METHOD__, $fieldPrefix='page')
Preprocess the result set to fill the GenderCache with the necessary information before using self::a...
select( $method, $extraQuery=[], array &$hookData=null)
Execute a SELECT query based on the values in the internal arrays.
addWhereFld( $field, $value)
Equivalent to addWhere( [ $field => $value ] )
addWhere( $value)
Add a set of WHERE clauses to the internal array.
setContinueEnumParameter( $paramName, $paramValue)
Overridden to set the generator param if in generator mode.
Query module to get list of random pages.
run( $resultPageSet=null)
getExamplesMessages()
Returns usage examples for this module.
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
__construct(ApiQuery $query, $moduleName)
getCacheMode( $params)
Get the cache mode for the data generated by this module.
executeGenerator( $resultPageSet)
Execute this module as a generator.
getHelpUrls()
Return links to more detailed help pages about the module.
runQuery( $resultPageSet, $limit, $start, $startId, $end)
Actually perform the query and add pages to the result.
This is the main query class.