MediaWiki REL1_37
SqliteInstaller.php
Go to the documentation of this file.
1<?php
27
35
36 public static $minimumVersion = '3.8.0';
37 protected static $notMinimumVersionMessage = 'config-outdated-sqlite';
38
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
60 public function checkPrerequisites() {
61 // Bail out if SQLite is too old
62 $db = DatabaseSqlite::newStandaloneInstance( ':memory:' );
63 $result = static::meetsMinimumRequirement( $db->getServerVersion() );
64 // Check for FTS3 full-text search module
65 if ( DatabaseSqlite::getFulltextSearchModule() != 'FTS3' ) {
66 $result->warning( 'config-no-fts3' );
67 }
68
69 return $result;
70 }
71
72 public function getGlobalDefaults() {
73 global $IP;
74 $defaults = parent::getGlobalDefaults();
75 if ( !empty( $_SERVER['DOCUMENT_ROOT'] ) ) {
76 $path = dirname( $_SERVER['DOCUMENT_ROOT'] );
77 } else {
78 // We use $IP when unable to get $_SERVER['DOCUMENT_ROOT']
79 $path = $IP;
80 }
81 $defaults['wgSQLiteDataDir'] = str_replace(
82 [ '/', '\\' ],
83 DIRECTORY_SEPARATOR,
84 $path . '/data'
85 );
86 return $defaults;
87 }
88
89 public function getConnectForm() {
90 return $this->getTextBox(
91 'wgSQLiteDataDir',
92 'config-sqlite-dir', [],
93 $this->parent->getHelpBox( 'config-sqlite-dir-help' )
94 ) .
95 $this->getTextBox(
96 'wgDBname',
97 'config-db-name',
98 [],
99 $this->parent->getHelpBox( 'config-sqlite-name-help' )
100 );
101 }
102
110 private static function realpath( $path ) {
111 $result = realpath( $path );
112 if ( !$result ) {
113 return $path;
114 }
115
116 return $result;
117 }
118
122 public function submitConnectForm() {
123 $this->setVarsFromRequest( [ 'wgSQLiteDataDir', 'wgDBname' ] );
124
125 # Try realpath() if the directory already exists
126 $dir = self::realpath( $this->getVar( 'wgSQLiteDataDir' ) );
127 $result = self::checkDataDir( $dir );
128 if ( $result->isOK() ) {
129 # Try expanding again in case we've just created it
130 $dir = self::realpath( $dir );
131 $this->setVar( 'wgSQLiteDataDir', $dir );
132 }
133 # Table prefix is not used on SQLite, keep it empty
134 $this->setVar( 'wgDBprefix', '' );
135
136 return $result;
137 }
138
144 private static function checkDataDir( $dir ): Status {
145 if ( is_dir( $dir ) ) {
146 if ( !is_readable( $dir ) ) {
147 return Status::newFatal( 'config-sqlite-dir-unwritable', $dir );
148 }
149 } else {
150 // Check the parent directory if $dir not exists
151 if ( !is_writable( dirname( $dir ) ) ) {
153 if ( $webserverGroup !== null ) {
154 return Status::newFatal(
155 'config-sqlite-parent-unwritable-group',
156 $dir, dirname( $dir ), basename( $dir ),
157 $webserverGroup
158 );
159 } else {
160 return Status::newFatal(
161 'config-sqlite-parent-unwritable-nogroup',
162 $dir, dirname( $dir ), basename( $dir )
163 );
164 }
165 }
166 }
167 return Status::newGood();
168 }
169
174 private static function createDataDir( $dir ): Status {
175 if ( !is_dir( $dir ) ) {
176 Wikimedia\suppressWarnings();
177 $ok = wfMkdirParents( $dir, 0700, __METHOD__ );
178 Wikimedia\restoreWarnings();
179 if ( !$ok ) {
180 return Status::newFatal( 'config-sqlite-mkdir-error', $dir );
181 }
182 }
183 # Put a .htaccess file in case the user didn't take our advice
184 file_put_contents( "$dir/.htaccess", "Deny from all\n" );
185 return Status::newGood();
186 }
187
191 public function openConnection() {
192 $status = Status::newGood();
193 $dir = $this->getVar( 'wgSQLiteDataDir' );
194 $dbName = $this->getVar( 'wgDBname' );
195 try {
196 # @todo FIXME: Need more sensible constructor parameters, e.g. single associative array
197 $db = Database::factory( 'sqlite', [ 'dbname' => $dbName, 'dbDirectory' => $dir ] );
198 $status->value = $db;
199 } catch ( DBConnectionError $e ) {
200 $status->fatal( 'config-sqlite-connection-error', $e->getMessage() );
201 }
202
203 return $status;
204 }
205
209 public function needsUpgrade() {
210 $dir = $this->getVar( 'wgSQLiteDataDir' );
211 $dbName = $this->getVar( 'wgDBname' );
212 // Don't create the data file yet
213 if ( !file_exists( DatabaseSqlite::generateFileName( $dir, $dbName ) ) ) {
214 return false;
215 }
216
217 // If the data file exists, look inside it
218 return parent::needsUpgrade();
219 }
220
224 public function setupDatabase() {
225 $dir = $this->getVar( 'wgSQLiteDataDir' );
226
227 # Sanity check (Only available in web installation). We checked this before but maybe someone
228 # deleted the data dir between then and now
229 $dir_status = self::checkDataDir( $dir );
230 if ( $dir_status->isGood() ) {
231 // @phan-suppress-next-line SecurityCheck-PathTraversal
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 l10n cache DB
259 try {
260 $conn = Database::factory(
261 'sqlite', [ 'dbname' => "{$db}_l10n_cache", 'dbDirectory' => $dir ] );
262 # @todo: don't duplicate l10n_cache definition, though it's very simple
263 $sql =
264<<<EOT
265 CREATE TABLE l10n_cache (
266 lc_lang BLOB NOT NULL,
267 lc_key TEXT NOT NULL,
268 lc_value BLOB NOT NULL,
269 PRIMARY KEY (lc_lang, lc_key)
270 );
271EOT;
272 $conn->query( $sql, __METHOD__ );
273 $conn->query( "PRAGMA journal_mode=WAL", __METHOD__ ); // this is permanent
274 $conn->close( __METHOD__ );
275 } catch ( DBConnectionError $e ) {
276 return Status::newFatal( 'config-sqlite-connection-error', $e->getMessage() );
277 }
278
279 # Create the job queue DB
280 try {
281 $conn = Database::factory(
282 'sqlite', [ 'dbname' => "{$db}_jobqueue", 'dbDirectory' => $dir ] );
283 # @todo: don't duplicate job definition, though it's very static
284 $sql =
285<<<EOT
286 CREATE TABLE job (
287 job_id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
288 job_cmd BLOB NOT NULL default '',
289 job_namespace INTEGER NOT NULL,
290 job_title TEXT NOT NULL,
291 job_timestamp BLOB NULL default NULL,
292 job_params BLOB NOT NULL,
293 job_random integer NOT NULL default 0,
294 job_attempts integer NOT NULL default 0,
295 job_token BLOB NOT NULL default '',
296 job_token_timestamp BLOB NULL default NULL,
297 job_sha1 BLOB NOT NULL default ''
298 );
299 CREATE INDEX job_sha1 ON job (job_sha1);
300 CREATE INDEX job_cmd_token ON job (job_cmd,job_token,job_random);
301 CREATE INDEX job_cmd_token_id ON job (job_cmd,job_token,job_id);
302 CREATE INDEX job_cmd ON job (job_cmd, job_namespace, job_title, job_params);
303 CREATE INDEX job_timestamp ON job (job_timestamp);
304EOT;
305 $conn->query( $sql, __METHOD__ );
306 $conn->query( "PRAGMA journal_mode=WAL", __METHOD__ ); // this is permanent
307 $conn->close( __METHOD__ );
308 } catch ( DBConnectionError $e ) {
309 return Status::newFatal( 'config-sqlite-connection-error', $e->getMessage() );
310 }
311
312 # Open the main DB
313 return $this->getConnection();
314 }
315
321 protected function makeStubDBFile( $dir, $db ) {
322 $file = DatabaseSqlite::generateFileName( $dir, $db );
323 if ( file_exists( $file ) ) {
324 if ( !is_writable( $file ) ) {
325 return Status::newFatal( 'config-sqlite-readonly', $file );
326 }
327 } elseif ( file_put_contents( $file, '' ) === false ) {
328 return Status::newFatal( 'config-sqlite-cant-create-db', $file );
329 }
330
331 return Status::newGood();
332 }
333
337 public function createTables() {
338 $status = parent::createTables();
339 if ( $status->isGood() ) {
340 $status = parent::createManualTables();
341 }
342
343 return $this->setupSearchIndex( $status );
344 }
345
346 public function createManualTables() {
347 // Already handled above. Do nothing.
348 return Status::newGood();
349 }
350
355 public function setupSearchIndex( &$status ) {
356 global $IP;
357
358 $module = DatabaseSqlite::getFulltextSearchModule();
359 $searchIndexSql = (string)$this->db->selectField(
360 $this->db->addIdentifierQuotes( 'sqlite_master' ),
361 'sql',
362 [ 'tbl_name' => $this->db->tableName( 'searchindex', 'raw' ) ],
363 __METHOD__
364 );
365 $fts3tTable = ( stristr( $searchIndexSql, 'fts' ) !== false );
366
367 if ( $fts3tTable && !$module ) {
368 $status->warning( 'config-sqlite-fts3-downgrade' );
369 $this->db->sourceFile( "$IP/maintenance/sqlite/archives/searchindex-no-fts.sql" );
370 } elseif ( !$fts3tTable && $module == 'FTS3' ) {
371 $this->db->sourceFile( "$IP/maintenance/sqlite/archives/searchindex-fts3.sql" );
372 }
373
374 return $status;
375 }
376
380 public function getLocalSettings() {
381 $dir = LocalSettingsGenerator::escapePhpString( $this->getVar( 'wgSQLiteDataDir' ) );
382 // These tables have frequent writes and are thus split off from the main one.
383 // Since the code using these tables only uses transactions for writes then set
384 // them to using BEGIN IMMEDIATE. This avoids frequent lock errors on first write.
385 return "# SQLite-specific settings
386\$wgSQLiteDataDir = \"{$dir}\";
387\$wgObjectCaches[CACHE_DB] = [
388 'class' => SqlBagOStuff::class,
389 'loggroup' => 'SQLBagOStuff',
390 'server' => [
391 'type' => 'sqlite',
392 'dbname' => 'wikicache',
393 'tablePrefix' => '',
394 'variables' => [ 'synchronous' => 'NORMAL' ],
395 'dbDirectory' => \$wgSQLiteDataDir,
396 'trxMode' => 'IMMEDIATE',
397 'flags' => 0
398 ]
399];
400\$wgLocalisationCacheConf['storeServer'] = [
401 'type' => 'sqlite',
402 'dbname' => \"{\$wgDBname}_l10n_cache\",
403 'tablePrefix' => '',
404 'variables' => [ 'synchronous' => 'NORMAL' ],
405 'dbDirectory' => \$wgSQLiteDataDir,
406 'trxMode' => 'IMMEDIATE',
407 'flags' => 0
408];
409\$wgJobTypeConf['default'] = [
410 'class' => 'JobQueueDB',
411 'claimTTL' => 3600,
412 'server' => [
413 'type' => 'sqlite',
414 'dbname' => \"{\$wgDBname}_jobqueue\",
415 'tablePrefix' => '',
416 'variables' => [ 'synchronous' => 'NORMAL' ],
417 'dbDirectory' => \$wgSQLiteDataDir,
418 'trxMode' => 'IMMEDIATE',
419 'flags' => 0
420 ]
421];";
422 }
423}
wfMkdirParents( $dir, $mode=null, $caller=null)
Make directory, and make all parent directories if they don't exist.
if(ini_get('mbstring.func_overload')) if(!defined('MW_ENTRY_POINT'))
Pre-config setup: Before loading LocalSettings.php.
Definition Setup.php:88
$IP
Definition WebStart.php:49
Base class for DBMS-specific installation helper classes.
static checkExtension( $name)
Convenience function.
setVarsFromRequest( $varNames)
Convenience function to set variables based on form data.
getVar( $var, $default=null)
Get a variable, taking local defaults into account.
getTextBox( $var, $label, $attribs=[], $helpData="")
Get a labelled text box to configure a local variable.
setVar( $name, $value)
Convenience alias for $this->parent->setVar()
static maybeGetWebserverPrimaryGroup()
On POSIX systems return the primary group of the webserver we're running under.
Class for setting up the MediaWiki database using SQLLite.
setupSearchIndex(&$status)
static createDataDir( $dir)
getGlobalDefaults()
Get a name=>value map of MW configuration globals for the default values.
makeStubDBFile( $dir, $db)
static realpath( $path)
Safe wrapper for PHP's realpath() that fails gracefully if it's unable to canonicalize the path.
getName()
Return the internal name, e.g.
DatabaseSqlite $db
getConnectForm()
Get HTML for a web form that configures this database.
static $notMinimumVersionMessage
static checkDataDir( $dir)
Check if the data directory is writable or can be created.
createManualTables()
Create database tables from scratch.
static newFatal( $message,... $parameters)
Factory function for fatal errors.
static newGood( $value=null)
Factory function for good results.
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition Status.php:44
Relational database abstraction object.
Definition Database.php:52
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition router.php:42