Merge "Run SpecialPageFatalTest with lang=qqx"
[lhc/web/wiklou.git] / includes / libs / rdbms / database / DatabaseSqlite.php
index aff3774..c875e56 100644 (file)
@@ -23,6 +23,7 @@
  */
 namespace Wikimedia\Rdbms;
 
+use NullLockManager;
 use PDO;
 use PDOException;
 use Exception;
@@ -39,7 +40,7 @@ class DatabaseSqlite extends Database {
        /** @var bool Whether full text is enabled */
        private static $fulltextEnabled = null;
 
-       /** @var string Directory */
+       /** @var string|null Directory */
        protected $dbDir;
        /** @var string File name for SQLite database file */
        protected $dbPath;
@@ -91,10 +92,16 @@ class DatabaseSqlite extends Database {
                        $this->queryLogger->warning( "Invalid SQLite transaction mode provided." );
                }
 
-               $this->lockMgr = new FSLockManager( [
-                       'domain' => $lockDomain,
-                       'lockDirectory' => "{$this->dbDir}/locks"
-               ] );
+               if ( $this->hasProcessMemoryPath() ) {
+                       $this->lockMgr = new NullLockManager( [ 'domain' => $lockDomain ] );
+               } else {
+                       $this->lockMgr = new FSLockManager( [
+                               'domain' => $lockDomain,
+                               'lockDirectory' => is_string( $this->dbDir )
+                                       ? "{$this->dbDir}/locks"
+                                       : dirname( $this->dbPath ) . "/locks"
+                       ] );
+               }
 
                parent::__construct( $p );
        }
