*
* @ingroup SpecialPage
*/
-class SpecialRandomInCategory extends SpecialPage {
+class SpecialRandomInCategory extends FormSpecialPage {
protected $extra = array(); // Extra SQL statements
protected $category = false; // Title object of category
protected $maxOffset = 30; // Max amount to fudge randomness by.
$this->minTimestamp = null;
}
- public function execute( $par ) {
+ protected function getFormFields() {
+ $form = array(
+ 'category' => array(
+ 'type' => 'text',
+ 'label-message' => 'randomincategory-category',
+ 'required' => true,
+ )
+ );
+
+ return $form;
+ }
+
+ public function requiresWrite() {
+ return false;
+ }
+
+ public function requiresUnblock() {
+ return false;
+ }
+
+ protected function setParameter( $par ) {
+ // if subpage present, fake form submission
+ $this->onSubmit( array( 'category' => $par ) );
+ }
+
+ public function onSubmit( array $data ) {
$cat = false;
- $categoryStr = $this->getRequest()->getText( 'category', $par );
+ $categoryStr = $data['category'];
if ( $categoryStr ) {
$cat = Title::newFromText( $categoryStr, NS_CATEGORY );
}
if ( !$this->category && $categoryStr ) {
- $this->setHeaders();
- $this->getOutput()->addWikiMsg( 'randomincategory-invalidcategory',
+ $msg = $this->msg( 'randomincategory-invalidcategory',
wfEscapeWikiText( $categoryStr ) );
- return;
+ return Status::newFatal( $msg );
+
} elseif ( !$this->category ) {
- $this->setHeaders();
- $input = Html::input( 'category' );
- $submitText = $this->msg( 'randomincategory-selectcategory-submit' )->text();
- $submit = Html::input( '', $submitText, 'submit' );
-
- $msg = $this->msg( 'randomincategory-selectcategory' );
- $form = Html::rawElement( 'form', array( 'action' => wfScript() ),
- Html::hidden( 'title', $this->getPageTitle()->getPrefixedText() ) .
- $msg->rawParams( $input, $submit )->parse()
- );
- $this->getOutput()->addHtml( $form );
-
- return;
+ return false; // no data sent
}
$title = $this->getRandomTitle();
if ( is_null( $title ) ) {
- $this->setHeaders();
- $this->getOutput()->addWikiMsg( 'randomincategory-nopages',
+ $msg = $this->msg( 'randomincategory-nopages',
$this->category->getText() );
- return;
+ return Status::newFatal( $msg );
}
- $query = $this->getRequest()->getValues();
- unset( $query['title'] );
- unset( $query['category'] );
- $this->getOutput()->redirect( $title->getFullURL( $query ) );
+ $this->getOutput()->redirect( $title->getFullURL() );
}
/**
* @param float $rand Random number between 0 and 1
* @param int $offset Extra offset to fudge randomness
* @param bool $up True to get the result above the random number, false for below
- *
+ * @return array Query information.
+ * @throws MWException
* @note The $up parameter is supposed to counteract what would happen if there
* was a large gap in the distribution of cl_timestamp values. This way instead
* of things to the right of the gap being favoured, both sides of the gap
* are favoured.
- * @return array Query information.
*/
protected function getQueryInfo( $rand, $offset, $up ) {
$op = $up ? '>=' : '<=';
if ( !$this->minTimestamp || !$this->maxTimestamp ) {
try {
list( $this->minTimestamp, $this->maxTimestamp ) = $this->getMinAndMaxForCat( $this->category );
- } catch ( MWException $e ) {
+ } catch ( Exception $e ) {
// Possibly no entries in category.
return false;
}