25 namespace MediaWiki\Specials;
27 use BadMethodCallException;
68 private $maxTimestamp =
null;
70 private $minTimestamp =
null;
78 parent::__construct(
'RandomInCategory' );
79 $this->dbProvider = $dbProvider;
87 $this->category = $cat;
88 $this->maxTimestamp =
null;
89 $this->minTimestamp =
null;
100 'label-message' =>
'randomincategory-category',
125 $categoryStr = $data[
'category'];
127 if ( $categoryStr ) {
131 if ( $cat && $cat->getNamespace() !==
NS_CATEGORY ) {
140 if ( !$this->category && $categoryStr ) {
141 $msg = $this->
msg(
'randomincategory-invalidcategory',
146 } elseif ( !$this->category ) {
152 if ( $title ===
null ) {
153 $msg = $this->
msg(
'randomincategory-nopages',
154 $this->category->getText() );
159 $query = $this->
getRequest()->getQueryValues();
160 unset( $query[
'title'] );
161 $this->
getOutput()->redirect( $title->getFullURL( $query ) );
174 $offset = mt_rand( 0, $this->maxOffset );
176 if ( mt_rand( 0, 1 ) ) {
182 $row = $this->selectRandomPageFromDB( $rand, $offset, $up, __METHOD__ );
186 $row = $this->selectRandomPageFromDB(
false, $offset, $up, __METHOD__ );
191 $row = $this->selectRandomPageFromDB( $rand, 0, $up, __METHOD__ );
196 $row = $this->selectRandomPageFromDB(
false, 0,
true, __METHOD__ );
217 $op = $up ?
'>=' :
'<=';
218 if ( !$this->category instanceof
Title ) {
219 throw new BadMethodCallException(
'No category set' );
221 $dbr = $this->dbProvider->getReplicaDatabase();
222 $queryBuilder = $dbr->newSelectQueryBuilder()
223 ->select( [
'page_title',
'page_namespace' ] )
224 ->from(
'categorylinks' )
225 ->join(
'page',
null,
'cl_from = page_id' )
226 ->where( [
'cl_to' => $this->category->getDBkey() ] )
227 ->andWhere( $this->extra )
228 ->orderBy(
'cl_timestamp', $up ? SelectQueryBuilder::SORT_ASC : SelectQueryBuilder::SORT_DESC )
234 $queryBuilder->andWhere(
235 $dbr->buildComparison( $op, [
'cl_timestamp' => $dbr->timestamp( $minClTime ) ] )
239 return $queryBuilder;
248 if ( $rand ===
false ) {
251 if ( !$this->minTimestamp || !$this->maxTimestamp ) {
253 if ( $minAndMax ===
null ) {
257 [ $this->minTimestamp, $this->maxTimestamp ] = $minAndMax;
260 $ts = ( $this->maxTimestamp - $this->minTimestamp ) * $rand + $this->minTimestamp;
262 return intval( $ts );
271 $dbr = $this->dbProvider->getReplicaDatabase();
272 $res = $dbr->newSelectQueryBuilder()
273 ->select( [
'low' =>
'MIN( cl_timestamp )',
'high' =>
'MAX( cl_timestamp )' ] )
274 ->from(
'categorylinks' )
275 ->where( [
'cl_to' => $this->category->getDBkey(), ] )
276 ->caller( __METHOD__ )->fetchRow();
291 private function selectRandomPageFromDB( $rand, $offset, $up, $fname = __METHOD__ ) {
292 return $this->
getQueryBuilder( $rand, $offset, $up )->caller( $fname )->fetchRow();
304 class_alias( SpecialRandomInCategory::class,
'SpecialRandomInCategory' );
wfRandom()
Get a random decimal value in the domain of [0, 1), in a way not likely to give duplicate values for ...
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
wfEscapeWikiText( $text)
Escapes the given text so that it may be output using addWikiText() without any linking,...
Special page which uses an HTMLForm to handle processing.
getRequest()
Get the WebRequest being used for this instance.
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
getOutput()
Get the OutputPage being used for this instance.
addHelpLink( $to, $overrideBaseUrl=false)
Adds help link with an icon via page indicators.
static newFatal( $message,... $parameters)
Factory function for fatal errors.