PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/francisreboucas/revolution
PHP | 212 lines | 192 code | 0 blank | 20 comment | 0 complexity | f081b3bbbf929ff620e18ba8fd0c9122 MD5 | raw file
  1. <?php
  2. /*
  3. * MODX Revolution
  4. *
  5. * Copyright 2006-2011 by MODX, LLC.
  6. * All rights reserved.
  7. *
  8. * This program is free software; you can redistribute it and/or modify it under
  9. * the terms of the GNU General Public License as published by the Free Software
  10. * Foundation; either version 2 of the License, or (at your option) any later
  11. * version.
  12. *
  13. * This program is distributed in the hope that it will be useful, but WITHOUT
  14. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  15. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU General Public License along with
  19. * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  20. * Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. require_once (strtr(realpath(dirname(__FILE__)), '\\', '/') . '/modinstalldriver.class.php');
  23. /**
  24. * Provides query abstraction for setup using the MySQL database
  25. *
  26. * @package setup
  27. * @subpackage drivers
  28. */
  29. class modInstallDriver_mysql extends modInstallDriver {
  30. /**
  31. * MySQL check for mysql extension
  32. * {@inheritDoc}
  33. */
  34. public function verifyExtension() {
  35. return extension_loaded('mysql') && function_exists('mysql_connect');
  36. }
  37. /**
  38. * MySQL check for mysql_pdo extension
  39. * {@inheritDoc}
  40. */
  41. public function verifyPDOExtension() {
  42. return extension_loaded('pdo_mysql');
  43. }
  44. /**
  45. * MySQL process for getting the default collation
  46. * {@inheritDoc}
  47. */
  48. public function getCollation() {
  49. $collation = 'utf8_bin';
  50. $stmt = $this->xpdo->query("SHOW SESSION VARIABLES LIKE 'collation_database'");
  51. if ($stmt && $stmt instanceof PDOStatement) {
  52. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  53. $collation = $row['Value'];
  54. $stmt->closeCursor();
  55. }
  56. return $collation;
  57. }
  58. /**
  59. * MySQL collation listing
  60. * {@inheritDoc}
  61. */
  62. public function getCollations($collation = '') {
  63. $collations = null;
  64. $stmt = $this->xpdo->query("SHOW COLLATION");
  65. if ($stmt && $stmt instanceof PDOStatement) {
  66. $collations = array();
  67. if (empty($collation)) $collation = $this->getCollation();
  68. while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
  69. $col = array();
  70. $col['selected'] = ($row['Collation']==$collation ? ' selected="selected"' : '');
  71. $col['value'] = $row['Collation'];
  72. $col['name'] = $row['Collation'];
  73. $collations[$row['Collation']] = $col;
  74. }
  75. ksort($collations);
  76. }
  77. return $collations;
  78. }
  79. /**
  80. * Get the MySQL charset based on collation, or default.
  81. * {@inheritDoc}
  82. */
  83. public function getCharset($collation = '') {
  84. $charset = 'utf8';
  85. if (empty($collation)) {
  86. $collation = $this->getCollation();
  87. }
  88. $pos = strpos($collation, '_');
  89. if ($pos > 0) {
  90. $charset = substr($collation, 0, $pos);
  91. }
  92. return $charset;
  93. }
  94. /**
  95. * Get charset listing for MySQL.
  96. * {@inheritDoc}
  97. */
  98. public function getCharsets($charset = '') {
  99. $charsets = null;
  100. $stmt = $this->xpdo->query('SHOW CHARSET');
  101. if ($stmt && $stmt instanceof PDOStatement) {
  102. $charsets = array();
  103. if (empty($charset)) $charset = $this->getCharset();
  104. while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
  105. $col = array();
  106. $col['selected'] = $row['Charset']==$charset ? ' selected="selected"' : '';
  107. $col['value'] = $row['Charset'];
  108. $col['name'] = $row['Charset'];
  109. $charsets[$row['Charset']] = $col;
  110. }
  111. ksort($charsets);
  112. }
  113. return $charsets;
  114. }
  115. /**
  116. * MySQL syntax for table prefix check
  117. * {@inheritDoc}
  118. */
  119. public function testTablePrefix($database,$prefix) {
  120. return 'SELECT COUNT('.$this->xpdo->escape('id').') AS '.$this->xpdo->escape('ct').' FROM '.$this->xpdo->escape($database).'.'.$this->xpdo->escape($prefix.'site_content');
  121. }
  122. /**
  123. * MySQL syntax for table truncation
  124. * {@inheritDoc}
  125. */
  126. public function truncate($table) {
  127. return 'TRUNCATE '.$this->xpdo->escape($table);
  128. }
  129. /**
  130. * MySQL check for server version
  131. * {@inheritDoc}
  132. */
  133. public function verifyServerVersion() {
  134. $handler = @mysql_connect($this->install->settings->get('database_server'),$this->install->settings->get('database_user'),$this->install->settings->get('database_password'));
  135. $mysqlVersion = @mysql_get_server_info($handler);
  136. $mysqlVersion = $this->_sanitizeVersion($mysqlVersion);
  137. if (empty($mysqlVersion)) {
  138. return array('result' => 'warning', 'message' => $this->install->lexicon('mysql_version_server_nf'),'version' => $mysqlVersion);
  139. }
  140. $mysql_ver_comp = version_compare($mysqlVersion,'4.1.20','>=');
  141. $mysql_ver_comp_5051 = version_compare($mysqlVersion,'5.0.51','==');
  142. $mysql_ver_comp_5051a = version_compare($mysqlVersion,'5.0.51a','==');
  143. if (!$mysql_ver_comp) { /* ancient driver warning */
  144. return array('result' => 'failure','message' => $this->install->lexicon('mysql_version_fail',array('version' => $mysqlVersion)),'version' => $mysqlVersion);
  145. } else if ($mysql_ver_comp_5051 || $mysql_ver_comp_5051a) { /* 5.0.51a. bad. berry bad. */
  146. return array('result' => 'failure','message' => $this->install->lexicon('mysql_version_5051',array('version' => $mysqlVersion)),'version' => $mysqlVersion);
  147. } else {
  148. return array('result' => 'success','message' => $this->install->lexicon('mysql_version_success',array('version' => $mysqlVersion)),'version' => $mysqlVersion);
  149. }
  150. }
  151. /**
  152. * MySQL check for client version
  153. * {@inheritDoc}
  154. */
  155. public function verifyClientVersion() {
  156. $mysqlVersion = @mysql_get_client_info();
  157. $mysqlVersion = $this->_sanitizeVersion($mysqlVersion);
  158. if (empty($mysqlVersion)) {
  159. return array('result' => 'warning','message' => $this->install->lexicon('mysql_version_client_nf'),'version' => $mysqlVersion);
  160. }
  161. $mysql_ver_comp = version_compare($mysqlVersion,'4.1.20','>=');
  162. if (!$mysql_ver_comp) {
  163. return array('result' => 'warning','message' => $this->install->lexicon('mysql_version_client_old',array('version' => $mysqlVersion)),'version' => $mysqlVersion);
  164. } else {
  165. return array('result' => 'success','message' => $this->install->lexicon('mysql_version_success',array('version' => $mysqlVersion)),'version' => $mysqlVersion);
  166. }
  167. }
  168. /**
  169. * MySQL syntax to add an index
  170. * {@inheritDoc}
  171. */
  172. public function addIndex($table,$name,$column) {
  173. return 'ALTER TABLE '.$this->xpdo->escape($table).' ADD INDEX '.$this->xpdo->escape($name).' ('.$this->xpdo->escape($column).')"';
  174. }
  175. /**
  176. * MySQL syntax to drop an index
  177. * {@inheritDoc}
  178. */
  179. public function dropIndex($table,$index) {
  180. return 'ALTER TABLE '.$this->xpdo->escape($table).' DROP INDEX '.$this->xpdo->escape($index);
  181. }
  182. /**
  183. * Cleans a mysql version string that often has extra junk in certain distros
  184. *
  185. * @param string $mysqlVersion The version note to sanitize
  186. * @return string The sanitized version
  187. */
  188. protected function _sanitizeVersion($mysqlVersion) {
  189. $mysqlVersion = str_replace(array(
  190. 'mysqlnd ',
  191. '-dev',
  192. ' ',
  193. ),'',$mysqlVersion);
  194. return $mysqlVersion;
  195. }
  196. }