Make sure all functions in Database.php are documented
[lhc/web/wiklou.git] / includes / libs / rdbms / database / Database.php
index 0915b7d..ee7644f 100644 (file)
@@ -416,6 +416,13 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware
                return $conn;
        }
 
+       /**
+        * Set the PSR-3 logger interface to use for query logging. (The logger
+        * interfaces for connection logging and error logging can be set with the
+        * constructor.)
+        *
+        * @param LoggerInterface $logger
+        */
        public function setLogger( LoggerInterface $logger ) {
                $this->queryLogger = $logger;
        }
@@ -576,6 +583,12 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware
                return $this->mTrxLevel ? $this->mTrxWriteCallers : [];
        }
 
+       /**
+        * Get the list of method names that have pending write queries or callbacks
+        * for this transaction
+        *
+        * @return array
+        */
        protected function pendingWriteAndCallbackCallers() {
                if ( !$this->mTrxLevel ) {
                        return [];
@@ -664,6 +677,9 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware
         */
        abstract function strencode( $s );
 
+       /**
+        * Set a custom error handler for logging errors during database connection
+        */
        protected function installErrorHandler() {
                $this->mPHPError = false;
                $this->htmlErrors = ini_set( 'html_errors', '0' );
@@ -671,6 +687,8 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware
        }
 
        /**
+        * Restore the previous error handler and return the last PHP error for this DB
+        *
         * @return bool|string
         */
        protected function restoreErrorHandler() {
@@ -697,6 +715,7 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware
        }
 
        /**
+        * Error handler for logging errors during database connection
         * This method should not be used outside of Database classes
         *
         * @param int $errno
@@ -817,7 +836,7 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware
 
        /**
         * @param string $sql A SQL query
-        * @return bool Whether $sql is SQL for creating/dropping a new TEMPORARY table
+        * @return bool Whether $sql is SQL for TEMPORARY table operation
         */
        protected function registerTempTableOperation( $sql ) {
                if ( preg_match(
@@ -833,9 +852,16 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware
                        $sql,
                        $matches
                ) ) {
+                       $isTemp = isset( $this->mSessionTempTables[$matches[1]] );
                        unset( $this->mSessionTempTables[$matches[1]] );
 
-                       return true;
+                       return $isTemp;
+               } elseif ( preg_match(
+                       '/^TRUNCATE\s+(?:TEMPORARY\s+)?TABLE\s+(?:IF\s+EXISTS\s+)?[`"\']?(\w+)[`"\']?/i',
+                       $sql,
+                       $matches
+               ) ) {
+                       return isset( $this->mSessionTempTables[$matches[1]] );
                } elseif ( preg_match(
                        '/^(?:INSERT\s+(?:\w+\s+)?INTO|UPDATE|DELETE\s+FROM)\s+[`"\']?(\w+)[`"\']?/i',
                        $sql,
@@ -851,8 +877,16 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware
                $priorWritesPending = $this->writesOrCallbacksPending();
                $this->mLastQuery = $sql;
 
-               $isWrite = $this->isWriteQuery( $sql ) && !$this->registerTempTableOperation( $sql );
+               $isWrite = $this->isWriteQuery( $sql );
                if ( $isWrite ) {
+                       $isNonTempWrite = !$this->registerTempTableOperation( $sql );
+               } else {
+                       $isNonTempWrite = false;
+               }
+
+               if ( $isWrite ) {
+                       # In theory, non-persistent writes are allowed in read-only mode, but due to things
+                       # like https://bugs.mysql.com/bug.php?id=33669 that might not work anyway...
                        $reason = $this->getReadOnlyReason();
                        if ( $reason !== false ) {
                                throw new DBReadOnlyError( $this, "Database is read-only: $reason" );
@@ -861,8 +895,8 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware
                        $this->mLastWriteTime = microtime( true );
                }
 
-               // Add trace comment to the begin of the sql string, right after the operator.
-               // Or, for one-word queries (like "BEGIN" or COMMIT") add it to the end (T44598)
+               # Add trace comment to the begin of the sql string, right after the operator.
+               # Or, for one-word queries (like "BEGIN" or COMMIT") add it to the end (T44598)
                $commentedSql = preg_replace( '/\s|$/', " /* $fname {$this->agent} */ ", $sql, 1 );
 
                # Start implicit transactions that wrap the request if DBO_TRX is enabled
@@ -888,7 +922,7 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware
                $this->assertOpen();
 
                # Send the query to the server
-               $ret = $this->doProfiledQuery( $sql, $commentedSql, $isWrite, $fname );
+               $ret = $this->doProfiledQuery( $sql, $commentedSql, $isNonTempWrite, $fname );
 
                # Try reconnecting if the connection was lost
                if ( false === $ret && $this->wasErrorReissuable() ) {
@@ -909,7 +943,7 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware
                                        $this->reportQueryError( $lastError, $lastErrno, $sql, $fname );
                                } else {
                                        # Should be safe to silently retry the query
-                                       $ret = $this->doProfiledQuery( $sql, $commentedSql, $isWrite, $fname );
+                                       $ret = $this->doProfiledQuery( $sql, $commentedSql, $isNonTempWrite, $fname );
                                }
                        } else {
                                $msg = __METHOD__ . ": lost connection to {$this->getServer()} permanently";
@@ -938,6 +972,17 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware
                return $res;
        }
 
+       /**
+        * Helper method for query() that handles profiling and logging and sends
+        * the query to doQuery()
+        *
+        * @param string $sql Original SQL query
+        * @param string $commentedSql SQL query with debugging/trace comment
+        * @param bool $isWrite Whether the query is a (non-temporary) write operation
+        * @param string $fname Name of the calling function
+        * @return bool|ResultWrapper True for a successful write query, ResultWrapper
+        *     object for a successful read query, or false on failure
+        */
        private function doProfiledQuery( $sql, $commentedSql, $isWrite, $fname ) {
                $isMaster = !is_null( $this->getLBInfo( 'master' ) );
                # generalizeSQL() will probably cut down the query to reasonable
@@ -1019,6 +1064,16 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware
                }
        }
 
+       /**
+        * Determine whether or not it is safe to retry queries after a database
+        * connection is lost
+        *
+        * @param string $sql SQL query
+        * @param bool $priorWritesPending Whether there is a transaction open with
+        *     possible write queries or transaction pre-commit/idle callbacks
+        *     waiting on it to finish.
+        * @return bool True if it is safe to retry the query, false otherwise
+        */
        private function canRecoverFromDisconnect( $sql, $priorWritesPending ) {
                # Transaction dropped; this can mean lost writes, or REPEATABLE-READ snapshots.
                # Dropped connections also mean that named locks are automatically released.
@@ -1039,6 +1094,11 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware
                return true;
        }
 
+       /**
+        * Clean things up after transaction loss due to disconnection
+        *
+        * @return null|Exception
+        */
        private function handleSessionLoss() {
                $this->mTrxLevel = 0;
                $this->mTrxIdleCallbacks = []; // T67263
@@ -1690,6 +1750,10 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware
                return $field;
        }
 
+       public function databasesAreIndependent() {
+               return false;
+       }
+
        public function selectDB( $db ) {
                # Stub. Shouldn't cause serious problems if it's not overridden, but
                # if your database engine supports a concept similar to MySQL's
@@ -2019,8 +2083,10 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware
         * @param string $s
         * @return string
         */
-       protected function escapeLikeInternal( $s ) {
-               return addcslashes( $s, '\%_' );
+       protected function escapeLikeInternal( $s, $escapeChar = '`' ) {
+               return str_replace( [ $escapeChar, '%', '_' ],
+                       [ "{$escapeChar}{$escapeChar}", "{$escapeChar}%", "{$escapeChar}_" ],
+                       $s );
        }
 
        public function buildLike() {
@@ -2032,15 +2098,21 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware
 
                $s = '';
 
+               // We use ` instead of \ as the default LIKE escape character, since addQuotes()
+               // may escape backslashes, creating problems of double escaping. The `
+               // character has good cross-DBMS compatibility, avoiding special operators
+               // in MS SQL like ^ and %
+               $escapeChar = '`';
+
                foreach ( $params as $value ) {
                        if ( $value instanceof LikeMatch ) {
                                $s .= $value->toString();
                        } else {
-                               $s .= $this->escapeLikeInternal( $value );
+                               $s .= $this->escapeLikeInternal( $value, $escapeChar );
                        }
                }
 
-               return " LIKE {$this->addQuotes( $s )} ";
+               return ' LIKE ' . $this->addQuotes( $s ) . ' ESCAPE ' . $this->addQuotes( $escapeChar ) . ' ';
        }
 
        public function anyChar() {
@@ -2311,6 +2383,12 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware
                return $this->insert( $destTable, $rows, $fname, $insertOptions );
        }
 
+       /**
+        * Native server-side implementation of insertSelect() for situations where
+        * we don't want to select everything into memory
+        *
+        * @see IDatabase::insertSelect()
+        */
        protected function nativeInsertSelect( $destTable, $srcTable, $varMap, $conds,
                $fname = __METHOD__,
                $insertOptions = [], $selectOptions = []
@@ -2955,7 +3033,9 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware
        }
 
        /**
-        * @return bool
+        * Close existing database connection and open a new connection
+        *
+        * @return bool True if new connection is opened successfully, false if error
         */
        protected function reconnect() {
                $this->closeConnection();
@@ -3323,6 +3403,14 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware
                return $this->doLockTables( $read, $write, $method );
        }
 
+       /**
+        * Helper function for lockTables() that handles the actual table locking
+        *
+        * @param array $read Array of tables to lock for read access
+        * @param array $write Array of tables to lock for write access
+        * @param string $method Name of caller
+        * @return true
+        */
        protected function doLockTables( array $read, array $write, $method ) {
                return true;
        }
@@ -3337,6 +3425,12 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware
                return $this->doUnlockTables( $method );
        }
 
+       /**
+        * Helper function for unlockTables() that handles the actual table unlocking
+        *
+        * @param string $method Name of caller
+        * @return true
+        */
        protected function doUnlockTables( $method ) {
                return true;
        }