(bug 20911) Installer failed to create a SQLite database, regression from r55669.
authorMax Semenik <maxsem@users.mediawiki.org>
Tue, 20 Oct 2009 20:23:14 +0000 (20:23 +0000)
committerMax Semenik <maxsem@users.mediawiki.org>
Tue, 20 Oct 2009 20:23:14 +0000 (20:23 +0000)
RELEASE-NOTES
config/Installer.php
includes/db/DatabaseSqlite.php

index 0f00ecf..71a0b66 100644 (file)
@@ -573,6 +573,7 @@ it from source control: http://www.mediawiki.org/wiki/Download_from_SVN
 * (bug 20880) Fixed updater failure on SQLite backend
 * (bug 21182) Fixed invalid HTML in Special:Listgrouprights
 * (bug 20242) Installer no longer promts for user credentials for SQLite databases
+* (bug 20911) Installer failed to create a SQLite database
 
 == API changes in 1.16 ==
 
index 3bd87ae..9699f25 100644 (file)
@@ -975,8 +975,8 @@ if( $conf->posted && ( 0 == count( $errs ) ) ) {
                        if ("$wgSQLiteDataDir" == '') {
                                $wgSQLiteDataDir = dirname($_SERVER['DOCUMENT_ROOT']).'/data';
                        }
-                       echo "<li>Attempting to connect to SQLite database at \"" . 
-                               htmlspecialchars( $wgSQLiteDataDir ) .  "\"";
+                       echo '<li>Attempting to connect to SQLite database at "' . 
+                               htmlspecialchars( $wgSQLiteDataDir ) . '": ';
                        if ( !is_dir( $wgSQLiteDataDir ) ) {
                                if ( is_writable( dirname( $wgSQLiteDataDir ) ) ) {
                                        $ok = wfMkdirParents( $wgSQLiteDataDir, $wgSQLiteDataDirMode );
@@ -984,25 +984,40 @@ if( $conf->posted && ( 0 == count( $errs ) ) ) {
                                        $ok = false;
                                }
                                if ( !$ok ) {
-                                       echo "cannot create data directory</li>";
+                                       echo "cannot create data directory</li>";
                                        $errs['SQLiteDataDir'] = 'Enter a valid data directory';
                                        continue;
                                }
                        }
                        if ( !is_writable( $wgSQLiteDataDir ) ) {
-                               echo "data directory not writable</li>";
+                               echo "data directory not writable</li>";
                                $errs['SQLiteDataDir'] = 'Enter a writable data directory';
                                continue;
                        }
-                       $dataFile = "$wgSQLiteDataDir/$wgDBname.sqlite";
-                       if ( file_exists( $dataFile ) && !is_writable( $dataFile ) ) {
-                               echo ": data file not writable</li>";
-                               $errs['SQLiteDataDir'] = "$wgDBname.sqlite is not writable";
+                       $dataFile = DatabaseSqlite::generateFileName( $wgSQLiteDataDir, $wgDBname );
+                       if ( file_exists( $dataFile ) ) {
+                               if ( !is_writable( $dataFile ) ) {
+                                       echo "data file not writable</li>";
+                                       $errs['SQLiteDataDir'] = basename( $dataFile ) . " is not writable";
+                                       continue;
+                               }
+                       } else {
+                               if ( file_put_contents( $dataFile, '' ) === false ) {
+                                       echo 'could not create database file "' . htmlspecialchars( basename( $dataFile ) ) . "\"</li>\n";
+                                       $errs['SQLiteDataDir'] = "couldn't create " . basename( $dataFile );
+                                       continue;
+                               }
+                       }
+                       try {
+                               $wgDatabase = new DatabaseSqlite( false, false, false, $wgDBname, 1 );
+                       }
+                       catch( MWException $ex ) {
+                               echo 'error: ' . htmlspecialchars( $ex->getMessage() ) . "</li>\n";
                                continue;
                        }
-                       $wgDatabase = new DatabaseSqlite( false, false, false, $wgDBname, 1 );
+                       
                        if (!$wgDatabase->isOpen()) {
-                               print "error: " . htmlspecialchars( $wgDatabase->lastError() ) . "</li>\n";
+                               print "error: " . htmlspecialchars( $wgDatabase->lastError() ) . "</li>\n";
                                $errs['SQLiteDataDir'] = 'Could not connect to database';
                                continue;
                        } else {
index e9fe549..720c3a1 100644 (file)
@@ -24,7 +24,7 @@ class DatabaseSqlite extends DatabaseBase {
                global $wgSQLiteDataDir;
                $this->mFailFunction = $failFunction;
                $this->mFlags = $flags;
-               $this->mDatabaseFile = "$wgSQLiteDataDir/$dbName.sqlite";
+               $this->mDatabaseFile = self::generateFileName( $wgSQLiteDataDir, $dbName );
                if( !is_readable( $this->mDatabaseFile ) )
                        throw new DBConnectionError( $this, "SQLite database not accessible" );
                $this->mName = $dbName;
@@ -86,6 +86,16 @@ class DatabaseSqlite extends DatabaseBase {
                return true;
        }
 
+       /**
+        * Generates a database file name. Explicitly public for installer.
+        * @param $dir String: Directory where database resides
+        * @param $dbName String: Database name
+        * @return String
+        */
+       public static function generateFileName( $dir, $dbName ) {
+               return "$dir/$dbName.sqlite";
+       }
+
        /**
         * SQLite doesn't allow buffered results or data seeking etc, so we'll use fetchAll as the result
         */
@@ -420,8 +430,7 @@ class DatabaseSqlite extends DatabaseBase {
         * - this is the same way PostgreSQL works, MySQL reads in tables.sql and interwiki.sql using dbsource (which calls db->sourceFile)
         */
        public function setup_database() {
-               global $IP, $wgSQLiteDataDir, $wgDBTableOptions;
-               $wgDBTableOptions = '';
+               global $IP;
 
                # Process common MySQL/SQLite table definitions
                $err = $this->sourceFile( "$IP/maintenance/tables.sql" );