Fix installation failure due to unexpected dbpath under CLI installation
[lhc/web/wiklou.git] / includes / installer / SqliteInstaller.php
1 <?php
2 /**
3 * Sqlite-specific installer.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Deployment
22 */
23
24 use Wikimedia\Rdbms\Database;
25 use Wikimedia\Rdbms\DatabaseSqlite;
26 use Wikimedia\Rdbms\DBConnectionError;
27
28 /**
29 * Class for setting up the MediaWiki database using SQLLite.
30 *
31 * @ingroup Deployment
32 * @since 1.17
33 */
34 class SqliteInstaller extends DatabaseInstaller {
35
36 public static $minimumVersion = '3.8.0';
37 protected static $notMinimumVersionMessage = 'config-outdated-sqlite';
38
39 /**
40 * @var DatabaseSqlite
41 */
42 public $db;
43
44 protected $globalNames = [
45 'wgDBname',
46 'wgSQLiteDataDir',
47 ];
48
49 public function getName() {
50 return 'sqlite';
51 }
52
53 public function isCompiled() {
54 return self::checkExtension( 'pdo_sqlite' );
55 }
56
57 /**
58 *
59 * @return Status
60 */
61 public function checkPrerequisites() {
62 // Bail out if SQLite is too old
63 $db = DatabaseSqlite::newStandaloneInstance( ':memory:' );
64 $result = static::meetsMinimumRequirement( $db->getServerVersion() );
65 // Check for FTS3 full-text search module
66 if ( DatabaseSqlite::getFulltextSearchModule() != 'FTS3' ) {
67 $result->warning( 'config-no-fts3' );
68 }
69
70 return $result;
71 }
72
73 public function getGlobalDefaults() {
74 global $IP;
75 $defaults = parent::getGlobalDefaults();
76 if ( !empty( $_SERVER['DOCUMENT_ROOT'] ) ) {
77 $path = dirname( $_SERVER['DOCUMENT_ROOT'] );
78 } else {
79 // We use $IP when unable to get $_SERVER['DOCUMENT_ROOT']
80 $path = $IP;
81 }
82 $defaults['wgSQLiteDataDir'] = str_replace(
83 [ '/', '\\' ],
84 DIRECTORY_SEPARATOR,
85 $path . '/data'
86 );
87 return $defaults;
88 }
89
90 public function getConnectForm() {
91 return $this->getTextBox(
92 'wgSQLiteDataDir',
93 'config-sqlite-dir', [],
94 $this->parent->getHelpBox( 'config-sqlite-dir-help' )
95 ) .
96 $this->getTextBox(
97 'wgDBname',
98 'config-db-name',
99 [],
100 $this->parent->getHelpBox( 'config-sqlite-name-help' )
101 );
102 }
103
104 /**
105 * Safe wrapper for PHP's realpath() that fails gracefully if it's unable to canonicalize the path.
106 *
107 * @param string $path
108 *
109 * @return string
110 */
111 private static function realpath( $path ) {
112 $result = realpath( $path );
113 if ( !$result ) {
114 return $path;
115 }
116
117 return $result;
118 }
119
120 /**
121 * @return Status
122 */
123 public function submitConnectForm() {
124 $this->setVarsFromRequest( [ 'wgSQLiteDataDir', 'wgDBname' ] );
125
126 # Try realpath() if the directory already exists
127 $dir = self::realpath( $this->getVar( 'wgSQLiteDataDir' ) );
128 $result = self::checkDataDir( $dir );
129 if ( $result->isOK() ) {
130 # Try expanding again in case we've just created it
131 $dir = self::realpath( $dir );
132 $this->setVar( 'wgSQLiteDataDir', $dir );
133 }
134 # Table prefix is not used on SQLite, keep it empty
135 $this->setVar( 'wgDBprefix', '' );
136
137 return $result;
138 }
139
140 /**
141 * Check if the data directory is writable or can be created
142 * @param string $dir Path to the data directory
143 * @return Status Return fatal Status if $dir un-writable or no permission to create a directory
144 */
145 private static function checkDataDir( $dir ) : Status {
146 if ( is_dir( $dir ) ) {
147 if ( !is_readable( $dir ) ) {
148 return Status::newFatal( 'config-sqlite-dir-unwritable', $dir );
149 }
150 } else {
151 // Check the parent directory if $dir not exists
152 if ( !is_writable( dirname( $dir ) ) ) {
153 $webserverGroup = Installer::maybeGetWebserverPrimaryGroup();
154 if ( $webserverGroup !== null ) {
155 return Status::newFatal(
156 'config-sqlite-parent-unwritable-group',
157 $dir, dirname( $dir ), basename( $dir ),
158 $webserverGroup
159 );
160 } else {
161 return Status::newFatal(
162 'config-sqlite-parent-unwritable-nogroup',
163 $dir, dirname( $dir ), basename( $dir )
164 );
165 }
166 }
167 }
168 return Status::newGood();
169 }
170
171 /**
172 * @param string $dir Path to the data directory
173 * @return Status Return good Status if without error
174 */
175 private static function createDataDir( $dir ) : Status {
176 if ( !is_dir( $dir ) ) {
177 Wikimedia\suppressWarnings();
178 $ok = wfMkdirParents( $dir, 0700, __METHOD__ );
179 Wikimedia\restoreWarnings();
180 if ( !$ok ) {
181 return Status::newFatal( 'config-sqlite-mkdir-error', $dir );
182 }
183 }
184 # Put a .htaccess file in in case the user didn't take our advice
185 file_put_contents( "$dir/.htaccess", "Deny from all\n" );
186 return Status::newGood();
187 }
188
189 /**
190 * @return Status
191 */
192 public function openConnection() {
193 $status = Status::newGood();
194 $dir = $this->getVar( 'wgSQLiteDataDir' );
195 $dbName = $this->getVar( 'wgDBname' );
196 try {
197 # @todo FIXME: Need more sensible constructor parameters, e.g. single associative array
198 $db = Database::factory( 'sqlite', [ 'dbname' => $dbName, 'dbDirectory' => $dir ] );
199 $status->value = $db;
200 } catch ( DBConnectionError $e ) {
201 $status->fatal( 'config-sqlite-connection-error', $e->getMessage() );
202 }
203
204 return $status;
205 }
206
207 /**
208 * @return bool
209 */
210 public function needsUpgrade() {
211 $dir = $this->getVar( 'wgSQLiteDataDir' );
212 $dbName = $this->getVar( 'wgDBname' );
213 // Don't create the data file yet
214 if ( !file_exists( DatabaseSqlite::generateFileName( $dir, $dbName ) ) ) {
215 return false;
216 }
217
218 // If the data file exists, look inside it
219 return parent::needsUpgrade();
220 }
221
222 /**
223 * @return Status
224 */
225 public function setupDatabase() {
226 $dir = $this->getVar( 'wgSQLiteDataDir' );
227
228 # Sanity check (Only available in web installation). We checked this before but maybe someone
229 # deleted the data dir between then and now
230 $dir_status = self::checkDataDir( $dir );
231 if ( $dir_status->isGood() ) {
232 $res = self::createDataDir( $dir );
233 if ( !$res->isGood() ) {
234 return $res;
235 }
236 } else {
237 return $dir_status;
238 }
239
240 $db = $this->getVar( 'wgDBname' );
241
242 # Make the main and cache stub DB files
243 $status = Status::newGood();
244 $status->merge( $this->makeStubDBFile( $dir, $db ) );
245 $status->merge( $this->makeStubDBFile( $dir, "wikicache" ) );
246 $status->merge( $this->makeStubDBFile( $dir, "{$db}_l10n_cache" ) );
247 $status->merge( $this->makeStubDBFile( $dir, "{$db}_jobqueue" ) );
248 if ( !$status->isOK() ) {
249 return $status;
250 }
251
252 # Nuke the unused settings for clarity
253 $this->setVar( 'wgDBserver', '' );
254 $this->setVar( 'wgDBuser', '' );
255 $this->setVar( 'wgDBpassword', '' );
256 $this->setupSchemaVars();
257
258 # Create the global cache DB
259 try {
260 $conn = Database::factory(
261 'sqlite', [ 'dbname' => 'wikicache', 'dbDirectory' => $dir ] );
262 # @todo: don't duplicate objectcache definition, though it's very simple
263 $sql =
264 <<<EOT
265 CREATE TABLE IF NOT EXISTS objectcache (
266 keyname BLOB NOT NULL default '' PRIMARY KEY,
267 value BLOB,
268 exptime TEXT
269 )
270 EOT;
271 $conn->query( $sql );
272 $conn->query( "CREATE INDEX IF NOT EXISTS exptime ON objectcache (exptime)" );
273 $conn->query( "PRAGMA journal_mode=WAL" ); // this is permanent
274 $conn->close();
275 } catch ( DBConnectionError $e ) {
276 return Status::newFatal( 'config-sqlite-connection-error', $e->getMessage() );
277 }
278
279 # Create the l10n cache DB
280 try {
281 $conn = Database::factory(
282 'sqlite', [ 'dbname' => "{$db}_l10n_cache", 'dbDirectory' => $dir ] );
283 # @todo: don't duplicate l10n_cache definition, though it's very simple
284 $sql =
285 <<<EOT
286 CREATE TABLE l10n_cache (
287 lc_lang BLOB NOT NULL,
288 lc_key TEXT NOT NULL,
289 lc_value BLOB NOT NULL,
290 PRIMARY KEY (lc_lang, lc_key)
291 );
292 EOT;
293 $conn->query( $sql );
294 $conn->query( "PRAGMA journal_mode=WAL" ); // this is permanent
295 $conn->close();
296 } catch ( DBConnectionError $e ) {
297 return Status::newFatal( 'config-sqlite-connection-error', $e->getMessage() );
298 }
299
300 # Create the job queue DB
301 try {
302 $conn = Database::factory(
303 'sqlite', [ 'dbname' => "{$db}_jobqueue", 'dbDirectory' => $dir ] );
304 # @todo: don't duplicate job definition, though it's very static
305 $sql =
306 <<<EOT
307 CREATE TABLE job (
308 job_id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
309 job_cmd BLOB NOT NULL default '',
310 job_namespace INTEGER NOT NULL,
311 job_title TEXT NOT NULL,
312 job_timestamp BLOB NULL default NULL,
313 job_params BLOB NOT NULL,
314 job_random integer NOT NULL default 0,
315 job_attempts integer NOT NULL default 0,
316 job_token BLOB NOT NULL default '',
317 job_token_timestamp BLOB NULL default NULL,
318 job_sha1 BLOB NOT NULL default ''
319 );
320 CREATE INDEX job_sha1 ON job (job_sha1);
321 CREATE INDEX job_cmd_token ON job (job_cmd,job_token,job_random);
322 CREATE INDEX job_cmd_token_id ON job (job_cmd,job_token,job_id);
323 CREATE INDEX job_cmd ON job (job_cmd, job_namespace, job_title, job_params);
324 CREATE INDEX job_timestamp ON job (job_timestamp);
325 EOT;
326 $conn->query( $sql );
327 $conn->query( "PRAGMA journal_mode=WAL" ); // this is permanent
328 $conn->close();
329 } catch ( DBConnectionError $e ) {
330 return Status::newFatal( 'config-sqlite-connection-error', $e->getMessage() );
331 }
332
333 # Open the main DB
334 return $this->getConnection();
335 }
336
337 /**
338 * @param string $dir
339 * @param string $db
340 * @return Status
341 */
342 protected function makeStubDBFile( $dir, $db ) {
343 $file = DatabaseSqlite::generateFileName( $dir, $db );
344 if ( file_exists( $file ) ) {
345 if ( !is_writable( $file ) ) {
346 return Status::newFatal( 'config-sqlite-readonly', $file );
347 }
348 } elseif ( file_put_contents( $file, '' ) === false ) {
349 return Status::newFatal( 'config-sqlite-cant-create-db', $file );
350 }
351
352 return Status::newGood();
353 }
354
355 /**
356 * @return Status
357 */
358 public function createTables() {
359 $status = parent::createTables();
360
361 return $this->setupSearchIndex( $status );
362 }
363
364 /**
365 * @param Status &$status
366 * @return Status
367 */
368 public function setupSearchIndex( &$status ) {
369 global $IP;
370
371 $module = DatabaseSqlite::getFulltextSearchModule();
372 $fts3tTable = $this->db->checkForEnabledSearch();
373 if ( $fts3tTable && !$module ) {
374 $status->warning( 'config-sqlite-fts3-downgrade' );
375 $this->db->sourceFile( "$IP/maintenance/sqlite/archives/searchindex-no-fts.sql" );
376 } elseif ( !$fts3tTable && $module == 'FTS3' ) {
377 $this->db->sourceFile( "$IP/maintenance/sqlite/archives/searchindex-fts3.sql" );
378 }
379
380 return $status;
381 }
382
383 /**
384 * @return string
385 */
386 public function getLocalSettings() {
387 $dir = LocalSettingsGenerator::escapePhpString( $this->getVar( 'wgSQLiteDataDir' ) );
388 // These tables have frequent writes and are thus split off from the main one.
389 // Since the code using these tables only uses transactions for writes then set
390 // them to using BEGIN IMMEDIATE. This avoids frequent lock errors on first write.
391 return "# SQLite-specific settings
392 \$wgSQLiteDataDir = \"{$dir}\";
393 \$wgObjectCaches[CACHE_DB] = [
394 'class' => SqlBagOStuff::class,
395 'loggroup' => 'SQLBagOStuff',
396 'server' => [
397 'type' => 'sqlite',
398 'dbname' => 'wikicache',
399 'tablePrefix' => '',
400 'variables' => [ 'synchronous' => 'NORMAL' ],
401 'dbDirectory' => \$wgSQLiteDataDir,
402 'trxMode' => 'IMMEDIATE',
403 'flags' => 0
404 ]
405 ];
406 \$wgLocalisationCacheConf['storeServer'] = [
407 'type' => 'sqlite',
408 'dbname' => \"{\$wgDBname}_l10n_cache\",
409 'tablePrefix' => '',
410 'variables' => [ 'synchronous' => 'NORMAL' ],
411 'dbDirectory' => \$wgSQLiteDataDir,
412 'trxMode' => 'IMMEDIATE',
413 'flags' => 0
414 ];
415 \$wgJobTypeConf['default'] = [
416 'class' => 'JobQueueDB',
417 'claimTTL' => 3600,
418 'server' => [
419 'type' => 'sqlite',
420 'dbname' => \"{\$wgDBname}_jobqueue\",
421 'tablePrefix' => '',
422 'variables' => [ 'synchronous' => 'NORMAL' ],
423 'dbDirectory' => \$wgSQLiteDataDir,
424 'trxMode' => 'IMMEDIATE',
425 'flags' => 0
426 ]
427 ];";
428 }
429 }