PageRenderTime 26ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/setup/includes/drivers/modinstalldriver_mysql.class.php

http://github.com/modxcms/revolution
PHP | 229 lines | 221 code | 0 blank | 8 comment | 0 complexity | 63b68f4412552951266717500ee495c9 MD5 | raw file
Possible License(s): GPL-2.0, Apache-2.0, BSD-3-Clause, LGPL-2.1
  1. <?php
  2. /*
  3. * This file is part of MODX Revolution.
  4. *
  5. * Copyright (c) MODX, LLC. All Rights Reserved.
  6. *
  7. * For complete copyright and license information, see the COPYRIGHT and LICENSE
  8. * files found in the top-level directory of this distribution.
  9. */
  10. require_once (strtr(realpath(dirname(__FILE__)), '\\', '/') . '/modinstalldriver.class.php');
  11. /**
  12. * Provides query abstraction for setup using the MySQL database
  13. *
  14. * @package setup
  15. * @subpackage drivers
  16. */
  17. class modInstallDriver_mysql extends modInstallDriver {
  18. /**
  19. * MySQL only needs PDO extension
  20. * {@inheritDoc}
  21. */
  22. public function verifyExtension() {
  23. return true;
  24. }
  25. /**
  26. * MySQL check for mysql_pdo extension
  27. * {@inheritDoc}
  28. */
  29. public function verifyPDOExtension() {
  30. return extension_loaded('pdo_mysql');
  31. }
  32. /**
  33. * MySQL process for getting the default collation
  34. * {@inheritDoc}
  35. */
  36. public function getCollation() {
  37. $collation = 'utf8_bin';
  38. $stmt = $this->xpdo->query("SHOW SESSION VARIABLES LIKE 'collation_database'");
  39. if ($stmt && $stmt instanceof PDOStatement) {
  40. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  41. $collation = $row['Value'];
  42. $stmt->closeCursor();
  43. }
  44. return $collation;
  45. }
  46. /**
  47. * MySQL collation listing
  48. * {@inheritDoc}
  49. */
  50. public function getCollations($collation = '') {
  51. $collations = null;
  52. $stmt = $this->xpdo->query("SHOW COLLATION");
  53. if ($stmt && $stmt instanceof PDOStatement) {
  54. $collations = array();
  55. if (empty($collation)) $collation = $this->getCollation();
  56. while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
  57. $col = array();
  58. $col['selected'] = ($row['Collation']==$collation ? ' selected="selected"' : '');
  59. $col['value'] = $row['Collation'];
  60. $col['name'] = $row['Collation'];
  61. $collations[$row['Collation']] = $col;
  62. }
  63. ksort($collations);
  64. }
  65. return $collations;
  66. }
  67. /**
  68. * Get the MySQL charset based on collation, or default.
  69. * {@inheritDoc}
  70. */
  71. public function getCharset($collation = '') {
  72. $charset = 'utf8';
  73. if (empty($collation)) {
  74. $collation = $this->getCollation();
  75. }
  76. $pos = strpos($collation, '_');
  77. if ($pos > 0) {
  78. $charset = substr($collation, 0, $pos);
  79. }
  80. return $charset;
  81. }
  82. /**
  83. * Get charset listing for MySQL.
  84. * {@inheritDoc}
  85. */
  86. public function getCharsets($charset = '') {
  87. $charsets = null;
  88. $stmt = $this->xpdo->query('SHOW CHARSET');
  89. if ($stmt && $stmt instanceof PDOStatement) {
  90. $charsets = array();
  91. if (empty($charset)) $charset = $this->getCharset();
  92. while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
  93. $col = array();
  94. $col['selected'] = $row['Charset']==$charset ? ' selected="selected"' : '';
  95. $col['value'] = $row['Charset'];
  96. $col['name'] = $row['Charset'];
  97. $charsets[$row['Charset']] = $col;
  98. }
  99. ksort($charsets);
  100. }
  101. return $charsets;
  102. }
  103. /**
  104. * MySQL syntax for table prefix check
  105. * {@inheritDoc}
  106. */
  107. public function testTablePrefix($database,$prefix) {
  108. return 'SELECT COUNT('.$this->xpdo->escape('id').') AS '.$this->xpdo->escape('ct').' FROM '.$this->xpdo->escape($database).'.'.$this->xpdo->escape($prefix.'site_content');
  109. }
  110. /**
  111. * MySQL syntax for table truncation
  112. * {@inheritDoc}
  113. */
  114. public function truncate($table) {
  115. return 'TRUNCATE '.$this->xpdo->escape($table);
  116. }
  117. /**
  118. * MySQL check for server version
  119. * {@inheritDoc}
  120. */
  121. public function verifyServerVersion() {
  122. $version = $this->getServerVersion();
  123. $isMariaDB = stripos($version, 'mariadb') !== false;
  124. $mysqlVersion = $this->_sanitizeVersion($version);
  125. if (empty($mysqlVersion)) {
  126. $config_options = $this->install->settings->get('config_options');
  127. $config_options[xPDO::OPT_OVERRIDE_TABLE_TYPE] = 'MyISAM';
  128. $this->install->settings->set('config_options', $config_options);
  129. $this->install->settings->store();
  130. return array('result' => 'warning', 'message' => $this->install->lexicon('mysql_version_server_nf'),'version' => $mysqlVersion);
  131. }
  132. $mysql_ver_comp = version_compare($mysqlVersion,'4.1.20','>=');
  133. $mysql_ver_comp_5051 = version_compare($mysqlVersion,'5.0.51','==');
  134. $mysql_ver_comp_5051a = version_compare($mysqlVersion,'5.0.51a','==');
  135. if ($isMariaDB) {
  136. $mysql_ver_comp_myisam = version_compare($mysqlVersion, '10.0.5', '<');
  137. } else {
  138. $mysql_ver_comp_myisam = version_compare($mysqlVersion, '5.6', '<');
  139. }
  140. if ($mysql_ver_comp_myisam) {
  141. $config_options = $this->install->settings->get('config_options');
  142. $config_options[xPDO::OPT_OVERRIDE_TABLE_TYPE] = 'MyISAM';
  143. $this->install->settings->set('config_options', $config_options);
  144. $this->install->settings->store();
  145. }
  146. if (!$mysql_ver_comp) { /* ancient driver warning */
  147. return array('result' => 'failure','message' => $this->install->lexicon('mysql_version_fail',array('version' => $mysqlVersion)),'version' => $mysqlVersion);
  148. } else if ($mysql_ver_comp_5051 || $mysql_ver_comp_5051a) { /* 5.0.51a. bad. berry bad. */
  149. return array('result' => 'failure','message' => $this->install->lexicon('mysql_version_5051',array('version' => $mysqlVersion)),'version' => $mysqlVersion);
  150. } else {
  151. return array('result' => 'success','message' => $this->install->lexicon('mysql_version_success',array('version' => $mysqlVersion)),'version' => $mysqlVersion);
  152. }
  153. }
  154. /**
  155. * MySQL check for client version
  156. * {@inheritDoc}
  157. */
  158. public function verifyClientVersion() {
  159. $mysqlVersion = $this->xpdo->getAttribute(PDO::ATTR_CLIENT_VERSION);
  160. $mysqlVersion = $this->_sanitizeVersion($mysqlVersion);
  161. if (empty($mysqlVersion)) {
  162. return array('result' => 'warning','message' => $this->install->lexicon('mysql_version_client_nf'),'version' => $mysqlVersion);
  163. }
  164. $mysql_ver_comp = version_compare($mysqlVersion,'4.1.20','>=');
  165. if (!$mysql_ver_comp) {
  166. return array('result' => 'warning','message' => $this->install->lexicon('mysql_version_client_old',array('version' => $mysqlVersion)),'version' => $mysqlVersion);
  167. } else {
  168. return array('result' => 'success','message' => $this->install->lexicon('mysql_version_success',array('version' => $mysqlVersion)),'version' => $mysqlVersion);
  169. }
  170. }
  171. /**
  172. * MySQL syntax to add an index
  173. * {@inheritDoc}
  174. */
  175. public function addIndex($table,$name,$column) {
  176. return 'ALTER TABLE '.$this->xpdo->escape($table).' ADD INDEX '.$this->xpdo->escape($name).' ('.$this->xpdo->escape($column).')"';
  177. }
  178. /**
  179. * MySQL syntax to drop an index
  180. * {@inheritDoc}
  181. */
  182. public function dropIndex($table,$index) {
  183. return 'ALTER TABLE '.$this->xpdo->escape($table).' DROP INDEX '.$this->xpdo->escape($index);
  184. }
  185. protected function getServerVersion() {
  186. try {
  187. $stmt = $this->xpdo->query('SELECT VERSION();');
  188. $value = $this->xpdo->getValue($stmt);
  189. } catch (Exception $e) {
  190. $value = $this->xpdo->getAttribute(PDO::ATTR_SERVER_VERSION);
  191. }
  192. return $value;
  193. }
  194. /**
  195. * Cleans a mysql version string that often has extra junk in certain distros
  196. *
  197. * @param string $mysqlVersion The version note to sanitize
  198. * @return string The sanitized version
  199. */
  200. protected function _sanitizeVersion($mysqlVersion) {
  201. $mysqlVersion = str_replace(array(
  202. 'mysqlnd ',
  203. '-dev',
  204. ' ',
  205. ),'',$mysqlVersion);
  206. return $mysqlVersion;
  207. }
  208. }