Merge pull request #13676 from civicrm/5.11
[civicrm-core.git] / Civi / Install / Requirements.php
index ad0848e80befbce89c3cb8681a2f584d5004bd0f..4d2d3f72d423a3af9ab9497d850b315185765123 100644 (file)
@@ -28,6 +28,7 @@ class Requirements {
     'checkServerVariables',
     'checkMysqlConnectExists',
     'checkJsonEncodeExists',
+    'checkMultibyteExists',
   );
 
   protected $database_checks = array(
@@ -39,6 +40,7 @@ class Requirements {
     'checkMysqlTrigger',
     'checkMysqlThreadStack',
     'checkMysqlLockTables',
+    'checkMysqlUtf8mb4',
   );
 
   /**
@@ -216,6 +218,24 @@ class Requirements {
     return $results;
   }
 
+  /**
+   * CHeck that PHP Multibyte functions are enabled.
+   * @return array
+   */
+  public function checkMultibyteExists() {
+    $results = array(
+      'title' => 'CiviCRM MultiByte encoding support',
+      'severity' => $this::REQUIREMENT_OK,
+      'details' => 'PHP Multibyte etension found',
+    );
+    if (!function_exists('mb_substr')) {
+      $results['severity'] = $this::REQUIREMENT_ERROR;
+      $results['details'] = 'PHP Multibyte extension has not been installed and enabled';
+    }
+
+    return $results;
+  }
+
   /**
    * @return array
    */
@@ -563,4 +583,65 @@ class Requirements {
     return $results;
   }
 
+  /**
+   * @param $db_config
+   *
+   * @return array
+   */
+  public function checkMysqlUtf8mb4($db_config) {
+    $results = array(
+      'title' => 'CiviCRM MySQL utf8mb4 Support',
+      'severity' => $this::REQUIREMENT_OK,
+      'details' => 'Your system supports the MySQL utf8mb4 character set.',
+    );
+
+    $conn = $this->connect($db_config);
+    if (!$conn) {
+      $results['severity'] = $this::REQUIREMENT_ERROR;
+      $results['details'] = 'Could not connect to database';
+      return $results;
+    }
+
+    if (!@mysqli_select_db($conn, $db_config['database'])) {
+      $results['severity'] = $this::REQUIREMENT_ERROR;
+      $results['details'] = 'Could not select the database';
+      mysqli_close($conn);
+      return $results;
+    }
+
+    $r = mysqli_query($conn, 'CREATE TABLE civicrm_utf8mb4_test (id VARCHAR(255), PRIMARY KEY(id(255))) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ROW_FORMAT=DYNAMIC ENGINE=INNODB');
+    if (!$r) {
+      $results['severity'] = $this::REQUIREMENT_WARNING;
+      $results['details'] = 'It is recommended, though not yet required, to configure your MySQL server for utf8mb4 support. You will need the following MySQL server configuration: innodb_large_prefix=true innodb_file_format=barracuda innodb_file_per_table=true';
+      mysqli_close($conn);
+      return $results;
+    }
+    mysqli_query('DROP TABLE civicrm_utf8mb4_test');
+
+    // Ensure that the MySQL driver supports utf8mb4 encoding.
+    $version = mysqli_get_client_info($conn);
+    if (strpos($version, 'mysqlnd') !== FALSE) {
+      // The mysqlnd driver supports utf8mb4 starting at version 5.0.9.
+      $version = preg_replace('/^\D+([\d.]+).*/', '$1', $version);
+      if (version_compare($version, '5.0.9', '<')) {
+        $results['severity'] = $this::REQUIREMENT_WARNING;
+        $results['details'] = 'It is recommended, though not yet required, to upgrade your PHP MySQL driver (mysqlnd) to >= 5.0.9 for utf8mb4 support.';
+        mysqli_close($conn);
+        return $results;
+      }
+    }
+    else {
+      // The libmysqlclient driver supports utf8mb4 starting at version 5.5.3.
+      if (version_compare($version, '5.5.3', '<')) {
+        $results['severity'] = $this::REQUIREMENT_WARNING;
+        $results['details'] = 'It is recommended, though not yet required, to upgrade your PHP MySQL driver (libmysqlclient) to >= 5.5.3 for utf8mb4 support.';
+        mysqli_close($conn);
+        return $results;
+      }
+    }
+
+    mysqli_close($conn);
+    return $results;
+  }
+
 }