web/drupal/includes/install.mysql.inc
author cavaliet@caf4f556-3d62-0410-8435-a86758001935
Fri, 21 Aug 2009 16:26:26 +0000
branchdrupal
changeset 74 0ff3ba646492
permissions -rw-r--r--
Create branch for drupal with first commit from local working copy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
74
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
     1
<?php
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
     2
// $Id: install.mysql.inc,v 1.9 2008/01/23 09:59:29 goba Exp $
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
     3
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
     4
// MySQL specific install functions
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
     5
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
     6
/**
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
     7
 * Check if MySQL is available.
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
     8
 *
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
     9
 * @return
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    10
 *  TRUE/FALSE
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    11
 */
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    12
function mysql_is_available() {
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    13
  return function_exists('mysql_connect');
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    14
}
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    15
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    16
/**
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    17
 * Check if we can connect to MySQL.
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    18
 *
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    19
 * @return
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    20
 *  TRUE/FALSE
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    21
 */
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    22
function drupal_test_mysql($url, &$success) {
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    23
  if (!mysql_is_available()) {
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    24
    drupal_set_message(st('PHP MySQL support not enabled.'), 'error');
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    25
    return FALSE;
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    26
  }
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    27
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    28
  $url = parse_url($url);
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    29
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    30
  // Decode url-encoded information in the db connection string.
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    31
  $url['user'] = urldecode($url['user']);
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    32
  $url['pass'] = isset($url['pass']) ? urldecode($url['pass']) : '';
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    33
  $url['host'] = urldecode($url['host']);
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    34
  $url['path'] = urldecode($url['path']);
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    35
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    36
  // Allow for non-standard MySQL port.
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    37
  if (isset($url['port'])) {
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    38
    $url['host'] = $url['host'] .':'. $url['port'];
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    39
  }
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    40
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    41
  // Test connecting to the database.
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    42
  $connection = @mysql_connect($url['host'], $url['user'], $url['pass'], TRUE, 2);
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    43
  if (!$connection) {
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    44
    drupal_set_message(st('Failed to connect to your MySQL database server. MySQL reports the following message: %error.<ul><li>Are you sure you have the correct username and password?</li><li>Are you sure that you have typed the correct database hostname?</li><li>Are you sure that the database server is running?</li></ul>For more help, see the <a href="http://drupal.org/node/258">Installation and upgrading handbook</a>. If you are unsure what these terms mean you should probably contact your hosting provider.', array('%error' => mysql_error())), 'error');
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    45
    return FALSE;
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    46
  }
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    47
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    48
  // Test selecting the database.
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    49
  if (!mysql_select_db(substr($url['path'], 1))) {
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    50
    drupal_set_message(st('Failed to select your database on your MySQL database server, which means the connection username and password are valid, but there is a problem accessing your data. MySQL reports the following message: %error.<ul><li>Are you sure you have the correct database name?</li><li>Are you sure the database exists?</li><li>Are you sure the username has permission to access the database?</li></ul>For more help, see the <a href="http://drupal.org/node/258">Installation and upgrading handbook</a>. If you are unsure what these terms mean you should probably contact your hosting provider.', array('%error' => mysql_error())), 'error');
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    51
    return FALSE;
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    52
  }
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    53
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    54
  $success = array('CONNECT');
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    55
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    56
  // Test CREATE.
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    57
  $query = 'CREATE TABLE drupal_install_test (id int NULL)';
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    58
  $result = mysql_query($query);
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    59
  if ($error = mysql_error()) {
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    60
    drupal_set_message(st('Failed to create a test table on your MySQL database server with the command %query. MySQL reports the following message: %error.<ul><li>Are you sure the configured username has the necessary MySQL permissions to create tables in the database?</li></ul>For more help, see the <a href="http://drupal.org/node/258">Installation and upgrading handbook</a>. If you are unsure what these terms mean you should probably contact your hosting provider.', array('%query' => $query, '%error' => $error)), 'error');
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    61
    return FALSE;
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    62
  }
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    63
  $err = FALSE;
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    64
  $success[] = 'SELECT';
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    65
  $success[] = 'CREATE';
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    66
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    67
  // Test INSERT.
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    68
  $query = 'INSERT INTO drupal_install_test (id) VALUES (1)';
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    69
  $result = mysql_query($query);
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    70
  if ($error = mysql_error()) {
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    71
    drupal_set_message(st('Failed to insert a value into a test table on your MySQL database server. We tried inserting a value with the command %query and MySQL reported the following error: %error.', array('%query' => $query, '%error' => $error)), 'error');
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    72
    $err = TRUE;
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    73
  }
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    74
  else {
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    75
    $success[] = 'INSERT';
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    76
  }
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    77
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    78
  // Test UPDATE.
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    79
  $query = 'UPDATE drupal_install_test SET id = 2';
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    80
  $result = mysql_query($query);
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    81
  if ($error = mysql_error()) {
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    82
    drupal_set_message(st('Failed to update a value in a test table on your MySQL database server. We tried updating a value with the command %query and MySQL reported the following error: %error.', array('%query' => $query, '%error' => $error)), 'error');
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    83
    $err = TRUE;
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    84
  }
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    85
  else {
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    86
    $success[] = 'UPDATE';
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    87
  }
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    88
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    89
  // Test DELETE.
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    90
  $query = 'DELETE FROM drupal_install_test';
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    91
  $result = mysql_query($query);
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    92
  if ($error = mysql_error()) {
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    93
    drupal_set_message(st('Failed to delete a value from a test table on your MySQL database server. We tried deleting a value with the command %query and MySQL reported the following error: %error.', array('%query' => $query, '%error' => $error)), 'error');
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    94
    $err = TRUE;
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    95
  }
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    96
  else {
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    97
    $success[] = 'DELETE';
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    98
  }
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    99
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
   100
  // Test DROP.
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
   101
  $query = 'DROP TABLE drupal_install_test';
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
   102
  $result = mysql_query($query);
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
   103
  if ($error = mysql_error()) {
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
   104
    drupal_set_message(st('Failed to drop a test table from your MySQL database server. We tried dropping a table with the command %query and MySQL reported the following error %error.', array('%query' => $query, '%error' => $error)), 'error');
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
   105
    $err = TRUE;
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
   106
  }
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
   107
  else {
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
   108
    $success[] = 'DROP';
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
   109
  }
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
   110
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
   111
  if ($err) {
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
   112
    return FALSE;
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
   113
  }
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
   114
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
   115
  mysql_close($connection);
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
   116
  return TRUE;
0ff3ba646492 Create branch for drupal with first commit from local working copy
cavaliet@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
   117
}