23use BadMethodCallException;
67 private $maxTimestamp =
null;
69 private $minTimestamp =
null;
74 parent::__construct(
'RandomInCategory' );
75 $this->dbProvider = $dbProvider;
82 $this->category = $cat;
83 $this->maxTimestamp =
null;
84 $this->minTimestamp =
null;
95 'label-message' =>
'randomincategory-category',
120 $categoryStr = $data[
'category'];
122 if ( $categoryStr ) {
123 $cat = Title::newFromText( $categoryStr,
NS_CATEGORY );
126 if ( $cat && $cat->getNamespace() !==
NS_CATEGORY ) {
128 $cat = Title::makeTitleSafe(
NS_CATEGORY, $categoryStr );
135 if ( !$this->category && $categoryStr ) {
136 $msg = $this->
msg(
'randomincategory-invalidcategory',
139 return Status::newFatal( $msg );
141 } elseif ( !$this->category ) {
147 if ( $title ===
null ) {
148 $msg = $this->
msg(
'randomincategory-nopages',
149 $this->category->getText() );
151 return Status::newFatal( $msg );
154 $query = $this->
getRequest()->getQueryValues();
155 unset( $query[
'title'] );
156 $this->
getOutput()->redirect( $title->getFullURL( $query ) );
169 $offset = mt_rand( 0, $this->maxOffset );
171 if ( mt_rand( 0, 1 ) ) {
177 $row = $this->selectRandomPageFromDB( $rand, $offset, $up, __METHOD__ );
181 $row = $this->selectRandomPageFromDB(
false, $offset, $up, __METHOD__ );
186 $row = $this->selectRandomPageFromDB( $rand, 0, $up, __METHOD__ );
191 $row = $this->selectRandomPageFromDB(
false, 0,
true, __METHOD__ );
195 return Title::makeTitle( $row->page_namespace, $row->page_title );
213 if ( !$this->category instanceof
Title ) {
214 throw new BadMethodCallException(
'No category set' );
216 $dbr = $this->dbProvider->getReplicaDatabase();
220 $queryBuilder = $dbr->newSelectQueryBuilder()
221 ->select( [
'page_title',
'page_namespace' ] )
222 ->from(
'categorylinks' )
223 ->join(
'page',
null,
'cl_from = page_id' )
225 ->andWhere( $this->extra )
226 ->orderBy(
'cl_timestamp', $up ? SelectQueryBuilder::SORT_ASC : SelectQueryBuilder::SORT_DESC )
230 $queryBuilder->where( [
'cl_to' => $this->category->getDBkey() ] );
232 $queryBuilder->join(
'linktarget',
null,
'cl_target_id = lt_id' )
233 ->where( [
'lt_title' => $this->category->getDBkey(),
'lt_namespace' =>
NS_CATEGORY ] );
238 $op = $up ?
'>=' :
'<=';
239 $queryBuilder->andWhere(
240 $dbr->expr(
'cl_timestamp', $op, $dbr->timestamp( $minClTime ) )
244 return $queryBuilder;
252 if ( $rand ===
false ) {
255 if ( !$this->minTimestamp || !$this->maxTimestamp ) {
257 if ( $minAndMax ===
null ) {
261 [ $this->minTimestamp, $this->maxTimestamp ] = $minAndMax;
264 $ts = ( $this->maxTimestamp - $this->minTimestamp ) * $rand + $this->minTimestamp;
266 return intval( $ts );
275 $dbr = $this->dbProvider->getReplicaDatabase();
279 $queryBuilder = $dbr->newSelectQueryBuilder()
280 ->select( [
'low' =>
'MIN( cl_timestamp )',
'high' =>
'MAX( cl_timestamp )' ] )
281 ->from(
'categorylinks' );
283 $queryBuilder->where( [
'cl_to' => $this->category->getDBkey(), ] );
285 $queryBuilder->join(
'linktarget',
null,
'cl_target_id = lt_id' )
286 ->where( [
'lt_title' => $this->category->getDBkey(),
'lt_namespace' =>
NS_CATEGORY ] );
288 $res = $queryBuilder->caller( __METHOD__ )->fetchRow();
303 private function selectRandomPageFromDB( $rand, $offset, $up, $fname ) {
304 return $this->
getQueryBuilder( $rand, $offset, $up )->caller( $fname )->fetchRow();
316class_alias( SpecialRandomInCategory::class,
'SpecialRandomInCategory' );
const SCHEMA_COMPAT_READ_OLD
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.
A class containing constants representing the names of configuration variables.
const CategoryLinksSchemaMigrationStage
Name constant for the CategoryLinksSchemaMigrationStage setting, for use with Config::get()
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.