Merge "AJAXify watchlist editor"
[lhc/web/wiklou.git] / includes / Block.php
index c153335..301a45a 100644 (file)
@@ -28,11 +28,15 @@ class Block {
 
                $mBlockEmail,
                $mDisableUsertalk,
-               $mCreateAccount;
+               $mCreateAccount,
+               $mParentBlockId;
 
        /// @var User|String
        protected $target;
 
+       // @var Integer Hack for foreign blocking (CentralAuth)
+       protected $forcedTargetID;
+
        /// @var Block::TYPE_ constant.  Can only be USER, IP or RANGE internally
        protected $type;
 
@@ -71,6 +75,9 @@ class Block {
                }
 
                $this->setTarget( $address );
+               if ( $this->target instanceof User && $user ) {
+                       $this->forcedTargetID = $user; // needed for foreign users
+               }
                if ( $by ) { // local user
                        $this->setBlocker( User::newFromID( $by ) );
                } else { // foreign user
@@ -105,6 +112,7 @@ class Block {
         * @deprecated since 1.18
         */
        public static function newFromDB( $address, $user = 0 ) {
+               wfDeprecated( __METHOD__, '1.18' );
                return self::newFromTarget( User::whoIs( $user ), $address );
        }
 
@@ -118,17 +126,42 @@ class Block {
                $dbr = wfGetDB( DB_SLAVE );
                $res = $dbr->selectRow(
                        'ipblocks',
-                       '*',
+                       self::selectFields(),
                        array( 'ipb_id' => $id ),
                        __METHOD__
                );
                if ( $res ) {
-                       return Block::newFromRow( $res );
+                       return self::newFromRow( $res );
                } else {
                        return null;
                }
        }
 
+       /**
+        * Return the list of ipblocks fields that should be selected to create
+        * a new block.
+        * @return array
+        */
+       public static function selectFields() {
+               return array(
+                       'ipb_id',
+                       'ipb_address',
+                       'ipb_by',
+                       'ipb_by_text',
+                       'ipb_reason',
+                       'ipb_timestamp',
+                       'ipb_auto',
+                       'ipb_anon_only',
+                       'ipb_create_account',
+                       'ipb_enable_autoblock',
+                       'ipb_expiry',
+                       'ipb_deleted',
+                       'ipb_block_email',
+                       'ipb_allow_usertalk',
+                       'ipb_parent_block_id',
+               );
+       }
+
        /**
         * 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   *
@@ -159,6 +192,7 @@ class Block {
         * @deprecated since 1.18
         */
        public function clear() {
+               wfDeprecated( __METHOD__, '1.18' );
                # Noop
        }
 
@@ -171,7 +205,7 @@ class Block {
         * @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 );
@@ -238,7 +272,7 @@ class Block {
                        }
                }
 
-               $res = $db->select( 'ipblocks', '*', $conds, __METHOD__ );
+               $res = $db->select( 'ipblocks', self::selectFields(), $conds, __METHOD__ );
 
                # This result could contain a block on the user, a block on the IP, and a russian-doll
                # set of rangeblocks.  We want to choose the most specific one, so keep a leader board.
@@ -251,7 +285,7 @@ class Block {
                $bestBlockPreventsEdit = null;
 
                foreach( $res as $row ){
-                       $block = Block::newFromRow( $row );
+                       $block = self::newFromRow( $row );
 
                        # Don't use expired blocks
                        if( $block->deleteIfExpired() ){
@@ -360,6 +394,7 @@ class Block {
                $this->mAuto = $row->ipb_auto;
                $this->mHideName = $row->ipb_deleted;
                $this->mId = $row->ipb_id;
+               $this->mParentBlockId = $row->ipb_parent_block_id;
 
                // I wish I didn't have to do this
                $db = wfGetDB( DB_SLAVE );
@@ -403,6 +438,7 @@ class Block {
                }
 
                $dbw = wfGetDB( DB_MASTER );
+               $dbw->delete( 'ipblocks', array( 'ipb_parent_block_id' => $this->getId() ), __METHOD__ );
                $dbw->delete( 'ipblocks', array( 'ipb_id' => $this->getId() ), __METHOD__ );
 
                return $dbw->affectedRows() > 0;
@@ -428,7 +464,7 @@ class Block {
 
                $row = $this->getDatabaseArray();
                $row['ipb_id'] = $dbw->nextSequenceValue("ipblocks_ipb_id_seq");
-               
+
                $dbw->insert(
                        'ipblocks',
                        $row,
@@ -478,9 +514,15 @@ class Block {
                }
                $expiry = $db->encodeExpiry( $this->mExpiry );
 
+               if ( $this->forcedTargetID ) {
+                       $uid = $this->forcedTargetID;
+               } else {
+                       $uid = $this->target instanceof User ? $this->target->getID() : 0;
+               }
+
                $a = array(
                        'ipb_address'          => (string)$this->target,
-                       'ipb_user'             => $this->target instanceof User ? $this->target->getID() : 0,
+                       'ipb_user'             => $uid,
                        'ipb_by'               => $this->getBy(),
                        'ipb_by_text'          => $this->getByName(),
                        'ipb_reason'           => $this->mReason,
@@ -494,7 +536,8 @@ class Block {
                        'ipb_range_end'        => $this->getRangeEnd(),
                        'ipb_deleted'          => intval( $this->mHideName ), // typecast required for SQLite
                        'ipb_block_email'      => $this->prevents( 'sendemail' ),
-                       'ipb_allow_usertalk'   => !$this->prevents( 'editownusertalk' )
+                       'ipb_allow_usertalk'   => !$this->prevents( 'editownusertalk' ),
+                       'ipb_parent_block_id'            => $this->mParentBlockId
                );
 
                return $a;
@@ -652,6 +695,7 @@ class Block {
                # Continue suppressing the name if needed
                $autoblock->mHideName = $this->mHideName;
                $autoblock->prevents( 'editownusertalk', $this->prevents( 'editownusertalk' ) );
+               $autoblock->mParentBlockId = $this->mId;
 
                if ( $this->mExpiry == 'infinity' ) {
                        # Original block was indefinite, start an autoblock now
@@ -807,6 +851,7 @@ class Block {
         * @param $x Bool
         */
        public function forUpdate( $x = null ) {
+               wfDeprecated( __METHOD__, '1.18' );
                # noop
        }
 
@@ -890,11 +935,12 @@ class Block {
         * Encode expiry for DB
         *
         * @param $expiry String: timestamp for expiry, or
-        * @param $db Database object
+        * @param $db DatabaseBase object
         * @return String
         * @deprecated since 1.18; use $dbw->encodeExpiry() instead
         */
        public static function encodeExpiry( $expiry, $db ) {
+               wfDeprecated( __METHOD__, '1.18' );
                return $db->encodeExpiry( $expiry );
        }
 
@@ -907,6 +953,7 @@ class Block {
         * @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 );
        }
@@ -931,6 +978,7 @@ class Block {
         * @deprecated since 1.18, call IP::sanitizeRange() directly
         */
        public static function normaliseRange( $range ) {
+               wfDeprecated( __METHOD__, '1.18' );
                return IP::sanitizeRange( $range );
        }
 
@@ -950,6 +998,7 @@ class Block {
         * @return String
         */
        public static function infinity() {
+               wfDeprecated( __METHOD__, '1.18' );
                return wfGetDB( DB_SLAVE )->getInfinity();
        }
 
@@ -961,6 +1010,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;
 
@@ -994,7 +1045,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 );
        }
 
@@ -1030,7 +1081,7 @@ class Block {
                        # 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 );
 
@@ -1040,12 +1091,9 @@ class Block {
 
                        if( $block->newLoad( $vagueTarget ) ){
                                return $block;
-                       } else {
-                               return null;
                        }
-               } else {
-                       return null;
                }
+               return null;
        }
 
        /**
@@ -1057,8 +1105,6 @@ class Block {
         * @return array( User|String, Block::TYPE_ constant )
         */
        public static function parseTarget( $target ) {
-               $target = trim( $target );
-
                # We may have been through this before
                if( $target instanceof User ){
                        if( IP::isValid( $target->getName() ) ){
@@ -1070,6 +1116,8 @@ class Block {
                        return array( null, null );
                }
 
+               $target = trim( $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)