Follow-up r67045. Delete it from the English message file too.
[mediawiki.git] / includes / installer / SqliteInstaller.php
blob5dea22224ed23ba45e6f9c2bc061e912b008cb5e
1 <?php
3 class SqliteInstaller extends InstallerDBType {
4 protected $globalNames = array(
5 'wgDBname',
6 'wgSQLiteDataDir',
7 );
9 function getName() {
10 return 'sqlite';
13 function isCompiled() {
14 return $this->checkExtension( 'pdo_sqlite' );
17 function getGlobalDefaults() {
18 if ( isset( $_SERVER['DOCUMENT_ROOT'] ) ) {
19 $path = str_replace(
20 array( '/', '\\' ),
21 DIRECTORY_SEPARATOR,
22 dirname( $_SERVER['DOCUMENT_ROOT'] ) . '/data'
24 return array( 'wgSQLiteDataDir' => $path );
25 } else {
26 return array();
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' ) );
42 if ( !$dir ) {
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 );
56 } else {
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
63 if ( $create ) {
64 wfSuppressWarnings();
65 $ok = wfMkdirParents( $dir, 0700 );
66 wfRestoreWarnings();
67 if ( !$ok ) {
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' );
89 try {
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() );
98 return $status;
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 ) ) ) {
106 return false;
109 // If the data file exists, look inside it
110 return parent::needsUpgrade();
113 function getSettingsForm() {
114 return false;
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() ) {
128 return $dir_status;
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 );
137 } else {
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() {
150 global $IP;
151 $status = $this->getConnection();
152 if ( !$status->isOK() ) {
153 return $status;
155 // Process common MySQL/SQLite table definitions
156 $err = $this->db->sourceFile( "$IP/maintenance/tables.sql" );
157 if ( $err !== true ) {
158 //@todo or...?
159 $this->db->reportQueryError( $err, 0, $sql, __FUNCTION__ );
161 //@todo set up searchindex
162 // Create default interwikis
163 return Status::newGood();
166 function doUpgrade() {
167 global $wgDatabase;
168 LBFactory::enableBackend();
169 $wgDatabase = wfGetDB( DB_MASTER );
170 ob_start( array( 'SqliteInstaller', 'outputHandler' ) );
171 do_all_updates( false, true );
172 ob_end_flush();
173 return true;
176 static function outputHandler( $string ) {
177 return htmlspecialchars( $string );
180 function getLocalSettings() {
181 $dir = LocalSettingsGenerator::escapePhpString( $this->getVar( 'wgSQLiteDataDir' ) );
182 return
183 "# SQLite-specific settings
184 \$wgSQLiteDataDir = \"{$dir}\";";