Tasks.php 9.55 KB
Newer Older
1 2
<?php

3
namespace Drupal\Core\Database\Install;
4

5
use Drupal\Core\Database\Database;
6 7 8 9 10 11 12 13

/**
 * Database installer structure.
 *
 * Defines basic Drupal requirements for databases.
 */
abstract class Tasks {

14 15 16 17 18 19 20
  /**
   * The name of the PDO driver this database type requires.
   *
   * @var string
   */
  protected $pdoDriver;

21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
  /**
   * Structure that describes each task to run.
   *
   * @var array
   *
   * Each value of the tasks array is an associative array defining the function
   * to call (optional) and any arguments to be passed to the function.
   */
  protected $tasks = array(
    array(
      'function'    => 'checkEngineVersion',
      'arguments'   => array(),
    ),
    array(
      'arguments'   => array(
        'CREATE TABLE {drupal_install_test} (id int NULL)',
        'Drupal can use CREATE TABLE database commands.',
        'Failed to <strong>CREATE</strong> a test table on your database server with the command %query. The server reports the following message: %error.<p>Are you sure the configured username has the necessary permissions to create tables in the database?</p>',
        TRUE,
      ),
    ),
    array(
      'arguments'   => array(
        'INSERT INTO {drupal_install_test} (id) VALUES (1)',
        'Drupal can use INSERT database commands.',
        'Failed to <strong>INSERT</strong> a value into a test table on your database server. We tried inserting a value with the command %query and the server reported the following error: %error.',
      ),
    ),
    array(
      'arguments'   => array(
        'UPDATE {drupal_install_test} SET id = 2',
        'Drupal can use UPDATE database commands.',
        'Failed to <strong>UPDATE</strong> a value in a test table on your database server. We tried updating a value with the command %query and the server reported the following error: %error.',
      ),
    ),
    array(
      'arguments'   => array(
        'DELETE FROM {drupal_install_test}',
        'Drupal can use DELETE database commands.',
        'Failed to <strong>DELETE</strong> a value from a test table on your database server. We tried deleting a value with the command %query and the server reported the following error: %error.',
      ),
    ),
    array(
      'arguments'   => array(
        'DROP TABLE {drupal_install_test}',
        'Drupal can use DROP TABLE database commands.',
        'Failed to <strong>DROP</strong> a test table from your database server. We tried dropping a table with the command %query and the server reported the following error %error.',
      ),
    ),
  );

  /**
   * Results from tasks.
   *
   * @var array
   */
77 78 79 80
  protected $results = array(
    'fail' => array(),
    'pass' => array(),
  );
81 82 83 84 85

  /**
   * Ensure the PDO driver is supported by the version of PHP in use.
   */
  protected function hasPdoDriver() {
86
    return in_array($this->pdoDriver, \PDO::getAvailableDrivers());
87 88 89 90 91 92
  }

  /**
   * Assert test as failed.
   */
  protected function fail($message) {
93
    $this->results['fail'][] = $message;
94 95 96 97 98 99
  }

  /**
   * Assert test as a pass.
   */
  protected function pass($message) {
100
    $this->results['pass'][] = $message;
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
  }

  /**
   * Check whether Drupal is installable on the database.
   */
  public function installable() {
    return $this->hasPdoDriver() && empty($this->error);
  }

  /**
   * Return the human-readable name of the driver.
   */
  abstract public function name();

  /**
   * Return the minimum required version of the engine.
   *
   * @return
   *   A version string. If not NULL, it will be checked against the version
   *   reported by the Database engine using version_compare().
   */
  public function minimumVersion() {
    return NULL;
  }

  /**
   * Run database tasks and tests to see if Drupal can run on the database.
128 129 130
   *
   * @return array
   *   A list of error messages.
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
   */
  public function runTasks() {
    // We need to establish a connection before we can run tests.
    if ($this->connect()) {
      foreach ($this->tasks as $task) {
        if (!isset($task['function'])) {
          $task['function'] = 'runTestQuery';
        }
        if (method_exists($this, $task['function'])) {
          // Returning false is fatal. No other tasks can run.
          if (FALSE === call_user_func_array(array($this, $task['function']), $task['arguments'])) {
            break;
          }
        }
        else {
146
          $this->fail(t("Failed to run all tasks against the database server. The task %task wasn't found.", array('%task' => $task['function'])));
147 148 149
        }
      }
    }
150
    return $this->results['fail'];
151 152 153 154 155 156 157 158 159 160 161 162 163
  }

  /**
   * Check if we can connect to the database.
   */
  protected function connect() {
    try {
      // This doesn't actually test the connection.
      db_set_active();
      // Now actually do a check.
      Database::getConnection();
      $this->pass('Drupal can CONNECT to the database ok.');
    }
164
    catch (\Exception $e) {
165
      $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>', array('%error' => $e->getMessage())));
166 167 168 169 170 171 172 173 174 175
      return FALSE;
    }
    return TRUE;
  }

