23use BadMethodCallException;
65 private $maxTimestamp =
null;
67 private $minTimestamp =
null;
75 parent::__construct(
'RandomInCategory' );
76 $this->dbProvider = $dbProvider;
84 $this->category = $cat;
85 $this->maxTimestamp =
null;
86 $this->minTimestamp =
null;
97 'label-message' =>
'randomincategory-category',
122 $categoryStr = $data[
'category'];
124 if ( $categoryStr ) {
125 $cat = Title::newFromText( $categoryStr,
NS_CATEGORY );
128 if ( $cat && $cat->getNamespace() !==
NS_CATEGORY ) {
130 $cat = Title::makeTitleSafe(
NS_CATEGORY, $categoryStr );
137 if ( !$this->category && $categoryStr ) {
138 $msg = $this->
msg(
'randomincategory-invalidcategory',
141 return Status::newFatal( $msg );
143 } elseif ( !$this->category ) {
149 if ( $title ===
null ) {
150 $msg = $this->
msg(
'randomincategory-nopages',
151 $this->category->getText() );
153 return Status::newFatal( $msg );
156 $query = $this->
getRequest()->getQueryValues();
157 unset( $query[
'title'] );
158 $this->
getOutput()->redirect( $title->getFullURL( $query ) );
171 $offset = mt_rand( 0, $this->maxOffset );
173 if ( mt_rand( 0, 1 ) ) {
179 $row = $this->selectRandomPageFromDB( $rand, $offset, $up, __METHOD__ );
183 $row = $this->selectRandomPageFromDB(
false, $offset, $up, __METHOD__ );
188 $row = $this->selectRandomPageFromDB( $rand, 0, $up, __METHOD__ );
193 $row = $this->selectRandomPageFromDB(
false, 0,
true, __METHOD__ );
197 return Title::makeTitle( $row->page_namespace, $row->page_title );
215 if ( !$this->category instanceof
Title ) {
216 throw new BadMethodCallException(
'No category set' );
218 $dbr = $this->dbProvider->getReplicaDatabase();
219 $queryBuilder = $dbr->newSelectQueryBuilder()
220 ->select( [
'page_title',
'page_namespace' ] )
221 ->from(
'categorylinks' )
222 ->join(
'page',
null,
'cl_from = page_id' )
223 ->where( [
'cl_to' => $this->category->getDBkey() ] )
224 ->andWhere( $this->extra )
225 ->orderBy(
'cl_timestamp', $up ? SelectQueryBuilder::SORT_ASC : SelectQueryBuilder::SORT_DESC )
231 $op = $up ?
'>=' :
'<=';
232 $queryBuilder->andWhere(
233 $dbr->expr(
'cl_timestamp', $op, $dbr->timestamp( $minClTime ) )
237 return $queryBuilder;
245 if ( $rand ===
false ) {
248 if ( !$this->minTimestamp || !$this->maxTimestamp ) {
250 if ( $minAndMax ===
null ) {
254 [ $this->minTimestamp, $this->maxTimestamp ] = $minAndMax;
257 $ts = ( $this->maxTimestamp - $this->minTimestamp ) * $rand + $this->minTimestamp;
259 return intval( $ts );
268 $dbr = $this->dbProvider->getReplicaDatabase();
269 $res = $dbr->newSelectQueryBuilder()
270 ->select( [
'low' =>
'MIN( cl_timestamp )',
'high' =>
'MAX( cl_timestamp )' ] )
271 ->from(
'categorylinks' )
272 ->where( [
'cl_to' => $this->category->getDBkey(), ] )
273 ->caller( __METHOD__ )->fetchRow();
288 private function selectRandomPageFromDB( $rand, $offset, $up, $fname = __METHOD__ ) {
289 return $this->
getQueryBuilder( $rand, $offset, $up )->caller( $fname )->fetchRow();
301class_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 ...
wfEscapeWikiText( $input)
Escapes the given text so that it may be output using addWikiText() without any linking,...
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
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.