1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27: 28: 29: 30: 31: 32: 33:
34: class Mage_Install_Model_Installer_Db extends Mage_Install_Model_Installer_Abstract
35: {
36: 37: 38:
39: protected $_dbResource;
40:
41: 42: 43: 44: 45: 46: 47:
48: public function checkDbConnectionData($data)
49: {
50: $data = $this->_getCheckedData($data);
51:
52: try {
53: $dbModel = ($data['db_model']);
54:
55: if (!$resource = $this->_getDbResource($dbModel)) {
56: Mage::throwException(Mage::helper('install')->__('No resource for %s DB model.', $dbModel));
57: }
58:
59: $resource->setConfig($data);
60:
61:
62: $absenteeExtensions = array();
63: $extensions = $resource->getRequiredExtensions();
64: foreach ($extensions as $extName) {
65: if (!extension_loaded($extName)) {
66: $absenteeExtensions[] = $extName;
67: }
68: }
69: if (!empty($absenteeExtensions)) {
70: Mage::throwException(
71: Mage::helper('install')->__('PHP Extensions "%s" must be loaded.', implode(',', $absenteeExtensions))
72: );
73: }
74:
75: $version = $resource->getVersion();
76: $requiredVersion = (string) Mage::getConfig()
77: ->getNode(sprintf('install/databases/%s/min_version', $dbModel));
78:
79:
80: if (version_compare($version, $requiredVersion) == -1) {
81: Mage::throwException(
82: Mage::helper('install')->__('The database server version doesn\'t match system requirements (required: %s, actual: %s).', $requiredVersion, $version)
83: );
84: }
85:
86:
87: if (!$resource->supportEngine()) {
88: Mage::throwException(
89: Mage::helper('install')->__('Database server does not support the InnoDB storage engine.')
90: );
91: }
92:
93:
94: }
95: catch (Mage_Core_Exception $e) {
96: Mage::logException($e);
97: Mage::throwException(Mage::helper('install')->__($e->getMessage()));
98: }
99: catch (Exception $e) {
100: Mage::logException($e);
101: Mage::throwException(Mage::helper('install')->__('Database connection error.'));
102: }
103:
104: return $data;
105: }
106:
107: 108: 109: 110: 111: 112:
113: protected function _getCheckedData($data)
114: {
115: if (!isset($data['db_name']) || empty($data['db_name'])) {
116: Mage::throwException(Mage::helper('install')->__('Database Name cannot be empty.'));
117: }
118:
119: if ($data['db_prefix'] != '') {
120: $data['db_prefix'] = strtolower($data['db_prefix']);
121: }
122:
123: if ($data['db_prefix'] != '') {
124: if (!preg_match('/^[a-z]+[a-z0-9_]*$/', $data['db_prefix'])) {
125: Mage::throwException(
126: Mage::helper('install')->__('The table prefix should contain only letters (a-z), numbers (0-9) or underscores (_), the first character should be a letter.')
127: );
128: }
129: }
130:
131: if (!isset($data['db_model']) || empty($data['db_model'])) {
132: $data['db_model'] = Mage::getConfig()
133: ->getResourceConnectionConfig(Mage_Core_Model_Resource::DEFAULT_SETUP_RESOURCE)->model;
134: }
135:
136: if (!isset($data['db_type'])) {
137: $data['db_type'] = (string) Mage::getConfig()
138: ->getNode(sprintf('install/databases/%s/type', $data['db_model']));
139: }
140:
141: $dbResource = $this->_getDbResource($data['db_model']);
142: $data['db_pdo_type'] = $dbResource->getPdoType();
143:
144: if (!isset($data['db_init_statemants'])) {
145: $data['db_init_statemants'] = (string) Mage::getConfig()
146: ->getNode(sprintf('install/databases/%s/initStatements', $data['db_model']));
147: }
148:
149: return $data;
150: }
151:
152: 153: 154: 155: 156: 157:
158: protected function _getDbResource($model)
159: {
160: if (!isset($this->_dbResource)) {
161: $resource = Mage::getSingleton(sprintf('install/installer_db_%s', $model));
162: if (!$resource) {
163: Mage::throwException(
164: Mage::helper('install')->__('Installer does not exist for %s database type', $model)
165: );
166: }
167: $this->_dbResource = $resource;
168: }
169: return $this->_dbResource;
170: }
171:
172: 173: 174: 175: 176: 177: 178:
179: protected function _getConnenctionType()
180: {
181: return (string) Mage::getConfig()->getNode('global/resources/default_setup/connection/type');
182: }
183:
184:
185: 186: 187: 188: 189: 190: 191:
192: public function checkDatabase($data)
193: {
194: $this->checkDbConnectionData($data);
195: }
196: }
197: