Merge "copy paste comment should probably have end instead of start"
[lhc/web/wiklou.git] / includes / Block.php
index 3f00a51..34b89e7 100644 (file)
@@ -65,11 +65,11 @@ class Block {
                $timestamp = 0, $auto = 0, $expiry = '', $anonOnly = 0, $createAccount = 0, $enableAutoblock = 0,
                $hideName = 0, $blockEmail = 0, $allowUsertalk = 0, $byText = '' )
        {
-               if( $timestamp === 0 ) {
+               if ( $timestamp === 0 ) {
                        $timestamp = wfTimestampNow();
                }
 
-               if( count( func_get_args() ) > 0 ) {
+               if ( count( func_get_args() ) > 0 ) {
                        # Soon... :D
                        # wfDeprecated( __METHOD__ . " with arguments" );
                }
@@ -206,16 +206,16 @@ class Block {
         */
        public function load( $address = '', $user = 0 ) {
                wfDeprecated( __METHOD__, '1.18' );
-               if( $user ) {
+               if ( $user ) {
                        $username = User::whoIs( $user );
                        $block = self::newFromTarget( $username, $address );
                } else {
                        $block = self::newFromTarget( null, $address );
                }
 
-               if( $block instanceof Block ) {
+               if ( $block instanceof Block ) {
                        # This is mildly evil, but hey, it's B/C :D
-                       foreach( $block as $variable => $value ) {
+                       foreach ( $block as $variable => $value ) {
                                $this->$variable = $value;
                        }
                        return true;
@@ -237,7 +237,7 @@ class Block {
        protected function newLoad( $vagueTarget = null ) {
                $db = wfGetDB( $this->mFromMaster ? DB_MASTER : DB_SLAVE );
 
-               if( $this->type !== null ) {
+               if ( $this->type !== null ) {
                        $conds = array(
                                'ipb_address' => array( (string)$this->target ),
                        );
@@ -247,9 +247,9 @@ class Block {
 
                # Be aware that the != '' check is explicit, since empty values will be
                # passed by some callers (bug 29116)
-               if( $vagueTarget != '' ) {
+               if ( $vagueTarget != '' ) {
                        list( $target, $type ) = self::parseTarget( $vagueTarget );
-                       switch( $type ) {
+                       switch ( $type ) {
                                case self::TYPE_USER:
                                        # Slightly weird, but who are we to argue?
                                        $conds['ipb_address'][] = (string)$target;
@@ -285,20 +285,20 @@ class Block {
                # This is begging for $this = $bestBlock, but that's not allowed in PHP :(
                $bestBlockPreventsEdit = null;
 
-               foreach( $res as $row ) {
+               foreach ( $res as $row ) {
                        $block = self::newFromRow( $row );
 
                        # Don't use expired blocks
-                       if( $block->deleteIfExpired() ) {
+                       if ( $block->deleteIfExpired() ) {
                                continue;
                        }
 
                        # Don't use anon only blocks on users
-                       if( $this->type == self::TYPE_USER && !$block->isHardblock() ) {
+                       if ( $this->type == self::TYPE_USER && !$block->isHardblock() ) {
                                continue;
                        }
 
-                       if( $block->getType() == self::TYPE_RANGE ) {
+                       if ( $block->getType() == self::TYPE_RANGE ) {
                                # This is the number of bits that are allowed to vary in the block, give
                                # or take some floating point errors
                                $end = wfBaseconvert( $block->getRangeEnd(), 16, 10 );
@@ -313,14 +313,14 @@ class Block {
                                $score = $block->getType();
                        }
 
-                       if( $score < $bestBlockScore ) {
+                       if ( $score < $bestBlockScore ) {
                                $bestBlockScore = $score;
                                $bestRow = $row;
                                $bestBlockPreventsEdit = $block->prevents( 'edit' );
                        }
                }
 
-               if( $bestRow !== null ) {
+               if ( $bestRow !== null ) {
                        $this->initFromRow( $bestRow );
                        $this->prevents( 'edit', $bestBlockPreventsEdit );
                        return true;
@@ -511,7 +511,7 @@ class Block {
         * @return Array
         */
        protected function getDatabaseArray( $db = null ) {
-               if( !$db ) {
+               if ( !$db ) {
                        $db = wfGetDB( DB_SLAVE );
                }
                $expiry = $db->encodeExpiry( $this->mExpiry );
@@ -579,7 +579,7 @@ class Block {
                global $wgPutIPinRC;
 
                // No IPs are in recentchanges table, so nothing to select
-               if( !$wgPutIPinRC ) {
+               if ( !$wgPutIPinRC ) {
                        return;
                }
 
@@ -601,7 +601,9 @@ class Block {
                        foreach ( $res as $row ) {
                                if ( $row->rc_ip ) {
                                        $id = $block->doAutoblock( $row->rc_ip );
-                                       if ( $id ) $blockIds[] = $id;
+                                       if ( $id ) {
+                                               $blockIds[] = $id;
+                                       }
                                }
                        }
                }
@@ -681,7 +683,7 @@ class Block {
                if ( $ipblock ) {
                        # Check if the block is an autoblock and would exceed the user block
                        # if renewed. If so, do nothing, otherwise prolong the block time...
-                       if ( $ipblock->mAuto && // @TODO: why not compare $ipblock->mExpiry?
+                       if ( $ipblock->mAuto && // @todo Why not compare $ipblock->mExpiry?
                                $this->mExpiry > Block::getAutoblockExpiry( $ipblock->mTimestamp )
                        ) {
                                # Reset block timestamp to now and its expiry to
@@ -793,7 +795,7 @@ class Block {
         * @return String IP in Hex form
         */
        public function getRangeStart() {
-               switch( $this->type ) {
+               switch ( $this->type ) {
                        case self::TYPE_USER:
                                return '';
                        case self::TYPE_IP:
@@ -807,12 +809,12 @@ class Block {
        }
 
        /**
-        * Get the IP address at the start of the range in Hex form
+        * Get the IP address at the end of the range in Hex form
         * @throws MWException
         * @return String IP in Hex form
         */
        public function getRangeEnd() {
-               switch( $this->type ) {
+               switch ( $this->type ) {
                        case self::TYPE_USER:
                                return '';
                        case self::TYPE_IP:
@@ -909,7 +911,7 @@ class Block {
         * @return Bool
         */
        public function prevents( $action, $x = null ) {
-               switch( $action ) {
+               switch ( $action ) {
                        case 'edit':
                                # For now... <evil laugh>
                                return true;
@@ -1057,24 +1059,24 @@ class Block {
        public static function newFromTarget( $specificTarget, $vagueTarget = null, $fromMaster = false ) {
 
                list( $target, $type ) = self::parseTarget( $specificTarget );
-               if( $type == Block::TYPE_ID || $type == Block::TYPE_AUTO ) {
+               if ( $type == Block::TYPE_ID || $type == Block::TYPE_AUTO ) {
                        return Block::newFromID( $target );
 
-               } elseif( $target === null && $vagueTarget == '' ) {
+               } elseif ( $target === null && $vagueTarget == '' ) {
                        # We're not going to find anything useful here
                        # Be aware that the == '' check is explicit, since empty values will be
                        # passed by some callers (bug 29116)
                        return null;
 
-               } elseif( in_array( $type, array( Block::TYPE_USER, Block::TYPE_IP, Block::TYPE_RANGE, null ) ) ) {
+               } elseif ( in_array( $type, array( Block::TYPE_USER, Block::TYPE_IP, Block::TYPE_RANGE, null ) ) ) {
                        $block = new Block();
                        $block->fromMaster( $fromMaster );
 
-                       if( $type !== null ) {
+                       if ( $type !== null ) {
                                $block->setTarget( $target );
                        }
 
-                       if( $block->newLoad( $vagueTarget ) ) {
+                       if ( $block->newLoad( $vagueTarget ) ) {
                                return $block;
                        }
                }
@@ -1090,7 +1092,7 @@ class Block {
         * @param Bool $isAnon Exclude anonymous-only blocks if false
         * @param Bool $fromMaster Whether to query the master or slave database
         * @return Array of Blocks
-        * @since 1.21
+        * @since 1.22
         */
        public static function getBlocksForIPList( array $ipChain, $isAnon, $fromMaster = false ) {
                if ( !count( $ipChain ) ) {
@@ -1171,7 +1173,7 @@ class Block {
         * @param Array $block Array of blocks
         * @return Block|null the "best" block from the list
         */
-       public static function chooseBlock( array $blocks, array $ipChain  ) {
+       public static function chooseBlock( array $blocks, array $ipChain ) {
                if ( !count( $blocks ) ) {
                        return null;
                } elseif ( count( $blocks ) == 1 ) {
@@ -1278,13 +1280,13 @@ class Block {
         */
        public static function parseTarget( $target ) {
                # We may have been through this before
-               if( $target instanceof User ) {
-                       if( IP::isValid( $target->getName() ) ) {
+               if ( $target instanceof User ) {
+                       if ( IP::isValid( $target->getName() ) ) {
                                return array( $target, self::TYPE_IP );
                        } else {
                                return array( $target, self::TYPE_USER );
                        }
-               } elseif( $target === null ) {
+               } elseif ( $target === null ) {
                        return array( null, null );
                }
 
@@ -1305,7 +1307,7 @@ class Block {
 
                # Consider the possibility that this is not a username at all
                # but actually an old subpage (bug #29797)
-               if( strpos( $target, '/' ) !== false ) {
+               if ( strpos( $target, '/' ) !== false ) {
                        # An old subpage, drill down to the user behind it
                        $parts = explode( '/', $target );
                        $target = $parts[0];
@@ -1391,4 +1393,43 @@ class Block {
        public function setBlocker( $user ) {
                $this->blocker = $user;
        }
+
+       /**
+        * Get the key and parameters for the corresponding error message.
+        *
+        * @since 1.22
+        * @param IContextSource $context
+        * @return array
+        */
+       public function getPermissionsError( IContextSource $context ) {
+               $blocker = $this->getBlocker();
+               if ( $blocker instanceof User ) { // local user
+                       $blockerUserpage = $blocker->getUserPage();
+                       $link = "[[{$blockerUserpage->getPrefixedText()}|{$blockerUserpage->getText()}]]";
+               } else { // foreign user
+                       $link = $blocker;
+               }
+
+               $reason = $this->mReason;
+               if ( $reason == '' ) {
+                       $reason = $context->msg( 'blockednoreason' )->text();
+               }
+
+               /* $ip returns who *is* being blocked, $intended contains who was meant to be blocked.
+                * This could be a username, an IP range, or a single IP. */
+               $intended = $this->getTarget();
+
+               $lang = $context->getLanguage();
+               return array(
+                       $this->mAuto ? 'autoblockedtext' : 'blockedtext',
+                       $link,
+                       $reason,
+                       $context->getRequest()->getIP(),
+                       $this->getByName(),
+                       $this->getId(),
+                       $lang->formatExpiry( $this->mExpiry ),
+                       (string)$intended,
+                       $lang->timeanddate( wfTimestamp( TS_MW, $this->mTimestamp ), true ),
+               );
+       }
 }