Merge "Complete coverage of Parser::getTemplateDom()"
[lhc/web/wiklou.git] / includes / user / User.php
index b8186d6..b5fa97f 100644 (file)
@@ -414,7 +414,8 @@ class User implements IDBAccessObject, UserIdentity {
                                break;
                        case 'actor':
                                // Make sure this thread sees its own changes
-                               if ( wfGetLB()->hasOrMadeRecentMasterChanges() ) {
+                               $lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
+                               if ( $lb->hasOrMadeRecentMasterChanges() ) {
                                        $flags |= self::READ_LATEST;
                                        $this->queryFlagsUsed = $flags;
                                }
@@ -1344,32 +1345,54 @@ class User implements IDBAccessObject, UserIdentity {
                $user = $session->getUser();
                if ( $user->isLoggedIn() ) {
                        $this->loadFromUserObject( $user );
-
-                       // If this user is autoblocked, set a cookie to track the Block. This has to be done on
-                       // every session load, because an autoblocked editor might not edit again from the same
-                       // IP address after being blocked.
-                       $config = RequestContext::getMain()->getConfig();
-                       if ( $config->get( 'CookieSetOnAutoblock' ) === true ) {
-                               $block = $this->getBlock();
-                               $shouldSetCookie = $this->getRequest()->getCookie( 'BlockID' ) === null
-                                       && $block
-                                       && $block->getType() === Block::TYPE_USER
-                                       && $block->isAutoblocking();
-                               if ( $shouldSetCookie ) {
-                                       wfDebug( __METHOD__ . ': User is autoblocked, setting cookie to track' );
-                                       $block->setCookie( $this->getRequest()->response() );
-                               }
+                       if ( $user->isBlocked() ) {
+                               // If this user is autoblocked, set a cookie to track the Block. This has to be done on
+                               // every session load, because an autoblocked editor might not edit again from the same
+                               // IP address after being blocked.
+                               $this->trackBlockWithCookie();
                        }
 
                        // Other code expects these to be set in the session, so set them.
                        $session->set( 'wsUserID', $this->getId() );
                        $session->set( 'wsUserName', $this->getName() );
                        $session->set( 'wsToken', $this->getToken() );
+
                        return true;
                }
+
                return false;
        }
 
+       /**
+        * Set the 'BlockID' cookie depending on block type and user authentication status.
+        */
+       public function trackBlockWithCookie() {
+               $block = $this->getBlock();
+               if ( $block && $this->getRequest()->getCookie( 'BlockID' ) === null ) {
+                       $config = RequestContext::getMain()->getConfig();
+                       $shouldSetCookie = false;
+
+                       if ( $this->isAnon() && $config->get( 'CookieSetOnIpBlock' ) ) {
+                               // If user is logged-out, set a cookie to track the Block
+                               $shouldSetCookie = in_array( $block->getType(), [
+                                       Block::TYPE_IP, Block::TYPE_RANGE
+                               ] );
+                               if ( $shouldSetCookie ) {
+                                       $block->setCookie( $this->getRequest()->response() );
+
+                                       // temporary measure the use of cookies on ip blocks
+                                       $stats = MediaWikiServices::getInstance()->getStatsdDataFactory();
+                                       $stats->increment( 'block.ipblock.setCookie.success' );
+                               }
+                       } elseif ( $this->isLoggedIn() && $config->get( 'CookieSetOnAutoblock' ) ) {
+                               $shouldSetCookie = $block->getType() === Block::TYPE_USER && $block->isAutoblocking();
+                               if ( $shouldSetCookie ) {
+                                       $block->setCookie( $this->getRequest()->response() );
+                               }
+                       }
+               }
+       }
+
        /**
         * Load user and user_group data from the database.
         * $this->mId must be set, this is how the user is identified.
@@ -1812,14 +1835,14 @@ class User implements IDBAccessObject, UserIdentity {
                        if ( self::isLocallyBlockedProxy( $ip ) ) {
                                $block = new Block( [
                                        'byText' => wfMessage( 'proxyblocker' )->text(),
-                                       'reason' => wfMessage( 'proxyblockreason' )->text(),
+                                       'reason' => wfMessage( 'proxyblockreason' )->plain(),
                                        'address' => $ip,
                                        'systemBlock' => 'proxy',
                                ] );
                        } elseif ( $this->isAnon() && $this->isDnsBlacklisted( $ip ) ) {
                                $block = new Block( [
                                        'byText' => wfMessage( 'sorbs' )->text(),
-                                       'reason' => wfMessage( 'sorbsreason' )->text(),
+                                       'reason' => wfMessage( 'sorbsreason' )->plain(),
                                        'address' => $ip,
                                        'systemBlock' => 'dnsbl',
                                ] );
@@ -1840,7 +1863,7 @@ class User implements IDBAccessObject, UserIdentity {
                        if ( $block instanceof Block ) {
                                # Mangle the reason to alert the user that the block
                                # originated from matching the X-Forwarded-For header.
-                               $block->mReason = wfMessage( 'xffblockreason', $block->mReason )->text();
+                               $block->mReason = wfMessage( 'xffblockreason', $block->mReason )->plain();
                        }
                }
 
@@ -1852,7 +1875,7 @@ class User implements IDBAccessObject, UserIdentity {
                        $block = new Block( [
                                'address' => $ip,
                                'byText' => 'MediaWiki default',
-                               'reason' => wfMessage( 'softblockrangesreason', $ip )->text(),
+                               'reason' => wfMessage( 'softblockrangesreason', $ip )->plain(),
                                'anonOnly' => true,
                                'systemBlock' => 'wgSoftBlockRanges',
                        ] );
@@ -1895,12 +1918,24 @@ class User implements IDBAccessObject, UserIdentity {
                        // An ID was found in the cookie.
                        $tmpBlock = Block::newFromID( $blockCookieId );
                        if ( $tmpBlock instanceof Block ) {
-                               // Check the validity of the block.
-                               $blockIsValid = $tmpBlock->getType() == Block::TYPE_USER
-                                       && !$tmpBlock->isExpired()
-                                       && $tmpBlock->isAutoblocking();
                                $config = RequestContext::getMain()->getConfig();
-                               $useBlockCookie = ( $config->get( 'CookieSetOnAutoblock' ) === true );
+
+                               switch ( $tmpBlock->getType() ) {
+                                       case Block::TYPE_USER:
+                                               $blockIsValid = !$tmpBlock->isExpired() && $tmpBlock->isAutoblocking();
+                                               $useBlockCookie = ( $config->get( 'CookieSetOnAutoblock' ) === true );
+                                               break;
+                                       case Block::TYPE_IP:
+                                       case Block::TYPE_RANGE:
+                                               // If block is type IP or IP range, load only if user is not logged in (T152462)
+                                               $blockIsValid = !$tmpBlock->isExpired() && !$this->isLoggedIn();
+                                               $useBlockCookie = ( $config->get( 'CookieSetOnIpBlock' ) === true );
+                                               break;
+                                       default:
+                                               $blockIsValid = false;
+                                               $useBlockCookie = false;
+                               }
+
                                if ( $blockIsValid && $useBlockCookie ) {
                                        // Use the block.
                                        return $tmpBlock;
@@ -5662,11 +5697,12 @@ class User implements IDBAccessObject, UserIdentity {
        /**
         * Checks if two user objects point to the same user.
         *
-        * @since 1.25
-        * @param User $user
+        * @since 1.25 ; takes a UserIdentity instead of a User since 1.32
+        * @param UserIdentity $user
         * @return bool
         */
-       public function equals( User $user ) {
+       public function equals( UserIdentity $user ) {
+               // XXX it's not clear whether central ID providers are supposed to obey this
                return $this->getName() === $user->getName();
        }
 }