Tasks.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. namespace Drupal\Core\Database\Driver\sqlite\Install;
  3. use Drupal\Core\Database\Database;
  4. use Drupal\Core\Database\Driver\sqlite\Connection;
  5. use Drupal\Core\Database\DatabaseNotFoundException;
  6. use Drupal\Core\Database\Install\Tasks as InstallTasks;
  7. /**
  8. * Specifies installation tasks for SQLite databases.
  9. */
  10. class Tasks extends InstallTasks {
  11. /**
  12. * {@inheritdoc}
  13. */
  14. protected $pdoDriver = 'sqlite';
  15. /**
  16. * {@inheritdoc}
  17. */
  18. public function name() {
  19. return t('SQLite');
  20. }
  21. /**
  22. * {@inheritdoc}
  23. */
  24. public function minimumVersion() {
  25. return '3.7.11';
  26. }
  27. /**
  28. * {@inheritdoc}
  29. */
  30. public function getFormOptions(array $database) {
  31. $form = parent::getFormOptions($database);
  32. // Remove the options that only apply to client/server style databases.
  33. unset($form['username'], $form['password'], $form['advanced_options']['host'], $form['advanced_options']['port']);
  34. // Make the text more accurate for SQLite.
  35. $form['database']['#title'] = t('Database file');
  36. $form['database']['#description'] = t('The absolute path to the file where @drupal data will be stored. This must be writable by the web server and should exist outside of the web root.', ['@drupal' => drupal_install_profile_distribution_name()]);
  37. $default_database = \Drupal::service('site.path') . '/files/.ht.sqlite';
  38. $form['database']['#default_value'] = empty($database['database']) ? $default_database : $database['database'];
  39. return $form;
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. protected function connect() {
  45. try {
  46. // This doesn't actually test the connection.
  47. Database::setActiveConnection();
  48. // Now actually do a check.
  49. Database::getConnection();
  50. $this->pass('Drupal can CONNECT to the database ok.');
  51. }
  52. catch (\Exception $e) {
  53. // Attempt to create the database if it is not found.
  54. if ($e->getCode() == Connection::DATABASE_NOT_FOUND) {
  55. // Remove the database string from connection info.
  56. $connection_info = Database::getConnectionInfo();
  57. $database = $connection_info['default']['database'];
  58. // We cannot use file_directory_temp() here because we haven't yet
  59. // successfully connected to the database.
  60. $connection_info['default']['database'] = \Drupal::service('file_system')->tempnam(sys_get_temp_dir(), 'sqlite');
  61. // In order to change the Database::$databaseInfo array, need to remove
  62. // the active connection, then re-add it with the new info.
  63. Database::removeConnection('default');
  64. Database::addConnectionInfo('default', 'default', $connection_info['default']);
  65. try {
  66. Database::getConnection()->createDatabase($database);
  67. Database::closeConnection();
  68. // Now, restore the database config.
  69. Database::removeConnection('default');
  70. $connection_info['default']['database'] = $database;
  71. Database::addConnectionInfo('default', 'default', $connection_info['default']);
  72. // Check the database connection.
  73. Database::getConnection();
  74. $this->pass('Drupal can CONNECT to the database ok.');
  75. }
  76. catch (DatabaseNotFoundException $e) {
  77. // Still no dice; probably a permission issue. Raise the error to the
  78. // installer.
  79. $this->fail(t('Failed to open or create database file %database. The database engine reports the following message when attempting to create the database: %error.', ['%database' => $database, '%error' => $e->getMessage()]));
  80. }
  81. }
  82. else {
  83. // Database connection failed for some other reason than the database
  84. // not existing.
  85. $this->fail(t('Failed to connect to database. The database engine reports the following message: %error.<ul><li>Does the database file exist?</li><li>Does web server have permission to write to the database file?</li>Does the web server have permission to write to the directory the database file should be created in?</li></ul>', ['%error' => $e->getMessage()]));
  86. return FALSE;
  87. }
  88. }
  89. return TRUE;
  90. }
  91. }