* Make the MySQL updater work in the new installer
[lhc/web/wiklou.git] / includes / installer / SqliteInstaller.php
1 <?php
2 /**
3 * Sqlite-specific installer.
4 *
5 * @file
6 * @ingroup Deployment
7 */
8
9 /**
10 * Class for setting up the MediaWiki database using SQLLite.
11 *
12 * @ingroup Deployment
13 * @since 1.17
14 */
15 class SqliteInstaller extends DatabaseInstaller {
16
17 protected $globalNames = array(
18 'wgDBname',
19 'wgSQLiteDataDir',
20 );
21
22 public function getName() {
23 return 'sqlite';
24 }
25
26 public function isCompiled() {
27 return self::checkExtension( 'pdo_sqlite' );
28 }
29
30 public function getGlobalDefaults() {
31 if ( isset( $_SERVER['DOCUMENT_ROOT'] ) ) {
32 $path = str_replace(
33 array( '/', '\\' ),
34 DIRECTORY_SEPARATOR,
35 dirname( $_SERVER['DOCUMENT_ROOT'] ) . '/data'
36 );
37 return array( 'wgSQLiteDataDir' => $path );
38 } else {
39 return array();
40 }
41 }
42
43 public function getConnectForm() {
44 return $this->getTextBox( 'wgSQLiteDataDir', 'config-sqlite-dir' ) .
45 $this->parent->getHelpBox( 'config-sqlite-dir-help' ) .
46 $this->getTextBox( 'wgDBname', 'config-db-name' ) .
47 $this->parent->getHelpBox( 'config-sqlite-name-help' );
48 }
49
50 public function submitConnectForm() {
51 $this->setVarsFromRequest( array( 'wgSQLiteDataDir', 'wgDBname' ) );
52
53 $dir = realpath( $this->getVar( 'wgSQLiteDataDir' ) );
54 if ( !$dir ) {
55 // realpath() sometimes fails, especially on Windows
56 $dir = $this->getVar( 'wgSQLiteDataDir' );
57 }
58 $this->setVar( 'wgSQLiteDataDir', $dir );
59 return self::dataDirOKmaybeCreate( $dir, true /* create? */ );
60 }
61
62 private static function dataDirOKmaybeCreate( $dir, $create = false ) {
63 if ( !is_dir( $dir ) ) {
64 if ( !is_writable( dirname( $dir ) ) ) {
65 $webserverGroup = Installer::maybeGetWebserverPrimaryGroup();
66 if ( $webserverGroup !== null ) {
67 return Status::newFatal( 'config-sqlite-parent-unwritable-group', $dir, dirname( $dir ), basename( $dir ), $webserverGroup );
68 } else {
69 return Status::newFatal( 'config-sqlite-parent-unwritable-nogroup', $dir, dirname( $dir ), basename( $dir ) );
70 }
71 }
72
73 # Called early on in the installer, later we just want to sanity check
74 # if it's still writable
75 if ( $create ) {
76 wfSuppressWarnings();
77 $ok = wfMkdirParents( $dir, 0700 );
78 wfRestoreWarnings();
79 if ( !$ok ) {
80 return Status::newFatal( 'config-sqlite-mkdir-error', $dir );
81 }
82 # Put a .htaccess file in in case the user didn't take our advice
83 file_put_contents( "$dir/.htaccess", "Deny from all\n" );
84 }
85 }
86 if ( !is_writable( $dir ) ) {
87 return Status::newFatal( 'config-sqlite-dir-unwritable', $dir );
88 }
89
90 # We haven't blown up yet, fall through
91 return Status::newGood();
92 }
93
94 public function getConnection() {
95 global $wgSQLiteDataDir;
96
97 $status = Status::newGood();
98 $dir = $this->getVar( 'wgSQLiteDataDir' );
99 $dbName = $this->getVar( 'wgDBname' );
100
101 try {
102 # FIXME: need more sensible constructor parameters, e.g. single associative array
103 # Setting globals kind of sucks
104 $wgSQLiteDataDir = $dir;
105 $this->db = new DatabaseSqlite( false, false, false, $dbName );
106 $status->value = $this->db;
107 } catch ( DBConnectionError $e ) {
108 $status->fatal( 'config-sqlite-connection-error', $e->getMessage() );
109 }
110 return $status;
111 }
112
113 public function needsUpgrade() {
114 $dir = $this->getVar( 'wgSQLiteDataDir' );
115 $dbName = $this->getVar( 'wgDBname' );
116 // Don't create the data file yet
117 if ( !file_exists( DatabaseSqlite::generateFileName( $dir, $dbName ) ) ) {
118 return false;
119 }
120
121 // If the data file exists, look inside it
122 return parent::needsUpgrade();
123 }
124
125 public function getSettingsForm() {
126 return false;
127 }
128
129 public function submitSettingsForm() {
130 return Status::newGood();
131 }
132
133 public function setupDatabase() {
134 $dir = $this->getVar( 'wgSQLiteDataDir' );
135
136 # Sanity check. We checked this before but maybe someone deleted the
137 # data dir between then and now
138 $dir_status = self::dataDirOKmaybeCreate( $dir, false /* create? */ );
139 if ( !$dir_status->isOK() ) {
140 return $dir_status;
141 }
142
143 $db = $this->getVar( 'wgDBname' );
144 $file = DatabaseSqlite::generateFileName( $dir, $db );
145 if ( file_exists( $file ) ) {
146 if ( !is_writable( $file ) ) {
147 return Status::newFatal( 'config-sqlite-readonly', $file );
148 }
149 } else {
150 if ( file_put_contents( $file, '' ) === false ) {
151 return Status::newFatal( 'config-sqlite-cant-create-db', $file );
152 }
153 }
154 // nuke the unused settings for clarity
155 $this->setVar( 'wgDBserver', '' );
156 $this->setVar( 'wgDBuser', '' );
157 $this->setVar( 'wgDBpassword', '' );
158 return $this->getConnection();
159 }
160
161 public function createTables() {
162 global $IP;
163 $status = $this->getConnection();
164 if ( !$status->isOK() ) {
165 return $status;
166 }
167 // Process common MySQL/SQLite table definitions
168 $err = $this->db->sourceFile( "$IP/maintenance/tables.sql" );
169 if ( $err !== true ) {
170 //@todo or...?
171 $this->db->reportQueryError( $err, 0, $sql, __FUNCTION__ );
172 }
173 return $this->setupSearchIndex();
174 }
175
176 public function setupSearchIndex() {
177 global $IP;
178
179 $status = Status::newGood();
180
181 $module = $this->db->getFulltextSearchModule();
182 $fts3tTable = $this->db->checkForEnabledSearch();
183 if ( $fts3tTable && !$module ) {
184 $status->warning( 'config-sqlite-fts3-downgrade' );
185 $this->db->sourceFile( "$IP/maintenance/sqlite/archives/searchindex-no-fts.sql" );
186 } elseif ( !$fts3tTable && $module == 'FTS3' ) {
187 $status->warning( 'config-sqlite-fts3-add' );
188 $this->db->sourceFile( "$IP/maintenance/sqlite/archives/searchindex-fts3.sql" );
189 }
190 return $status;
191 }
192
193 public function doUpgrade() {
194 global $wgDatabase;
195 LBFactory::enableBackend();
196 $wgDatabase = wfGetDB( DB_MASTER );
197 ob_start( array( 'SqliteInstaller', 'outputHandler' ) );
198 do_all_updates( false, true );
199 ob_end_flush();
200 return true;
201 }
202
203 public static function outputHandler( $string ) {
204 return htmlspecialchars( $string );
205 }
206
207 public function getLocalSettings() {
208 $dir = LocalSettingsGenerator::escapePhpString( $this->getVar( 'wgSQLiteDataDir' ) );
209 return
210 "# SQLite-specific settings
211 \$wgSQLiteDataDir = \"{$dir}\";";
212 }
213 }