Merge "RCFilters: Remove excluded params from URL"
[lhc/web/wiklou.git] / includes / specials / pagers / ContribsPager.php
index d7819c4..85faddc 100644 (file)
@@ -87,6 +87,10 @@ class ContribsPager extends RangeChronologicalPager {
                }
                $this->getDateRangeCond( $startTimestamp, $endTimestamp );
 
+               // This property on IndexPager is set by $this->getIndexField() in parent::__construct().
+               // We need to reassign it here so that it is used when the actual query is ran.
+               $this->mIndexField = $this->getIndexField();
+
                // Most of this code will use the 'contributions' group DB, which can map to replica DBs
                // with extra user based indexes or partioning by user. The additional metadata
                // queries should use a regular replica DB since the lookup pattern is not all by user.
@@ -171,73 +175,25 @@ class ContribsPager extends RangeChronologicalPager {
        }
 
        function getQueryInfo() {
-               list( $tables, $index, $userCond, $join_cond ) = $this->getUserCond();
-
-               $user = $this->getUser();
-               $conds = array_merge( $userCond, $this->getNamespaceCond() );
-
-               // Paranoia: avoid brute force searches (T19342)
-               if ( !$user->isAllowed( 'deletedhistory' ) ) {
-                       $conds[] = $this->mDb->bitAnd( 'rev_deleted', Revision::DELETED_USER ) . ' = 0';
-               } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
-                       $conds[] = $this->mDb->bitAnd( 'rev_deleted', Revision::SUPPRESSED_USER ) .
-                               ' != ' . Revision::SUPPRESSED_USER;
-               }
-
-               # Don't include orphaned revisions
-               $join_cond['page'] = Revision::pageJoinCond();
-               # Get the current user name for accounts
-               $join_cond['user'] = Revision::userJoinCond();
-
-               $options = [];
-               if ( $index ) {
-                       $options['USE INDEX'] = [ 'revision' => $index ];
-               }
-
+               $revQuery = Revision::getQueryInfo( [ 'page', 'user' ] );
                $queryInfo = [
-                       'tables' => $tables,
-                       'fields' => array_merge(
-                               Revision::selectFields(),
-                               Revision::selectUserFields(),
-                               [ 'page_namespace', 'page_title', 'page_is_new',
-                                       'page_latest', 'page_is_redirect', 'page_len' ]
-                       ),
-                       'conds' => $conds,
-                       'options' => $options,
-                       'join_conds' => $join_cond
+                       'tables' => $revQuery['tables'],
+                       'fields' => array_merge( $revQuery['fields'], [ 'page_is_new' ] ),
+                       'conds' => [],
+                       'options' => [],
+                       'join_conds' => $revQuery['joins'],
                ];
 
