Merge "Move section ID fallbacks into headers themselves"
[lhc/web/wiklou.git] / includes / specials / SpecialWatchlist.php
index 51ddc0b..ba3cb87 100644 (file)
@@ -32,6 +32,8 @@ use Wikimedia\Rdbms\IDatabase;
  * @ingroup SpecialPage
  */
 class SpecialWatchlist extends ChangesListSpecialPage {
+       private $maxDays;
+
        public function __construct( $page = 'Watchlist', $restriction = 'viewmywatchlist' ) {
                parent::__construct( $page, $restriction );
 
@@ -58,6 +60,7 @@ class SpecialWatchlist extends ChangesListSpecialPage {
                        'mediawiki.special.changeslist.visitedstatus',
                        'mediawiki.special.watchlist',
                ] );
+               $output->addModuleStyles( [ 'mediawiki.special.watchlist.styles' ] );
 
                $mode = SpecialEditWatchlist::getMode( $request, $subpage );
                if ( $mode !== false ) {
@@ -92,6 +95,26 @@ class SpecialWatchlist extends ChangesListSpecialPage {
                }
 
                parent::execute( $subpage );
+
+               if ( $this->isStructuredFilterUiEnabled() ) {
+                       $output->addModuleStyles( [ 'mediawiki.rcfilters.highlightCircles.seenunseen.styles' ] );
+
+                       $output->addJsConfigVars( 'wgStructuredChangeFiltersLiveUpdateSupported', false );
+                       $output->addJsConfigVars(
+                               'wgStructuredChangeFiltersSavedQueriesPreferenceName',
+                               'rcfilters-wl-saved-queries'
+                       );
+                       $output->addJsConfigVars(
+                               'wgStructuredChangeFiltersEditWatchlistUrl',
+                               SpecialPage::getTitleFor( 'EditWatchlist' )->getLocalURL()
+                       );
+               }
+       }
+
+       protected function isStructuredFilterUiEnabled() {
+               return parent::isStructuredFilterUiEnabled()
+                       && ( $this->getConfig()->get( 'StructuredChangeFiltersOnWatchlist' )
+                               || $this->getRequest()->getBool( 'rcfilters' ) );
        }
 
        /**
@@ -125,6 +148,87 @@ class SpecialWatchlist extends ChangesListSpecialPage {
        protected function registerFilters() {
                parent::registerFilters();
 
+               // legacy 'extended' filter
+               $this->registerFilterGroup( new ChangesListBooleanFilterGroup( [
+                       'name' => 'extended-group',
+                       'filters' => [
+                               [
+                                       'name' => 'extended',
+                                       'isReplacedInStructuredUi' => true,
+                                       'activeValue' => false,
+                                       'default' => $this->getUser()->getBoolOption( 'extendwatchlist' ),
+                                       'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables,
+                                                                                                 &$fields, &$conds, &$query_options, &$join_conds ) {
+                                               $nonRevisionTypes = [ RC_LOG ];
+                                               Hooks::run( 'SpecialWatchlistGetNonRevisionTypes', [ &$nonRevisionTypes ] );
+                                               if ( $nonRevisionTypes ) {
+                                                       $conds[] = $dbr->makeList(
+                                                               [
+                                                                       'rc_this_oldid=page_latest',
+                                                                       'rc_type' => $nonRevisionTypes,
+                                                               ],
+                                                               LIST_OR
+                                                       );
+                                               }
+                                       },
+                               ]
+                       ],
+
+               ] ) );
+
+               if ( $this->isStructuredFilterUiEnabled() ) {
+                       $this->getFilterGroup( 'lastRevision' )
+                               ->getFilter( 'hidepreviousrevisions' )
+                               ->setDefault( !$this->getUser()->getBoolOption( 'extendwatchlist' ) );
+               }
+
+               $this->registerFilterGroup( new ChangesListStringOptionsFilterGroup( [
+                       'name' => 'watchlistactivity',
+                       'title' => 'rcfilters-filtergroup-watchlistactivity',
+                       'class' => ChangesListStringOptionsFilterGroup::class,
+                       'priority' => 3,
+                       'isFullCoverage' => true,
+                       'filters' => [
+                               [
+                                       'name' => 'unseen',
+                                       'label' => 'rcfilters-filter-watchlistactivity-unseen-label',
+                                       'description' => 'rcfilters-filter-watchlistactivity-unseen-description',
+                                       'cssClassSuffix' => 'watchedunseen',
+                                       'isRowApplicableCallable' => function ( $ctx, $rc ) {
+                                               $changeTs = $rc->getAttribute( 'rc_timestamp' );
+                                               $lastVisitTs = $rc->getAttribute( 'wl_notificationtimestamp' );
+                                               return $lastVisitTs !== null && $changeTs >= $lastVisitTs;
+                                       },
+                               ],
+                               [
+                                       'name' => 'seen',
+                                       'label' => 'rcfilters-filter-watchlistactivity-seen-label',
+                                       'description' => 'rcfilters-filter-watchlistactivity-seen-description',
+                                       'cssClassSuffix' => 'watchedseen',
+                                       'isRowApplicableCallable' => function ( $ctx, $rc ) {
+                                               $changeTs = $rc->getAttribute( 'rc_timestamp' );
+                                               $lastVisitTs = $rc->getAttribute( 'wl_notificationtimestamp' );
+                                               return $lastVisitTs === null || $changeTs < $lastVisitTs;
+                                       }
+                               ],
+                       ],
+                       'default' => ChangesListStringOptionsFilterGroup::NONE,
+                       'queryCallable' => function ( $specialPageClassName, $context, $dbr,
+                                                                                 &$tables, &$fields, &$conds, &$query_options, &$join_conds, $selectedValues ) {
+                               if ( $selectedValues === [ 'seen' ] ) {
+                                       $conds[] = $dbr->makeList( [
+                                               'wl_notificationtimestamp IS NULL',
+                                               'rc_timestamp < wl_notificationtimestamp'
+                                       ], LIST_OR );
+                               } elseif ( $selectedValues === [ 'unseen' ] ) {
+                                       $conds[] = $dbr->makeList( [
+                                               'wl_notificationtimestamp IS NOT NULL',
+                                               'rc_timestamp >= wl_notificationtimestamp'
+                                       ], LIST_AND );
+                               }
+                       }
+               ] ) );
+
                $user = $this->getUser();
 
                $significance = $this->getFilterGroup( 'significance' );
@@ -170,13 +274,14 @@ class SpecialWatchlist extends ChangesListSpecialPage {
                $user = $this->getUser();
 
                $opts->add( 'days', $user->getOption( 'watchlistdays' ), FormOptions::FLOAT );
-               $opts->add( 'extended', $user->getBoolOption( 'extendwatchlist' ) );
+               $opts->add( 'limit', $user->getIntOption( 'wllimit' ), FormOptions::INT );
 
                return $opts;
        }
 
        public function validateOptions( FormOptions $opts ) {
                $opts->validateBounds( 'days', 0, $this->maxDays );
+               $opts->validateIntBounds( 'limit', 0, 5000 );
                parent::validateOptions( $opts );
        }
 
@@ -232,8 +337,11 @@ class SpecialWatchlist extends ChangesListSpecialPage {
                        // unchecked boxes.
                        foreach ( $this->filterGroups as $filterGroup ) {
                                if ( $filterGroup instanceof ChangesListBooleanFilterGroup ) {
+                                       /** @var ChangesListBooleanFilter $filter */
                                        foreach ( $filterGroup->getFilters() as $filter ) {
-                                               $allBooleansFalse[$filter->getName()] = false;
+                                               if ( $filter->displaysOnUnstructuredUi() ) {
+                                                       $allBooleansFalse[$filter->getName()] = false;
+                                               }
                                        }
                                }
                        }
