Adding new debugging toolbar
[lhc/web/wiklou.git] / includes / db / Database.php
index 239b430..3037769 100644 (file)
@@ -47,7 +47,7 @@ interface DatabaseType {
         * Fields can be retrieved with $row->fieldname, with fields acting like
         * member variables.
         *
-        * @param $res SQL result object as returned from DatabaseBase::query(), etc.
+        * @param $res ResultWrapper|object as returned from DatabaseBase::query(), etc.
         * @return Row object
         * @throws DBUnexpectedError Thrown if the database returns an error
         */
@@ -226,6 +226,8 @@ abstract class DatabaseBase implements DatabaseType {
 
        protected $preparedArgs;
 
+       protected $htmlErrors;
+
 # ------------------------------------------------------------------------------
 # Accessors
 # ------------------------------------------------------------------------------
@@ -294,7 +296,7 @@ abstract class DatabaseBase implements DatabaseType {
         *
         * @param $ignoreErrors bool|null
         *
-        * @return The previous value of the flag.
+        * @return bool The previous value of the flag.
         */
        function ignoreErrors( $ignoreErrors = null ) {
                return wfSetBit( $this->mFlags, DBO_IGNORE, $ignoreErrors );
@@ -608,21 +610,24 @@ abstract class DatabaseBase implements DatabaseType {
                }
        }
 
-       /**
-        * Called by unserialize. Needed to reopen DB connection, which
-        * is not saved by serialize.
+       /**
+        * Called by serialize. Throw an exception when DB connection is serialized.
+        * This causes problems on some database engines because the connection is
+        * not restored on unserialize.
         */
-       public function __wakeup() {
-        if ( $this->isOpen() ) {
-                       $this->open( $this->mServer, $this->mUser,
-                                    $this->mPassword, $this->mDBname);
-               }
+       public function __sleep() {
+               throw new MWException( 'Database serialization may cause problems, since the connection is not restored on wakeup.' );
        }
 
        /**
         * Same as new DatabaseMysql( ... ), kept for backward compatibility
         * @deprecated since 1.17
         *
+        * @param $server
+        * @param $user
+        * @param $password
+        * @param $dbName
+        * @param $flags int
         * @return DatabaseMysql
         */
        static function newFromParams( $server, $user, $password, $dbName, $flags = 0 ) {
@@ -694,6 +699,10 @@ abstract class DatabaseBase implements DatabaseType {
                }
        }
 
+       /**
+        * @param $errno
+        * @param $errstr
+        */
        protected function connectionErrorHandler( $errno,  $errstr ) {
                $this->mPHPError = $errstr;
        }
@@ -809,7 +818,7 @@ abstract class DatabaseBase implements DatabaseType {
                        # that would delay transaction initializations to once connection
                        # is really used by application
                        $sqlstart = substr( $sql, 0, 10 ); // very much worth it, benchmark certified(tm)
-                       if ( strpos( $sqlstart, "SHOW " ) !== 0 and strpos( $sqlstart, "SET " ) !== 0 )
+                       if ( strpos( $sqlstart, "SHOW " ) !== 0 && strpos( $sqlstart, "SET " ) !== 0 )
                                $this->begin();
                }
 
@@ -831,9 +840,13 @@ abstract class DatabaseBase implements DatabaseType {
                        throw new MWException( 'Tainted query found' );
                }
 
+               $queryId = MWDebug::query( $sql, $fname, $isMaster );
+
                # Do the query and handle errors
                $ret = $this->doQuery( $commentedSql );
 
+               MWDebug::queryTime( $queryId );
+
                # Try reconnecting if the connection was lost
                if ( false === $ret && $this->wasErrorReissuable() ) {
                        # Transaction is gone, like it or not
@@ -921,6 +934,7 @@ abstract class DatabaseBase implements DatabaseType {
 
        /**
         * Free a prepared query, generated by prepare().
+        * @param $prepared
         */
        function freePrepared( $prepared ) {
                /* No-op by default */
@@ -1530,21 +1544,24 @@ abstract class DatabaseBase implements DatabaseType {
         * Query whether a given table exists
         *
         * @param $table string
+        * @param $fname string
         *
         * @return bool
         */
-       function tableExists( $table ) {
+       function tableExists( $table, $fname = __METHOD__ ) {
                $table = $this->tableName( $table );
                $old = $this->ignoreErrors( true );
-               $res = $this->query( "SELECT 1 FROM $table LIMIT 1", __METHOD__ );
+               $res = $this->query( "SELECT 1 FROM $table LIMIT 1", $fname );
                $this->ignoreErrors( $old );
 
                return (bool)$res;
        }
 
        /**
-        * @todo document
         * mysql_field_type() wrapper
+        * @param $res
+        * @param $index
+        * @return string
         */
        function fieldType( $res, $index ) {
                if ( $res instanceof ResultWrapper ) {
@@ -2259,6 +2276,12 @@ abstract class DatabaseBase implements DatabaseType {
         * Returns an appropriately quoted sequence value for inserting a new row.
         * MySQL has autoincrement fields, so this is just NULL. But the PostgreSQL
         * subclass will return an integer, and save the value for insertId()
+        *
+        * Any implementation of this function should *not* involve reusing
+        * sequence numbers created for rolled-back transactions.
+        * See http://bugs.mysql.com/bug.php?id=30767 for details.
+        * @param $seqName
+        * @return null
         */
        function nextSequenceValue( $seqName ) {
                return null;
@@ -2271,6 +2294,8 @@ abstract class DatabaseBase implements DatabaseType {
         * which index to pick.  Anyway, other databases might have different
         * indexes on a given table.  So don't bother overriding this unless you're
         * MySQL.
+        * @param $index
+        * @return string
         */
        function useIndexClause( $index ) {
                return '';
@@ -2533,7 +2558,10 @@ abstract class DatabaseBase implements DatabaseType {
                        " FROM $srcTable $useIndex ";
 
                if ( $conds != '*' ) {
-                       $sql .= ' WHERE ' . $this->makeList( $conds, LIST_AND );
+                       if ( is_array( $conds ) ) {
+                               $conds = $this->makeList( $conds, LIST_AND );
+                       }
+                       $sql .= " WHERE $conds";
                }
 
                $sql .= " $tailOpts";
@@ -2629,6 +2657,16 @@ abstract class DatabaseBase implements DatabaseType {
                return "REPLACE({$orig}, {$old}, {$new})";
        }
 
+       /**
+        * Determines how long the server has been up
+        * STUB
+        *
+        * @return int
+        */
+       function getServerUptime() {
+               return 0;
+       }
+
        /**
         * Determines if the last failure was due to a deadlock
         * STUB
@@ -2639,6 +2677,16 @@ abstract class DatabaseBase implements DatabaseType {
                return false;
        }
 
+       /**
+        * Determines if the last failure was due to a lock timeout
+        * STUB
+        *
+        * @return bool
+        */
+       function wasLockTimeout() {
+               return false;
+       }
+
        /**
         * Determines if the last query error was something that should be dealt
         * with by pinging the connection and reissuing the query.
@@ -2905,7 +2953,7 @@ abstract class DatabaseBase implements DatabaseType {
         *
         * @param $result bool|ResultWrapper
         *
-        * @param bool|ResultWrapper
+        * @return bool|ResultWrapper
         */
        function resultObject( $result ) {
                if ( empty( $result ) ) {
@@ -2969,6 +3017,8 @@ abstract class DatabaseBase implements DatabaseType {
         * don't allow simple quoted strings to be inserted. To insert into such
         * a field, pass the data through this function before passing it to
         * DatabaseBase::insert().
+        * @param $b
+        * @return
         */
        function encodeBlob( $b ) {
                return $b;
@@ -2978,20 +3028,35 @@ abstract class DatabaseBase implements DatabaseType {
         * Some DBMSs return a special placeholder object representing blob fields
         * in result objects. Pass the object through this function to return the
         * original string.
+        * @param $b
+        * @return
         */
        function decodeBlob( $b ) {
                return $b;
        }
 
        /**
-        * Override database's default connection timeout.  May be useful for very
-        * long batch queries such as full-wiki dumps, where a single query reads
-        * out over hours or days.  May or may not be necessary for non-MySQL
-        * databases.  For most purposes, leaving it as a no-op should be fine.
+        * Override database's default connection timeout
         *
         * @param $timeout Integer in seconds
+        * @return void
+        * @deprecated since 1.19; use setSessionOptions()
+        */
+       public function setTimeout( $timeout ) {
+               $this->setSessionOptions( array( 'connTimeout' => $timeout ) );
+       }
+
+       /**
+        * Override database's default behavior. $options include:
+        *     'connTimeout' : Set the connection timeout value in seconds.
+        *                     May be useful for very long batch queries such as
+        *                     full-wiki dumps, where a single query reads out over
+        *                     hours or days.
+        *
+        * @param $options Array
+        * @return void
         */
-       public function setTimeout( $timeout ) {}
+       public function setSessionOptions( array $options ) {}
 
        /**
         * Read and execute SQL commands from a file.
@@ -3004,6 +3069,7 @@ abstract class DatabaseBase implements DatabaseType {
         * @param $resultCallback Callback: Optional function called for each MySQL result
         * @param $fname String: Calling function name or false if name should be
         *      generated dynamically using $filename
+        * @return bool|string
         */
        function sourceFile( $filename, $lineCallback = false, $resultCallback = false, $fname = false ) {
                wfSuppressWarnings();
@@ -3071,6 +3137,7 @@ abstract class DatabaseBase implements DatabaseType {
         * @param $lineCallback Callback: Optional function called before reading each line
         * @param $resultCallback Callback: Optional function called for each MySQL result
         * @param $fname String: Calling function name
+        * @return bool|string
         */
        function sourceStream( $fp, $lineCallback = false, $resultCallback = false,
                $fname = 'DatabaseBase::sourceStream' )
@@ -3194,6 +3261,8 @@ abstract class DatabaseBase implements DatabaseType {
        /**
         * Get schema variables. If none have been set via setSchemaVars(), then
         * use some defaults from the current object.
+        *
+        * @return array
         */
        protected function getSchemaVars() {
                if ( $this->mSchemaVars ) {
@@ -3305,9 +3374,10 @@ abstract class DatabaseBase implements DatabaseType {
         * @param $tableName string
         * @param $fName string
         * @return bool|ResultWrapper
+        * @since 1.18
         */
        public function dropTable( $tableName, $fName = 'DatabaseBase::dropTable' ) {
-               if( !$this->tableExists( $tableName ) ) {
+               if( !$this->tableExists( $tableName, $fName ) ) {
                        return false;
                }
                $sql = "DROP TABLE " . $this->tableName( $tableName );