saper has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/92179


Change subject: Allow robotic Users to determine their IP address
......................................................................

Allow robotic Users to determine their IP address

If some robotic user is making a change (for
example, AbuseFilter user), it is not always
correct to use IP address coming from the request.

Allow instances of User class to provide their
own way to determine IP address for the recent
changes logging.

Bug: 42345
Change-Id: I272db0c07808cd20e07f4d017220817f6d878209
---
M includes/User.php
M includes/changes/RecentChange.php
2 files changed, 31 insertions(+), 16 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/79/92179/1

diff --git a/includes/User.php b/includes/User.php
index 12912e1..818d9e8 100644
--- a/includes/User.php
+++ b/includes/User.php
@@ -1297,7 +1297,7 @@
                # user is not immune to autoblocks/hardblocks, and they are the 
current user so we
                # know which IP address they're actually coming from
                if ( !$this->isAllowed( 'ipblock-exempt' ) && $this->getID() == 
$wgUser->getID() ) {
-                       $ip = $this->getRequest()->getIP();
+                       $ip = $this->getUserIP();
                } else {
                        $ip = null;
                }
@@ -1472,7 +1472,7 @@
         */
        public function isPingLimitable() {
                global $wgRateLimitsExcludedIPs;
-               if ( in_array( $this->getRequest()->getIP(), 
$wgRateLimitsExcludedIPs ) ) {
+               if ( in_array( $this->getUserIP(), $wgRateLimitsExcludedIPs ) ) 
{
                        // No other good way currently to disable rate limits
                        // for specific IPs. :P
                        // But this is a crappy hack and should die.
@@ -1529,11 +1529,11 @@
                                $keys[wfMemcKey( 'limiter', $action, 'user', 
$id )] = $limits['newbie'];
                        }
                        if ( isset( $limits['ip'] ) ) {
-                               $ip = $this->getRequest()->getIP();
+                               $ip = $this->getUserIP();
                                $keys["mediawiki:limiter:$action:ip:$ip"] = 
$limits['ip'];
                        }
                        if ( isset( $limits['subnet'] ) ) {
-                               $ip = $this->getRequest()->getIP();
+                               $ip = $this->getUserIP();
                                $matches = array();
                                $subnet = false;
                                if ( IP::isIPv6( $ip ) ) {
@@ -1687,7 +1687,7 @@
                if ( IP::isIPAddress( $this->getName() ) ) {
                        $ip = $this->getName();
                } elseif ( !$ip ) {
-                       $ip = $this->getRequest()->getIP();
+                       $ip = $this->getUserIP();
                }
                $blocked = false;
                wfRunHooks( 'UserIsBlockedGlobally', array( &$this, $ip, 
&$blocked ) );
@@ -1766,7 +1766,7 @@
                        $this->load();
                        if ( $this->mName === false ) {
                                // Clean up IPs
-                               $this->mName = IP::sanitizeIP( 
$this->getRequest()->getIP() );
+                               $this->mName = IP::sanitizeIP( 
$this->getUserIP() );
                        }
                        return $this->mName;
                }
@@ -2620,7 +2620,7 @@
                        $https = $this->getBoolOption( 'prefershttps' );
                        wfRunHooks( 'UserRequiresHTTPS', array( $this, &$https 
) );
                        if ( $https ) {
-                               $https = wfCanIPUseHTTPS( 
$this->getRequest()->getIP() );
+                               $https = wfCanIPUseHTTPS( $this->getUserIP() );
                        }
                        return $https;
                }
@@ -3491,7 +3491,7 @@
                        return false;
                }
 
-               return (bool)$userblock->doAutoblock( 
$this->getRequest()->getIP() );
+               return (bool)$userblock->doAutoblock( $this->getUserIP() );
        }
 
        /**
@@ -3558,7 +3558,7 @@
                # blocked with createaccount disabled, prevent new account 
creation there even
                # when the user is logged in
                if ( $this->mBlockedFromCreateAccount === false && 
!$this->isAllowed( 'ipblock-exempt' ) ) {
-                       $this->mBlockedFromCreateAccount = 
Block::newFromTarget( null, $this->getRequest()->getIP() );
+                       $this->mBlockedFromCreateAccount = 
Block::newFromTarget( null, $this->getUserIP() );
                }
                return $this->mBlockedFromCreateAccount instanceof Block && 
$this->mBlockedFromCreateAccount->prevents( 'createaccount' )
                        ? $this->mBlockedFromCreateAccount
@@ -3789,7 +3789,7 @@
 
                return $this->sendMail( wfMessage( 'confirmemail_subject' 
)->text(),
                        wfMessage( $message,
-                               $this->getRequest()->getIP(),
+                               $this->getUserIP(),
                                $this->getName(),
                                $url,
                                $wgLang->timeanddate( $expiration, false ),
@@ -4795,4 +4795,18 @@
                        return Status::newFatal( 'badaccess-group0' );
                }
        }
+
+       /** 
+        * Allow user objects to override which IP address gets used as the 
users' IP
+        *
+        * @since 1.22
+        * @param WebRequest
+        * @return string
+        */
+       public function getUserIP( $request = null ) {
+               if( is_null( $request ) ) {
+                       $request = $this->getRequest();
+               }
+               return $request->getIP();
+       }
 }