-               ChangeTags::modifyDisplayQuery(
-                       $queryInfo['tables'],
-                       $queryInfo['fields'],
-                       $queryInfo['conds'],
-                       $queryInfo['join_conds'],
-                       $queryInfo['options'],
-                       $this->tagFilter
-               );
-
-               // Avoid PHP 7.1 warning from passing $this by reference
-               $pager = $this;
-               Hooks::run( 'ContribsPager::getQueryInfo', [ &$pager, &$queryInfo ] );
-
-               return $queryInfo;
-       }
-
-       function getUserCond() {
-               $condition = [];
-               $join_conds = [];
-               $tables = [ 'revision', 'page', 'user' ];
-               $index = false;
                if ( $this->contribs == 'newbie' ) {
                        $max = $this->mDb->selectField( 'user', 'max(user_id)', false, __METHOD__ );
-                       $condition[] = 'rev_user >' . (int)( $max - $max / 100 );
+                       $queryInfo['conds'][] = 'rev_user >' . (int)( $max - $max / 100 );
                        # ignore local groups with the bot right
                        # @todo FIXME: Global groups may have 'bot' rights
                        $groupsWithBotPermission = User::getGroupsWithPermission( 'bot' );
                        if ( count( $groupsWithBotPermission ) ) {
-                               $tables[] = 'user_groups';
-                               $condition[] = 'ug_group IS NULL';
-                               $join_conds['user_groups'] = [
+                               $queryInfo['tables'][] = 'user_groups';
+                               $queryInfo['conds'][] = 'ug_group IS NULL';
+                               $queryInfo['join_conds']['user_groups'] = [
                                        'LEFT JOIN', [
                                                'ug_user = rev_user',
                                                'ug_group' => $groupsWithBotPermission,
@@ -249,36 +205,76 @@ class ContribsPager extends RangeChronologicalPager {
                        // (T140537) Disallow looking too far in the past for 'newbies' queries. If the user requested
                        // a timestamp offset far in the past such that there are no edits by users with user_ids in
                        // the range, we would end up scanning all revisions from that offset until start of time.
-                       $condition[] = 'rev_timestamp > ' .
+                       $queryInfo['conds'][] = 'rev_timestamp > ' .
                                $this->mDb->addQuotes( $this->mDb->timestamp( wfTimestamp() - 30 * 24 * 60 * 60 ) );
                } else {
                        $uid = User::idFromName( $this->target );
                        if ( $uid ) {
-                               $condition['rev_user'] = $uid;
-                               $index = 'user_timestamp';
+                               $queryInfo['conds']['rev_user'] = $uid;
+                               $queryInfo['options']['USE INDEX']['revision'] = 'user_timestamp';
                        } else {
-                               $condition['rev_user_text'] = $this->target;
-                               $index = 'usertext_timestamp';
+                               $ipRangeConds = $this->getIpRangeConds( $this->mDb, $this->target );
+
+                               if ( $ipRangeConds ) {
+                                       $queryInfo['tables'][] = 'ip_changes';
+                                       $queryInfo['join_conds']['ip_changes'] = [
+                                               'LEFT JOIN', [ 'ipc_rev_id = rev_id' ]
+                                       ];
+                                       $queryInfo['conds'][] = $ipRangeConds;
+                               } else {
+                                       $queryInfo['conds']['rev_user_text'] = $this->target;
+                                       $queryInfo['options']['USE INDEX']['revision'] = 'usertext_timestamp';
+                               }
                        }
                }
 
                if ( $this->deletedOnly ) {
-                       $condition[] = 'rev_deleted != 0';
+                       $queryInfo['conds'][] = 'rev_deleted != 0';
                }
 
                if ( $this->topOnly ) {
-                       $condition[] = 'rev_id = page_latest';
+                       $queryInfo['conds'][] = 'rev_id = page_latest';
                }
 
                if ( $this->newOnly ) {
-                       $condition[] = 'rev_parent_id = 0';
+                       $queryInfo['conds'][] = 'rev_parent_id = 0';
                }
 
                if ( $this->hideMinor ) {
-                       $condition[] = 'rev_minor_edit = 0';
+                       $queryInfo['conds'][] = 'rev_minor_edit = 0';
                }
 
-               return [ $tables, $index, $condition, $join_conds ];
+               $user = $this->getUser();
+               $queryInfo['conds'] = array_merge( $queryInfo['conds'], $this->getNamespaceCond() );
+
+               // Paranoia: avoid brute force searches (T19342)
+               if ( !$user->isAllowed( 'deletedhistory' ) ) {
+                       $queryInfo['conds'][] = $this->mDb->bitAnd( 'rev_deleted', Revision::DELETED_USER ) . ' = 0';
+               } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
+                       $queryInfo['conds'][] = $this->mDb->bitAnd( 'rev_deleted', Revision::SUPPRESSED_USER ) .
+                               ' != ' . Revision::SUPPRESSED_USER;
+               }
+
+               // For IPv6, we use ipc_rev_timestamp on ip_changes as the index field,
+               // which will be referenced when parsing the results of a query.
+               if ( self::isQueryableRange( $this->target ) ) {
+                       $queryInfo['fields'][] = 'ipc_rev_timestamp';
+               }
+
+               ChangeTags::modifyDisplayQuery(
+                       $queryInfo['tables'],
+                       $queryInfo['fields'],
+                       $queryInfo['conds'],
+                       $queryInfo['join_conds'],
+                       $queryInfo['options'],
+                       $this->tagFilter
+               );
+
+               // Avoid PHP 7.1 warning from passing $this by reference
+               $pager = $this;
+               Hooks::run( 'ContribsPager::getQueryInfo', [ &$pager, &$queryInfo ] );
+
+               return $queryInfo;
        }
 
        function getNamespaceCond() {
@@ -305,8 +301,57 @@ class ContribsPager extends RangeChronologicalPager {
                return [];
        }
 
-       function getIndexField() {
-               return 'rev_timestamp';
+       /**
+        * Get SQL conditions for an IP range, if applicable
+        * @param IDatabase      $db
+        * @param string         $ip The IP address or CIDR
+        * @return string|false  SQL for valid IP ranges, false if invalid
+        */
+       private function getIpRangeConds( $db, $ip ) {
+               // First make sure it is a valid range and they are not outside the CIDR limit
+               if ( !$this->isQueryableRange( $ip ) ) {
+                       return false;
+               }
+
+               list( $start, $end ) = IP::parseRange( $ip );
+
+               return 'ipc_hex BETWEEN ' . $db->addQuotes( $start ) . ' AND ' . $db->addQuotes( $end );
+       }
+
+       /**
+        * Is the given IP a range and within the CIDR limit?
+        *
+        * @param string $ipRange
+        * @return bool True if it is valid
+        * @since 1.30
+        */
+       public function isQueryableRange( $ipRange ) {
+               $limits = $this->getConfig()->get( 'RangeContributionsCIDRLimit' );
+
+               $bits = IP::parseCIDR( $ipRange )[1];
+               if (
+                       ( $bits === false ) ||
+                       ( IP::isIPv4( $ipRange ) && $bits < $limits['IPv4'] ) ||
+                       ( IP::isIPv6( $ipRange ) && $bits < $limits['IPv6'] )
+               ) {
+                       return false;
+               }
+
+               return true;
+       }
+
+       /**
+        * Override of getIndexField() in IndexPager.
+        * For IP ranges, it's faster to use the replicated ipc_rev_timestamp
+        * on the `ip_changes` table than the rev_timestamp on the `revision` table.
+        * @return string Name of field
+        */
+       public function getIndexField() {
+               if ( $this->isQueryableRange( $this->target ) ) {
+                       return 'ipc_rev_timestamp';
+               } else {
+                       return 'rev_timestamp';
+               }
        }
 
        function doBatchLookups() {
@@ -315,6 +360,7 @@ class ContribsPager extends RangeChronologicalPager {
                $parentRevIds = [];
                $this->mParentLens = [];
                $batch = new LinkBatch();
+               $isIpRange = $this->isQueryableRange( $this->target );
                # Give some pointers to make (last) links
                foreach ( $this->mResult as $row ) {
                        if ( isset( $row->rev_parent_id ) && $row->rev_parent_id ) {
@@ -325,6 +371,9 @@ class ContribsPager extends RangeChronologicalPager {
                                if ( $this->contribs === 'newbie' ) { // multiple users
                                        $batch->add( NS_USER, $row->user_name );
                                        $batch->add( NS_USER_TALK, $row->user_name );
+                               } elseif ( $isIpRange ) {
+                                       // If this is an IP range, batch the IP's talk page
+                                       $batch->add( NS_USER_TALK, $row->rev_user_text );
                                }
                                $batch->add( $row->page_namespace, $row->page_title );
                        }
@@ -400,6 +449,7 @@ class ContribsPager extends RangeChronologicalPager {
                        # Mark current revisions
                        $topmarktext = '';
                        $user = $this->getUser();
+
                        if ( $row->rev_id === $row->page_latest ) {
                                $topmarktext .= '<span class="mw-uctop">' . $this->messages['uctop'] . '</span>';
                                $classes[] = 'mw-contributions-current';
@@ -473,8 +523,10 @@ class ContribsPager extends RangeChronologicalPager {
 
                        # Show user names for /newbies as there may be different users.
                        # Note that only unprivileged users have rows with hidden user names excluded.
+                       # When querying for an IP range, we want to always show user and user talk links.
                        $userlink = '';
-                       if ( $this->contribs == 'newbie' && !$rev->isDeleted( Revision::DELETED_USER ) ) {
+                       if ( ( $this->contribs == 'newbie' && !$rev->isDeleted( Revision::DELETED_USER ) )
+                               || $this->isQueryableRange( $this->target ) ) {
                                $userlink = ' . . ' . $lang->getDirMark()
                                        . Linker::userLink( $rev->getUser(), $rev->getUserText() );
                                $userlink .= ' ' . $this->msg( 'parentheses' )->rawParams(