26use Wikimedia\RequestTimeout\TimeoutException;
60 private $maxTimestamp =
null;
62 private $minTimestamp =
null;
65 private $loadBalancer;
71 parent::__construct(
'RandomInCategory' );
72 $this->loadBalancer = $loadBalancer;
80 $this->category = $cat;
81 $this->maxTimestamp =
null;
82 $this->minTimestamp =
null;
93 'label-message' =>
'randomincategory-category',
123 $categoryStr = $data[
'category'];
125 if ( $categoryStr ) {
126 $cat = Title::newFromText( $categoryStr,
NS_CATEGORY );
129 if ( $cat && $cat->getNamespace() !==
NS_CATEGORY ) {
131 $cat = Title::makeTitleSafe(
NS_CATEGORY, $categoryStr );
138 if ( !$this->category && $categoryStr ) {
139 $msg = $this->
msg(
'randomincategory-invalidcategory',
142 return Status::newFatal( $msg );
144 } elseif ( !$this->category ) {
151 $msg = $this->
msg(
'randomincategory-nopages',
152 $this->category->getText() );
154 return Status::newFatal( $msg );
157 $query = $this->
getRequest()->getQueryValues();
158 unset( $query[
'title'] );
172 $offset = mt_rand( 0, $this->maxOffset );
174 if ( mt_rand( 0, 1 ) ) {
180 $row = $this->selectRandomPageFromDB( $rand, $offset, $up, __METHOD__ );
184 $row = $this->selectRandomPageFromDB(
false, $offset, $up, __METHOD__ );
189 $row = $this->selectRandomPageFromDB( $rand, 0, $up, __METHOD__ );
194 $row = $this->selectRandomPageFromDB(
false, 0,
true, __METHOD__ );
198 return Title::makeTitle( $row->page_namespace, $row->page_title );
216 $op = $up ?
'>=' :
'<=';
217 $dir = $up ?
'ASC' :
'DESC';
218 if ( !$this->category instanceof
Title ) {
222 'tables' => [
'categorylinks',
'page' ],
223 'fields' => [
'page_title',
'page_namespace' ],
224 'conds' => array_merge( [
225 'cl_to' => $this->category->getDBkey(),
228 'ORDER BY' =>
'cl_timestamp ' . $dir,
233 'page' => [
'JOIN',
'cl_from = page_id' ]
237 $dbr = $this->loadBalancer->getConnectionRef( ILoadBalancer::DB_REPLICA );
240 $qi[
'conds'][] =
'cl_timestamp ' . $op .
' ' .
241 $dbr->addQuotes(
$dbr->timestamp( $minClTime ) );
253 if ( $rand ===
false ) {
256 if ( !$this->minTimestamp || !$this->maxTimestamp ) {
258 list( $this->minTimestamp, $this->maxTimestamp ) = $this->
getMinAndMaxForCat( $this->category );
259 }
catch ( TimeoutException $e ) {
261 }
catch ( Exception $e ) {
267 $ts = ( $this->maxTimestamp - $this->minTimestamp ) * $rand + $this->minTimestamp;
269 return intval( $ts );
280 $dbr = $this->loadBalancer->getConnectionRef( ILoadBalancer::DB_REPLICA );
284 'low' =>
'MIN( cl_timestamp )',
285 'high' =>
'MAX( cl_timestamp )'
288 'cl_to' => $this->category->getDBkey(),
306 private function selectRandomPageFromDB( $rand, $offset, $up, $fname = __METHOD__ ) {
307 $dbr = $this->loadBalancer->getConnectionRef( ILoadBalancer::DB_REPLICA );
319 return $res->fetchObject();
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.
string null $par
The sub-page of the special page.
getOutput()
Get the OutputPage being used for this instance.
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
getRequest()
Get the WebRequest being used for this instance.
addHelpLink( $to, $overrideBaseUrl=false)
Adds help link with an icon via page indicators.
Special page to direct the user to a random page.
setParameter( $par)
Maybe do something interesting with the subpage parameter.
requiresWrite()
Whether this action requires the wiki not to be locked.
getDisplayFormat()
Get display format for the form.
requiresUnblock()
Whether this action cannot be executed by a blocked user.
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
getRandomTitle()
Choose a random title.
alterForm(HTMLForm $form)
Play with the HTMLForm if you need to more substantially.
onSubmit(array $data)
Process the form on POST submission.
getMinAndMaxForCat(Title $category)
Get the lowest and highest timestamp for a category.
setCategory(Title $cat)
Set which category to use.
getQueryInfo( $rand, $offset, $up)
getTimestampOffset( $rand)
getFormFields()
Get an HTMLForm descriptor array.
__construct(ILoadBalancer $loadBalancer)
Represents a title within MediaWiki.