w/s fixup
[lhc/web/wiklou.git] / includes / Block.php
index a59d935..3a2fa6e 100644 (file)
@@ -59,7 +59,7 @@ class Block {
         */
        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,13 +71,17 @@ 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 );
-               if ( $expiry == 'infinity' || $expiry == Block::infinity() ) {
+               if ( $expiry == 'infinity' || $expiry == wfGetDB( DB_SLAVE )->getInfinity() ) {
                        $this->mExpiry = 'infinity';
                } else {
                        $this->mExpiry = wfTimestamp( TS_MW, $expiry );
@@ -345,14 +349,18 @@ 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;
-               
+
                // I wish I didn't have to do this
                $db = wfGetDB( DB_SLAVE );
                if ( $row->ipb_expiry == $db->getInfinity() ) {
@@ -418,7 +426,6 @@ class Block {
                # Don't collide with expired blocks
                Block::purgeExpired();
 
-               $ipb_id = $dbw->nextSequenceValue( 'ipblocks_ipb_id_seq' );
                $dbw->insert(
                        'ipblocks',
                        $this->getDatabaseArray(),
@@ -471,8 +478,8 @@ class Block {
                $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,
@@ -643,7 +650,6 @@ class Block {
                $autoblock->mHideName = $this->mHideName;
                $autoblock->prevents( 'editownusertalk', $this->prevents( 'editownusertalk' ) );
 
-                $dbr = wfGetDB( DB_SLAVE );
                if ( $this->mExpiry == 'infinity' ) {
                        # Original block was indefinite, start an autoblock now
                        $autoblock->mExpiry = Block::getAutoblockExpiry( $timestamp );
@@ -762,11 +768,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;
        }
 
@@ -776,9 +783,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
        }
 
        /**
@@ -893,7 +901,7 @@ class Block {
         * @param $expiry String: Database expiry 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;
@@ -928,7 +936,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__ );
        }
 
        /**
@@ -1058,6 +1067,19 @@ class Block {
                        return array( null, null );
                }
 
+               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(
+                               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 );
+               }
+
                # Consider the possibility that this is not a username at all
                # but actually an old subpage (bug #29797)
                if( strpos( $target, '/' ) !== false ){
@@ -1073,18 +1095,6 @@ class Block {
                        # 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 );
@@ -1136,7 +1146,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;
@@ -1144,9 +1154,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;
        }
 }