Follow-up r84358 CR: rename 'editusertalk' to 'editownusertalk', private --> protecte...
[lhc/web/wiklou.git] / includes / Block.php
index 2367525..aa2d98e 100644 (file)
  * Globals used: $wgAutoblockExpiry, $wgAntiLockFlags
  *
  * @todo This could be used everywhere, but it isn't.
+ * FIXME: this whole class is a cesspit, needs a complete rewrite
  */
 class Block {
        /* public*/ var $mAddress, $mUser, $mBy, $mReason, $mTimestamp, $mAuto, $mId, $mExpiry,
-                               $mRangeStart, $mRangeEnd, $mAnonOnly, $mEnableAutoblock, $mHideName,
-                               $mBlockEmail, $mByName, $mAngryAutoblock, $mAllowUsertalk;
-       /* private */ var $mNetworkBits, $mIntegerAddr, $mForUpdate, $mFromMaster;
-
-       const EB_KEEP_EXPIRED = 1;
-       const EB_FOR_UPDATE = 2;
-       const EB_RANGE_ONLY = 4;
+                               $mEnableAutoblock, $mHideName,
+                               $mByName, $mAngryAutoblock;
+       protected
+               $mFromMaster,
+               $mRangeStart,
+               $mRangeEnd,
+               $mAnonOnly,
+               $mBlockEmail,
+               $mAllowUsertalk,
+               $mCreateAccount;
+
+       # TYPE constants
+       const TYPE_USER = 1;
+       const TYPE_IP = 2;
+       const TYPE_RANGE = 3;
+       const TYPE_AUTO = 4;
+       const TYPE_ID = 5;
 
        function __construct( $address = '', $user = 0, $by = 0, $reason = '',
-               $timestamp = '' , $auto = 0, $expiry = '', $anonOnly = 0, $createAccount = 0, $enableAutoblock = 0,
-               $hideName = 0, $blockEmail = 0, $allowUsertalk = 0 )
+               $timestamp = 0, $auto = 0, $expiry = '', $anonOnly = 0, $createAccount = 0, $enableAutoblock = 0,
+               $hideName = 0, $blockEmail = 0, $allowUsertalk = 0, $byName = false )
        {
                $this->mId = 0;
                # Expand valid IPv6 addresses
@@ -38,14 +49,13 @@ class Block {
                $this->mAuto = $auto;
                $this->mAnonOnly = $anonOnly;
                $this->mCreateAccount = $createAccount;
-               $this->mExpiry = self::decodeExpiry( $expiry );
+               $this->mExpiry = $expiry;
                $this->mEnableAutoblock = $enableAutoblock;
                $this->mHideName = $hideName;
                $this->mBlockEmail = $blockEmail;
                $this->mAllowUsertalk = $allowUsertalk;
-               $this->mForUpdate = false;
                $this->mFromMaster = false;
-               $this->mByName = false;
+               $this->mByName = $byName;
                $this->mAngryAutoblock = false;
                $this->initialiseRange();
        }
@@ -81,6 +91,7 @@ class Block {
                $res = $dbr->resultObject( $dbr->select( 'ipblocks', '*',
                        array( 'ipb_id' => $id ), __METHOD__ ) );
                $block = new Block;
+
                if ( $block->loadFromResult( $res ) ) {
                        return $block;
                } else {
@@ -121,30 +132,6 @@ class Block {
                $this->mByName = false;
        }
 
-       /**
-        * Get the DB object and set the reference parameter to the select options.
-        * The options array will contain FOR UPDATE if appropriate.
-        *
-        * @param $options Array
-        * @return Database
-        */
-       protected function &getDBOptions( &$options ) {
-               global $wgAntiLockFlags;
-
-               if ( $this->mForUpdate || $this->mFromMaster ) {
-                       $db = wfGetDB( DB_MASTER );
-                       if ( !$this->mForUpdate || ( $wgAntiLockFlags & ALF_NO_BLOCK_LOCK ) ) {
-                               $options = array();
-                       } else {
-                               $options = array( 'FOR UPDATE' );
-                       }
-               } else {
-                       $db = wfGetDB( DB_SLAVE );
-                       $options = array();
-               }
-               return $db;
-       }
-
        /**
         * Get a block from the DB, with either the given address or the given username
         *
@@ -157,19 +144,24 @@ class Block {
        public function load( $address = '', $user = 0, $killExpired = true ) {
                wfDebug( "Block::load: '$address', '$user', $killExpired\n" );
 
-               $options = array();
-               $db =& $this->getDBOptions( $options );
+               $db = wfGetDB( $this->mFromMaster ? DB_MASTER : DB_SLAVE );
 
                if ( 0 == $user && $address === '' ) {
                        # Invalid user specification, not blocked
                        $this->clear();
+
                        return false;
                }
 
                # Try user block
                if ( $user ) {
-                       $res = $db->resultObject( $db->select( 'ipblocks', '*', array( 'ipb_user' => $user ),
-                               __METHOD__, $options ) );
+                       $res = $db->resultObject( $db->select(
+                               'ipblocks',
+                               '*',
+                               array( 'ipb_user' => $user ),
+                               __METHOD__
+                       ) );
+
                        if ( $this->loadFromResult( $res, $killExpired ) ) {
                                return true;
                        }
@@ -180,7 +172,12 @@ class Block {
                # Slightly tricky while handling killExpired as well
                if ( $address !== '' ) {
                        $conds = array( 'ipb_address' => $address, 'ipb_auto' => 0 );
-                       $res = $db->resultObject( $db->select( 'ipblocks', '*', $conds, __METHOD__, $options ) );
+                       $res = $db->resultObject( $db->select(
+                               'ipblocks',
+                               '*',
+                               $conds,
+                               __METHOD__
+                       ) );
 
                        if ( $this->loadFromResult( $res, $killExpired ) ) {
                                if ( $user && $this->mAnonOnly ) {
@@ -190,6 +187,7 @@ class Block {
                                        if ( !$this->mCreateAccount ) {
                                                $this->clear();
                                        }
+
                                        return false;
                                } else {
                                        return true;
@@ -204,6 +202,7 @@ class Block {
                                if ( !$this->mCreateAccount ) {
                                        $this->clear();
                                }
+
                                return false;
                        } else {
                                return true;
@@ -218,7 +217,12 @@ class Block {
                                $conds['ipb_anon_only'] = 0;
                        }
 
-                       $res = $db->resultObject( $db->select( 'ipblocks', '*', $conds, __METHOD__, $options ) );
+                       $res = $db->resultObject( $db->select(
+                               'ipblocks',
+                               '*',
+                               $conds,
+                               __METHOD__
+                       ) );
 
                        if ( $this->loadFromResult( $res, $killExpired ) ) {
                                return true;
@@ -230,6 +234,54 @@ class Block {
                return false;
        }
 
+       /**
+        * Get a set of SQL conditions which will select rangeblocks encompasing a given range
+        * @param $start String Hexadecimal IP representation
+        * @param $end String Hexadecimal IP represenation, or null to use $start = $end
+        * @return String
+        */
+       public static function getRangeCond( $start, $end = null ) {
+               if ( $end === null ) {
+                       $end = $start;
+               }
+               # Per bug 14634, we want to include relevant active rangeblocks; for
+               # rangeblocks, we want to include larger ranges which enclose the given
+               # range. We know that all blocks must be smaller than $wgBlockCIDRLimit,
+               # so we can improve performance by filtering on a LIKE clause
+               $chunk = self::getIpFragment( $start );
+               $dbr = wfGetDB( DB_SLAVE );
+               $like = $dbr->buildLike( $chunk, $dbr->anyString() );
+
+               # Fairly hard to make a malicious SQL statement out of hex characters,
+               # but stranger things have happened...
+               $safeStart = $dbr->addQuotes( $start );
+               $safeEnd = $dbr->addQuotes( $end );
+
+               return $dbr->makeList(
+                       array(
+                               "ipb_range_start $like",
+                               "ipb_range_start <= $safeStart",
+                               "ipb_range_end >= $safeEnd",
+                       ),
+                       LIST_AND
+               );
+       }
+
+       /**
+        * Get the component of an IP address which is certain to be the same between an IP
+        * address and a rangeblock containing that IP address.
+        * @param  $hex String Hexadecimal IP representation
+        * @return String
+        */
+       protected static function getIpFragment( $hex ) {
+               global $wgBlockCIDRLimit;
+               if ( substr( $hex, 0, 3 ) == 'v6-' ) {
+                       return 'v6-' . substr( substr( $hex, 3 ), 0,  floor( $wgBlockCIDRLimit['IPv6'] / 4 ) );
+               } else {
+                       return substr( $hex, 0,  floor( $wgBlockCIDRLimit['IPv4'] / 4 ) );
+               }
+       }
+
        /**
         * Fill in member variables from a result wrapper
         *
@@ -266,6 +318,7 @@ class Block {
                        }
                }
                $res->free();
+
                return $ret;
        }
 
@@ -275,10 +328,10 @@ class Block {
         *
         * @param $address String: IP address range
         * @param $killExpired Boolean: whether to delete expired rows while loading
-        * @param $userid Integer: if not 0, then sets ipb_anon_only
+        * @param $user Integer: if not 0, then sets ipb_anon_only
         * @return Boolean
         */
-       public function loadRange( $address, $killExpired = true, $user = 0 ) {
+       protected function loadRange( $address, $killExpired = true, $user = 0 ) {
                $iaddr = IP::toHex( $address );
 
                if ( $iaddr === false ) {
@@ -290,8 +343,7 @@ class Block {
                # Blocks should not cross a /16 boundary.
                $range = substr( $iaddr, 0, 4 );
 
-               $options = array();
-               $db =& $this->getDBOptions( $options );
+               $db = wfGetDB( $this->mFromMaster ? DB_MASTER : DB_SLAVE );
                $conds = array(
                        'ipb_range_start' . $db->buildLike( $range, $db->anyString() ),
                        "ipb_range_start <= '$iaddr'",
@@ -302,8 +354,9 @@ class Block {
                        $conds['ipb_anon_only'] = 0;
                }
 
-               $res = $db->resultObject( $db->select( 'ipblocks', '*', $conds, __METHOD__, $options ) );
+               $res = $db->resultObject( $db->select( 'ipblocks', '*', $conds, __METHOD__ ) );
                $success = $this->loadFromResult( $res, $killExpired );
+
                return $success;
        }
 
@@ -313,7 +366,7 @@ class Block {
         *
         * @param $row ResultWrapper: a row from the ipblocks table
         */
-       public function initFromRow( $row ) {
+       protected function initFromRow( $row ) {
                $this->mAddress = $row->ipb_address;
                $this->mReason = $row->ipb_reason;
                $this->mTimestamp = wfTimestamp( TS_MW, $row->ipb_timestamp );
@@ -327,7 +380,7 @@ class Block {
                $this->mAllowUsertalk = $row->ipb_allow_usertalk;
                $this->mHideName = $row->ipb_deleted;
                $this->mId = $row->ipb_id;
-               $this->mExpiry = self::decodeExpiry( $row->ipb_expiry );
+               $this->mExpiry = $row->ipb_expiry;
 
                if ( isset( $row->user_name ) ) {
                        $this->mByName = $row->user_name;
@@ -368,6 +421,7 @@ class Block {
 
                $dbw = wfGetDB( DB_MASTER );
                $dbw->delete( 'ipblocks', array( 'ipb_id' => $this->mId ), __METHOD__ );
+
                return $dbw->affectedRows() > 0;
        }
 
@@ -377,9 +431,11 @@ class Block {
         *
         * @return Boolean: whether or not the insertion was successful.
         */
-       public function insert() {
+       public function insert( $dbw = null ) {
                wfDebug( "Block::insert; timestamp {$this->mTimestamp}\n" );
-               $dbw = wfGetDB( DB_MASTER );
+
+               if ( $dbw === null )
+                       $dbw = wfGetDB( DB_MASTER );
 
                $this->validateBlockParams();
                $this->initialiseRange();
@@ -402,7 +458,7 @@ class Block {
                                'ipb_anon_only' => $this->mAnonOnly,
                                'ipb_create_account' => $this->mCreateAccount,
                                'ipb_enable_autoblock' => $this->mEnableAutoblock,
-                               'ipb_expiry' => self::encodeExpiry( $this->mExpiry, $dbw ),
+                               'ipb_expiry' => $dbw->encodeExpiry( $this->mExpiry ),
                                'ipb_range_start' => $this->mRangeStart,
                                'ipb_range_end' => $this->mRangeEnd,
                                'ipb_deleted'   => intval( $this->mHideName ), // typecast required for SQLite
@@ -442,7 +498,7 @@ class Block {
                                'ipb_anon_only' => $this->mAnonOnly,
                                'ipb_create_account' => $this->mCreateAccount,
                                'ipb_enable_autoblock' => $this->mEnableAutoblock,
-                               'ipb_expiry' => self::encodeExpiry( $this->mExpiry, $dbw ),
+                               'ipb_expiry' => $dbw->encodeExpiry( $this->mExpiry ),
                                'ipb_range_start' => $this->mRangeStart,
                                'ipb_range_end' => $this->mRangeEnd,
                                'ipb_deleted'   => $this->mHideName,
@@ -475,6 +531,7 @@ class Block {
                if ( !$this->mUser && $this->mAnonOnly ) {
                        $this->mBlockEmail = 0;
                }
+
                if ( !$this->mByName ) {
                        if ( $this->mBy ) {
                                $this->mByName = User::whoIs( $this->mBy );
@@ -491,7 +548,7 @@ class Block {
         *
         * @return Boolean: whether or not a retroactive autoblock was made.
         */
-       public function doRetroactiveAutoblock() {
+       protected function doRetroactiveAutoblock() {
                $dbr = wfGetDB( DB_SLAVE );
                # If autoblock is enabled, autoblock the LAST IP used
                # - stolen shamelessly from CheckUser_body.php
@@ -518,9 +575,10 @@ class Block {
                                # No results, don't autoblock anything
                                wfDebug( "No IP found to retroactively autoblock\n" );
                        } else {
-                               while ( $row = $dbr->fetchObject( $res ) ) {
-                                       if ( $row->rc_ip )
+                               foreach ( $res as $row ) {
+                                       if ( $row->rc_ip ) {
                                                $this->doAutoblock( $row->rc_ip );
+                                       }
                                }
                        }
                }
@@ -601,13 +659,16 @@ class Block {
                        # exceed the user block. If it would exceed, then do nothing, else
                        # prolong block time
                        if ( $this->mExpiry &&
-                       ( $this->mExpiry < Block::getAutoblockExpiry( $ipblock->mTimestamp ) ) ) {
+                               ( $this->mExpiry < Block::getAutoblockExpiry( $ipblock->mTimestamp ) )
+                       ) {
                                return;
                        }
+
                        # Just update the timestamp
                        if ( !$justInserted ) {
                                $ipblock->updateTimestamp();
                        }
+
                        return;
                } else {
                        $ipblock = new Block;
@@ -626,6 +687,7 @@ class Block {
                # Continue suppressing the name if needed
                $ipblock->mHideName = $this->mHideName;
                $ipblock->mAllowUsertalk = $this->mAllowUsertalk;
+
                # If the user is already blocked with an expiry date, we don't
                # want to pile on top of that!
                if ( $this->mExpiry ) {
@@ -633,6 +695,7 @@ class Block {
                } else {
                        $ipblock->mExpiry = Block::getAutoblockExpiry( $this->mTimestamp );
                }
+
                # Insert it
                return $ipblock->insert();
        }
@@ -643,6 +706,7 @@ class Block {
         */
        public function deleteIfExpired() {
                wfProfileIn( __METHOD__ );
+
                if ( $this->isExpired() ) {
                        wfDebug( "Block::deleteIfExpired() -- deleting\n" );
                        $this->delete();
@@ -651,6 +715,7 @@ class Block {
                        wfDebug( "Block::deleteIfExpired() -- not expired\n" );
                        $retVal = false;
                }
+
                wfProfileOut( __METHOD__ );
                return $retVal;
        }
@@ -661,6 +726,7 @@ class Block {
         */
        public function isExpired() {
                wfDebug( "Block::isExpired() checking current " . wfTimestampNow() . " vs $this->mExpiry\n" );
+
                if ( !$this->mExpiry ) {
                        return false;
                } else {
@@ -696,6 +762,38 @@ class Block {
                }
        }
 
+       /**
+        * Get the IP address at the start of the range in Hex form
+        * @return String IP in Hex form
+        */
+       public function getRangeStart() {
+               switch( $this->type ) {
+                       case self::TYPE_USER:
+                               return null;
+                       case self::TYPE_IP:
+                               return IP::toHex( $this->target );
+                       case self::TYPE_RANGE:
+                               return $this->mRangeStart;
+                       default: throw new MWException( "Block with invalid type" );
+               }
+       }
+
+       /**
+        * Get the IP address at the start of the range in Hex form
+        * @return String IP in Hex form
+        */
+       public function getRangeEnd() {
+               switch( $this->type ) {
+                       case self::TYPE_USER:
+                               return null;
+                       case self::TYPE_IP:
+                               return IP::toHex( $this->target );
+                       case self::TYPE_RANGE:
+                               return $this->mRangeEnd;
+                       default: throw new MWException( "Block with invalid type" );
+               }
+       }
+
        /**
         * Get the user id of the blocking sysop
         *
@@ -716,9 +814,10 @@ class Block {
 
        /**
         * Get/set the SELECT ... FOR UPDATE flag
+        * @deprecated since 1.18
         */
        public function forUpdate( $x = null ) {
-               return wfSetVar( $this->mForUpdate, $x );
+               # noop
        }
 
        /**
@@ -728,15 +827,62 @@ class Block {
                return wfSetVar( $this->mFromMaster, $x );
        }
 
+       /**
+        * Get/set whether the Block is a hardblock (affects logged-in users on a given IP/range
+        * @param $x Bool
+        * @return  Bool
+        */
+       public function isHardblock( $x = null ) {
+               $y = $this->mAnonOnly;
+               if ( $x !== null ) {
+                       $this->mAnonOnly = !$x;
+               }
+               return !$y;
+       }
+
+       /**
+        * Get/set whether the Block prevents a given action
+        * @param $action String
+        * @param $x Bool
+        * @return Bool
+        */
+       public function prevents( $action, $x = null ) {
+               switch( $action ) {
+                       case 'edit':
+                               # TODO Not actually quite this simple (bug 13611 etc)
+                               return true;
+
+                       case 'createaccount':
+                               return wfSetVar( $this->mCreateAccount, $x );
+
+                       case 'sendemail':
+                               return wfSetVar( $this->mBlockEmail, $x );
+
+                       case 'editownusertalk':
+                               $y = $this->mAllowUsertalk;
+                               if ( $x !== null ) {
+                                       $this->mAllowUsertalk = !$x;
+                               }
+                               return !$y;
+
+                       default:
+                               return null;
+               }
+       }
+
        /**
         * Get the block name, but with autoblocked IPs hidden as per standard privacy policy
-        * @return String
+        * @return String, text is escaped
         */
        public function getRedactedName() {
                if ( $this->mAuto ) {
-                       return '#' . $this->mId;
+                       return HTML::rawElement(
+                               'span',
+                               array( 'class' => 'mw-autoblockid' ),
+                               wfMessage( 'autoblockid', $this->mId )
+                       );
                } else {
-                       return $this->mAddress;
+                       return htmlspecialchars( $this->mAddress );
                }
        }
 
@@ -746,13 +892,10 @@ class Block {
         * @param $expiry String: timestamp for expiry, or
         * @param $db Database object
         * @return String
+        * @deprecated since 1.18; use $dbw->encodeExpiry() instead
         */
        public static function encodeExpiry( $expiry, $db ) {
-               if ( $expiry == '' || $expiry == Block::infinity() ) {
-                       return Block::infinity();
-               } else {
-                       return $db->timestamp( $expiry );
-               }
+               return $db->encodeExpiry( $expiry );
        }
 
        /**
@@ -761,13 +904,11 @@ class Block {
         * @param $expiry String: Database expiry format
         * @param $timestampType Requested timestamp format
         * @return String
+        * @deprecated since 1.18; use $wgLang->decodeExpiry() instead
         */
        public static function decodeExpiry( $expiry, $timestampType = TS_MW ) {
-               if ( $expiry == '' || $expiry == Block::infinity() ) {
-                       return Block::infinity();
-               } else {
-                       return wfTimestamp( $timestampType, $expiry );
-               }
+               global $wgContLang;
+               return $wgContLang->formatExpiry( $expiry, $timestampType );
        }
 
        /**
@@ -777,6 +918,7 @@ class Block {
         */
        public static function getAutoblockExpiry( $timestamp ) {
                global $wgAutoblockExpiry;
+
                return wfTimestamp( TS_MW, wfTimestamp( TS_UNIX, $timestamp ) + $wgAutoblockExpiry );
        }
 
@@ -785,34 +927,10 @@ class Block {
         * For example, 127.111.113.151/24 -> 127.111.113.0/24
         * @param $range String: IP address to normalize
         * @return string
+        * @deprecated since 1.18, call IP::sanitizeRange() directly
         */
        public static function normaliseRange( $range ) {
-               $parts = explode( '/', $range );
-               if ( count( $parts ) == 2 ) {
-                       // IPv6
-                       if ( IP::isIPv6( $range ) && $parts[1] >= 64 && $parts[1] <= 128 ) {
-                               $bits = $parts[1];
-                               $ipint = IP::toUnsigned6( $parts[0] );
-                               # Native 32 bit functions WON'T work here!!!
-                               # Convert to a padded binary number
-                               $network = wfBaseConvert( $ipint, 10, 2, 128 );
-                               # Truncate the last (128-$bits) bits and replace them with zeros
-                               $network = str_pad( substr( $network, 0, $bits ), 128, 0, STR_PAD_RIGHT );
-                               # Convert back to an integer
-                               $network = wfBaseConvert( $network, 2, 10 );
-                               # Reform octet address
-                               $newip = IP::toOctet( $network );
-                               $range = "$newip/{$parts[1]}";
-                       } // IPv4
-                       elseif ( IP::isIPv4( $range ) && $parts[1] >= 16 && $parts[1] <= 32 ) {
-                               $shift = 32 - $parts[1];
-                               $ipint = IP::toUnsigned( $parts[0] );
-                               $ipint = $ipint >> $shift << $shift;
-                               $newip = long2ip( $ipint );
-                               $range = "$newip/{$parts[1]}";
-                       }
-               }
-               return $range;
+               return IP::sanitizeRange( $range );
        }
 
        /**
@@ -825,15 +943,12 @@ class Block {
 
        /**
         * Get a value to insert into expiry field of the database when infinite expiry
-        * is desired. In principle this could be DBMS-dependant, but currently all
-        * supported DBMS's support the string "infinity", so we just use that.
-        *
+        * is desired
+        * @deprecated since 1.18, call $dbr->getInfinity() directly
         * @return String
         */
        public static function infinity() {
-               # This is a special keyword for timestamps in PostgreSQL, and
-               # works with CHAR(14) as well because "i" sorts after all numbers.
-               return 'infinity';
+               return wfGetDB( DB_SLAVE )->getInfinity();
        }
 
        /**
@@ -841,20 +956,23 @@ class Block {
         *
         * @param $encoded_expiry String: Database encoded expiry time
         * @return Html-escaped String
+        * @deprecated since 1.18; use $wgLang->formatExpiry() instead
         */
        public static function formatExpiry( $encoded_expiry ) {
+               global $wgContLang;
                static $msg = null;
 
                if ( is_null( $msg ) ) {
                        $msg = array();
                        $keys = array( 'infiniteblock', 'expiringblock' );
+
                        foreach ( $keys as $key ) {
                                $msg[$key] = wfMsgHtml( $key );
                        }
                }
 
-               $expiry = Block::decodeExpiry( $encoded_expiry );
-               if ( $expiry == 'infinity' ) {
+               $expiry = $wgContLang->formatExpiry( $encoded_expiry, TS_MW );
+               if ( $expiry == wfGetDB( DB_SLAVE )->getInfinity() ) {
                        $expirystr = $msg['infiniteblock'];
                } else {
                        global $wgLang;
@@ -862,25 +980,129 @@ class Block {
                        $expiretimestr = htmlspecialchars( $wgLang->time( $expiry, true ) );
                        $expirystr = wfMsgReplaceArgs( $msg['expiringblock'], array( $expiredatestr, $expiretimestr ) );
                }
+
                return $expirystr;
        }
 
+       # FIXME: everything above here is a mess, needs much cleaning up
+
        /**
-        * Convert a typed-in expiry time into something we can put into the database.
-        * @param $expiry_input String: whatever was typed into the form
-        * @return String: more database friendly
+        * Convert a submitted expiry time, which may be relative ("2 weeks", etc) or absolute
+        * ("24 May 2034"), into an absolute timestamp we can put into the database.
+        * @param $expiry String: whatever was typed into the form
+        * @return String: timestamp or "infinity" string for th DB implementation
+        * @deprecated since 1.18 moved to SpecialBlock::parseExpiryInput()
         */
-       public static function parseExpiryInput( $expiry_input ) {
-               if ( $expiry_input == 'infinite' || $expiry_input == 'indefinite' ) {
-                       $expiry = 'infinity';
-               } else {
-                       $expiry = strtotime( $expiry_input );
+       public static function parseExpiryInput( $expiry ) {
+               wfDeprecated( __METHOD__ );
+               return SpecialBlock::parseExpiryInput( $expiry );
+       }
 
-                       if ( $expiry < 0 || $expiry === false ) {
-                               return false;
+       /**
+        * Given a target and the target's type, get an existing Block object if possible.
+        * Note that passing an IP address will get an applicable rangeblock if the IP is
+        * not individually blocked but falls within that range
+        * TODO: check that that fallback handles nested rangeblocks nicely (should return
+        *     smallest one)
+        * @param $target String|User|Int a block target, which may be one of several types:
+        *     * A user to block, in which case $target will be a User
+        *     * An IP to block, in which case $target will be a User generated by using
+        *       User::newFromName( $ip, false ) to turn off name validation
+        *     * An IP range, in which case $target will be a String "123.123.123.123/18" etc
+        *     * The ID of an existing block, in which case $target will be an Int
+        * @param $type Block::TYPE_ constant the type of block as described above
+        * @return Block|null (null if the target is not blocked)
+        */
+       public static function newFromTargetAndType( $target, $type ) {
+               if ( $target instanceof User ) {
+                       if ( $type == Block::TYPE_IP ) {
+                               return Block::newFromDB( $target->getName(), 0 );
+                       } elseif ( $type == Block::TYPE_USER ) {
+                               return Block::newFromDB( '', $target->getId() );
+                       } else {
+                               # Should be unreachable;
+                               return null;
                        }
+
+               } elseif ( $type == Block::TYPE_RANGE ) {
+                       return Block::newFromDB( $target, 0 );
+
+               } elseif ( $type == Block::TYPE_ID || $type == Block::TYPE_AUTO ) {
+                       return Block::newFromID( $target );
+
+               } else {
+                       return null;
                }
-               return $expiry;
        }
 
+       public static function newFromTarget( $target ) {
+               list( $target, $type ) = self::parseTarget( $target );
+               return self::newFromTargetAndType( $target, $type );
+       }
+
+       /**
+        * 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().
+        * @return array( User|String, Block::TYPE_ constant )
+        */
+       public static function parseTarget( $target ) {
+               $target = trim( $target );
+
+               $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 ) ) {
+                       # 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(
+                               User::newFromName( IP::sanitizeIP( $target ), false ),
+                               Block::TYPE_IP
+                       );
+
+               } elseif ( IP::isValidBlock( $target ) ) {
+                       # Can't create a User from an IP range
+                       return array( IP::sanitizeRange( $target ), Block::TYPE_RANGE );
+
+               } elseif ( preg_match( '/^#\d+$/', $target ) ) {
+                       # Autoblock reference in the form "#12345"
+                       return array( substr( $target, 1 ), Block::TYPE_AUTO );
+
+               } else {
+                       # WTF?
+                       return array( null, null );
+               }
+       }
+
+       /**
+        * Get the target and target type for this particular Block.  Note that for autoblocks,
+        * 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
+        */
+       public function getTargetAndType() {
+               list( $target, $type ) = self::parseTarget( $this->mAddress );
+
+               # Check whether it's an autoblock
+               if ( $this->mAuto ) {
+                       $type = self::TYPE_AUTO;
+               }
+
+               return array( $target, $type );
+       }
+
+       public function getType() {
+               list( /*...*/, $type ) = $this->getTargetAndType();
+               return $type;
+       }
+
+       public function getTarget() {
+               list( $target, /*...*/ ) = $this->getTargetAndType();
+               return $target;
+       }
 }