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