Merge "Rename autonym for 'no' from 'norsk bokmål' to 'norsk'"
[lhc/web/wiklou.git] / includes / specials / SpecialWatchlist.php
index 4a17533..9e01d2d 100644 (file)
@@ -23,6 +23,7 @@
 
 use MediaWiki\MediaWikiServices;
 use Wikimedia\Rdbms\ResultWrapper;
+use Wikimedia\Rdbms\IDatabase;
 
 /**
  * A special page that lists last changes made to the wiki,
@@ -33,6 +34,8 @@ use Wikimedia\Rdbms\ResultWrapper;
 class SpecialWatchlist extends ChangesListSpecialPage {
        public function __construct( $page = 'Watchlist', $restriction = 'viewmywatchlist' ) {
                parent::__construct( $page, $restriction );
+
+               $this->maxDays = $this->getConfig()->get( 'RCMaxAge' ) / ( 3600 * 24 );
        }
 
        public function doesWrites() {
@@ -80,6 +83,7 @@ class SpecialWatchlist extends ChangesListSpecialPage {
                if ( ( $config->get( 'EnotifWatchlist' ) || $config->get( 'ShowUpdatedMarker' ) )
                        && $request->getVal( 'reset' )
                        && $request->wasPosted()
+                       && $user->matchEditToken( $request->getVal( 'token' ) )
                ) {
                        $user->clearAllNotifications();
                        $output->redirect( $this->getPageTitle()->getFullURL( $opts->getChangedValues() ) );
@@ -107,16 +111,12 @@ class SpecialWatchlist extends ChangesListSpecialPage {
        /**
         * @inheritdoc
         */
