40 parent::__construct( $query, $moduleName,
'rn' );
48 $this->
run( $resultPageSet );
60 protected function runQuery( $resultPageSet, $limit, $start, $startId, $end ) {
65 $this->
addFields( [
'page_id',
'page_random' ] );
66 if ( $resultPageSet ===
null ) {
67 $this->
addFields( [
'page_title',
'page_namespace' ] );
69 $this->
addFields( $resultPageSet->getPageTableFields() );
71 $this->
addWhereFld(
'page_namespace', $params[
'namespace'] );
72 if ( $params[
'redirect'] || $params[
'filterredir'] ===
'redirects' ) {
74 } elseif ( $params[
'filterredir'] ===
'nonredirects' ) {
76 } elseif ( $resultPageSet ===
null ) {
77 $this->
addFields( [
'page_is_redirect' ] );
81 if ( $start !==
null ) {
84 $this->
addWhere( $db->buildComparison(
'>=', [
85 'page_random' => $start,
86 'page_id' => $startId,
89 $this->
addWhere( $db->buildComparison(
'>=', [
90 'page_random' => $start,
94 if ( $end !==
null ) {
95 $this->
addWhere(
'page_random < ' . $this->
getDB()->addQuotes( $end ) );
97 $this->
addOption(
'ORDER BY', [
'page_random',
'page_id' ] );
104 if ( $resultPageSet ===
null ) {
109 foreach (
$res as $row ) {
110 if ( $count++ >= $limit ) {
111 return [ 0,
"{$row->page_random}|{$row->page_id}" ];
113 if ( $resultPageSet ===
null ) {
114 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
116 'id' => (int)$row->page_id,
119 if ( isset( $row->page_is_redirect ) ) {
120 $page[
'redirect'] = (bool)$row->page_is_redirect;
122 $fit = $result->addValue(
$path,
null, $page );
124 return [ 0,
"{$row->page_random}|{$row->page_id}" ];
127 $resultPageSet->processDbRow( $row );
131 return [ $limit - $count, null ];
137 public function run( $resultPageSet =
null ) {
142 $request = $this->
getMain()->getRequest();
143 if ( $request->getCheck( $this->encodeParamName(
'filterredir' ) ) ) {
147 if ( isset( $params[
'continue'] ) ) {
152 $end = $cont[3] ? $rand :
null;
165 if ( $resultPageSet !==
null ) {
166 $endFlag = $end ===
null ? 0 : 1;
168 $this,
'continue',
"$rand|$start|$startId|$endFlag"
172 [ $left, $continue ] =
173 $this->
runQuery( $resultPageSet, $params[
'limit'], $start, $startId, $end );
174 if ( $end ===
null && $continue ===
null ) {
179 [ , $continue ] = $this->
runQuery( $resultPageSet, $left,
null,
null, $end );
182 if ( $continue !==
null ) {
183 $endFlag = $end ===
null ? 0 : 1;
187 if ( $resultPageSet ===
null ) {
199 ParamValidator::PARAM_TYPE =>
'namespace',
200 ParamValidator::PARAM_ISMULTI =>
true
203 ParamValidator::PARAM_TYPE => [
'all',
'redirects',
'nonredirects' ],
204 ParamValidator::PARAM_DEFAULT =>
'nonredirects',
207 ParamValidator::PARAM_DEPRECATED =>
true,
208 ParamValidator::PARAM_DEFAULT =>
false,
211 ParamValidator::PARAM_TYPE =>
'limit',
212 ParamValidator::PARAM_DEFAULT => 1,
213 IntegerDef::PARAM_MIN => 1,
225 'action=query&list=random&rnnamespace=0&rnlimit=2'
226 =>
'apihelp-query+random-example-simple',
227 'action=query&generator=random&grnnamespace=0&grnlimit=2&prop=info'
228 =>
'apihelp-query+random-example-generator',
233 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.
parseContinueParamOrDie(string $continue, array $types)
Parse the 'continue' parameter in the usual format and validate the types of each part,...
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.