MediaWiki  1.34.0
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 
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 
111  private static function realpath( $path ) {
112  $result = realpath( $path );
113  if ( !$result ) {
114  return $path;
115  }
116 
117  return $result;
118  }
119 
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 
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 
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 
192  public function openConnection() {
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 
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 
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
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  );
271 EOT;
272  $conn->query( $sql );
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 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);
304 EOT;
305  $conn->query( $sql );
306  $conn->query( "PRAGMA journal_mode=WAL" ); // this is permanent
307  $conn->close();
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 
340  return $this->setupSearchIndex( $status );
341  }
342 
347  public function setupSearchIndex( &$status ) {
348  global $IP;
349 
350  $module = DatabaseSqlite::getFulltextSearchModule();
351  $searchIndexSql = (string)$this->db->selectField(
352  $this->db->addIdentifierQuotes( 'sqlite_master' ),
353  'sql',
354  [ 'tbl_name' => $this->db->tableName( 'searchindex', 'raw' ) ],
355  __METHOD__
356  );
357  $fts3tTable = ( stristr( $searchIndexSql, 'fts' ) !== false );
358 
359  if ( $fts3tTable && !$module ) {
360  $status->warning( 'config-sqlite-fts3-downgrade' );
361  $this->db->sourceFile( "$IP/maintenance/sqlite/archives/searchindex-no-fts.sql" );
362  } elseif ( !$fts3tTable && $module == 'FTS3' ) {
363  $this->db->sourceFile( "$IP/maintenance/sqlite/archives/searchindex-fts3.sql" );
364  }
365 
366  return $status;
367  }
368 
372  public function getLocalSettings() {
373  $dir = LocalSettingsGenerator::escapePhpString( $this->getVar( 'wgSQLiteDataDir' ) );
374  // These tables have frequent writes and are thus split off from the main one.
375  // Since the code using these tables only uses transactions for writes then set
376  // them to using BEGIN IMMEDIATE. This avoids frequent lock errors on first write.
377  return "# SQLite-specific settings
378 \$wgSQLiteDataDir = \"{$dir}\";
379 \$wgObjectCaches[CACHE_DB] = [
380  'class' => SqlBagOStuff::class,
381  'loggroup' => 'SQLBagOStuff',
382  'server' => [
383  'type' => 'sqlite',
384  'dbname' => 'wikicache',
385  'tablePrefix' => '',
386  'variables' => [ 'synchronous' => 'NORMAL' ],
387  'dbDirectory' => \$wgSQLiteDataDir,
388  'trxMode' => 'IMMEDIATE',
389  'flags' => 0
390  ]
391 ];
392 \$wgLocalisationCacheConf['storeServer'] = [
393  'type' => 'sqlite',
394  'dbname' => \"{\$wgDBname}_l10n_cache\",
395  'tablePrefix' => '',
396  'variables' => [ 'synchronous' => 'NORMAL' ],
397  'dbDirectory' => \$wgSQLiteDataDir,
398  'trxMode' => 'IMMEDIATE',
399  'flags' => 0
400 ];
401 \$wgJobTypeConf['default'] = [
402  'class' => 'JobQueueDB',
403  'claimTTL' => 3600,
404  'server' => [
405  'type' => 'sqlite',
406  'dbname' => \"{\$wgDBname}_jobqueue\",
407  'tablePrefix' => '',
408  'variables' => [ 'synchronous' => 'NORMAL' ],
409  'dbDirectory' => \$wgSQLiteDataDir,
410  'trxMode' => 'IMMEDIATE',
411  'flags' => 0
412  ]
413 ];";
414  }
415 }
SqliteInstaller\createDataDir
static createDataDir( $dir)
Definition: SqliteInstaller.php:175
SqliteInstaller\makeStubDBFile
makeStubDBFile( $dir, $db)
Definition: SqliteInstaller.php:321
Wikimedia\Rdbms\Database
Relational database abstraction object.
Definition: Database.php:49
SqliteInstaller\setupDatabase
setupDatabase()
Definition: SqliteInstaller.php:225
StatusValue\newFatal
static newFatal( $message,... $parameters)
Factory function for fatal errors.
Definition: StatusValue.php:69
Wikimedia\Rdbms\DatabaseSqlite
Definition: DatabaseSqlite.php:38
SqliteInstaller
Class for setting up the MediaWiki database using SQLLite.
Definition: SqliteInstaller.php:34
SqliteInstaller\openConnection
openConnection()
Definition: SqliteInstaller.php:192
DatabaseInstaller\checkExtension
static checkExtension( $name)
Convenience function.
Definition: DatabaseInstaller.php:443
wfMkdirParents
wfMkdirParents( $dir, $mode=null, $caller=null)
Make directory, and make all parent directories if they don't exist.
Definition: GlobalFunctions.php:1966
SqliteInstaller\checkDataDir
static checkDataDir( $dir)
Check if the data directory is writable or can be created.
Definition: SqliteInstaller.php:145
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:514
Wikimedia\Rdbms\DatabaseSqlite\getServerVersion
getServerVersion()
Definition: DatabaseSqlite.php:793
SqliteInstaller\getConnectForm
getConnectForm()
Get HTML for a web form that configures this database.
Definition: SqliteInstaller.php:90
$res
$res
Definition: testCompression.php:52
SqliteInstaller\isCompiled
isCompiled()
Definition: SqliteInstaller.php:53
Status
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition: Status.php:40
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:111
$IP
$IP
Definition: update.php:3
DatabaseInstaller\setupSchemaVars
setupSchemaVars()
Set appropriate schema variables in the current database connection.
Definition: DatabaseInstaller.php:342
SqliteInstaller\setupSearchIndex
setupSearchIndex(&$status)
Definition: SqliteInstaller.php:347
SqliteInstaller\submitConnectForm
submitConnectForm()
Definition: SqliteInstaller.php:123
DatabaseInstaller\getVar
getVar( $var, $default=null)
Get a variable, taking local defaults into account.
Definition: DatabaseInstaller.php:484
StatusValue\newGood
static newGood( $value=null)
Factory function for good results.
Definition: StatusValue.php:81
SqliteInstaller\createTables
createTables()
Definition: SqliteInstaller.php:337
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:73
SqliteInstaller\checkPrerequisites
checkPrerequisites()
Definition: SqliteInstaller.php:61
$status
return $status
Definition: SyntaxHighlight.php:347
SqliteInstaller\getLocalSettings
getLocalSettings()
Definition: SqliteInstaller.php:372
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:210
DatabaseInstaller\setVar
setVar( $name, $value)
Convenience alias for $this->parent->setVar()
Definition: DatabaseInstaller.php:501
DatabaseInstaller\setVarsFromRequest
setVarsFromRequest( $varNames)
Convenience function to set variables based on form data.
Definition: DatabaseInstaller.php:607
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
Installer\maybeGetWebserverPrimaryGroup
static maybeGetWebserverPrimaryGroup()
On POSIX systems return the primary group of the webserver we're running under.
Definition: Installer.php:661