MediaWiki  1.23.12
SpecialRandomInCategory.php
Go to the documentation of this file.
1 <?php
50  protected $extra = array(); // Extra SQL statements
51  protected $category = false; // Title object of category
52  protected $maxOffset = 30; // Max amount to fudge randomness by.
53  private $maxTimestamp = null;
54  private $minTimestamp = null;
55 
56  public function __construct( $name = 'RandomInCategory' ) {
57  parent::__construct( $name );
58  }
59 
64  public function setCategory( Title $cat ) {
65  $this->category = $cat;
66  $this->maxTimestamp = null;
67  $this->minTimestamp = null;
68  }
69 
70  public function execute( $par ) {
71  global $wgScript;
72 
73  $cat = false;
74 
75  $categoryStr = $this->getRequest()->getText( 'category', $par );
76 
77  if ( $categoryStr ) {
78  $cat = Title::newFromText( $categoryStr, NS_CATEGORY );
79  }
80 
81  if ( $cat && $cat->getNamespace() !== NS_CATEGORY ) {
82  // Someone searching for something like "Wikipedia:Foo"
83  $cat = Title::makeTitleSafe( NS_CATEGORY, $categoryStr );
84  }
85 
86  if ( $cat ) {
87  $this->setCategory( $cat );
88  }
89 
90  if ( !$this->category && $categoryStr ) {
91  $this->setHeaders();
92  $this->getOutput()->addWikiMsg( 'randomincategory-invalidcategory',
93  wfEscapeWikiText( $categoryStr ) );
94 
95  return;
96  } elseif ( !$this->category ) {
97  $this->setHeaders();
98  $input = Html::input( 'category' );
99  $submitText = $this->msg( 'randomincategory-selectcategory-submit' )->text();
100  $submit = Html::input( '', $submitText, 'submit' );
101 
102  $msg = $this->msg( 'randomincategory-selectcategory' );
103  $form = Html::rawElement( 'form', array( 'action' => $wgScript ),
104  Html::hidden( 'title', $this->getPageTitle()->getPrefixedText() ) .
105  $msg->rawParams( $input, $submit )->parse()
106  );
107  $this->getOutput()->addHtml( $form );
108 
109  return;
110  }
111 
112  $title = $this->getRandomTitle();
113 
114  if ( is_null( $title ) ) {
115  $this->setHeaders();
116  $this->getOutput()->addWikiMsg( 'randomincategory-nopages',
117  $this->category->getText() );
118 
119  return;
120  }
121 
122  $query = $this->getRequest()->getValues();
123  unset( $query['title'] );
124  unset( $query['category'] );
125  $this->getOutput()->redirect( $title->getFullURL( $query ) );
126  }
127 
132  public function getRandomTitle() {
133  // Convert to float, since we do math with the random number.
134  $rand = (float)wfRandom();
135  $title = null;
136 
137  // Given that timestamps are rather unevenly distributed, we also
138  // use an offset between 0 and 30 to make any biases less noticeable.
139  $offset = mt_rand( 0, $this->maxOffset );
140 
141  if ( mt_rand( 0, 1 ) ) {
142  $up = true;
143  } else {
144  $up = false;
145  }
146 
147  $row = $this->selectRandomPageFromDB( $rand, $offset, $up );
148 
149  // Try again without the timestamp offset (wrap around the end)
150  if ( !$row ) {
151  $row = $this->selectRandomPageFromDB( false, $offset, $up );
152  }
153 
154  // Maybe the category is really small and offset too high
155  if ( !$row ) {
156  $row = $this->selectRandomPageFromDB( $rand, 0, $up );
157  }
158 
159  // Just get the first entry.
160  if ( !$row ) {
161  $row = $this->selectRandomPageFromDB( false, 0, true );
162  }
163 
164  if ( $row ) {
165  return Title::makeTitle( $row->page_namespace, $row->page_title );
166  }
167 
168  return null;
169  }
170 
182  protected function getQueryInfo( $rand, $offset, $up ) {
183  $op = $up ? '>=' : '<=';
184  $dir = $up ? 'ASC' : 'DESC';
185  if ( !$this->category instanceof Title ) {
186  throw new MWException( 'No category set' );
187  }
188  $qi = array(
189  'tables' => array( 'categorylinks', 'page' ),
190  'fields' => array( 'page_title', 'page_namespace' ),
191  'conds' => array_merge( array(
192  'cl_to' => $this->category->getDBKey(),
193  ), $this->extra ),
194  'options' => array(
195  'ORDER BY' => 'cl_timestamp ' . $dir,
196  'LIMIT' => 1,
197  'OFFSET' => $offset
198  ),
199  'join_conds' => array(
200  'page' => array( 'INNER JOIN', 'cl_from = page_id' )
201  )
202  );
203 
204  $dbr = wfGetDB( DB_SLAVE );
205  $minClTime = $this->getTimestampOffset( $rand );
206  if ( $minClTime ) {
207  $qi['conds'][] = 'cl_timestamp ' . $op . ' ' .
208  $dbr->addQuotes( $dbr->timestamp( $minClTime ) );
209  }
210 
211  return $qi;
212  }
213 
219  protected function getTimestampOffset( $rand ) {
220  if ( $rand === false ) {
221  return false;
222  }
223  if ( !$this->minTimestamp || !$this->maxTimestamp ) {
224  try {
225  list( $this->minTimestamp, $this->maxTimestamp ) = $this->getMinAndMaxForCat( $this->category );
226  } catch ( MWException $e ) {
227  // Possibly no entries in category.
228  return false;
229  }
230  }
231 
232  $ts = ( $this->maxTimestamp - $this->minTimestamp ) * $rand + $this->minTimestamp;
233 
234  return intval( $ts );
235  }
236 
244  protected function getMinAndMaxForCat( Title $category ) {
245  $dbr = wfGetDB( DB_SLAVE );
246  $res = $dbr->selectRow(
247  'categorylinks',
248  array(
249  'low' => 'MIN( cl_timestamp )',
250  'high' => 'MAX( cl_timestamp )'
251  ),
252  array(
253  'cl_to' => $this->category->getDBKey(),
254  ),
255  __METHOD__,
256  array(
257  'LIMIT' => 1
258  )
259  );
260  if ( !$res ) {
261  throw new MWException( 'No entries in category' );
262  }
263 
264  return array( wfTimestamp( TS_UNIX, $res->low ), wfTimestamp( TS_UNIX, $res->high ) );
265  }
266 
274  private function selectRandomPageFromDB( $rand, $offset, $up, $fname = __METHOD__ ) {
275  $dbr = wfGetDB( DB_SLAVE );
276 
277  $query = $this->getQueryInfo( $rand, $offset, $up );
278  $res = $dbr->select(
279  $query['tables'],
280  $query['fields'],
281  $query['conds'],
282  $fname,
283  $query['options'],
284  $query['join_conds']
285  );
286 
287  return $res->fetchObject();
288  }
289 
290  protected function getGroupName() {
291  return 'redirects';
292  }
293 }
SpecialPage\getPageTitle
getPageTitle( $subpage=false)
Get a self-referential title object.
Definition: SpecialPage.php:488
Title\makeTitle
static & makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:398
SpecialRandomInCategory\$maxOffset
$maxOffset
Definition: SpecialRandomInCategory.php:52
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:189
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
SpecialRandomInCategory\getRandomTitle
getRandomTitle()
Choose a random title.
Definition: SpecialRandomInCategory.php:132
SpecialPage\getOutput
getOutput()
Get the OutputPage being used for this instance.
Definition: SpecialPage.php:535
SpecialRandomInCategory\getTimestampOffset
getTimestampOffset( $rand)
Definition: SpecialRandomInCategory.php:219
SpecialRandomInCategory\getMinAndMaxForCat
getMinAndMaxForCat(Title $category)
Get the lowest and highest timestamp for a category.
Definition: SpecialRandomInCategory.php:244
wfGetDB
& wfGetDB( $db, $groups=array(), $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:3706
$form
usually copyright or history_copyright This message must be in HTML not wikitext $subpages will be ignored and the rest of subPageSubtitle() will run. 'SkinTemplateBuildNavUrlsNav_urlsAfterPermalink' whether MediaWiki currently thinks this is a CSS JS page Hooks may change this value to override the return value of Title::isCssOrJsPage(). 'TitleIsAlwaysKnown' whether MediaWiki currently thinks this page is known isMovable() always returns false. $title whether MediaWiki currently thinks this page is movable Hooks may change this value to override the return value of Title::isMovable(). 'TitleIsWikitextPage' whether MediaWiki currently thinks this is a wikitext page Hooks may change this value to override the return value of Title::isWikitextPage() 'TitleMove' use UploadVerification and UploadVerifyFile instead $form
Definition: hooks.txt:2578
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:2530
$fname
if(!defined( 'MEDIAWIKI')) $fname
This file is not a valid entry point, perform no further processing unless MEDIAWIKI is defined.
Definition: Setup.php:35
SpecialRandomInCategory\__construct
__construct( $name='RandomInCategory')
Definition: SpecialRandomInCategory.php:56
Html\hidden
static hidden( $name, $value, $attribs=array())
Convenience function to produce an input element with type=hidden.
Definition: Html.php:665
$dbr
$dbr
Definition: testCompression.php:48
MWException
MediaWiki exception.
Definition: MWException.php:26
SpecialRandomInCategory\$extra
$extra
Definition: SpecialRandomInCategory.php:50
SpecialRandomInCategory\setCategory
setCategory(Title $cat)
Set which category to use.
Definition: SpecialRandomInCategory.php:64
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
SpecialPage\setHeaders
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes!
Definition: SpecialPage.php:352
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
NS_CATEGORY
const NS_CATEGORY
Definition: Defines.php:93
Html\input
static input( $name, $value='', $type='text', $attribs=array())
Convenience function to produce an "<input>" element.
Definition: Html.php:648
list
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition: deferred.txt:11
Title\makeTitleSafe
static makeTitleSafe( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:422
$title
presenting them properly to the user as errors is done by the caller $title
Definition: hooks.txt:1324
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:336
SpecialPage\msg
msg()
Wrapper around wfMessage that sets the current context.
Definition: SpecialPage.php:609
SpecialPage
Parent class for all special pages.
Definition: SpecialPage.php:33
SpecialPage\getRequest
getRequest()
Get the WebRequest being used for this instance.
Definition: SpecialPage.php:525
SpecialRandomInCategory\$category
$category
Definition: SpecialRandomInCategory.php:51
wfEscapeWikiText
wfEscapeWikiText( $text)
Escapes the given text so that it may be output using addWikiText() without any linking,...
Definition: GlobalFunctions.php:2124
SpecialRandomInCategory
Special page to direct the user to a random page.
Definition: SpecialRandomInCategory.php:49
DB_SLAVE
const DB_SLAVE
Definition: Defines.php:55
Title
Represents a title within MediaWiki.
Definition: Title.php:35
wfRandom
wfRandom()
Get a random decimal value between 0 and 1, in a way not likely to give duplicate values for any real...
Definition: GlobalFunctions.php:328
SpecialRandomInCategory\getGroupName
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
Definition: SpecialRandomInCategory.php:290
$dir
if(count( $args)==0) $dir
Definition: importImages.php:49
SpecialRandomInCategory\$maxTimestamp
$maxTimestamp
Definition: SpecialRandomInCategory.php:53
SpecialRandomInCategory\$minTimestamp
$minTimestamp
Definition: SpecialRandomInCategory.php:54
TS_UNIX
const TS_UNIX
Unix time - the number of seconds since 1970-01-01 00:00:00 UTC.
Definition: GlobalFunctions.php:2473
SpecialRandomInCategory\getQueryInfo
getQueryInfo( $rand, $offset, $up)
Definition: SpecialRandomInCategory.php:182
SpecialRandomInCategory\selectRandomPageFromDB
selectRandomPageFromDB( $rand, $offset, $up, $fname=__METHOD__)
Definition: SpecialRandomInCategory.php:274
Html\rawElement
static rawElement( $element, $attribs=array(), $contents='')
Returns an HTML element in a string.
Definition: Html.php:124
$query
return true to allow those checks to and false if checking is done use this to change the tables headers temp or archived zone change it to an object instance and return false override the list derivative used the name of the old file when set the default code will be skipped add a value to it if you want to add a cookie that have to vary cache options can modify $query
Definition: hooks.txt:1105
$res
$res
Definition: database.txt:21
SpecialRandomInCategory\execute
execute( $par)
Default execute method Checks user permissions, calls the function given in mFunction.
Definition: SpecialRandomInCategory.php:70
$e
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException' returning false will NOT prevent logging $e
Definition: hooks.txt:1632