/app/code/core/Mage/Install/Model/Installer/Db.php

https://github.com/rgranadino/magento-mirror · PHP · 192 lines · 98 code · 19 blank · 75 comment · 19 complexity · e05f6595b1545b72bb13d4358db68e3a MD5 · raw file

  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Install
  23. * @copyright Copyright (c) 2011 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * DB Installer
  28. *
  29. * @category Mage
  30. * @package Mage_Install
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Install_Model_Installer_Db extends Mage_Install_Model_Installer_Abstract
  34. {
  35. /**
  36. * @var database resource
  37. */
  38. protected $_dbResource;
  39. /**
  40. * Check database connection
  41. * and return checked connection data
  42. *
  43. * @param array $data
  44. * @return array
  45. */
  46. public function checkDbConnectionData($data)
  47. {
  48. $data = $this->_getCheckedData($data);
  49. try {
  50. $dbModel = ($data['db_model']);
  51. if (!$resource = $this->_getDbResource($dbModel)) {
  52. Mage::throwException(Mage::helper('install')->__('No resource for %s DB model.', $dbModel));
  53. }
  54. $resource->setConfig($data);
  55. // check required extensions
  56. $absenteeExtensions = array();
  57. $extensions = $resource->getRequiredExtensions();
  58. foreach ($extensions as $extName) {
  59. if (!extension_loaded($extName)) {
  60. $absenteeExtensions[] = $extName;
  61. }
  62. }
  63. if (!empty($absenteeExtensions)) {
  64. Mage::throwException(Mage::helper('install')->__('PHP Extensions "%s" must be loaded.',
  65. implode(',', $absenteeExtensions))
  66. );
  67. }
  68. $version = $resource->getVersion();
  69. $requiredVersion = (string) Mage::getConfig()
  70. ->getNode(sprintf('install/databases/%s/min_version', $dbModel));
  71. // check DB server version
  72. if (version_compare($version, $requiredVersion) == -1) {
  73. Mage::throwException(Mage::helper('install')->__('The database server version '
  74. . 'does not match system requirements (required: %s, actual: %s).', $requiredVersion, $version));
  75. }
  76. // check InnoDB support
  77. if (!$resource->supportEngine()) {
  78. Mage::throwException(Mage::helper('install')->__('Database server does not support '
  79. . 'the InnoDB storage engine.'));
  80. }
  81. // TODO: check user roles
  82. }
  83. catch (Mage_Core_Exception $e) {
  84. Mage::logException($e);
  85. Mage::throwException(Mage::helper('install')->__($e->getMessage()));
  86. }
  87. catch (Exception $e) {
  88. Mage::logException($e);
  89. Mage::throwException(Mage::helper('install')->__('Database connection error.'));
  90. }
  91. return $data;
  92. }
  93. /**
  94. * Check database connection data
  95. *
  96. * @param array $data
  97. * @return array
  98. */
  99. protected function _getCheckedData($data)
  100. {
  101. if (!isset($data['db_name']) || empty($data['db_name'])) {
  102. Mage::throwException(Mage::helper('install')->__('Database Name cannot be empty.'));
  103. }
  104. //make all table prefix to lower letter
  105. if ($data['db_prefix'] != '') {
  106. $data['db_prefix'] = strtolower($data['db_prefix']);
  107. }
  108. //check table prefix
  109. if ($data['db_prefix'] != '') {
  110. if (!preg_match('/^[a-z]+[a-z0-9_]*$/', $data['db_prefix'])) {
  111. Mage::throwException(
  112. Mage::helper('install')->__('The table prefix should contain only letters (a-z), '
  113. . 'numbers (0-9) or underscores (_), the first character should be a letter.'));
  114. }
  115. }
  116. //set default db model
  117. if (!isset($data['db_model']) || empty($data['db_model'])) {
  118. $data['db_model'] = Mage::getConfig()
  119. ->getResourceConnectionConfig(Mage_Core_Model_Resource::DEFAULT_SETUP_RESOURCE)->model;
  120. }
  121. //set db type according the db model
  122. if (!isset($data['db_type'])) {
  123. $data['db_type'] = (string) Mage::getConfig()
  124. ->getNode(sprintf('install/databases/%s/type', $data['db_model']));
  125. }
  126. $dbResource = $this->_getDbResource($data['db_model']);
  127. $data['db_pdo_type'] = $dbResource->getPdoType();
  128. if (!isset($data['db_init_statemants'])) {
  129. $data['db_init_statemants'] = (string) Mage::getConfig()
  130. ->getNode(sprintf('install/databases/%s/initStatements', $data['db_model']));
  131. }
  132. return $data;
  133. }
  134. /**
  135. * Retrieve the database resource
  136. *
  137. * @param string $model database type
  138. * @return Mage_Install_Model_Installer_Db_Abstract
  139. */
  140. protected function _getDbResource($model)
  141. {
  142. if (!isset($this->_dbResource)) {
  143. $resource = Mage::getSingleton(sprintf('install/installer_db_%s', $model));
  144. if (!$resource) {
  145. Mage::throwException(Mage::helper('install')->__(sprintf('Installer does not exist for %s database type', $model)));
  146. }
  147. $this->_dbResource = $resource;
  148. }
  149. return $this->_dbResource;
  150. }
  151. /**
  152. * Retrieve Connection Type
  153. *
  154. * @return string
  155. *
  156. * @deprecated since 1.5.0.0
  157. */
  158. protected function _getConnenctionType()
  159. {
  160. return (string) Mage::getConfig()->getNode('global/resources/default_setup/connection/type');
  161. }
  162. /**
  163. * Check database connection
  164. *
  165. * @param array $data
  166. *
  167. * @deprecated since 1.5.0.0
  168. */
  169. public function checkDatabase($data)
  170. {
  171. $this->checkDbConnectionData($data);
  172. }
  173. }