MediaWiki  master
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 ) ) ) {
152  $webserverGroup = Installer::maybeGetWebserverPrimaryGroup();
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 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  $res = self::createDataDir( $dir );
232  if ( !$res->isGood() ) {
233  return $res;
234  }
235  } else {
236  return $dir_status;
237  }
238 
239  $db = $this->getVar( 'wgDBname' );
240 
241  # Make the main and cache stub DB files
242  $status = Status::newGood();
243  $status->merge( $this->makeStubDBFile( $dir, $db ) );
244  $status->merge( $this->makeStubDBFile( $dir, "wikicache" ) );
245  $status->merge( $this->makeStubDBFile( $dir, "{$db}_l10n_cache" ) );
246  $status->merge( $this->makeStubDBFile( $dir, "{$db}_jobqueue" ) );
247  if ( !$status->isOK() ) {
248  return $status;
249  }
250 
251  # Nuke the unused settings for clarity
252  $this->setVar( 'wgDBserver', '' );
253  $this->setVar( 'wgDBuser', '' );
254  $this->setVar( 'wgDBpassword', '' );
255  $this->setupSchemaVars();
256 
257  # Create the l10n cache DB
258  try {
259  $conn = Database::factory(
260  'sqlite', [ 'dbname' => "{$db}_l10n_cache", 'dbDirectory' => $dir ] );
261  # @todo: don't duplicate l10n_cache definition, though it's very simple
262  $sql =
263 <<<EOT
264  CREATE TABLE l10n_cache (
265  lc_lang BLOB NOT NULL,
266  lc_key TEXT NOT NULL,
267  lc_value BLOB NOT NULL,
268  PRIMARY KEY (lc_lang, lc_key)
269  );
270 EOT;
271  $conn->query( $sql, __METHOD__ );
272  $conn->query( "PRAGMA journal_mode=WAL", __METHOD__ ); // this is permanent
273  $conn->close( __METHOD__ );
274  } catch ( DBConnectionError $e ) {
275  return Status::newFatal( 'config-sqlite-connection-error', $e->getMessage() );
276  }
277 
278  # Create the job queue DB
279  try {
280  $conn = Database::factory(
281  'sqlite', [ 'dbname' => "{$db}_jobqueue", 'dbDirectory' => $dir ] );
282  # @todo: don't duplicate job definition, though it's very static
283  $sql =
284 <<<EOT
285  CREATE TABLE job (
286  job_id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
287  job_cmd BLOB NOT NULL default '',
288  job_namespace INTEGER NOT NULL,
289  job_title TEXT NOT NULL,
290  job_timestamp BLOB NULL default NULL,
291  job_params BLOB NOT NULL,
292  job_random integer NOT NULL default 0,
293  job_attempts integer NOT NULL default 0,
294  job_token BLOB NOT NULL default '',
295  job_token_timestamp BLOB NULL default NULL,
296  job_sha1 BLOB NOT NULL default ''
297  );
298  CREATE INDEX job_sha1 ON job (job_sha1);
299  CREATE INDEX job_cmd_token ON job (job_cmd,job_token,job_random);
300  CREATE INDEX job_cmd_token_id ON job (job_cmd,job_token,job_id);
301  CREATE INDEX job_cmd ON job (job_cmd, job_namespace, job_title, job_params);
302  CREATE INDEX job_timestamp ON job (job_timestamp);
303 EOT;
304  $conn->query( $sql, __METHOD__ );
305  $conn->query( "PRAGMA journal_mode=WAL", __METHOD__ ); // this is permanent
306  $conn->close( __METHOD__ );
307  } catch ( DBConnectionError $e ) {
308  return Status::newFatal( 'config-sqlite-connection-error', $e->getMessage() );
309  }
310 
311  # Open the main DB
312  return $this->getConnection();
313  }
314 
320  protected function makeStubDBFile( $dir, $db ) {
321  $file = DatabaseSqlite::generateFileName( $dir, $db );
322  if ( file_exists( $file ) ) {
323  if ( !is_writable( $file ) ) {
324  return Status::newFatal( 'config-sqlite-readonly', $file );
325  }
326  } elseif ( file_put_contents( $file, '' ) === false ) {
327  return Status::newFatal( 'config-sqlite-cant-create-db', $file );
328  }
329 
330  return Status::newGood();
331  }
332 
336  public function createTables() {
337  $status = parent::createTables();
338  if ( $status->isGood() ) {
339  $status = parent::createManualTables();
340  }
341 
342  return $this->setupSearchIndex( $status );
343  }
344 
349  public function setupSearchIndex( &$status ) {
350  global $IP;
351 
352  $module = DatabaseSqlite::getFulltextSearchModule();
353  $searchIndexSql = (string)$this->db->selectField(
354  $this->db->addIdentifierQuotes( 'sqlite_master' ),
355  'sql',
356  [ 'tbl_name' => $this->db->tableName( 'searchindex', 'raw' ) ],
357  __METHOD__
358  );
359  $fts3tTable = ( stristr( $searchIndexSql, 'fts' ) !== false );
360 
361  if ( $fts3tTable && !$module ) {
362  $status->warning( 'config-sqlite-fts3-downgrade' );
363  $this->db->sourceFile( "$IP/maintenance/sqlite/archives/searchindex-no-fts.sql" );
364  } elseif ( !$fts3tTable && $module == 'FTS3' ) {
365  $this->db->sourceFile( "$IP/maintenance/sqlite/archives/searchindex-fts3.sql" );
366  }
367 
368  return $status;
369  }
370 
374  public function getLocalSettings() {
375  $dir = LocalSettingsGenerator::escapePhpString( $this->getVar( 'wgSQLiteDataDir' ) );
376  // These tables have frequent writes and are thus split off from the main one.
377  // Since the code using these tables only uses transactions for writes then set
378  // them to using BEGIN IMMEDIATE. This avoids frequent lock errors on first write.
379  return "# SQLite-specific settings
380 \$wgSQLiteDataDir = \"{$dir}\";
381 \$wgObjectCaches[CACHE_DB] = [
382  'class' => SqlBagOStuff::class,
383  'loggroup' => 'SQLBagOStuff',
384  'server' => [
385  'type' => 'sqlite',
386  'dbname' => 'wikicache',
387  'tablePrefix' => '',
388  'variables' => [ 'synchronous' => 'NORMAL' ],
389  'dbDirectory' => \$wgSQLiteDataDir,
390  'trxMode' => 'IMMEDIATE',
391  'flags' => 0
392  ]
393 ];
394 \$wgLocalisationCacheConf['storeServer'] = [
395  'type' => 'sqlite',
396  'dbname' => \"{\$wgDBname}_l10n_cache\",
397  'tablePrefix' => '',
398  'variables' => [ 'synchronous' => 'NORMAL' ],
399  'dbDirectory' => \$wgSQLiteDataDir,
400  'trxMode' => 'IMMEDIATE',
401  'flags' => 0
402 ];
403 \$wgJobTypeConf['default'] = [
404  'class' => 'JobQueueDB',
405  'claimTTL' => 3600,
406  'server' => [
407  'type' => 'sqlite',
408  'dbname' => \"{\$wgDBname}_jobqueue\",
409  'tablePrefix' => '',
410  'variables' => [ 'synchronous' => 'NORMAL' ],
411  'dbDirectory' => \$wgSQLiteDataDir,
412  'trxMode' => 'IMMEDIATE',
413  'flags' => 0
414  ]
415 ];";
416  }
417 }
SqliteInstaller\createDataDir
static createDataDir( $dir)
Definition: SqliteInstaller.php:174
SqliteInstaller\makeStubDBFile
makeStubDBFile( $dir, $db)
Definition: SqliteInstaller.php:320
Wikimedia\Rdbms\Database
Relational database abstraction object.
Definition: Database.php:50
SqliteInstaller\setupDatabase
setupDatabase()
Definition: SqliteInstaller.php:224
StatusValue\newFatal
static newFatal( $message,... $parameters)
Factory function for fatal errors.
Definition: StatusValue.php:69
Wikimedia\Rdbms\DatabaseSqlite
Definition: DatabaseSqlite.php:37
SqliteInstaller
Class for setting up the MediaWiki database using SQLLite.
Definition: SqliteInstaller.php:34
SqliteInstaller\openConnection
openConnection()
Definition: SqliteInstaller.php:191
DatabaseInstaller\checkExtension
static checkExtension( $name)
Convenience function.
Definition: DatabaseInstaller.php:463
wfMkdirParents
wfMkdirParents( $dir, $mode=null, $caller=null)
Make directory, and make all parent directories if they don't exist.
Definition: GlobalFunctions.php:1893
SqliteInstaller\checkDataDir
static checkDataDir( $dir)
Check if the data directory is writable or can be created.
Definition: SqliteInstaller.php:144
DatabaseInstaller\getConnection
getConnection()
Connect to the database using the administrative user/password currently defined in the session.
Definition: DatabaseInstaller.php:182
$file
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition: router.php:42
DatabaseInstaller\getTextBox
getTextBox( $var, $label, $attribs=[], $helpData="")
Get a labelled text box to configure a local variable.
Definition: DatabaseInstaller.php:534
Wikimedia\Rdbms\DatabaseSqlite\getServerVersion
getServerVersion()
Definition: DatabaseSqlite.php:762
SqliteInstaller\getConnectForm
getConnectForm()
Get HTML for a web form that configures this database.
Definition: SqliteInstaller.php:89
$res
$res
Definition: testCompression.php:57
SqliteInstaller\isCompiled
isCompiled()
Definition: SqliteInstaller.php:53
Status
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition: Status.php:42
SqliteInstaller\$globalNames
$globalNames
Definition: SqliteInstaller.php:44
SqliteInstaller\realpath
static realpath( $path)
Safe wrapper for PHP's realpath() that fails gracefully if it's unable to canonicalize the path.
Definition: SqliteInstaller.php:110
DatabaseInstaller\setupSchemaVars
setupSchemaVars()
Set appropriate schema variables in the current database connection.
Definition: DatabaseInstaller.php:362
SqliteInstaller\setupSearchIndex
setupSearchIndex(&$status)
Definition: SqliteInstaller.php:349
SqliteInstaller\submitConnectForm
submitConnectForm()
Definition: SqliteInstaller.php:122
DatabaseInstaller\getVar
getVar( $var, $default=null)
Get a variable, taking local defaults into account.
Definition: DatabaseInstaller.php:504
StatusValue\newGood
static newGood( $value=null)
Factory function for good results.
Definition: StatusValue.php:81
SqliteInstaller\createTables
createTables()
Definition: SqliteInstaller.php:336
SqliteInstaller\$notMinimumVersionMessage
static $notMinimumVersionMessage
Definition: SqliteInstaller.php:37
DatabaseInstaller
Base class for DBMS-specific installation helper classes.
Definition: DatabaseInstaller.php:37
SqliteInstaller\getGlobalDefaults
getGlobalDefaults()
Get a name=>value map of MW configuration globals for the default values.
Definition: SqliteInstaller.php:72
SqliteInstaller\checkPrerequisites
checkPrerequisites()
Definition: SqliteInstaller.php:60
SqliteInstaller\getLocalSettings
getLocalSettings()
Definition: SqliteInstaller.php:374
SqliteInstaller\getName
getName()
Return the internal name, e.g.
Definition: SqliteInstaller.php:49
$path
$path
Definition: NoLocalSettings.php:25
SqliteInstaller\needsUpgrade
needsUpgrade()
Definition: SqliteInstaller.php:209
DatabaseInstaller\setVar
setVar( $name, $value)
Convenience alias for $this->parent->setVar()
Definition: DatabaseInstaller.php:521
DatabaseInstaller\setVarsFromRequest
setVarsFromRequest( $varNames)
Convenience function to set variables based on form data.
Definition: DatabaseInstaller.php:627
Wikimedia\Rdbms\DBConnectionError
Definition: DBConnectionError.php:26
SqliteInstaller\$minimumVersion
static $minimumVersion
Definition: SqliteInstaller.php:36
LocalSettingsGenerator\escapePhpString
static escapePhpString( $string)
Returns the escaped version of a string of php code.
Definition: LocalSettingsGenerator.php:113
SqliteInstaller\$db
DatabaseSqlite $db
Definition: SqliteInstaller.php:42
$IP
$IP
Definition: WebStart.php:49
Installer\maybeGetWebserverPrimaryGroup
static maybeGetWebserverPrimaryGroup()
On POSIX systems return the primary group of the webserver we're running under.
Definition: Installer.php:663