@@ -275,32 +383,9 @@ class SpecialWatchlist extends ChangesListSpecialPage {
                $dbr = $this->getDB();
                $user = $this->getUser();
 
-               # Toggle watchlist content (all recent edits or just the latest)
-               if ( $opts['extended'] ) {
-                       $usePage = false;
-               } else {
-                       # Top log Ids for a page are not stored
-                       $nonRevisionTypes = [ RC_LOG ];
-                       Hooks::run( 'SpecialWatchlistGetNonRevisionTypes', [ &$nonRevisionTypes ] );
-                       if ( $nonRevisionTypes ) {
-                               $conds[] = $dbr->makeList(
-                                       [
-                                               'rc_this_oldid=page_latest',
-                                               'rc_type' => $nonRevisionTypes,
-                                       ],
-                                       LIST_OR
-                               );
-                       }
-                       $usePage = true;
-               }
-
                $tables = array_merge( [ 'recentchanges', 'watchlist' ], $tables );
                $fields = array_merge( RecentChange::selectFields(), $fields );
 
-               $query_options = array_merge( [
-                       'ORDER BY' => 'rc_timestamp DESC',
-                       'LIMIT' => $user->getIntOption( 'wllimit' )
-               ], $query_options );
                $join_conds = array_merge(
                        [
                                'watchlist' => [
@@ -315,18 +400,11 @@ class SpecialWatchlist extends ChangesListSpecialPage {
                        $join_conds
                );
 
-               if ( $this->getConfig()->get( 'ShowUpdatedMarker' ) ) {
-                       $fields[] = 'wl_notificationtimestamp';
-               }
+               $tables[] = 'page';
+               $fields[] = 'page_latest';
+               $join_conds['page'] = [ 'LEFT JOIN', 'rc_cur_id=page_id' ];
 
-               $rollbacker = $user->isAllowed( 'rollback' );
-               if ( $usePage || $rollbacker ) {
-                       $tables[] = 'page';
-                       $join_conds['page'] = [ 'LEFT JOIN', 'rc_cur_id=page_id' ];
-                       if ( $rollbacker ) {
-                               $fields[] = 'page_latest';
-                       }
-               }
+               $fields[] = 'wl_notificationtimestamp';
 
                // Log entries with DELETED_ACTION must not show up unless the user has
                // the necessary rights.
@@ -344,17 +422,39 @@ class SpecialWatchlist extends ChangesListSpecialPage {
                        ], LIST_OR );
                }
 
+               $tagFilter = $opts['tagfilter'] ? explode( '|', $opts['tagfilter'] ) : [];
                ChangeTags::modifyDisplayQuery(
                        $tables,
                        $fields,
                        $conds,
                        $join_conds,
                        $query_options,
-                       ''
+                       $tagFilter
                );
 
                $this->runMainQueryHook( $tables, $fields, $conds, $query_options, $join_conds, $opts );
 
+               if ( $this->areFiltersInConflict() ) {
+                       return false;
+               }
+
+               $orderByAndLimit = [
+                       'ORDER BY' => 'rc_timestamp DESC',
+                       'LIMIT' => $opts['limit']
+               ];
+               if ( in_array( 'DISTINCT', $query_options ) ) {
+                       // ChangeTags::modifyDisplayQuery() adds DISTINCT when filtering on multiple tags.
+                       // In order to prevent DISTINCT from causing query performance problems,
+                       // we have to GROUP BY the primary key. This in turn requires us to add
+                       // the primary key to the end of the ORDER BY, and the old ORDER BY to the
+                       // start of the GROUP BY
+                       $orderByAndLimit['ORDER BY'] = 'rc_timestamp DESC, rc_id DESC';
+                       $orderByAndLimit['GROUP BY'] = 'rc_timestamp, rc_id';
+               }
+               // array_merge() is used intentionally here so that hooks can, should
+               // they so desire, override the ORDER BY / LIMIT condition(s)
+               $query_options = array_merge( $orderByAndLimit, $query_options );
+
                return $dbr->select(
                        $tables,
                        $fields,
@@ -431,6 +531,23 @@ class SpecialWatchlist extends ChangesListSpecialPage {
                $list = ChangesList::newFromContext( $this->getContext(), $this->filterGroups );
                $list->setWatchlistDivs();
                $list->initChangesListRows( $rows );
+               if ( $user->getOption( 'watchlistunwatchlinks' ) ) {
+                       $list->setChangeLinePrefixer( function ( RecentChange $rc, ChangesList $cl, $grouped ) {
+                               // Don't show unwatch link if the line is a grouped log entry using EnhancedChangesList,
+                               // since EnhancedChangesList groups log entries by performer rather than by target article
+                               if ( $rc->mAttribs['rc_type'] == RC_LOG && $cl instanceof EnhancedChangesList &&
+                                       $grouped ) {
+                                       return '';
+                               } else {
+                                       return $this->getLinkRenderer()
+                                                       ->makeKnownLink( $rc->getTitle(),
+                                                               $this->msg( 'watchlist-unwatch' )->text(), [
+                                                                       'class' => 'mw-unwatch-link',
+                                                                       'title' => $this->msg( 'tooltip-ca-unwatch' )->text()
+                                                               ], [ 'action' => 'unwatch' ] ) . '&#160;';
+                               }
+                       } );
+               }
                $dbr->dataSeek( $rows, 0 );
 
                if ( $this->getConfig()->get( 'RCShowWatchingUsers' )
@@ -440,6 +557,11 @@ class SpecialWatchlist extends ChangesListSpecialPage {
                }
 
                $s = $list->beginRecentChangesList();
+
+               if ( $this->isStructuredFilterUiEnabled() ) {
+                       $s .= $this->makeLegend();
+               }
+
                $userShowHiddenCats = $this->getUser()->getBoolOption( 'showhiddencats' );
                $counter = 1;
                foreach ( $rows as $obj ) {
@@ -500,6 +622,24 @@ class SpecialWatchlist extends ChangesListSpecialPage {
 
                $this->setTopText( $opts );
 
+               $form = '';
+
+               $form .= Xml::openElement( 'form', [
+                       'method' => 'get',
+                       'action' => wfScript(),
+                       'id' => 'mw-watchlist-form'
+               ] );
+               $form .= Html::hidden( 'title', $this->getPageTitle()->getPrefixedText() );
+               $form .= Xml::fieldset(
+                       $this->msg( 'watchlist-options' )->text(),
+                       false,
+                       [ 'id' => 'mw-watchlist-options', 'class' => 'cloptions' ]
+               );
+
+               if ( !$this->isStructuredFilterUiEnabled() ) {
+                       $form .= $this->makeLegend();
+               }
+
                $lang = $this->getLanguage();
                if ( $opts['days'] > 0 ) {
                        $days = $opts['days'];
@@ -507,16 +647,23 @@ class SpecialWatchlist extends ChangesListSpecialPage {
                        $days = $this->maxDays;
                }
                $timestamp = wfTimestampNow();
-               $wlInfo = $this->msg( 'wlnote' )->numParams( $numRows, round( $days * 24 ) )->params(
-                       $lang->userDate( $timestamp, $user ), $lang->userTime( $timestamp, $user )
-               )->parse() . "<br />\n";
+               $wlInfo = Html::rawElement(
+                       'span',
+                       [ 'class' => 'wlinfo' ],
+                       $this->msg( 'wlnote' )->numParams( $numRows, round( $days * 24 ) )->params(
+                               $lang->userDate( $timestamp, $user ), $lang->userTime( $timestamp, $user )
+                       )->parse()
+               ) . "<br />\n";
 
                $nondefaults = $opts->getChangedValues();
-               $cutofflinks = $this->msg( 'wlshowtime' ) . ' ' . $this->cutoffselector( $opts );
+               $cutofflinks = Html::rawElement(
+                       'span',
+                       [ 'class' => 'cldays cloption' ],
+                       $this->msg( 'wlshowtime' ) . ' ' . $this->cutoffselector( $opts )
+               );
 
                # Spit out some control panel links
                $links = [];
-               $context = $this->getContext();
                $namesOfDisplayedFilters = [];
                foreach ( $this->getFilterGroups() as $groupName => $group ) {
                        if ( !$group->isPerGroupRequestParameter() ) {
@@ -527,7 +674,8 @@ class SpecialWatchlist extends ChangesListSpecialPage {
                                                        $nondefaults,
                                                        $filter->getShowHide(),
                                                        $filterName,
-                                                       $opts[$filterName]
+                                                       $opts[$filterName],
+                                                       $filter->isFeatureAvailableOnStructuredUi( $this )
                                                );
                                        }
                                }
@@ -544,17 +692,19 @@ class SpecialWatchlist extends ChangesListSpecialPage {
                        unset( $hiddenFields[$filterName] );
                }
 
-               # Create output
-               $form = '';
-
                # Namespace filter and put the whole form together.
                $form .= $wlInfo;
                $form .= $cutofflinks;
-               $form .= $this->msg( 'watchlist-hide' ) .
+               $form .= Html::rawElement(
+                       'span',
+                       [ 'class' => 'clshowhide' ],
+                       $this->msg( 'watchlist-hide' ) .
                        $this->msg( 'colon-separator' )->escaped() .
-                       implode( ' ', $links );
+                       implode( ' ', $links )
+               );
                $form .= "\n<br />\n";
-               $form .= Html::namespaceSelector(
+
+               $namespaceForm = Html::namespaceSelector(
                        [
                                'selected' => $opts['namespace'],
                                'all' => '',
@@ -565,27 +715,66 @@ class SpecialWatchlist extends ChangesListSpecialPage {
                                'class' => 'namespaceselector',
                        ]
                ) . "\n";
-               $form .= '<span class="mw-input-with-label">' . Xml::checkLabel(
+               $namespaceForm .= '<span class="mw-input-with-label">' . Xml::checkLabel(
                        $this->msg( 'invert' )->text(),
                        'invert',
                        'nsinvert',
                        $opts['invert'],
                        [ 'title' => $this->msg( 'tooltip-invert' )->text() ]
                ) . "</span>\n";
-               $form .= '<span class="mw-input-with-label">' . Xml::checkLabel(
+               $namespaceForm .= '<span class="mw-input-with-label">' . Xml::checkLabel(
                        $this->msg( 'namespace_association' )->text(),
                        'associated',
                        'nsassociated',
                        $opts['associated'],
                        [ 'title' => $this->msg( 'tooltip-namespace_association' )->text() ]
                ) . "</span>\n";
-               $form .= Xml::submitButton( $this->msg( 'watchlist-submit' )->text() ) . "\n";
+               $form .= Html::rawElement(
+                       'span',
+                       [ 'class' => 'namespaceForm cloption' ],
+                       $namespaceForm
+               );
+
+               $form .= Xml::submitButton(
+                       $this->msg( 'watchlist-submit' )->text(),
+                       [ 'class' => 'cloption-submit' ]
+               ) . "\n";
                foreach ( $hiddenFields as $key => $value ) {
                        $form .= Html::hidden( $key, $value ) . "\n";
                }
                $form .= Xml::closeElement( 'fieldset' ) . "\n";
                $form .= Xml::closeElement( 'form' ) . "\n";
-               $this->getOutput()->addHTML( $form );
+
+               // Insert a placeholder for RCFilters
+               if ( $this->isStructuredFilterUiEnabled() ) {
+                       $rcfilterContainer = Html::element(
+                               'div',
+                               [ 'class' => 'rcfilters-container' ]
+                       );
+
+                       $loadingContainer = Html::rawElement(
+                               'div',
+                               [ 'class' => 'rcfilters-spinner' ],
+                               Html::element(
+                                       'div',
+                                       [ 'class' => 'rcfilters-spinner-bounce' ]
+                               )
+                       );
+
+                       // Wrap both with rcfilters-head
+                       $this->getOutput()->addHTML(
+                               Html::rawElement(
+                                       'div',
+                                       [ 'class' => 'rcfilters-head' ],
+                                       $rcfilterContainer . $form
+                               )
+                       );
+
+                       // Add spinner
+                       $this->getOutput()->addHTML( $loadingContainer );
+               } else {
+                       $this->getOutput()->addHTML( $form );
+               }
 
                $this->setBottomText( $opts );
        }
@@ -637,28 +826,36 @@ class SpecialWatchlist extends ChangesListSpecialPage {
 
        function setTopText( FormOptions $opts ) {
                $nondefaults = $opts->getChangedValues();
-               $form = "";
+               $form = '';
                $user = $this->getUser();
 
                $numItems = $this->countItems();
                $showUpdatedMarker = $this->getConfig()->get( 'ShowUpdatedMarker' );
 
                // Show watchlist header
-               $form .= "<p>";
+               $watchlistHeader = '';
                if ( $numItems == 0 ) {
-                       $form .= $this->msg( 'nowatchlist' )->parse() . "\n";
+                       $watchlistHeader = $this->msg( 'nowatchlist' )->parse();
                } else {
-                       $form .= $this->msg( 'watchlist-details' )->numParams( $numItems )->parse() . "\n";
+                       $watchlistHeader .= $this->msg( 'watchlist-details' )->numParams( $numItems )->parse() . "\n";
                        if ( $this->getConfig()->get( 'EnotifWatchlist' )
                                && $user->getOption( 'enotifwatchlistpages' )
                        ) {
-                               $form .= $this->msg( 'wlheader-enotif' )->parse() . "\n";
+                               $watchlistHeader .= $this->msg( 'wlheader-enotif' )->parse() . "\n";
                        }
                        if ( $showUpdatedMarker ) {
-                               $form .= $this->msg( 'wlheader-showupdated' )->parse() . "\n";
+                               $watchlistHeader .= $this->msg(
+                                       $this->isStructuredFilterUiEnabled() ?
+                                               'rcfilters-watchlist-showupdated' :
+                                               'wlheader-showupdated'
+                               )->parse() . "\n";
                        }
                }
-               $form .= "</p>";
+               $form .= Html::rawElement(
+                       'div',
+                       [ 'class' => 'watchlistDetails' ],
+                       $watchlistHeader
+               );
 
                if ( $numItems > 0 && $showUpdatedMarker ) {
                        $form .= Xml::openElement( 'form', [ 'method' => 'post',
@@ -674,32 +871,27 @@ class SpecialWatchlist extends ChangesListSpecialPage {
                        $form .= Xml::closeElement( 'form' ) . "\n";
                }
 
-               $form .= Xml::openElement( 'form', [
-                       'method' => 'get',
-                       'action' => wfScript(),
-                       'id' => 'mw-watchlist-form'
-               ] );
-               $form .= Html::hidden( 'title', $this->getPageTitle()->getPrefixedText() );
-               $form .= Xml::fieldset(
-                       $this->msg( 'watchlist-options' )->text(),
-                       false,
-                       [ 'id' => 'mw-watchlist-options' ]
-               );
-
-               $form .= $this->makeLegend();
-
                $this->getOutput()->addHTML( $form );
        }
 
-       protected function showHideCheck( $options, $message, $name, $value ) {
+       protected function showHideCheck( $options, $message, $name, $value, $inStructuredUi ) {
                $options[$name] = 1 - (int)$value;
 
-               return '<span class="mw-input-with-label">' . Xml::checkLabel(
-                       $this->msg( $message, '' )->text(),
-                       $name,
-                       $name,
-                       (int)$value
-               ) . '</span>';
+               $attribs = [ 'class' => 'mw-input-with-label clshowhideoption cloption' ];
+               if ( $inStructuredUi ) {
+                       $attribs[ 'data-feature-in-structured-ui' ] = true;
+               }
+
+               return Html::rawElement(
+                       'span',
+                       $attribs,
+                       Xml::checkLabel(
+                               $this->msg( $message, '' )->text(),
+                               $name,
+                               $name,
+                               (int)$value
+                       )
+               );
        }
 
        /**
@@ -714,4 +906,12 @@ class SpecialWatchlist extends ChangesListSpecialPage {
                $count = $store->countWatchedItems( $this->getUser() );
                return floor( $count / 2 );
        }
+
+       function getDefaultLimit() {
+               return $this->getUser()->getIntOption( 'wllimit' );
+       }
+
+       function getDefaultDays() {
+               return $this->getUser()->getIntOption( 'watchlistdays' );
+       }
 }