Add 'logid' parameter to Special:Log
[lhc/web/wiklou.git] / includes / logging / LogPager.php
index df432e1..c047e96 100644 (file)
@@ -45,6 +45,12 @@ class LogPager extends ReverseChronologicalPager {
        /** @var string */
        private $action = '';
 
+       /** @var bool */
+       private $performerRestrictionsEnforced = false;
+
+       /** @var bool */
+       private $actionRestrictionsEnforced = false;
+
        /** @var LogEventsList */
        public $mLogEventsList;
 
@@ -59,10 +65,11 @@ class LogPager extends ReverseChronologicalPager {
         * @param int|bool $month The month to start from. Default: false
         * @param string $tagFilter Tag
         * @param string $action Specific action (subtype) requested
+        * @param int $logId Log entry ID, to limit to a single log entry.
         */
        public function __construct( $list, $types = [], $performer = '', $title = '',
                $pattern = '', $conds = [], $year = false, $month = false, $tagFilter = '',
-               $action = ''
+               $action = '', $logId = false
        ) {
                parent::__construct( $list->getContext() );
                $this->mConds = $conds;
@@ -75,6 +82,7 @@ class LogPager extends ReverseChronologicalPager {
                $this->limitAction( $action );
                $this->getDateCond( $year, $month );
                $this->mTagFilter = $tagFilter;
+               $this->limitLogId( $logId );
 
                $this->mDb = wfGetDB( DB_REPLICA, 'logpager' );
        }
@@ -170,21 +178,13 @@ class LogPager extends ReverseChronologicalPager {
                // Normalize username first so that non-existent users used
                // in maintenance scripts work
                $name = $usertitle->getText();
-               /* Fetch userid at first, if known, provides awesome query plan afterwards */
-               $userid = User::idFromName( $name );
-               if ( !$userid ) {
-                       $this->mConds['log_user_text'] = IP::sanitizeIP( $name );
-               } else {
-                       $this->mConds['log_user'] = $userid;
-               }
-               // Paranoia: avoid brute force searches (T19342)
-               $user = $this->getUser();
-               if ( !$user->isAllowed( 'deletedhistory' ) ) {
-                       $this->mConds[] = $this->mDb->bitAnd( 'log_deleted', LogPage::DELETED_USER ) . ' = 0';
-               } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
-                       $this->mConds[] = $this->mDb->bitAnd( 'log_deleted', LogPage::SUPPRESSED_USER ) .
-                               ' != ' . LogPage::SUPPRESSED_USER;
-               }
+
+               // Assume no joins required for log_user
+               $this->mConds[] = ActorMigration::newMigration()->getWhere(
+                       wfGetDB( DB_REPLICA ), 'log_user', User::newFromName( $name, false )
+               )['conds'];
+
+               $this->enforcePerformerRestrictions();
 
                $this->performer = $name;
        }
@@ -252,14 +252,7 @@ class LogPager extends ReverseChronologicalPager {
                } else {
                        $this->mConds['log_title'] = $title->getDBkey();
                }
-               // Paranoia: avoid brute force searches (T19342)
-               $user = $this->getUser();
-               if ( !$user->isAllowed( 'deletedhistory' ) ) {
-                       $this->mConds[] = $db->bitAnd( 'log_deleted', LogPage::DELETED_ACTION ) . ' = 0';
-               } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
-                       $this->mConds[] = $db->bitAnd( 'log_deleted', LogPage::SUPPRESSED_ACTION ) .
-                               ' != ' . LogPage::SUPPRESSED_ACTION;
-               }
+               $this->enforceActionRestrictions();
        }
 
        /**
@@ -287,6 +280,17 @@ class LogPager extends ReverseChronologicalPager {
                }
        }
 
+       /**
+        * Limit to the (single) specified log ID.
+        * @param int $logId The log entry ID.
+        */
+       protected function limitLogId( $logId ) {
+               if ( !$logId ) {
+                       return;
+               }
+               $this->mConds['log_id'] = $logId;
+       }
+
        /**
         * Constructs the most part of the query. Extra conditions are sprinkled in
         * all over this class.
@@ -420,4 +424,39 @@ class LogPager extends ReverseChronologicalPager {
                parent::doQuery();
                $this->mDb->setBigSelects( 'default' );
        }
+
+       /**
+        * Paranoia: avoid brute force searches (T19342)
+        */
+       private function enforceActionRestrictions() {
+               if ( $this->actionRestrictionsEnforced ) {
+                       return;
+               }
+               $this->actionRestrictionsEnforced = true;
+               $user = $this->getUser();
+               if ( !$user->isAllowed( 'deletedhistory' ) ) {
+                       $this->mConds[] = $this->mDb->bitAnd( 'log_deleted', LogPage::DELETED_ACTION ) . ' = 0';
+               } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
+                       $this->mConds[] = $this->mDb->bitAnd( 'log_deleted', LogPage::SUPPRESSED_ACTION ) .
+                               ' != ' . LogPage::SUPPRESSED_USER;
+               }
+       }
+
+       /**
+        * Paranoia: avoid brute force searches (T19342)
+        */
+       private function enforcePerformerRestrictions() {
+               // Same as enforceActionRestrictions(), except for _USER instead of _ACTION bits.
+               if ( $this->performerRestrictionsEnforced ) {
+                       return;
+               }
+               $this->performerRestrictionsEnforced = true;
+               $user = $this->getUser();
+               if ( !$user->isAllowed( 'deletedhistory' ) ) {
+                       $this->mConds[] = $this->mDb->bitAnd( 'log_deleted', LogPage::DELETED_USER ) . ' = 0';
+               } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
+                       $this->mConds[] = $this->mDb->bitAnd( 'log_deleted', LogPage::SUPPRESSED_USER ) .
+                               ' != ' . LogPage::SUPPRESSED_ACTION;
+               }
+       }
 }