@@ -168,15 +175,13 @@ class DatabaseSqlite extends Database {
 
        protected function open( $server, $user, $pass, $dbName, $schema, $tablePrefix ) {
                $this->close();
-               $fileName = self::generateFileName( $this->dbDir, $dbName );
-               if ( !is_readable( $fileName ) ) {
-                       $this->conn = false;
-                       throw new DBConnectionError( $this, "SQLite database not accessible" );
+
+               if ( $schema !== null ) {
+                       throw new DBExpectedError( $this, __CLASS__ . ": domain schemas are not supported." );
                }
-               // Only $dbName is used, the other parameters are irrelevant for SQLite databases
-               $this->openFile( $fileName, $dbName, $tablePrefix );
 
-               return (bool)$this->conn;
+               // Only $dbName is used, the other parameters are irrelevant for SQLite databases
+               $this->openFile( self::generateFileName( $this->dbDir, $dbName ), $dbName, $tablePrefix );
        }
 
        /**
@@ -186,45 +191,57 @@ class DatabaseSqlite extends Database {
         * @param string $dbName
         * @param string $tablePrefix
         * @throws DBConnectionError
-        * @return PDO|bool SQL connection or false if failed
         */
        protected function openFile( $fileName, $dbName, $tablePrefix ) {
-               $err = false;
+               if ( !$this->hasProcessMemoryPath() && !is_readable( $fileName ) ) {
+                       $error = "SQLite database file not readable";
+                       $this->connLogger->error(
+                               "Error connecting to {db_server}: {error}",
+                               $this->getLogContext( [ 'method' => __METHOD__, 'error' => $error ] )
+                       );
+                       throw new DBConnectionError( $this, $error );
+               }
 
                $this->dbPath = $fileName;
                try {
-                       if ( $this->flags & self::DBO_PERSISTENT ) {
-                               $this->conn = new PDO( "sqlite:$fileName", '', '',
-                                       [ PDO::ATTR_PERSISTENT => true ] );
-                       } else {
-                               $this->conn = new PDO( "sqlite:$fileName", '', '' );
-                       }
+                       $this->conn = new PDO(
+                               "sqlite:$fileName",
+                               '',
+                               '',
+                               [ PDO::ATTR_PERSISTENT => (bool)( $this->flags & self::DBO_PERSISTENT ) ]
+                       );
+                       $error = 'unknown error';
                } catch ( PDOException $e ) {
-                       $err = $e->getMessage();
+                       $error = $e->getMessage();
                }
 
                if ( !$this->conn ) {
-                       $this->queryLogger->debug( "DB connection error: $err\n" );
-                       throw new DBConnectionError( $this, $err );
+                       $this->connLogger->error(
+                               "Error connecting to {db_server}: {error}",
+                               $this->getLogContext( [ 'method' => __METHOD__, 'error' => $error ] )
+                       );
+                       throw new DBConnectionError( $this, $error );
                }
 
-               $this->opened = is_object( $this->conn );
-               if ( $this->opened ) {
-                       $this->currentDomain = new DatabaseDomain( $dbName, null, $tablePrefix );
-                       # Set error codes only, don't raise exceptions
+               try {
+                       // Set error codes only, don't raise exceptions
                        $this->conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT );
-                       # Enforce LIKE to be case sensitive, just like MySQL
-                       $this->query( 'PRAGMA case_sensitive_like = 1' );
 
+                       $this->currentDomain = new DatabaseDomain( $dbName, null, $tablePrefix );
+
+                       $flags = self::QUERY_IGNORE_DBO_TRX | self::QUERY_NO_RETRY;
+                       // Enforce LIKE to be case sensitive, just like MySQL
+                       $this->query( 'PRAGMA case_sensitive_like = 1', __METHOD__, $flags );
+                       // Apply an optimizations or requirements regarding fsync() usage
                        $sync = $this->connectionVariables['synchronous'] ?? null;
                        if ( in_array( $sync, [ 'EXTRA', 'FULL', 'NORMAL', 'OFF' ], true ) ) {
-                               $this->query( "PRAGMA synchronous = $sync" );
+                               $this->query( "PRAGMA synchronous = $sync", __METHOD__ );
                        }
-
-                       return $this->conn;
+               } catch ( Exception $e ) {
+                       // Connection was not fully initialized and is not safe for use
+                       $this->conn = false;
+                       throw $e;
                }
-
-               return false;
        }
 
        /**
@@ -338,9 +355,9 @@ class DatabaseSqlite extends Database {
                        return false;
                }
 
-               $r = $res instanceof ResultWrapper ? $res->result : $res;
-               $this->lastAffectedRowCount = $r->rowCount();
-               $res = new ResultWrapper( $this, $r->fetchAll() );
+               $resource = ResultWrapper::unwrap( $res );
+               $this->lastAffectedRowCount = $resource->rowCount();
+               $res = new ResultWrapper( $this, $resource->fetchAll() );
 
                return $res;
        }
@@ -350,9 +367,7 @@ class DatabaseSqlite extends Database {
         */
        function freeResult( $res ) {
                if ( $res instanceof ResultWrapper ) {
-                       $res->result = null;
-               } else {
-                       $res = null;
+                       $res->free();
                }
        }
 
@@ -361,15 +376,11 @@ class DatabaseSqlite extends Database {
         * @return stdClass|bool
         */
        function fetchObject( $res ) {
-               if ( $res instanceof ResultWrapper ) {
-                       $r =& $res->result;
-               } else {
-                       $r =& $res;
-               }
+               $resource =& ResultWrapper::unwrap( $res );
 
-               $cur = current( $r );
+               $cur = current( $resource );
                if ( is_array( $cur ) ) {
-                       next( $r );
+                       next( $resource );
                        $obj = new stdClass;
                        foreach ( $cur as $k => $v ) {
                                if ( !is_numeric( $k ) ) {
@@ -388,14 +399,10 @@ class DatabaseSqlite extends Database {
         * @return array|bool
         */
        function fetchRow( $res ) {
-               if ( $res instanceof ResultWrapper ) {
-                       $r =& $res->result;
-               } else {
-                       $r =& $res;
-               }
-               $cur = current( $r );
+               $resource =& ResultWrapper::unwrap( $res );
+               $cur = current( $resource );
                if ( is_array( $cur ) ) {
-                       next( $r );
+                       next( $resource );
 
                        return $cur;
                }
@@ -411,9 +418,9 @@ class DatabaseSqlite extends Database {
         */
        function numRows( $res ) {
                // false does not implement Countable
-               $r = $res instanceof ResultWrapper ? $res->result : $res;
+               $resource = ResultWrapper::unwrap( $res );
 
-               return is_array( $r ) ? count( $r ) : 0;
+               return is_array( $resource ) ? count( $resource ) : 0;
        }
 
        /**
@@ -421,10 +428,10 @@ class DatabaseSqlite extends Database {
         * @return int
         */
        function numFields( $res ) {
-               $r = $res instanceof ResultWrapper ? $res->result : $res;
-               if ( is_array( $r ) && count( $r ) > 0 ) {
+               $resource = ResultWrapper::unwrap( $res );
+               if ( is_array( $resource ) && count( $resource ) > 0 ) {
                        // The size of the result array is twice the number of fields. (T67578)
-                       return count( $r[0] ) / 2;
+                       return count( $resource[0] ) / 2;
                } else {
                        // If the result is empty return 0
                        return 0;
@@ -437,9 +444,9 @@ class DatabaseSqlite extends Database {
         * @return bool
         */
        function fieldName( $res, $n ) {
-               $r = $res instanceof ResultWrapper ? $res->result : $res;
-               if ( is_array( $r ) ) {
-                       $keys = array_keys( $r[0] );
+               $resource = ResultWrapper::unwrap( $res );
+               if ( is_array( $resource ) ) {
+                       $keys = array_keys( $resource[0] );
 
                        return $keys[$n];
                }
@@ -478,15 +485,11 @@ class DatabaseSqlite extends Database {
         * @param int $row
         */
        function dataSeek( $res, $row ) {
-               if ( $res instanceof ResultWrapper ) {
-                       $r =& $res->result;
-               } else {
-                       $r =& $res;
-               }
-               reset( $r );
+               $resource =& ResultWrapper::unwrap( $res );
+               reset( $resource );
                if ( $row > 0 ) {
                        for ( $i = 0; $i < $row; $i++ ) {
-                               next( $r );
+                               next( $resource );
                        }
                }
        }
@@ -761,6 +764,17 @@ class DatabaseSqlite extends Database {
                return false;
        }
 
+       public function serverIsReadOnly() {
+               return ( !$this->hasProcessMemoryPath() && !is_writable( $this->dbPath ) );
+       }
+
+       /**
+        * @return bool
+        */
+       private function hasProcessMemoryPath() {
+               return ( strpos( $this->dbPath, ':memory:' ) === 0 );
+       }
+
        /**
         * @return string Wikitext of a link to the server software's web site
         */
@@ -804,7 +818,6 @@ class DatabaseSqlite extends Database {
                } else {
                        $this->query( 'BEGIN', $fname );
                }
-               $this->trxLevel = 1;
        }
 
        /**
@@ -954,17 +967,19 @@ class DatabaseSqlite extends Database {
        }
 
        public function lock( $lockName, $method, $timeout = 5 ) {
-               if ( !is_dir( "{$this->dbDir}/locks" ) ) { // create dir as needed
-                       if ( !is_writable( $this->dbDir ) || !mkdir( "{$this->dbDir}/locks" ) ) {
-                               throw new DBError( $this, "Cannot create directory \"{$this->dbDir}/locks\"." );
-                       }
+               // Give better error message for permission problems than just returning false
+               if (
+                       !is_dir( "{$this->dbDir}/locks" ) &&
+                       ( !is_writable( $this->dbDir ) || !mkdir( "{$this->dbDir}/locks" ) )
+               ) {
+                       throw new DBError( $this, "Cannot create directory \"{$this->dbDir}/locks\"." );
                }
 
                return $this->lockMgr->lock( [ $lockName ], LockManager::LOCK_EX, $timeout )->isOK();
        }
 
        public function unlock( $lockName, $method ) {
-               return $this->lockMgr->unlock( [ $lockName ], LockManager::LOCK_EX )->isOK();
+               return $this->lockMgr->unlock( [ $lockName ], LockManager::LOCK_EX )->isGood();
        }
 
        /**