(bug 16019) Make WebRequest::interpolateTitle() do nothing when run from api.php
[lhc/web/wiklou.git] / includes / Block.php
index 8e96e09..17ebeb2 100644 (file)
@@ -25,7 +25,7 @@ class Block {
 
        function __construct( $address = '', $user = 0, $by = 0, $reason = '',
                $timestamp = '' , $auto = 0, $expiry = '', $anonOnly = 0, $createAccount = 0, $enableAutoblock = 0,
-               $hideName = 0, $blockEmail = 0, $allowUsertalk = 0 )
+               $hideName = 0, $blockEmail = 0, $allowUsertalk = 0, $byName = false )
        {
                $this->mId = 0;
                # Expand valid IPv6 addresses
@@ -34,7 +34,7 @@ class Block {
                $this->mUser = $user;
                $this->mBy = $by;
                $this->mReason = $reason;
-               $this->mTimestamp = wfTimestamp(TS_MW,$timestamp);
+               $this->mTimestamp = wfTimestamp( TS_MW, $timestamp );
                $this->mAuto = $auto;
                $this->mAnonOnly = $anonOnly;
                $this->mCreateAccount = $createAccount;
@@ -45,7 +45,7 @@ class Block {
                $this->mAllowUsertalk = $allowUsertalk;
                $this->mForUpdate = false;
                $this->mFromMaster = false;
-               $this->mByName = false;
+               $this->mByName = $byName;
                $this->mAngryAutoblock = false;
                $this->initialiseRange();
        }
@@ -54,7 +54,7 @@ class Block {
         * Load a block from the database, using either the IP address or
         * user ID. Tries the user ID first, and if that doesn't work, tries
         * the address.
-        * 
+        *
         * @param $address String: IP address of user/anon
         * @param $user Integer: user id of user
         * @param $killExpired Boolean: delete expired blocks on load
@@ -63,6 +63,7 @@ class Block {
        public static function newFromDB( $address, $user = 0, $killExpired = true ) {
                $block = new Block;
                $block->load( $address, $user, $killExpired );
+
                if ( $block->isValid() ) {
                        return $block;
                } else {
@@ -81,6 +82,7 @@ class Block {
                $res = $dbr->resultObject( $dbr->select( 'ipblocks', '*',
                        array( 'ipb_id' => $id ), __METHOD__ ) );
                $block = new Block;
+
                if ( $block->loadFromResult( $res ) ) {
                        return $block;
                } else {
@@ -130,6 +132,7 @@ class Block {
         */
        protected function &getDBOptions( &$options ) {
                global $wgAntiLockFlags;
+
                if ( $this->mForUpdate || $this->mFromMaster ) {
                        $db = wfGetDB( DB_MASTER );
                        if ( !$this->mForUpdate || ( $wgAntiLockFlags & ALF_NO_BLOCK_LOCK ) ) {
@@ -141,6 +144,7 @@ class Block {
                        $db = wfGetDB( DB_SLAVE );
                        $options = array();
                }
+
                return $db;
        }
 
@@ -157,11 +161,12 @@ class Block {
                wfDebug( "Block::load: '$address', '$user', $killExpired\n" );
 
                $options = array();
-               $db =& $this->getDBOptions( $options );
+               $db = $this->getDBOptions( $options );
 
-               if ( 0 == $user && $address == '' ) {
+               if ( 0 == $user && $address === '' ) {
                        # Invalid user specification, not blocked
                        $this->clear();
+
                        return false;
                }
 
@@ -169,6 +174,7 @@ class Block {
                if ( $user ) {
                        $res = $db->resultObject( $db->select( 'ipblocks', '*', array( 'ipb_user' => $user ),
                                __METHOD__, $options ) );
+
                        if ( $this->loadFromResult( $res, $killExpired ) ) {
                                return true;
                        }
@@ -177,17 +183,19 @@ class Block {
                # Try IP block
                # TODO: improve performance by merging this query with the autoblock one
                # Slightly tricky while handling killExpired as well
-               if ( $address ) {
+               if ( $address !== '' ) {
                        $conds = array( 'ipb_address' => $address, 'ipb_auto' => 0 );
                        $res = $db->resultObject( $db->select( 'ipblocks', '*', $conds, __METHOD__, $options ) );
+
                        if ( $this->loadFromResult( $res, $killExpired ) ) {
                                if ( $user && $this->mAnonOnly ) {
                                        # Block is marked anon-only
                                        # Whitelist this IP address against autoblocks and range blocks
                                        # (but not account creation blocks -- bug 13611)
-                                       if( !$this->mCreateAccount ) {
+                                       if ( !$this->mCreateAccount ) {
                                                $this->clear();
                                        }
+
                                        return false;
                                } else {
                                        return true;
@@ -199,9 +207,10 @@ class Block {
                if ( $this->loadRange( $address, $killExpired, $user ) ) {
                        if ( $user && $this->mAnonOnly ) {
                                # Respect account creation blocks on logged-in users -- bug 13611
-                               if( !$this->mCreateAccount ) {
+                               if ( !$this->mCreateAccount ) {
                                        $this->clear();
                                }
+
                                return false;
                        } else {
                                return true;
@@ -211,10 +220,13 @@ class Block {
                # Try autoblock
                if ( $address ) {
                        $conds = array( 'ipb_address' => $address, 'ipb_auto' => 1 );
+
                        if ( $user ) {
                                $conds['ipb_anon_only'] = 0;
                        }
+
                        $res = $db->resultObject( $db->select( 'ipblocks', '*', $conds, __METHOD__, $options ) );
+
                        if ( $this->loadFromResult( $res, $killExpired ) ) {
                                return true;
                        }
@@ -234,6 +246,7 @@ class Block {
         */
        protected function loadFromResult( ResultWrapper $res, $killExpired = true ) {
                $ret = false;
+
                if ( 0 != $res->numRows() ) {
                        # Get first block
                        $row = $res->fetchObject();
@@ -260,6 +273,7 @@ class Block {
                        }
                }
                $res->free();
+
                return $ret;
        }
 
@@ -269,11 +283,12 @@ 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 ) {
                $iaddr = IP::toHex( $address );
+
                if ( $iaddr === false ) {
                        # Invalid address
                        return false;
@@ -284,7 +299,7 @@ class Block {
                $range = substr( $iaddr, 0, 4 );
 
                $options = array();
-               $db =& $this->getDBOptions( $options );
+               $db = $this->getDBOptions( $options );
                $conds = array(
                        'ipb_range_start' . $db->buildLike( $range, $db->anyString() ),
                        "ipb_range_start <= '$iaddr'",
@@ -297,6 +312,7 @@ class Block {
 
                $res = $db->resultObject( $db->select( 'ipblocks', '*', $conds, __METHOD__, $options ) );
                $success = $this->loadFromResult( $res, $killExpired );
+
                return $success;
        }
 
@@ -309,7 +325,7 @@ class Block {
        public function initFromRow( $row ) {
                $this->mAddress = $row->ipb_address;
                $this->mReason = $row->ipb_reason;
-               $this->mTimestamp = wfTimestamp(TS_MW,$row->ipb_timestamp);
+               $this->mTimestamp = wfTimestamp( TS_MW, $row->ipb_timestamp );
                $this->mUser = $row->ipb_user;
                $this->mBy = $row->ipb_by;
                $this->mAuto = $row->ipb_auto;
@@ -321,17 +337,19 @@ class Block {
                $this->mHideName = $row->ipb_deleted;
                $this->mId = $row->ipb_id;
                $this->mExpiry = self::decodeExpiry( $row->ipb_expiry );
+
                if ( isset( $row->user_name ) ) {
                        $this->mByName = $row->user_name;
                } else {
                        $this->mByName = $row->ipb_by_text;
                }
+
                $this->mRangeStart = $row->ipb_range_start;
                $this->mRangeEnd = $row->ipb_range_end;
        }
 
        /**
-        * Once $mAddress has been set, get the range they came from. 
+        * Once $mAddress has been set, get the range they came from.
         * Wrapper for IP::parseRange
         */
        protected function initialiseRange() {
@@ -352,12 +370,14 @@ class Block {
                if ( wfReadOnly() ) {
                        return false;
                }
+
                if ( !$this->mId ) {
                        throw new MWException( "Block::delete() now requires that the mId member be filled\n" );
                }
 
                $dbw = wfGetDB( DB_MASTER );
                $dbw->delete( 'ipblocks', array( 'ipb_id' => $this->mId ), __METHOD__ );
+
                return $dbw->affectedRows() > 0;
        }
 
@@ -367,9 +387,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();
@@ -377,8 +399,9 @@ class Block {
                # Don't collide with expired blocks
                Block::purgeExpired();
 
-               $ipb_id = $dbw->nextSequenceValue('ipblocks_ipb_id_seq');
-               $dbw->insert( 'ipblocks',
+               $ipb_id = $dbw->nextSequenceValue( 'ipblocks_ipb_id_seq' );
+               $dbw->insert(
+                       'ipblocks',
                        array(
                                'ipb_id' => $ipb_id,
                                'ipb_address' => $this->mAddress,
@@ -386,7 +409,7 @@ class Block {
                                'ipb_by' => $this->mBy,
                                'ipb_by_text' => $this->mByName,
                                'ipb_reason' => $this->mReason,
-                               'ipb_timestamp' => $dbw->timestamp($this->mTimestamp),
+                               'ipb_timestamp' => $dbw->timestamp( $this->mTimestamp ),
                                'ipb_auto' => $this->mAuto,
                                'ipb_anon_only' => $this->mAnonOnly,
                                'ipb_create_account' => $this->mCreateAccount,
@@ -394,10 +417,12 @@ class Block {
                                'ipb_expiry' => self::encodeExpiry( $this->mExpiry, $dbw ),
                                'ipb_range_start' => $this->mRangeStart,
                                'ipb_range_end' => $this->mRangeEnd,
-                               'ipb_deleted'   => $this->mHideName,
+                               'ipb_deleted'   => intval( $this->mHideName ), // typecast required for SQLite
                                'ipb_block_email' => $this->mBlockEmail,
                                'ipb_allow_usertalk' => $this->mAllowUsertalk
-                       ), 'Block::insert', array( 'IGNORE' )
+                       ),
+                       'Block::insert',
+                       array( 'IGNORE' )
                );
                $affected = $dbw->affectedRows();
 
@@ -417,13 +442,14 @@ class Block {
 
                $this->validateBlockParams();
 
-               $dbw->update( 'ipblocks',
+               $dbw->update(
+                       'ipblocks',
                        array(
                                'ipb_user' => $this->mUser,
                                'ipb_by' => $this->mBy,
                                'ipb_by_text' => $this->mByName,
                                'ipb_reason' => $this->mReason,
-                               'ipb_timestamp' => $dbw->timestamp($this->mTimestamp),
+                               'ipb_timestamp' => $dbw->timestamp( $this->mTimestamp ),
                                'ipb_auto' => $this->mAuto,
                                'ipb_anon_only' => $this->mAnonOnly,
                                'ipb_create_account' => $this->mCreateAccount,
@@ -433,13 +459,15 @@ class Block {
                                'ipb_range_end' => $this->mRangeEnd,
                                'ipb_deleted'   => $this->mHideName,
                                'ipb_block_email' => $this->mBlockEmail,
-                               'ipb_allow_usertalk' => $this->mAllowUsertalk ),
+                               'ipb_allow_usertalk' => $this->mAllowUsertalk
+                       ),
                        array( 'ipb_id' => $this->mId ),
-                       'Block::update' );
+                       'Block::update'
+               );
 
                return $dbw->affectedRows();
        }
-       
+
        /**
         * Make sure all the proper members are set to sane values
         * before adding/updating a block
@@ -459,8 +487,9 @@ class Block {
                if ( !$this->mUser && $this->mAnonOnly ) {
                        $this->mBlockEmail = 0;
                }
-               if( !$this->mByName ) {
-                       if( $this->mBy ) {
+
+               if ( !$this->mByName ) {
+                       if ( $this->mBy ) {
                                $this->mByName = User::whoIs( $this->mBy );
                        } else {
                                global $wgUser;
@@ -481,7 +510,7 @@ class Block {
                # - stolen shamelessly from CheckUser_body.php
 
                if ( $this->mEnableAutoblock && $this->mUser ) {
-                       wfDebug("Doing retroactive autoblocks for " . $this->mAddress . "\n");
+                       wfDebug( "Doing retroactive autoblocks for " . $this->mAddress . "\n" );
 
                        $options = array( 'ORDER BY' => 'rc_timestamp DESC' );
                        $conds = array( 'rc_user_text' => $this->mAddress );
@@ -500,11 +529,12 @@ class Block {
 
                        if ( !$dbr->numRows( $res ) ) {
                                # No results, don't autoblock anything
-                               wfDebug("No IP found to retroactively autoblock\n");
+                               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 );
+                                       }
                                }
                        }
                }
@@ -528,9 +558,9 @@ class Block {
                        $wgMemc->set( $key, $lines, 3600 * 24 );
                }
 
-               wfDebug("Checking the autoblock whitelist..\n");
+               wfDebug( "Checking the autoblock whitelist..\n" );
 
-               foreach( $lines as $line ) {
+               foreach ( $lines as $line ) {
                        # List items only
                        if ( substr( $line, 0, 1 ) !== '*' ) {
                                continue;
@@ -539,11 +569,11 @@ class Block {
                        $wlEntry = substr( $line, 1 );
                        $wlEntry = trim( $wlEntry );
 
-                       wfDebug("Checking $ip against $wlEntry...");
+                       wfDebug( "Checking $ip against $wlEntry..." );
 
                        # Is the IP in this range?
                        if ( IP::isInRange( $ip, $wlEntry ) ) {
-                               wfDebug(" IP $ip matches $wlEntry, not autoblocking\n");
+                               wfDebug( " IP $ip matches $wlEntry, not autoblocking\n" );
                                return true;
                        } else {
                                wfDebug( " No match\n" );
@@ -570,8 +600,8 @@ class Block {
                if ( Block::isWhitelistedFromAutoblocks( $autoblockIP ) ) {
                        return;
                }
-               
-               ## 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;
@@ -585,13 +615,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;
@@ -610,13 +643,15 @@ 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 ) {
+               if ( $this->mExpiry ) {
                        $ipblock->mExpiry = min( $this->mExpiry, Block::getAutoblockExpiry( $this->mTimestamp ) );
                } else {
                        $ipblock->mExpiry = Block::getAutoblockExpiry( $this->mTimestamp );
                }
+
                # Insert it
                return $ipblock->insert();
        }
@@ -627,6 +662,7 @@ class Block {
         */
        public function deleteIfExpired() {
                wfProfileIn( __METHOD__ );
+
                if ( $this->isExpired() ) {
                        wfDebug( "Block::deleteIfExpired() -- deleting\n" );
                        $this->delete();
@@ -635,6 +671,7 @@ class Block {
                        wfDebug( "Block::deleteIfExpired() -- not expired\n" );
                        $retVal = false;
                }
+
                wfProfileOut( __METHOD__ );
                return $retVal;
        }
@@ -645,6 +682,7 @@ class Block {
         */
        public function isExpired() {
                wfDebug( "Block::isExpired() checking current " . wfTimestampNow() . " vs $this->mExpiry\n" );
+
                if ( !$this->mExpiry ) {
                        return false;
                } else {
@@ -661,7 +699,7 @@ class Block {
        }
 
        /**
-        * Update the timestamp on autoblocks. 
+        * Update the timestamp on autoblocks.
         */
        public function updateTimestamp() {
                if ( $this->mAuto ) {
@@ -727,7 +765,7 @@ class Block {
        /**
         * Encode expiry for DB
         *
-        * @param $expiry String: timestamp for expiry, or 
+        * @param $expiry String: timestamp for expiry, or
         * @param $db Database object
         * @return String
         */
@@ -761,6 +799,7 @@ class Block {
         */
        public static function getAutoblockExpiry( $timestamp ) {
                global $wgAutoblockExpiry;
+
                return wfTimestamp( TS_MW, wfTimestamp( TS_UNIX, $timestamp ) + $wgAutoblockExpiry );
        }
 
@@ -776,7 +815,7 @@ class Block {
                        // IPv6
                        if ( IP::isIPv6( $range ) && $parts[1] >= 64 && $parts[1] <= 128 ) {
                                $bits = $parts[1];
-                               $ipint = IP::toUnsigned6( $parts[0] );
+                               $ipint = IP::toUnsigned( $parts[0] );
                                # Native 32 bit functions WON'T work here!!!
                                # Convert to a padded binary number
                                $network = wfBaseConvert( $ipint, 10, 2, 128 );
@@ -796,6 +835,7 @@ class Block {
                                $range = "$newip/{$parts[1]}";
                        }
                }
+
                return $range;
        }
 
@@ -809,7 +849,7 @@ 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 
+        * is desired. In principle this could be DBMS-dependant, but currently all
         * supported DBMS's support the string "infinity", so we just use that.
         *
         * @return String
@@ -817,6 +857,16 @@ class Block {
        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.
+
+               # BEGIN DatabaseMssql hack
+               # Since MSSQL doesn't recognize the infinity keyword, set date manually.
+               # TO-DO: Refactor for better DB portability and remove magic date
+               $dbr = wfGetDB( DB_SLAVE );
+               if ( $dbr->getType() == 'mssql' ) {
+                       return '3000-01-31 00:00:00.000';
+               }
+               # End DatabaseMssql hack
+
                return 'infinity';
        }
 
@@ -829,10 +879,11 @@ class Block {
        public static function formatExpiry( $encoded_expiry ) {
                static $msg = null;
 
-               if( is_null( $msg ) ) {
+               if ( is_null( $msg ) ) {
                        $msg = array();
                        $keys = array( 'infiniteblock', 'expiringblock' );
-                       foreach( $keys as $key ) {
+
+                       foreach ( $keys as $key ) {
                                $msg[$key] = wfMsgHtml( $key );
                        }
                }
@@ -846,6 +897,7 @@ class Block {
                        $expiretimestr = htmlspecialchars( $wgLang->time( $expiry, true ) );
                        $expirystr = wfMsgReplaceArgs( $msg['expiringblock'], array( $expiredatestr, $expiretimestr ) );
                }
+
                return $expirystr;
        }
 
@@ -859,11 +911,12 @@ class Block {
                        $expiry = 'infinity';
                } else {
                        $expiry = strtotime( $expiry_input );
+
                        if ( $expiry < 0 || $expiry === false ) {
                                return false;
                        }
                }
+
                return $expiry;
        }
-
 }