00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 class Mage_Core_Model_Resource_Setup
00033 {
00034 const VERSION_COMPARE_EQUAL = 0;
00035 const VERSION_COMPARE_LOWER = -1;
00036 const VERSION_COMPARE_GREATER = 1;
00037
00038 protected $_resourceName;
00039 protected $_resourceConfig;
00040 protected $_connectionConfig;
00041 protected $_moduleConfig;
00042
00043
00044
00045
00046
00047
00048 protected $_conn;
00049 protected $_tables = array();
00050 protected $_setupCache = array();
00051
00052 protected static $_hadUpdates;
00053
00054 public function __construct($resourceName)
00055 {
00056 $config = Mage::getConfig();
00057 $this->_resourceName = $resourceName;
00058 $this->_resourceConfig = $config->getResourceConfig($resourceName);
00059 $this->_connectionConfig = $config->getResourceConnectionConfig($resourceName);
00060 $modName = (string)$this->_resourceConfig->setup->module;
00061 $this->_moduleConfig = $config->getModuleConfig($modName);
00062 $this->_conn = Mage::getSingleton('core/resource')->getConnection($this->_resourceName);
00063 }
00064
00065
00066
00067
00068
00069
00070 public function getConnection()
00071 {
00072 return $this->_conn;
00073 }
00074
00075 public function setTable($tableName, $realTableName)
00076 {
00077 $this->_tables[$tableName] = $realTableName;
00078 return $this;
00079 }
00080
00081
00082
00083
00084
00085
00086
00087 public function getTable($tableName) {
00088 if (!isset($this->_tables[$tableName])) {
00089 $this->_tables[$tableName] = Mage::getSingleton('core/resource')->getTableName($tableName);
00090 }
00091 return $this->_tables[$tableName];
00092 }
00093
00094
00095
00096
00097
00098
00099 static public function applyAllUpdates()
00100 {
00101 Mage::app()->setUpdateMode(true);
00102 $res = Mage::getSingleton('core/resource');
00103
00104
00105
00106
00107
00108
00109 self::$_hadUpdates = false;
00110
00111 $resources = Mage::getConfig()->getNode('global/resources')->children();
00112 foreach ($resources as $resName=>$resource) {
00113 if (!$resource->setup) {
00114 continue;
00115 }
00116 $className = __CLASS__;
00117 if (isset($resource->setup->class)) {
00118 $className = $resource->setup->getClassName();
00119 }
00120 $setupClass = new $className($resName);
00121 $setupClass->applyUpdates();
00122 }
00123
00124
00125
00126
00127
00128
00129
00130 Mage::app()->setUpdateMode(false);
00131 return true;
00132 }
00133
00134 public function applyUpdates()
00135 {
00136 $dbVer = Mage::getResourceModel('core/resource')->getDbVersion($this->_resourceName);
00137 $configVer = (string)$this->_moduleConfig->version;
00138
00139 if ($dbVer!==false) {
00140 $status = version_compare($configVer, $dbVer);
00141 switch ($status) {
00142 case self::VERSION_COMPARE_LOWER:
00143 $this->_rollbackResourceDb($configVer, $dbVer);
00144 break;
00145 case self::VERSION_COMPARE_GREATER:
00146 $this->_upgradeResourceDb($dbVer, $configVer);
00147 break;
00148 default:
00149 return true;
00150 break;
00151 }
00152 }
00153
00154 elseif ($configVer) {
00155 $this->_installResourceDb($configVer);
00156 }
00157 }
00158
00159
00160
00161
00162
00163
00164
00165 protected function _installResourceDb($newVersion)
00166 {
00167 $oldVersion = $this->_modifyResourceDb('install', '', $newVersion);
00168 $this->_modifyResourceDb('upgrade', $oldVersion, $newVersion);
00169 }
00170
00171
00172
00173
00174
00175
00176
00177 protected function _upgradeResourceDb($oldVersion, $newVersion)
00178 {
00179 $this->_modifyResourceDb('upgrade', $oldVersion, $newVersion);
00180 }
00181
00182
00183
00184
00185
00186
00187
00188
00189 protected function _rollbackResourceDb($newVersion, $oldVersion)
00190 {
00191 $this->_modifyResourceDb('rollback', $newVersion, $oldVersion);
00192 }
00193
00194
00195
00196
00197
00198
00199
00200
00201 protected function _uninstallResourceDb($version)
00202 {
00203 $this->_modifyResourceDb('uninstall', $version, '');
00204 }
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215 protected function _modifyResourceDb($actionType, $fromVersion, $toVersion)
00216 {
00217 $resModel = (string)$this->_connectionConfig->model;
00218 $modName = (string)$this->_moduleConfig[0]->getName();
00219
00220 $sqlFilesDir = Mage::getModuleDir('sql', $modName).DS.$this->_resourceName;
00221 if (!is_dir($sqlFilesDir) || !is_readable($sqlFilesDir)) {
00222 Mage::getResourceModel('core/resource')->setDbVersion($this->_resourceName, $toVersion);
00223 return $toVersion;
00224 }
00225
00226 $arrAvailableFiles = array();
00227 $sqlDir = dir($sqlFilesDir);
00228 while (false !== ($sqlFile = $sqlDir->read())) {
00229 $matches = array();
00230 if (preg_match('#^'.$resModel.'-'.$actionType.'-(.*)\.(sql|php)$#i', $sqlFile, $matches)) {
00231 $arrAvailableFiles[$matches[1]] = $sqlFile;
00232 }
00233 }
00234 $sqlDir->close();
00235 if (empty($arrAvailableFiles)) {
00236 Mage::getResourceModel('core/resource')->setDbVersion($this->_resourceName, $toVersion);
00237 return $toVersion;
00238 }
00239
00240
00241 $arrModifyFiles = $this->_getModifySqlFiles($actionType, $fromVersion, $toVersion, $arrAvailableFiles);
00242 if (empty($arrModifyFiles)) {
00243 Mage::getResourceModel('core/resource')->setDbVersion($this->_resourceName, $toVersion);
00244 return $toVersion;
00245 }
00246
00247 $modifyVersion = null;
00248 foreach ($arrModifyFiles as $resourceFile) {
00249 $sqlFile = $sqlFilesDir.DS.$resourceFile['fileName'];
00250 $fileType = pathinfo($resourceFile['fileName'], PATHINFO_EXTENSION);
00251
00252
00253 if ($this->_conn) {
00254 try {
00255 switch ($fileType) {
00256 case 'sql':
00257 $sql = file_get_contents($sqlFile);
00258 if ($sql!='') {
00259 $result = $this->run($sql);
00260 } else {
00261 $result = true;
00262 }
00263 break;
00264
00265 case 'php':
00266 $conn = $this->_conn;
00267
00268
00269
00270
00271
00272 try {
00273 #$conn->beginTransaction();
00274 $result = include($sqlFile);
00275 #$conn->commit();
00276 } catch (Exception $e) {
00277 #$conn->rollback();
00278 throw ($e);
00279 }
00280 break;
00281
00282 default:
00283 $result = false;
00284 }
00285 if ($result) {
00286
00287 Mage::getResourceModel('core/resource')->setDbVersion($this->_resourceName, $resourceFile['toVersion']);
00288 }
00289 }
00290 catch (Exception $e){
00291 echo "<pre>".print_r($e,1)."</pre>";
00292 throw Mage::exception('Mage_Core', Mage::helper('core')->__('Error in file: "%s" - %s', $sqlFile, $e->getMessage()));
00293 }
00294 }
00295
00296 $modifyVersion = $resourceFile['toVersion'];
00297 }
00298
00299 if ($actionType == 'upgrade' && $modifyVersion != $toVersion) {
00300 Mage::getResourceModel('core/resource')->setDbVersion($this->_resourceName, $toVersion);
00301 }
00302 else {
00303 $toVersion = $modifyVersion;
00304 }
00305
00306 self::$_hadUpdates = true;
00307 return $toVersion;
00308 }
00309
00310
00311
00312
00313
00314
00315
00316
00317 protected function _getModifySqlFiles($actionType, $fromVersion, $toVersion, $arrFiles)
00318 {
00319 $arrRes = array();
00320
00321 switch ($actionType) {
00322 case 'install':
00323 uksort($arrFiles, 'version_compare');
00324 foreach ($arrFiles as $version => $file) {
00325 if (version_compare($version, $toVersion)!==self::VERSION_COMPARE_GREATER) {
00326 $arrRes[0] = array('toVersion'=>$version, 'fileName'=>$file);
00327 }
00328 }
00329 break;
00330
00331 case 'upgrade':
00332 uksort($arrFiles, 'version_compare');
00333 foreach ($arrFiles as $version => $file) {
00334 $version_info = explode('-', $version);
00335
00336
00337 if (count($version_info)!=2) {
00338 break;
00339 }
00340 $infoFrom = $version_info[0];
00341 $infoTo = $version_info[1];
00342 if (version_compare($infoFrom, $fromVersion)!==self::VERSION_COMPARE_LOWER
00343 && version_compare($infoTo, $toVersion)!==self::VERSION_COMPARE_GREATER) {
00344 $arrRes[] = array('toVersion'=>$infoTo, 'fileName'=>$file);
00345 }
00346 }
00347 break;
00348
00349 case 'rollback':
00350 break;
00351
00352 case 'uninstall':
00353 break;
00354 }
00355 return $arrRes;
00356 }
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372 public function getTableRow($table, $idField, $id, $field=null, $parentField=null, $parentId=0)
00373 {
00374 if (strpos($table, '/')!==false) {
00375 $table = $this->getTable($table);
00376 }
00377
00378 if (empty($this->_setupCache[$table][$parentId][$id])) {
00379 $sql = "select * from $table where $idField=?";
00380 if (!is_null($parentField)) {
00381 $sql .= $this->_conn->quoteInto(" and $parentField=?", $parentId);
00382 }
00383 $this->_setupCache[$table][$parentId][$id] = $this->_conn->fetchRow($sql, $id);
00384 $this->_conn->fetchAll($sql, $id);
00385 }
00386 if (is_null($field)) {
00387 return $this->_setupCache[$table][$parentId][$id];
00388 }
00389 return isset($this->_setupCache[$table][$parentId][$id][$field]) ? $this->_setupCache[$table][$parentId][$id][$field] : false;
00390 }
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402 public function deleteTableRow($table, $idField, $id, $parentField=null, $parentId=0)
00403 {
00404 if (strpos($table, '/')!==false) {
00405 $table = $this->getTable($table);
00406 }
00407
00408 $condition = $this->_conn->quoteInto("$idField=?", $id);
00409 if ($parentField !== null) {
00410 $condition.= $this->_conn->quoteInto(" AND $parentField=?", $parentId);
00411 }
00412 $this->_conn->delete($table, $condition);
00413
00414 if (isset($this->_setupCache[$table][$parentId][$id])) {
00415 unset($this->_setupCache[$table][$parentId][$id]);
00416 }
00417 return $this;
00418 }
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432 public function updateTableRow($table, $idField, $id, $field, $value=null, $parentField=null, $parentId=0)
00433 {
00434 if (is_array($field)) {
00435 $updateArr = array();
00436 foreach ($field as $f=>$v) {
00437 $updateArr[] = $this->_conn->quoteInto("$f=?", $v);
00438 }
00439 $updateStr = join(', ', $updateArr);
00440 } else {
00441 $updateStr = $this->_conn->quoteInto("$field=?", $value);
00442 }
00443 if (strpos($table, '/')!==false) {
00444 $table = $this->getTable($table);
00445 }
00446 $sql = "update $table set $updateStr where ".$this->_conn->quoteInto("$idField=?", $id);
00447 if (!is_null($parentField)) {
00448 $sql .= $this->_conn->quoteInto(" and $parentField=?", $parentId);
00449 }
00450 $this->_conn->query($sql);
00451
00452 return $this;
00453 }
00454
00455 public function updateTable($table, $conditionExpr, $valueExpr)
00456 {
00457 if (strpos($table, '/')!==false) {
00458 $table = $this->getTable($table);
00459 }
00460 $sql = 'update ' . $table . ' set ' . $valueExpr . ' where ' . $conditionExpr;
00461 $this->_conn->query($sql);
00462 return $this;
00463 }
00464
00465 public function tableExists($table)
00466 {
00467 $select = $this->getConnection()->quoteInto('SHOW TABLES LIKE ?', $table);
00468 $result = $this->getConnection()->fetchOne($select);
00469 return !empty($result);
00470 }
00471
00472
00473
00474 public function addConfigField($path, $label, array $data=array(), $default=null)
00475 {
00476 $data['level'] = sizeof(explode('/', $path));
00477 $data['path'] = $path;
00478 $data['frontend_label'] = $label;
00479 if ($id = $this->getTableRow('core/config_field', 'path', $path, 'field_id')) {
00480 $this->updateTableRow('core/config_field', 'field_id', $id, $data);
00481 } else {
00482 if (empty($data['sort_order'])) {
00483 $sql = "select max(sort_order) cnt from ".$this->getTable('core/config_field')." where level=".($data['level']+1);
00484 if ($data['level']>1) {
00485 $sql.= $this->_conn->quoteInto(" and path like ?", dirname($path).'/%');
00486 }
00487
00488 $result = $this->_conn->raw_fetchRow($sql);
00489 $this->_conn->fetchAll($sql);
00490 #print_r($result); die;
00491 $data['sort_order'] = $result['cnt']+1;
00492
00493
00494
00495
00496
00497
00498 }
00499
00500 #$this->_conn->raw_query("insert into ".$this->getTable('core/config_field')." (".join(',', array_keys($data)).") values ('".join("','", array_values($data))."')");
00501 $this->_conn->insert($this->getTable('core/config_field'), $data);
00502 }
00503
00504 if (!is_null($default)) {
00505 $this->setConfigData($path, $default);
00506 }
00507 return $this;
00508 }
00509
00510 public function setConfigData($path, $value, $scope='default', $scopeId=0, $inherit=0)
00511 {
00512 $this->_conn->raw_query("replace into ".$this->getTable('core/config_data')." (scope, scope_id, path, value) values ('$scope', $scopeId, '$path', '$value')");
00513 return $this;
00514 }
00515
00516
00517
00518
00519
00520
00521
00522
00523 public function deleteConfigData($path, $scope=null)
00524 {
00525 $sql = "delete from ".$this->getTable('core/config_data')." where path='".$path."'";
00526 if ($scope) {
00527 $sql.= " and scope='".$scope."'";
00528 }
00529 $this->_conn->raw_query($sql);
00530 return $this;
00531 }
00532
00533 public function run($sql)
00534 {
00535 $this->_conn->multi_query($sql);
00536 return $this;
00537 }
00538
00539 public function startSetup()
00540 {
00541 $this->_conn->multi_query("SET SQL_MODE='';
00542 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
00543 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO';
00544 ");
00545
00546 return $this;
00547 }
00548
00549 public function endSetup()
00550 {
00551 $this->_conn->multi_query("
00552 SET SQL_MODE=IFNULL(@OLD_SQL_MODE,'');
00553 SET FOREIGN_KEY_CHECKS=IFNULL(@OLD_FOREIGN_KEY_CHECKS,0);
00554 ");
00555 return $this;
00556 }
00557 }