(bug 34289) user.options CSS loaded twice. Fixed by splitting off the CSS part of...
[lhc/web/wiklou.git] / includes / Block.php
index 2505c69..ded9cf9 100644 (file)
@@ -20,8 +20,8 @@
  * @file
  */
 class Block {
-       /* public*/ var $mReason, $mTimestamp, $mAuto, $mExpiry, $mHideName, $mAngryAutoblock;
-       
+       /* public*/ var $mReason, $mTimestamp, $mAuto, $mExpiry, $mHideName;
+
        protected
                $mId,
                $mFromMaster,
@@ -54,12 +54,12 @@ class Block {
 
        /**
         * Constructor
-        * FIXME: Don't know what the best format to have for this constructor is, but fourteen
+        * @todo FIXME: Don't know what the best format to have for this constructor is, but fourteen
         * optional parameters certainly isn't it.
         */
        function __construct( $address = '', $user = 0, $by = 0, $reason = '',
                $timestamp = 0, $auto = 0, $expiry = '', $anonOnly = 0, $createAccount = 0, $enableAutoblock = 0,
-               $hideName = 0, $blockEmail = 0, $allowUsertalk = 0 )
+               $hideName = 0, $blockEmail = 0, $allowUsertalk = 0, $byText = '' )
        {
                if( $timestamp === 0 ){
                        $timestamp = wfTimestampNow();
@@ -71,20 +71,27 @@ class Block {
                }
 
                $this->setTarget( $address );
-               $this->setBlocker( User::newFromID( $by ) );
+               if ( $by ) { // local user
+                       $this->setBlocker( User::newFromID( $by ) );
+               } else { // foreign user
+                       $this->setBlocker( $byText );
+               }
                $this->mReason = $reason;
                $this->mTimestamp = wfTimestamp( TS_MW, $timestamp );
                $this->mAuto = $auto;
                $this->isHardblock( !$anonOnly );
                $this->prevents( 'createaccount', $createAccount );
-               $this->mExpiry = $expiry;
+               if ( $expiry == 'infinity' || $expiry == wfGetDB( DB_SLAVE )->getInfinity() ) {
+                       $this->mExpiry = 'infinity';
+               } else {
+                       $this->mExpiry = wfTimestamp( TS_MW, $expiry );
+               }
                $this->isAutoblocking( $enableAutoblock );
                $this->mHideName = $hideName;
                $this->prevents( 'sendemail', $blockEmail );
                $this->prevents( 'editownusertalk', !$allowUsertalk );
 
                $this->mFromMaster = false;
-               $this->mAngryAutoblock = false;
        }
 
        /**
@@ -94,11 +101,11 @@ class Block {
         *
         * @param $address String: IP address of user/anon
         * @param $user Integer: user id of user
-        * @param $killExpired Boolean: delete expired blocks on load
         * @return Block Object
         * @deprecated since 1.18
         */
        public static function newFromDB( $address, $user = 0 ) {
+               wfDeprecated( __METHOD__, '1.18' );
                return self::newFromTarget( User::whoIs( $user ), $address );
        }
 
@@ -106,7 +113,7 @@ class Block {
         * Load a blocked user from their block id.
         *
         * @param $id Integer: Block id to search for
-        * @return Block object
+        * @return Block object or null
         */
        public static function newFromID( $id ) {
                $dbr = wfGetDB( DB_SLAVE );
@@ -116,13 +123,20 @@ class Block {
                        array( 'ipb_id' => $id ),
                        __METHOD__
                );
-               return Block::newFromRow( $res );
+               if ( $res ) {
+                       return Block::newFromRow( $res );
+               } else {
+                       return null;
+               }
        }
 
        /**
         * Check if two blocks are effectively equal.  Doesn't check irrelevant things like
         * the blocking user or the block timestamp, only things which affect the blocked user   *
-        * @return Boolean
+        *
+        * @param $block Block
+        *
+        * @return bool
         */
        public function equals( Block $block ) {
                return (
@@ -146,6 +160,7 @@ class Block {
         * @deprecated since 1.18
         */
        public function clear() {
+               wfDeprecated( __METHOD__, '1.18' );
                # Noop
        }
 
@@ -154,12 +169,11 @@ class Block {
         *
         * @param $address string The IP address of the user, or blank to skip IP blocks
         * @param $user int The user ID, or zero for anonymous users
-        * @param $killExpired bool Whether to delete expired rows while loading
         * @return Boolean: the user is blocked from editing
         * @deprecated since 1.18
         */
        public function load( $address = '', $user = 0 ) {
-               wfDeprecated( __METHOD__ );
+               wfDeprecated( __METHOD__, '1.18' );
                if( $user ){
                        $username = User::whoIs( $user );
                        $block = self::newFromTarget( $username, $address );
@@ -184,7 +198,7 @@ class Block {
         *     2) A rangeblock encompasing the given IP (smallest first)
         *     3) An autoblock on the given IP
         * @param $vagueTarget User|String also search for blocks affecting this target.  Doesn't
-        *     make any sense to use TYPE_AUTO / TYPE_ID here
+        *     make any sense to use TYPE_AUTO / TYPE_ID here. Leave blank to skip IP lookups.
         * @return Bool whether a relevant block was found
         */
        protected function newLoad( $vagueTarget = null ) {
@@ -198,7 +212,9 @@ class Block {
                        $conds = array( 'ipb_address' => array() );
                }
 
-               if( $vagueTarget !== null ){
+               # Be aware that the != '' check is explicit, since empty values will be
+               # passed by some callers (bug 29116)
+               if( $vagueTarget != ''){
                        list( $target, $type ) = self::parseTarget( $vagueTarget );
                        switch( $type ) {
                                case self::TYPE_USER:
@@ -259,7 +275,7 @@ class Block {
                                # This has the nice property that a /32 block is ranked equally with a
                                # single-IP block, which is exactly what it is...
                                $score = self::TYPE_RANGE  - 1 + ( $size / 128 );
-                               
+
                        } else {
                                $score = $block->getType();
                        }
@@ -335,14 +351,25 @@ class Block {
         */
        protected function initFromRow( $row ) {
                $this->setTarget( $row->ipb_address );
-               $this->setBlocker( User::newFromId( $row->ipb_by ) );
+               if ( $row->ipb_by ) { // local user
+                       $this->setBlocker( User::newFromID( $row->ipb_by ) );
+               } else { // foreign user
+                       $this->setBlocker( $row->ipb_by_text );
+               }
 
                $this->mReason = $row->ipb_reason;
                $this->mTimestamp = wfTimestamp( TS_MW, $row->ipb_timestamp );
                $this->mAuto = $row->ipb_auto;
                $this->mHideName = $row->ipb_deleted;
                $this->mId = $row->ipb_id;
-               $this->mExpiry = $row->ipb_expiry;
+
+               // I wish I didn't have to do this
+               $db = wfGetDB( DB_SLAVE );
+               if ( $row->ipb_expiry == $db->getInfinity() ) {
+                       $this->mExpiry = 'infinity';
+               } else {
+                       $this->mExpiry = wfTimestamp( TS_MW, $row->ipb_expiry );
+               }
 
                $this->isHardblock( !$row->ipb_anon_only );
                $this->isAutoblocking( $row->ipb_enable_autoblock );
@@ -387,6 +414,7 @@ class Block {
         * Insert a block into the block table. Will fail if there is a conflicting
         * block (same name and options) already in the database.
         *
+        * @param $dbw DatabaseBase if you have one available
         * @return mixed: false on failure, assoc array on success:
         *      ('id' => block ID, 'autoIds' => array of autoblock IDs)
         */
@@ -400,18 +428,21 @@ class Block {
                # Don't collide with expired blocks
                Block::purgeExpired();
 
-               $ipb_id = $dbw->nextSequenceValue( 'ipblocks_ipb_id_seq' );
+               $row = $this->getDatabaseArray();
+               $row['ipb_id'] = $dbw->nextSequenceValue("ipblocks_ipb_id_seq");
+
                $dbw->insert(
                        'ipblocks',
-                       $this->getDatabaseArray(),
+                       $row,
                        __METHOD__,
                        array( 'IGNORE' )
                );
                $affected = $dbw->affectedRows();
+               $this->mId = $dbw->insertId();
 
                if ( $affected ) {
                        $auto_ipd_ids = $this->doRetroactiveAutoblock();
-                       return array( 'id' => $ipb_id, 'autoIds' => $auto_ipd_ids );
+                       return array( 'id' => $this->mId, 'autoIds' => $auto_ipd_ids );
                }
 
                return false;
@@ -420,6 +451,9 @@ class Block {
        /**
         * Update a block in the DB with new parameters.
         * The ID field needs to be loaded first.
+        *
+        * @return Int number of affected rows, which should probably be 1 or something's
+        *     gone slightly awry
         */
        public function update() {
                wfDebug( "Block::update; timestamp {$this->mTimestamp}\n" );
@@ -444,19 +478,20 @@ class Block {
                if( !$db ){
                        $db = wfGetDB( DB_SLAVE );
                }
+               $expiry = $db->encodeExpiry( $this->mExpiry );
 
                $a = array(
                        'ipb_address'          => (string)$this->target,
                        'ipb_user'             => $this->target instanceof User ? $this->target->getID() : 0,
-                       'ipb_by'               => $this->getBlocker()->getId(),
-                       'ipb_by_text'          => $this->getBlocker()->getName(),
+                       'ipb_by'               => $this->getBy(),
+                       'ipb_by_text'          => $this->getByName(),
                        'ipb_reason'           => $this->mReason,
                        'ipb_timestamp'        => $db->timestamp( $this->mTimestamp ),
                        'ipb_auto'             => $this->mAuto,
                        'ipb_anon_only'        => !$this->isHardblock(),
                        'ipb_create_account'   => $this->prevents( 'createaccount' ),
                        'ipb_enable_autoblock' => $this->isAutoblocking(),
-                       'ipb_expiry'           => $db->encodeExpiry( $this->mExpiry ),
+                       'ipb_expiry'           => $expiry,
                        'ipb_range_start'      => $this->getRangeStart(),
                        'ipb_range_end'        => $this->getRangeEnd(),
                        'ipb_deleted'          => intval( $this->mHideName ), // typecast required for SQLite
@@ -475,43 +510,51 @@ class Block {
         */
        protected function doRetroactiveAutoblock() {
                $blockIds = array();
-
-               $dbr = wfGetDB( DB_SLAVE );
-               # If autoblock is enabled, autoblock the LAST IP used
-               # - stolen shamelessly from CheckUser_body.php
-
+               # If autoblock is enabled, autoblock the LAST IP(s) used
                if ( $this->isAutoblocking() && $this->getType() == self::TYPE_USER ) {
                        wfDebug( "Doing retroactive autoblocks for " . $this->getTarget() . "\n" );
 
-                       $options = array( 'ORDER BY' => 'rc_timestamp DESC' );
-                       $conds = array( 'rc_user_text' => (string)$this->getTarget() );
+                       $continue = wfRunHooks(
+                               'PerformRetroactiveAutoblock', array( $this, &$blockIds ) );
 
-                       if ( $this->mAngryAutoblock ) {
-                               // Block any IP used in the last 7 days. Up to five IPs.
-                               $conds[] = 'rc_timestamp < ' .
-                                       $dbr->addQuotes( $dbr->timestamp( time() - ( 7 * 86400 ) ) );
-                               $options['LIMIT'] = 5;
-                       } else {
-                               // Just the last IP used.
-                               $options['LIMIT'] = 1;
+                       if ( $continue ) {
+                               self::defaultRetroactiveAutoblock( $this, $blockIds );
                        }
+               }
+               return $blockIds;
+       }
 
-                       $res = $dbr->select( 'recentchanges', array( 'rc_ip' ), $conds,
-                               __METHOD__ ,  $options );
+       /**
+        * Retroactively autoblocks the last IP used by the user (if it is a user)
+        * blocked by this Block. This will use the recentchanges table.
+        *
+        * @param Block $block
+        * @param Array &$blockIds
+        * @return Array: block IDs of retroactive autoblocks made
+        */
+       protected static function defaultRetroactiveAutoblock( Block $block, array &$blockIds ) {
+               $dbr = wfGetDB( DB_SLAVE );
 
-                       if ( !$dbr->numRows( $res ) ) {
-                               # No results, don't autoblock anything
-                               wfDebug( "No IP found to retroactively autoblock\n" );
-                       } else {
-                               foreach ( $res as $row ) {
-                                       if ( $row->rc_ip ) {
-                                               $id = $this->doAutoblock( $row->rc_ip );
-                                               if ( $id ) $blockIds[] = $id;
-                                       }
+               $options = array( 'ORDER BY' => 'rc_timestamp DESC' );
+               $conds = array( 'rc_user_text' => (string)$block->getTarget() );
+
+               // Just the last IP used.
+               $options['LIMIT'] = 1;
+
+               $res = $dbr->select( 'recentchanges', array( 'rc_ip' ), $conds,
+                       __METHOD__ ,  $options );
+
+               if ( !$dbr->numRows( $res ) ) {
+                       # No results, don't autoblock anything
+                       wfDebug( "No IP found to retroactively autoblock\n" );
+               } else {
+                       foreach ( $res as $row ) {
+                               if ( $row->rc_ip ) {
+                                       $id = $block->doAutoblock( $row->rc_ip );
+                                       if ( $id ) $blockIds[] = $id;
                                }
                        }
                }
-               return $blockIds;
        }
 
        /**
@@ -562,72 +605,70 @@ class Block {
         * Autoblocks the given IP, referring to this Block.
         *
         * @param $autoblockIP String: the IP to autoblock.
-        * @param $justInserted Boolean: the main block was just inserted.
         * @return mixed: block ID if an autoblock was inserted, false if not.
         */
-       public function doAutoblock( $autoblockIP, $justInserted = false ) {
+       public function doAutoblock( $autoblockIP ) {
                # If autoblocks are disabled, go away.
                if ( !$this->isAutoblocking() ) {
                        return false;
                }
 
-               # Check for presence on the autoblock whitelist
+               # Check for presence on the autoblock whitelist.
                if ( self::isWhitelistedFromAutoblocks( $autoblockIP ) ) {
                        return false;
                }
 
-               # Allow hooks to cancel the autoblock.
+               # Allow hooks to cancel the autoblock.
                if ( !wfRunHooks( 'AbortAutoblock', array( $autoblockIP, &$this ) ) ) {
                        wfDebug( "Autoblock aborted by hook.\n" );
                        return false;
                }
 
-               # It's okay to autoblock. Go ahead and create/insert the block.
+               # It's okay to autoblock. Go ahead and insert/update the block...
 
+               # Do not add a *new* block if the IP is already blocked.
                $ipblock = Block::newFromTarget( $autoblockIP );
                if ( $ipblock ) {
-                       # If the user is already blocked. Then check if the autoblock would
-                       # exceed the user block. If it would exceed, then do nothing, else
-                       # prolong block time
-                       if ( $this->mExpiry &&
-                               ( $this->mExpiry < Block::getAutoblockExpiry( $ipblock->mTimestamp ) )
+                       # 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?
+                               $this->mExpiry > Block::getAutoblockExpiry( $ipblock->mTimestamp )
                        ) {
-                               return false;
-                       }
-
-                       # Just update the timestamp
-                       if ( !$justInserted ) {
+                               # Reset block timestamp to now and its expiry to
+                               # $wgAutoblockExpiry in the future
                                $ipblock->updateTimestamp();
                        }
-
                        return false;
-               } else {
-                       $ipblock = new Block;
                }
 
-               # Make a new block object with the desired properties
+               # Make a new block object with the desired properties.
+               $autoblock = new Block;
                wfDebug( "Autoblocking {$this->getTarget()}@" . $autoblockIP . "\n" );
-               $ipblock->setTarget( $autoblockIP );
-               $ipblock->setBlocker( $this->getBlocker() );
-               $ipblock->mReason = wfMsgForContent( 'autoblocker', $this->getTarget(), $this->mReason );
-               $ipblock->mTimestamp = wfTimestampNow();
-               $ipblock->mAuto = 1;
-               $ipblock->mCreateAccount = $this->mCreateAccount;
+               $autoblock->setTarget( $autoblockIP );
+               $autoblock->setBlocker( $this->getBlocker() );
+               $autoblock->mReason = wfMsgForContent( 'autoblocker', $this->getTarget(), $this->mReason );
+               $timestamp = wfTimestampNow();
+               $autoblock->mTimestamp = $timestamp;
+               $autoblock->mAuto = 1;
+               $autoblock->prevents( 'createaccount', $this->prevents( 'createaccount' ) );
                # Continue suppressing the name if needed
-               $ipblock->mHideName = $this->mHideName;
-               $ipblock->mDisableUsertalk = $this->mDisableUsertalk;
+               $autoblock->mHideName = $this->mHideName;
+               $autoblock->prevents( 'editownusertalk', $this->prevents( 'editownusertalk' ) );
 
-               # If the user is already blocked with an expiry date, we don't
-               # want to pile on top of that!
-               if ( $this->mExpiry ) {
-                       $ipblock->mExpiry = min( $this->mExpiry, Block::getAutoblockExpiry( $this->mTimestamp ) );
+               if ( $this->mExpiry == 'infinity' ) {
+                       # Original block was indefinite, start an autoblock now
+                       $autoblock->mExpiry = Block::getAutoblockExpiry( $timestamp );
                } else {
-                       $ipblock->mExpiry = Block::getAutoblockExpiry( $this->mTimestamp );
+                       # If the user is already blocked with an expiry date, we don't
+                       # want to pile on top of that.
+                       $autoblock->mExpiry = min( $this->mExpiry, Block::getAutoblockExpiry( $timestamp ) );
                }
 
-               # Insert it
-               $status = $ipblock->insert();
-               return $status ? $status['id'] : false;
+               # Insert the block...
+               $status = $autoblock->insert();
+               return $status
+                       ? $status['id']
+                       : false;
        }
 
        /**
@@ -655,12 +696,13 @@ class Block {
         * @return Boolean
         */
        public function isExpired() {
-               wfDebug( "Block::isExpired() checking current " . wfTimestampNow() . " vs $this->mExpiry\n" );
+               $timestamp = wfTimestampNow();
+               wfDebug( "Block::isExpired() checking current " . $timestamp . " vs $this->mExpiry\n" );
 
                if ( !$this->mExpiry ) {
                        return false;
                } else {
-                       return wfTimestampNow() > $this->mExpiry;
+                       return $timestamp > $this->mExpiry;
                }
        }
 
@@ -701,7 +743,7 @@ class Block {
        public function getRangeStart() {
                switch( $this->type ) {
                        case self::TYPE_USER:
-                               return null;
+                               return '';
                        case self::TYPE_IP:
                                return IP::toHex( $this->target );
                        case self::TYPE_RANGE:
@@ -718,7 +760,7 @@ class Block {
        public function getRangeEnd() {
                switch( $this->type ) {
                        case self::TYPE_USER:
-                               return null;
+                               return '';
                        case self::TYPE_IP:
                                return IP::toHex( $this->target );
                        case self::TYPE_RANGE:
@@ -731,11 +773,12 @@ class Block {
        /**
         * Get the user id of the blocking sysop
         *
-        * @return Integer
+        * @return Integer (0 for foreign users)
         */
        public function getBy() {
-               return $this->getBlocker() instanceof User
-                       ? $this->getBlocker()->getId()
+               $blocker = $this->getBlocker();
+               return ( $blocker instanceof User )
+                       ? $blocker->getId()
                        : 0;
        }
 
@@ -745,9 +788,10 @@ class Block {
         * @return String
         */
        public function getByName() {
-               return $this->getBlocker() instanceof User
-                       ? $this->getBlocker()->getName()
-                       : null;
+               $blocker = $this->getBlocker();
+               return ( $blocker instanceof User )
+                       ? $blocker->getName()
+                       : (string)$blocker; // username
        }
 
        /**
@@ -761,13 +805,19 @@ class Block {
        /**
         * Get/set the SELECT ... FOR UPDATE flag
         * @deprecated since 1.18
+        *
+        * @param $x Bool
         */
        public function forUpdate( $x = null ) {
+               wfDeprecated( __METHOD__, '1.18' );
                # noop
        }
 
        /**
         * Get/set a flag determining whether the master is used for reads
+        *
+        * @param $x Bool
+        * @return Bool
         */
        public function fromMaster( $x = null ) {
                return wfSetVar( $this->mFromMaster, $x );
@@ -829,7 +879,7 @@ class Block {
         */
        public function getRedactedName() {
                if ( $this->mAuto ) {
-                       return HTML::rawElement(
+                       return Html::rawElement(
                                'span',
                                array( 'class' => 'mw-autoblockid' ),
                                wfMessage( 'autoblockid', $this->mId )
@@ -848,6 +898,7 @@ class Block {
         * @deprecated since 1.18; use $dbw->encodeExpiry() instead
         */
        public static function encodeExpiry( $expiry, $db ) {
+               wfDeprecated( __METHOD__, '1.18' );
                return $db->encodeExpiry( $expiry );
        }
 
@@ -855,11 +906,12 @@ class Block {
         * Decode expiry which has come from the DB
         *
         * @param $expiry String: Database expiry format
-        * @param $timestampType Requested timestamp format
+        * @param $timestampType Int Requested timestamp format
         * @return String
-        * @deprecated since 1.18; use $wgLang->decodeExpiry() instead
+        * @deprecated since 1.18; use $wgLang->formatExpiry() instead
         */
        public static function decodeExpiry( $expiry, $timestampType = TS_MW ) {
+               wfDeprecated( __METHOD__, '1.18' );
                global $wgContLang;
                return $wgContLang->formatExpiry( $expiry, $timestampType );
        }
@@ -867,6 +919,7 @@ class Block {
        /**
         * Get a timestamp of the expiry for autoblocks
         *
+        * @param $timestamp String|Int
         * @return String
         */
        public static function getAutoblockExpiry( $timestamp ) {
@@ -883,6 +936,7 @@ class Block {
         * @deprecated since 1.18, call IP::sanitizeRange() directly
         */
        public static function normaliseRange( $range ) {
+               wfDeprecated( __METHOD__, '1.18' );
                return IP::sanitizeRange( $range );
        }
 
@@ -891,7 +945,8 @@ class Block {
         */
        public static function purgeExpired() {
                $dbw = wfGetDB( DB_MASTER );
-               $dbw->delete( 'ipblocks', array( 'ipb_expiry < ' . $dbw->addQuotes( $dbw->timestamp() ) ), __METHOD__ );
+               $dbw->delete( 'ipblocks',
+                       array( 'ipb_expiry < ' . $dbw->addQuotes( $dbw->timestamp() ) ), __METHOD__ );
        }
 
        /**
@@ -901,6 +956,7 @@ class Block {
         * @return String
         */
        public static function infinity() {
+               wfDeprecated( __METHOD__, '1.18' );
                return wfGetDB( DB_SLAVE )->getInfinity();
        }
 
@@ -912,6 +968,8 @@ class Block {
         * @deprecated since 1.18; use $wgLang->formatExpiry() instead
         */
        public static function formatExpiry( $encoded_expiry ) {
+               wfDeprecated( __METHOD__, '1.18' );
+
                global $wgContLang;
                static $msg = null;
 
@@ -945,7 +1003,7 @@ class Block {
         * @deprecated since 1.18 moved to SpecialBlock::parseExpiryInput()
         */
        public static function parseExpiryInput( $expiry ) {
-               wfDeprecated( __METHOD__ );
+               wfDeprecated( __METHOD__, '1.18' );
                return SpecialBlock::parseExpiryInput( $expiry );
        }
 
@@ -963,22 +1021,25 @@ class Block {
         *     1.2.3.4 will not select 1.2.0.0/16 or even 1.2.3.4/32)
         * @param $vagueTarget String|User|Int as above, but we will search for *any* block which
         *     affects that target (so for an IP address, get ranges containing that IP; and also
-        *     get any relevant autoblocks)
+        *     get any relevant autoblocks). Leave empty or blank to skip IP-based lookups.
         * @param $fromMaster Bool whether to use the DB_MASTER database
         * @return Block|null (null if no relevant block could be found).  The target and type
         *     of the returned Block will refer to the actual block which was found, which might
         *     not be the same as the target you gave if you used $vagueTarget!
         */
        public static function newFromTarget( $specificTarget, $vagueTarget = null, $fromMaster = false ) {
+
                list( $target, $type ) = self::parseTarget( $specificTarget );
                if( $type == Block::TYPE_ID || $type == Block::TYPE_AUTO ){
                        return Block::newFromID( $target );
 
-               } elseif( $target === null && $vagueTarget === null ){
+               } 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 ) ) ) {
                        $block = new Block();
                        $block->fromMaster( $fromMaster );
 
@@ -988,18 +1049,17 @@ class Block {
 
                        if( $block->newLoad( $vagueTarget ) ){
                                return $block;
-                       } else {
-                               return null;
                        }
-               } else {
-                       return null;
                }
+               return null;
        }
 
        /**
         * From an existing Block, get the target and the type of target.  Note that it is
         * always safe to treat the target as a string; for User objects this will return
         * User::__toString() which in turn gives User::getName().
+        *
+        * @param $target String|Int|User
         * @return array( User|String, Block::TYPE_ constant )
         */
        public static function parseTarget( $target ) {
@@ -1008,22 +1068,15 @@ class Block {
                # We may have been through this before
                if( $target instanceof User ){
                        if( IP::isValid( $target->getName() ) ){
-                               return self::TYPE_IP;
+                               return array( $target, self::TYPE_IP );
                        } else {
-                               return self::TYPE_USER;
+                               return array( $target, self::TYPE_USER );
                        }
                } elseif( $target === null ){
                        return array( null, null );
                }
 
-               $userObj = User::newFromName( $target );
-               if ( $userObj instanceof User ) {
-                       # Note that since numbers are valid usernames, a $target of "12345" will be
-                       # considered a User.  If you want to pass a block ID, prepend a hash "#12345",
-                       # since hash characters are not valid in usernames or titles generally.
-                       return array( $userObj, Block::TYPE_USER );
-
-               } elseif ( IP::isValid( $target ) ) {
+               if ( IP::isValid( $target ) ) {
                        # We can still create a User if it's an IP address, but we need to turn
                        # off validation checking (which would exclude IP addresses)
                        return array(
@@ -1034,6 +1087,22 @@ class Block {
                } elseif ( IP::isValidBlock( $target ) ) {
                        # Can't create a User from an IP range
                        return array( IP::sanitizeRange( $target ), Block::TYPE_RANGE );
+               }
+
+               # Consider the possibility that this is not a username at all
+               # but actually an old subpage (bug #29797)
+               if( strpos( $target, '/' ) !== false ){
+                       # An old subpage, drill down to the user behind it
+                       $parts = explode( '/', $target );
+                       $target = $parts[0];
+               }
+
+               $userObj = User::newFromName( $target );
+               if ( $userObj instanceof User ) {
+                       # Note that since numbers are valid usernames, a $target of "12345" will be
+                       # considered a User.  If you want to pass a block ID, prepend a hash "#12345",
+                       # since hash characters are not valid in usernames or titles generally.
+                       return array( $userObj, Block::TYPE_USER );
 
                } elseif ( preg_match( '/^#\d+$/', $target ) ) {
                        # Autoblock reference in the form "#12345"
@@ -1060,7 +1129,7 @@ class Block {
         * this returns the unredacted name; frontend functions need to call $block->getRedactedName()
         * in this situation.
         * @return array( User|String, Block::TYPE_ constant )
-        * FIXME: this should be an integral part of the Block member variables
+        * @todo FIXME: This should be an integral part of the Block member variables
         */
        public function getTargetAndType() {
                return array( $this->getTarget(), $this->getType() );
@@ -1076,6 +1145,15 @@ class Block {
                return $this->target;
        }
 
+       /**
+        * @since 1.19
+        *
+        * @return Mixed|string
+        */
+       public function getExpiry() {
+               return $this->mExpiry;
+       }
+
        /**
         * Set the target for this block, and update $this->type accordingly
         * @param $target Mixed
@@ -1086,7 +1164,7 @@ class Block {
 
        /**
         * Get the user who implemented this block
-        * @return User
+        * @return User|string Local User object or string for a foreign user
         */
        public function getBlocker(){
                return $this->blocker;
@@ -1094,9 +1172,9 @@ class Block {
 
        /**
         * Set the user who implemented (or will implement) this block
-        * @param $user User
+        * @param $user User|string Local User object or username string for foriegn users
         */
-       public function setBlocker( User $user ){
+       public function setBlocker( $user ){
                $this->blocker = $user;
        }
 }