36 parent::__construct( $query, $moduleName,
'rn' );
44 $this->
run( $resultPageSet );
56 protected function runQuery( $resultPageSet, $limit, $start, $startId, $end ) {
61 $this->
addFields( [
'page_id',
'page_random' ] );
62 if ( $resultPageSet ===
null ) {
63 $this->
addFields( [
'page_title',
'page_namespace' ] );
65 $this->
addFields( $resultPageSet->getPageTableFields() );
67 $this->
addWhereFld(
'page_namespace', $params[
'namespace'] );
68 if ( $params[
'redirect'] || $params[
'filterredir'] ===
'redirects' ) {
70 } elseif ( $params[
'filterredir'] ===
'nonredirects' ) {
72 } elseif ( $resultPageSet ===
null ) {
73 $this->
addFields( [
'page_is_redirect' ] );
77 if ( $start !==
null ) {
78 $start = $this->
getDB()->addQuotes( $start );
80 $startId = (int)$startId;
81 $this->
addWhere(
"page_random = $start AND page_id >= $startId OR page_random > $start" );
83 $this->
addWhere(
"page_random >= $start" );
86 if ( $end !==
null ) {
87 $this->
addWhere(
'page_random < ' . $this->
getDB()->addQuotes( $end ) );
89 $this->
addOption(
'ORDER BY', [
'page_random',
'page_id' ] );
96 if ( $resultPageSet ===
null ) {
101 foreach (
$res as $row ) {
102 if ( $count++ >= $limit ) {
103 return [ 0,
"{$row->page_random}|{$row->page_id}" ];
105 if ( $resultPageSet ===
null ) {
106 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
108 'id' => (int)$row->page_id,
111 if ( isset( $row->page_is_redirect ) ) {
112 $page[
'redirect'] = (bool)$row->page_is_redirect;
114 $fit = $result->addValue(
$path,
null, $page );
116 return [ 0,
"{$row->page_random}|{$row->page_id}" ];
119 $resultPageSet->processDbRow( $row );
123 return [ $limit - $count, null ];
129 public function run( $resultPageSet =
null ) {
134 $request = $this->
getMain()->getRequest();
135 if ( $request->getCheck( $this->encodeParamName(
'filterredir' ) ) ) {
139 if ( isset( $params[
'continue'] ) ) {
140 $cont = explode(
'|', $params[
'continue'] );
144 $startId = (int)$cont[2];
145 $end = $cont[3] ? $rand :
null;
159 if ( $resultPageSet !==
null ) {
160 $endFlag = $end ===
null ? 0 : 1;
162 $this,
'continue',
"$rand|$start|$startId|$endFlag"
166 list( $left, $continue ) =
167 $this->
runQuery( $resultPageSet, $params[
'limit'], $start, $startId, $end );
168 if ( $end ===
null && $continue ===
null ) {
173 list( $left, $continue ) = $this->
runQuery( $resultPageSet, $left,
null,
null, $end );
176 if ( $continue !==
null ) {
177 $endFlag = $end ===
null ? 0 : 1;
181 if ( $resultPageSet ===
null ) {
219 'action=query&list=random&rnnamespace=0&rnlimit=2'
220 =>
'apihelp-query+random-example-simple',
221 'action=query&generator=random&grnnamespace=0&grnlimit=2&prop=info'
222 =>
'apihelp-query+random-example-generator',
227 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.