-       protected function registerFiltersFromDefinitions( array $definition ) {
-               foreach ( $definition as $groupName => &$groupDefinition ) {
-                       foreach ( $groupDefinition['filters'] as &$filterDefinition ) {
-                               if ( isset( $filterDefinition['showHideSuffix'] ) ) {
-                                       $filterDefinition['showHide'] = 'wl' . $filterDefinition['showHideSuffix'];
-                               }
-                       }
+       protected function transformFilterDefinition( array $filterDefinition ) {
+               if ( isset( $filterDefinition['showHideSuffix'] ) ) {
+                       $filterDefinition['showHide'] = 'wl' . $filterDefinition['showHideSuffix'];
                }
 
-               parent::registerFiltersFromDefinitions( $definition );
+               return $filterDefinition;
        }
 
        /**
@@ -142,8 +142,11 @@ class SpecialWatchlist extends ChangesListSpecialPage {
                $hideLiu->setDefault( $user->getBoolOption( 'watchlisthideliu' ) );
 
                $reviewStatus = $this->getFilterGroup( 'reviewStatus' );
-               $hidePatrolled = $reviewStatus->getFilter( 'hidepatrolled' );
-               $hidePatrolled->setDefault( $user->getBoolOption( 'watchlisthidepatrolled' ) );
+               if ( $reviewStatus !== null ) {
+                       // Conditional on feature being available and rights
+                       $hidePatrolled = $reviewStatus->getFilter( 'hidepatrolled' );
+                       $hidePatrolled->setDefault( $user->getBoolOption( 'watchlisthidepatrolled' ) );
+               }
 
                $authorship = $this->getFilterGroup( 'authorship' );
                $hideMyself = $authorship->getFilter( 'hidemyself' );
@@ -151,7 +154,10 @@ class SpecialWatchlist extends ChangesListSpecialPage {
 
                $changeType = $this->getFilterGroup( 'changeType' );
                $hideCategorization = $changeType->getFilter( 'hidecategorization' );
-               $hideCategorization->setDefault( $user->getBoolOption( 'watchlisthidecategorization' ) );
+               if ( $hideCategorization !== null ) {
+                       // Conditional on feature being available
+                       $hideCategorization->setDefault( $user->getBoolOption( 'watchlisthidecategorization' ) );
+               }
        }
 
        /**
@@ -169,6 +175,11 @@ class SpecialWatchlist extends ChangesListSpecialPage {
                return $opts;
        }
 
+       public function validateOptions( FormOptions $opts ) {
+               $opts->validateBounds( 'days', 0, $this->maxDays );
+               parent::validateOptions( $opts );
+       }
+
        /**
         * Get all custom filters
         *
@@ -242,8 +253,8 @@ class SpecialWatchlist extends ChangesListSpecialPage {
         * @inheritdoc
         */
        protected function buildQuery( &$tables, &$fields, &$conds, &$query_options,
-               &$join_conds, FormOptions $opts ) {
-
+               &$join_conds, FormOptions $opts
+       ) {
                $dbr = $this->getDB();
                parent::buildQuery( $tables, $fields, $conds, $query_options, $join_conds,
                        $opts );
@@ -251,7 +262,7 @@ class SpecialWatchlist extends ChangesListSpecialPage {
                // Calculate cutoff
                if ( $opts['days'] > 0 ) {
                        $conds[] = 'rc_timestamp > ' .
-                               $dbr->addQuotes( $dbr->timestamp( time() - intval( $opts['days'] * 86400 ) ) );
+                               $dbr->addQuotes( $dbr->timestamp( time() - $opts['days'] * 3600 * 24 ) );
                }
        }
 
@@ -259,14 +270,13 @@ class SpecialWatchlist extends ChangesListSpecialPage {
         * @inheritdoc
         */
        protected function doMainQuery( $tables, $fields, $conds, $query_options,
-               $join_conds, FormOptions $opts ) {
-
+               $join_conds, FormOptions $opts
+       ) {
                $dbr = $this->getDB();
                $user = $this->getUser();
 
                # Toggle watchlist content (all recent edits or just the latest)
                if ( $opts['extended'] ) {
-                       $limitWatchlist = $user->getIntOption( 'wllimit' );
                        $usePage = false;
                } else {
                        # Top log Ids for a page are not stored
@@ -281,14 +291,16 @@ class SpecialWatchlist extends ChangesListSpecialPage {
                                        LIST_OR
                                );
                        }
-                       $limitWatchlist = 0;
                        $usePage = true;
                }
 
                $tables = array_merge( [ 'recentchanges', 'watchlist' ], $tables );
                $fields = array_merge( RecentChange::selectFields(), $fields );
 
-               $query_options = array_merge( [ 'ORDER BY' => 'rc_timestamp DESC' ], $query_options );
+               $query_options = array_merge( [
+                       'ORDER BY' => 'rc_timestamp DESC',
+                       'LIMIT' => $user->getIntOption( 'wllimit' )
+               ], $query_options );
                $join_conds = array_merge(
                        [
                                'watchlist' => [
@@ -306,9 +318,6 @@ class SpecialWatchlist extends ChangesListSpecialPage {
                if ( $this->getConfig()->get( 'ShowUpdatedMarker' ) ) {
                        $fields[] = 'wl_notificationtimestamp';
                }
-               if ( $limitWatchlist ) {
-                       $query_options['LIMIT'] = $limitWatchlist;
-               }
 
                $rollbacker = $user->isAllowed( 'rollback' );
                if ( $usePage || $rollbacker ) {
@@ -495,7 +504,7 @@ class SpecialWatchlist extends ChangesListSpecialPage {
                if ( $opts['days'] > 0 ) {
                        $days = $opts['days'];
                } else {
-                       $days = $this->getConfig()->get( 'RCMaxAge' ) / ( 3600 * 24 );
+                       $days = $this->maxDays;
                }
                $timestamp = wfTimestampNow();
                $wlInfo = $this->msg( 'wlnote' )->numParams( $numRows, round( $days * 24 ) )->params(
@@ -595,7 +604,7 @@ class SpecialWatchlist extends ChangesListSpecialPage {
                        $days[] = $userWatchlistOption;
                }
 
-               $maxDays = (string)( $this->getConfig()->get( 'RCMaxAge' ) / ( 3600 * 24 ) );
+               $maxDays = (string)$this->maxDays;
                // add the maximum possible value, if it isn't available already
                if ( !in_array( $maxDays, $days ) ) {
                        $days[] = $maxDays;
@@ -657,6 +666,7 @@ class SpecialWatchlist extends ChangesListSpecialPage {
                                'id' => 'mw-watchlist-resetbutton' ] ) . "\n" .
                        Xml::submitButton( $this->msg( 'enotif_reset' )->text(),
                                [ 'name' => 'mw-watchlist-reset-submit' ] ) . "\n" .
+                       Html::hidden( 'token', $user->getEditToken() ) . "\n" .
                        Html::hidden( 'reset', 'all' ) . "\n";
                        foreach ( $nondefaults as $key => $value ) {
                                $form .= Html::hidden( $key, $value ) . "\n";