Add support for Number grouping(commafy) based on CLDR number grouping patterns like...
[lhc/web/wiklou.git] / includes / Block.php
index 5afe5c5..71e128d 100644 (file)
@@ -20,7 +20,7 @@
  * @file
  */
 class Block {
-       /* public*/ var $mReason, $mTimestamp, $mAuto, $mExpiry, $mHideName, $mAngryAutoblock;
+       /* public*/ var $mReason, $mTimestamp, $mAuto, $mExpiry, $mHideName;
 
        protected
                $mId,
@@ -77,14 +77,17 @@ class Block {
                $this->mAuto = $auto;
                $this->isHardblock( !$anonOnly );
                $this->prevents( 'createaccount', $createAccount );
-               $this->mExpiry = $expiry;
+               if ( $expiry == 'infinity' || $expiry == Block::infinity() ) {
+                       $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;
        }
 
        /**
@@ -160,7 +163,6 @@ 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
         */
@@ -204,7 +206,8 @@ class Block {
                        $conds = array( 'ipb_address' => array() );
                }
 
-               # Be aware that the != '' check is explicit, since empty values will be passed by some callers.
+               # 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 ) {
@@ -349,7 +352,14 @@ class Block {
                $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 );
@@ -394,6 +404,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)
         */
@@ -428,6 +439,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" );
@@ -452,7 +466,7 @@ class Block {
                if( !$db ){
                        $db = wfGetDB( DB_SLAVE );
                }
-               $this->mExpiry = $db->encodeExpiry( $this->mExpiry );
+               $expiry = $db->encodeExpiry( $this->mExpiry );
 
                $a = array(
                        'ipb_address'          => (string)$this->target,
@@ -465,7 +479,7 @@ class Block {
                        'ipb_anon_only'        => !$this->isHardblock(),
                        'ipb_create_account'   => $this->prevents( 'createaccount' ),
                        'ipb_enable_autoblock' => $this->isAutoblocking(),
-                       'ipb_expiry'           => $this->mExpiry,
+                       'ipb_expiry'           => $expiry,
                        'ipb_range_start'      => $this->getRangeStart(),
                        'ipb_range_end'        => $this->getRangeEnd(),
                        'ipb_deleted'          => intval( $this->mHideName ), // typecast required for SQLite
@@ -484,43 +498,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;
        }
 
        /**
@@ -579,58 +601,58 @@ class Block {
                        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 > 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 )
                        ) {
-                               # If the block is an autoblock, reset its timestamp to now and its expiry
-                               # to an $wgAutoblockExpiry in the future; otherwise do nothing
+                               # Reset block timestamp to now and its expiry to
+                               # $wgAutoblockExpiry in the future
                                $ipblock->updateTimestamp();
                        }
                        return false;
-
                }
 
-               # 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" );
                $autoblock->setTarget( $autoblockIP );
                $autoblock->setBlocker( $this->getBlocker() );
                $autoblock->mReason = wfMsgForContent( 'autoblocker', $this->getTarget(), $this->mReason );
-               $autoblock->mTimestamp = wfTimestampNow();
+               $timestamp = wfTimestampNow();
+               $autoblock->mTimestamp = $timestamp;
                $autoblock->mAuto = 1;
                $autoblock->prevents( 'createaccount', $this->prevents( 'createaccount' ) );
                # Continue suppressing the name if needed
                $autoblock->mHideName = $this->mHideName;
                $autoblock->prevents( 'editownusertalk', $this->prevents( 'editownusertalk' ) );
 
-               $dbr = wfGetDB( DB_SLAVE );
-               if ( $this->mTimestamp == $dbr->getInfinity() ) {
+               if ( $this->mExpiry == 'infinity' ) {
                        # Original block was indefinite, start an autoblock now
-                       $autoblock->mExpiry = Block::getAutoblockExpiry( wfTimestampNow() );
+                       $autoblock->mExpiry = Block::getAutoblockExpiry( $timestamp );
                } else {
                        # 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( wfTimestampNow() ) );
+                       $autoblock->mExpiry = min( $this->mExpiry, Block::getAutoblockExpiry( $timestamp ) );
                }
 
-               # Insert it
+               # Insert the block...
                $status = $autoblock->insert();
                return $status
                        ? $status['id']
@@ -662,12 +684,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;
                }
        }
 
@@ -768,6 +791,8 @@ class Block {
        /**
         * Get/set the SELECT ... FOR UPDATE flag
         * @deprecated since 1.18
+        *
+        * @param $x Bool
         */
        public function forUpdate( $x = null ) {
                # noop
@@ -775,6 +800,9 @@ class Block {
 
        /**
         * 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 );
@@ -862,9 +890,9 @@ 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 ) {
                global $wgContLang;
@@ -874,6 +902,7 @@ class Block {
        /**
         * Get a timestamp of the expiry for autoblocks
         *
+        * @param $timestamp String|Int
         * @return String
         */
        public static function getAutoblockExpiry( $timestamp ) {
@@ -977,11 +1006,6 @@ class Block {
         *     not be the same as the target you gave if you used $vagueTarget!
         */
        public static function newFromTarget( $specificTarget, $vagueTarget = null, $fromMaster = false ) {
-               # (bug 29116) passing $vagueTarget = '' is not unreasonable here, but int(0)
-               # is a valid username, so we can't just use weak comparisons.
-               if( $vagueTarget === '' ){
-                       $vagueTarget = null;
-               }
 
                list( $target, $type ) = self::parseTarget( $specificTarget );
                if( $type == Block::TYPE_ID || $type == Block::TYPE_AUTO ){
@@ -989,7 +1013,8 @@ class Block {
 
                } 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.
+                       # 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 ) ) ) {
@@ -1014,6 +1039,8 @@ class Block {
         * 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 ) {
@@ -1030,14 +1057,7 @@ class Block {
                        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(
@@ -1048,6 +1068,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"