  /**
   * Run SQL tests to ensure the database can execute commands with the current user.
   */
  protected function runTestQuery($query, $pass, $fail, $fatal = FALSE) {
    try {
176
      Database::getConnection()->query($query);
177
      $this->pass(t($pass));
178
    }
179
    catch (\Exception $e) {
180
      $this->fail(t($fail, array('%query' => $query, '%error' => $e->getMessage(), '%name' => $this->name())));
181 182 183 184 185 186 187 188
      return !$fatal;
    }
  }

  /**
   * Check the engine version.
   */
  protected function checkEngineVersion() {
189
    // Ensure that the database server has the right version.
190
    if ($this->minimumVersion() && version_compare(Database::getConnection()->version(), $this->minimumVersion(), '<')) {
191
      $this->fail(t("The database server version %version is less than the minimum required version %minimum_version.", array('%version' => Database::getConnection()->version(), '%minimum_version' => $this->minimumVersion())));
192 193 194 195 196 197 198
    }
  }

  /**
   * Return driver specific configuration options.
   *
   * @param $database
199
   *   An array of driver specific configuration options.
200 201 202 203
   *
   * @return
   *   The options form array.
   */
204
  public function getFormOptions(array $database) {
205 206
    $form['database'] = array(
      '#type' => 'textfield',
207
      '#title' => t('Database name'),
208 209
      '#default_value' => empty($database['database']) ? '' : $database['database'],
      '#size' => 45,
210
      '#required' => TRUE,
211 212 213 214 215
      '#states' => array(
        'required' => array(
          ':input[name=driver]' => array('value' => $this->pdoDriver),
        ),
      ),
216 217 218 219
    );

    $form['username'] = array(
      '#type' => 'textfield',
220
      '#title' => t('Database username'),
221 222
      '#default_value' => empty($database['username']) ? '' : $database['username'],
      '#size' => 45,
223
      '#required' => TRUE,
224 225 226 227 228
      '#states' => array(
        'required' => array(
          ':input[name=driver]' => array('value' => $this->pdoDriver),
        ),
      ),
229 230 231 232
    );

    $form['password'] = array(
      '#type' => 'password',
233
      '#title' => t('Database password'),
234 235 236 237 238 239
      '#default_value' => empty($database['password']) ? '' : $database['password'],
      '#required' => FALSE,
      '#size' => 45,
    );

    $form['advanced_options'] = array(
240
      '#type' => 'details',
241
      '#title' => t('Advanced options'),
242 243 244 245 246
      '#weight' => 10,
    );

    $profile = drupal_get_profile();
    $db_prefix = ($profile == 'standard') ? 'drupal_' : $profile . '_';
247
    $form['advanced_options']['prefix'] = array(
248
      '#type' => 'textfield',
249
      '#title' => t('Table name prefix'),
250
      '#default_value' => empty($database['prefix']) ? '' : $database['prefix'],
251
      '#size' => 45,
252
      '#description' => t('If more than one application will be sharing this database, a unique table name prefix – such as %prefix – will prevent collisions.', array('%prefix' => $db_prefix)),
253 254 255 256 257
      '#weight' => 10,
    );

    $form['advanced_options']['host'] = array(
      '#type' => 'textfield',
258
      '#title' => t('Host'),
259 260 261 262 263 264 265 266
      '#default_value' => empty($database['host']) ? 'localhost' : $database['host'],
      '#size' => 45,
      // Hostnames can be 255 characters long.
      '#maxlength' => 255,
      '#required' => TRUE,
    );

    $form['advanced_options']['port'] = array(
267 268
      '#type' => 'number',
      '#title' => t('Port number'),
269
      '#default_value' => empty($database['port']) ? '' : $database['port'],
270
      '#min' => 0,
271
      '#max' => 65535,
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
    );

    return $form;
  }

  /**
   * Validates driver specific configuration settings.
   *
   * Checks to ensure correct basic database settings and that a proper
   * connection to the database can be established.
   *
   * @param $database
   *   An array of driver specific configuration options.
   *
   * @return
   *   An array of driver configuration errors, keyed by form element name.
   */
  public function validateDatabaseSettings($database) {
    $errors = array();

    // Verify the table prefix.
    if (!empty($database['prefix']) && is_string($database['prefix']) && !preg_match('/^[A-Za-z0-9_.]+$/', $database['prefix'])) {
294
      $errors[$database['driver'] . '][prefix'] = t('The database table prefix you have entered, %prefix, is invalid. The table prefix can only contain alphanumeric characters, periods, or underscores.', array('%prefix' => $database['prefix']));
295 296 297 298 299 300
    }

    return $errors;
  }

}