diff --git a/includes/changes/RecentChange.php 
b/includes/changes/RecentChange.php
index 980bd0a..b109a98 100644
--- a/includes/changes/RecentChange.php
+++ b/includes/changes/RecentChange.php
@@ -480,6 +480,7 @@
         */
        public static function notifyEdit( $timestamp, &$title, $minor, &$user, 
$comment, $oldId,
                $lastTimestamp, $bot, $ip = '', $oldSize = 0, $newSize = 0, 
$newId = 0, $patrol = 0 ) {
+               global $wgRequest;
                $rc = new RecentChange;
                $rc->mTitle = $title;
                $rc->mPerformer = $user;
@@ -497,7 +498,7 @@
                        'rc_this_oldid' => $newId,
                        'rc_last_oldid' => $oldId,
                        'rc_bot'        => $bot ? 1 : 0,
-                       'rc_ip'         => self::checkIPAddress( $ip ),
+                       'rc_ip'         => self::checkIPAddress( $ip, 
$wgRequest, $user ),
                        'rc_patrolled'  => intval( $patrol ),
                        'rc_new'        => 0,  # obsolete
                        'rc_old_len'    => $oldSize,
@@ -539,6 +540,7 @@
         */
        public static function notifyNew( $timestamp, &$title, $minor, &$user, 
$comment, $bot,
                $ip = '', $size = 0, $newId = 0, $patrol = 0 ) {
+               global $wgRequest;
                $rc = new RecentChange;
                $rc->mTitle = $title;
                $rc->mPerformer = $user;
@@ -556,7 +558,7 @@
                        'rc_this_oldid'     => $newId,
                        'rc_last_oldid'     => 0,
                        'rc_bot'            => $bot ? 1 : 0,
-                       'rc_ip'             => self::checkIPAddress( $ip ),
+                       'rc_ip'             => self::checkIPAddress( $ip, 
$wgRequest, $user ),
                        'rc_patrolled'      => intval( $patrol ),
                        'rc_new'            => 1, # obsolete
                        'rc_old_len'        => 0,
@@ -665,7 +667,7 @@
                        'rc_this_oldid' => 0,
                        'rc_last_oldid' => 0,
                        'rc_bot'        => $user->isAllowed( 'bot' ) ? 
$wgRequest->getBool( 'bot', true ) : 0,
-                       'rc_ip'         => self::checkIPAddress( $ip ),
+                       'rc_ip'         => self::checkIPAddress( $ip, 
$wgRequest, $user ),
                        'rc_patrolled'  => 1,
                        'rc_new'        => 0, # obsolete
                        'rc_old_len'    => null,
@@ -815,14 +817,13 @@
                } );
        }
 
-       private static function checkIPAddress( $ip ) {
-               global $wgRequest;
+       private static function checkIPAddress( $ip, $request, $user) {
                if ( $ip ) {
                        if ( !IP::isIPAddress( $ip ) ) {
                                throw new MWException( "Attempt to write \"" . 
$ip . "\" as an IP address into recent changes" );
                        }
                } else {
-                       $ip = $wgRequest->getIP();
+                       $ip = $user->getUserIP( $request );
                        if ( !$ip ) {
                                $ip = '';
                        }

-- 
To view, visit https://gerrit.wikimedia.org/r/92179
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I272db0c07808cd20e07f4d017220817f6d878209
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: saper <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to