From c90bf711ad7d638cd0c909d8fcf4cd6d307ae5a3 Mon Sep 17 00:00:00 2001 From: Max Semenik Date: Tue, 20 Oct 2009 20:23:14 +0000 Subject: [PATCH] (bug 20911) Installer failed to create a SQLite database, regression from r55669. --- RELEASE-NOTES | 1 + config/Installer.php | 35 ++++++++++++++++++++++++---------- includes/db/DatabaseSqlite.php | 15 ++++++++++++--- 3 files changed, 38 insertions(+), 13 deletions(-) diff --git a/RELEASE-NOTES b/RELEASE-NOTES index 0f00ecf138..71a0b66b63 100644 --- a/RELEASE-NOTES +++ b/RELEASE-NOTES @@ -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 == diff --git a/config/Installer.php b/config/Installer.php index 3bd87aea1b..9699f254ee 100644 --- a/config/Installer.php +++ b/config/Installer.php @@ -975,8 +975,8 @@ if( $conf->posted && ( 0 == count( $errs ) ) ) { if ("$wgSQLiteDataDir" == '') { $wgSQLiteDataDir = dirname($_SERVER['DOCUMENT_ROOT']).'/data'; } - echo "
  • Attempting to connect to SQLite database at \"" . - htmlspecialchars( $wgSQLiteDataDir ) . "\""; + echo '
  • 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
  • "; + echo "cannot create data directory"; $errs['SQLiteDataDir'] = 'Enter a valid data directory'; continue; } } if ( !is_writable( $wgSQLiteDataDir ) ) { - echo ": data directory not writable"; + echo "data directory not writable"; $errs['SQLiteDataDir'] = 'Enter a writable data directory'; continue; } - $dataFile = "$wgSQLiteDataDir/$wgDBname.sqlite"; - if ( file_exists( $dataFile ) && !is_writable( $dataFile ) ) { - echo ": data file not writable"; - $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"; + $errs['SQLiteDataDir'] = basename( $dataFile ) . " is not writable"; + continue; + } + } else { + if ( file_put_contents( $dataFile, '' ) === false ) { + echo 'could not create database file "' . htmlspecialchars( basename( $dataFile ) ) . "\"\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() ) . "\n"; continue; } - $wgDatabase = new DatabaseSqlite( false, false, false, $wgDBname, 1 ); + if (!$wgDatabase->isOpen()) { - print ": error: " . htmlspecialchars( $wgDatabase->lastError() ) . "\n"; + print "error: " . htmlspecialchars( $wgDatabase->lastError() ) . "\n"; $errs['SQLiteDataDir'] = 'Could not connect to database'; continue; } else { diff --git a/includes/db/DatabaseSqlite.php b/includes/db/DatabaseSqlite.php index e9fe549e4d..720c3a1135 100644 --- a/includes/db/DatabaseSqlite.php +++ b/includes/db/DatabaseSqlite.php @@ -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" ); -- 2.20.1