3 class SqliteInstaller
extends InstallerDBType
{
4 protected $globalNames = array(
13 function isCompiled() {
14 return $this->checkExtension( 'pdo_sqlite' );
17 function getGlobalDefaults() {
18 if ( isset( $_SERVER['DOCUMENT_ROOT'] ) ) {
22 dirname( $_SERVER['DOCUMENT_ROOT'] ) . '/data'
24 return array( 'wgSQLiteDataDir' => $path );
30 function getConnectForm() {
31 return $this->getTextBox( 'wgSQLiteDataDir', 'config-sqlite-dir' ) .
32 $this->parent
->getHelpBox( 'config-sqlite-dir-help' ) .
33 $this->getTextBox( 'wgDBname', 'config-db-name' ) .
34 $this->parent
->getHelpBox( 'config-sqlite-name-help' );
37 function submitConnectForm() {
38 global $wgSQLiteDataDir;
39 $this->setVarsFromRequest( array( 'wgSQLiteDataDir', 'wgDBname' ) );
41 $dir = realpath( $this->getVar( 'wgSQLiteDataDir' ) );
43 // realpath() sometimes fails, especially on Windows
44 $dir = $this->getVar( 'wgSQLiteDataDir' );
46 $this->setVar( 'wgSQLiteDataDir', $dir );
47 return self
::dataDirOKmaybeCreate( $dir, true /* create? */ );
50 private static function dataDirOKmaybeCreate( $dir, $create = false ) {
51 if ( !is_dir( $dir ) ) {
52 if ( !is_writable( dirname( $dir ) ) ) {
53 $webserverGroup = Installer
::maybeGetWebserverPrimaryGroup();
54 if ( $webserverGroup !== null ) {
55 return Status
::newFatal( 'config-sqlite-parent-unwritable-group', $dir, dirname( $dir ), basename( $dir ), $webserverGroup );
57 return Status
::newFatal( 'config-sqlite-parent-unwritable-nogroup', $dir, dirname( $dir ), basename( $dir ) );
61 # Called early on in the installer, later we just want to sanity check
62 # if it's still writable
65 $ok = wfMkdirParents( $dir, 0700 );
68 return Status
::newFatal( 'config-sqlite-mkdir-error', $dir );
70 # Put a .htaccess file in in case the user didn't take our advice
71 file_put_contents( "$dir/.htaccess", "Deny from all\n" );
74 if ( !is_writable( $dir ) ) {
75 return Status
::newFatal( 'config-sqlite-dir-unwritable', $dir );
78 # We haven't blown up yet, fall through
79 return Status
::newGood();
82 function getConnection() {
83 global $wgSQLiteDataDir;
85 $status = Status
::newGood();
86 $dir = $this->getVar( 'wgSQLiteDataDir' );
87 $dbName = $this->getVar( 'wgDBname' );
90 # FIXME: need more sensible constructor parameters, e.g. single associative array
91 # Setting globals kind of sucks
92 $wgSQLiteDataDir = $dir;
93 $this->db
= new DatabaseSqlite( false, false, false, $dbName );
94 $status->value
= $this->db
;
95 } catch ( DBConnectionError
$e ) {
96 $status->fatal( 'config-sqlite-connection-error', $e->getMessage() );
101 function needsUpgrade() {
102 $dir = $this->getVar( 'wgSQLiteDataDir' );
103 $dbName = $this->getVar( 'wgDBname' );
104 // Don't create the data file yet
105 if ( !file_exists( DatabaseSqlite
::generateFileName( $dir, $dbName ) ) ) {
109 // If the data file exists, look inside it
110 return parent
::needsUpgrade();
113 function getSettingsForm() {
117 function submitSettingsForm() {
118 return Status
::newGood();
121 function setupDatabase() {
122 $dir = $this->getVar( 'wgSQLiteDataDir' );
124 # Sanity check. We checked this before but maybe someone deleted the
125 # data dir between then and now
126 $dir_status = self
::dataDirOKmaybeCreate( $dir, false /* create? */ );
127 if ( !$dir_status->isOK() ) {
131 $db = $this->getVar( 'wgDBname' );
132 $file = DatabaseSqlite
::generateFileName( $dir, $db );
133 if ( file_exists( $file ) ) {
134 if ( !is_writable( $file ) ) {
135 return Status
::newFatal( 'config-sqlite-readonly', $file );
138 if ( file_put_contents( $file, '' ) === false ) {
139 return Status
::newFatal( 'config-sqlite-cant-create-db', $file );
142 // nuke the unused settings for clarity
143 $this->setVar( 'wgDBserver', '' );
144 $this->setVar( 'wgDBuser', '' );
145 $this->setVar( 'wgDBpassword', '' );
146 return $this->getConnection();
149 function createTables() {
151 $status = $this->getConnection();
152 if ( !$status->isOK() ) {
155 // Process common MySQL/SQLite table definitions
156 $err = $this->db
->sourceFile( "$IP/maintenance/tables.sql" );
157 if ( $err !== true ) {
159 $this->db
->reportQueryError( $err, 0, $sql, __FUNCTION__
);
161 //@todo set up searchindex
162 // Create default interwikis
163 return Status
::newGood();
166 function doUpgrade() {
168 LBFactory
::enableBackend();
169 $wgDatabase = wfGetDB( DB_MASTER
);
170 ob_start( array( 'SqliteInstaller', 'outputHandler' ) );
171 do_all_updates( false, true );
176 static function outputHandler( $string ) {
177 return htmlspecialchars( $string );
180 function getLocalSettings() {
181 $dir = LocalSettingsGenerator
::escapePhpString( $this->getVar( 'wgSQLiteDataDir' ) );
183 "# SQLite-specific settings
184 \$wgSQLiteDataDir = \"{$dir}\";";