Overview

Packages

  • currencysymbol
  • MAbout
  • Mage
    • Admin
    • Adminhtml
    • AdminNotification
    • Api
    • Api2
    • Authorizenet
    • Backup
    • Bundle
    • Captcha
    • Catalog
    • CatalogIndex
    • CatalogInventory
    • CatalogRule
    • CatalogSearch
    • Centinel
    • Checkout
    • Cms
    • Compiler
    • Connect
    • Contacts
    • Core
    • Cron
    • CurrencySymbol
    • Customer
    • Dataflow
    • Directory
    • DirtectPost
    • Downloadable
    • Eav
    • GiftMessage
    • GoogleAnalytics
    • GoogleBase
    • GoogleCheckout
    • ImportExport
    • Index
    • Install
    • Log
    • Media
    • Newsletter
    • Oauth
    • Page
    • PageCache
    • Paygate
    • Payment
    • Paypal
    • PaypalUk
    • Persistent
    • Poll
    • ProductAlert
    • Rating
    • Reports
    • Review
    • Rss
    • Rule
    • Sales
    • SalesRule
    • Sedfriend
    • Sendfriend
    • Shipping
    • Sitemap
    • Tag
    • Tax
    • Usa
    • Weee
    • Widget
    • Wishlist
    • XmlConnect
  • None
  • Phoenix
    • Moneybookers
  • PHP
  • Zend
    • Date
    • Mime
    • XmlRpc

Classes

  • Mage
  • Mage_Adminhtml_Block_Html_Date
  • Mage_Adminhtml_Block_Html_Select
  • Mage_Api_Model_Wsdl_Config_Element
  • Mage_Captcha_Adminhtml_RefreshController
  • Mage_Captcha_RefreshController
  • Mage_Core_AjaxController
  • Mage_Core_Block_Abstract
  • Mage_Core_Block_Flush
  • Mage_Core_Block_Html_Calendar
  • Mage_Core_Block_Html_Date
  • Mage_Core_Block_Html_Link
  • Mage_Core_Block_Html_Select
  • Mage_Core_Block_Messages
  • Mage_Core_Block_Profiler
  • Mage_Core_Block_Store_Switcher
  • Mage_Core_Block_Template
  • Mage_Core_Block_Template_Facade
  • Mage_Core_Block_Template_Smarty
  • Mage_Core_Block_Template_Zend
  • Mage_Core_Block_Text
  • Mage_Core_Block_Text_List
  • Mage_Core_Block_Text_List_Item
  • Mage_Core_Block_Text_List_Link
  • Mage_Core_Block_Text_Tag
  • Mage_Core_Block_Text_Tag_Css
  • Mage_Core_Block_Text_Tag_Css_Admin
  • Mage_Core_Block_Text_Tag_Debug
  • Mage_Core_Block_Text_Tag_Js
  • Mage_Core_Block_Text_Tag_Meta
  • Mage_Core_Controller_Front_Action
  • Mage_Core_Controller_Front_Router
  • Mage_Core_Controller_Request_Http
  • Mage_Core_Controller_Response_Http
  • Mage_Core_Controller_Varien_Action
  • Mage_Core_Controller_Varien_Front
  • Mage_Core_Controller_Varien_Router_Abstract
  • Mage_Core_Controller_Varien_Router_Admin
  • Mage_Core_Controller_Varien_Router_Default
  • Mage_Core_Controller_Varien_Router_Standard
  • Mage_Core_Helper_Abstract
  • Mage_Core_Helper_Cookie
  • Mage_Core_Helper_Data
  • Mage_Core_Helper_File_Storage
  • Mage_Core_Helper_File_Storage_Database
  • Mage_Core_Helper_Hint
  • Mage_Core_Helper_Http
  • Mage_Core_Helper_Js
  • Mage_Core_Helper_String
  • Mage_Core_Helper_Translate
  • Mage_Core_Helper_Url
  • Mage_Core_Helper_Url_Rewrite
  • Mage_Core_IndexController
  • Mage_Core_Model_Abstract
  • Mage_Core_Model_App
  • Mage_Core_Model_App_Area
  • Mage_Core_Model_App_Emulation
  • Mage_Core_Model_Cache
  • Mage_Core_Model_Calculator
  • Mage_Core_Model_Config
  • Mage_Core_Model_Config_Base
  • Mage_Core_Model_Config_Data
  • Mage_Core_Model_Config_Element
  • Mage_Core_Model_Config_Options
  • Mage_Core_Model_Config_System
  • Mage_Core_Model_Cookie
  • Mage_Core_Model_Date
  • Mage_Core_Model_Design
  • Mage_Core_Model_Design_Package
  • Mage_Core_Model_Design_Source_Design
  • Mage_Core_Model_Email
  • Mage_Core_Model_Email_Info
  • Mage_Core_Model_Email_Template
  • Mage_Core_Model_Email_Template_Filter
  • Mage_Core_Model_Email_Template_Mailer
  • Mage_Core_Model_Email_Transport
  • Mage_Core_Model_Encryption
  • Mage_Core_Model_File_Storage
  • Mage_Core_Model_File_Storage_Abstract
  • Mage_Core_Model_File_Storage_Database
  • Mage_Core_Model_File_Storage_Database_Abstract
  • Mage_Core_Model_File_Storage_Directory_Database
  • Mage_Core_Model_File_Storage_File
  • Mage_Core_Model_File_Storage_Flag
  • Mage_Core_Model_File_Uploader
  • Mage_Core_Model_File_Validator_AvailablePath
  • Mage_Core_Model_File_Validator_NotProtectedExtension
  • Mage_Core_Model_Flag
  • Mage_Core_Model_Input_Filter
  • Mage_Core_Model_Input_Filter_MaliciousCode
  • Mage_Core_Model_Language
  • Mage_Core_Model_Layout
  • Mage_Core_Model_Layout_Data
  • Mage_Core_Model_Layout_Element
  • Mage_Core_Model_Layout_Update
  • Mage_Core_Model_Locale
  • Mage_Core_Model_Locale_Config
  • Mage_Core_Model_Log_Adapter
  • Mage_Core_Model_Magento_Api
  • Mage_Core_Model_Magento_Api_V2
  • Mage_Core_Model_Message
  • Mage_Core_Model_Message_Abstract
  • Mage_Core_Model_Message_Collection
  • Mage_Core_Model_Message_Error
  • Mage_Core_Model_Message_Notice
  • Mage_Core_Model_Message_Success
  • Mage_Core_Model_Message_Warning
  • Mage_Core_Model_Mysql4_Abstract
  • Mage_Core_Model_Mysql4_Cache
  • Mage_Core_Model_Mysql4_Collection_Abstract
  • Mage_Core_Model_Mysql4_Config
  • Mage_Core_Model_Mysql4_Config_Data
  • Mage_Core_Model_Mysql4_Config_Data_Collection
  • Mage_Core_Model_Mysql4_Design
  • Mage_Core_Model_Mysql4_Design_Collection
  • Mage_Core_Model_Mysql4_Design_Package_Collection
  • Mage_Core_Model_Mysql4_Design_Theme
  • Mage_Core_Model_Mysql4_Email_Template
  • Mage_Core_Model_Mysql4_Email_Template_Collection
  • Mage_Core_Model_Mysql4_File_Storage_Abstract
  • Mage_Core_Model_Mysql4_File_Storage_Database
  • Mage_Core_Model_Mysql4_File_Storage_Directory_Database
  • Mage_Core_Model_Mysql4_File_Storage_File
  • Mage_Core_Model_Mysql4_Flag
  • Mage_Core_Model_Mysql4_Language
  • Mage_Core_Model_Mysql4_Language_Collection
  • Mage_Core_Model_Mysql4_Layout
  • Mage_Core_Model_Mysql4_Resource
  • Mage_Core_Model_Mysql4_Session
  • Mage_Core_Model_Mysql4_Store
  • Mage_Core_Model_Mysql4_Store_Collection
  • Mage_Core_Model_Mysql4_Store_Group
  • Mage_Core_Model_Mysql4_Store_Group_Collection
  • Mage_Core_Model_Mysql4_Translate
  • Mage_Core_Model_Mysql4_Translate_String
  • Mage_Core_Model_Mysql4_Url_Rewrite
  • Mage_Core_Model_Mysql4_Url_Rewrite_Collection
  • Mage_Core_Model_Mysql4_Variable
  • Mage_Core_Model_Mysql4_Variable_Collection
  • Mage_Core_Model_Mysql4_Website
  • Mage_Core_Model_Mysql4_Website_Collection
  • Mage_Core_Model_Observer
  • Mage_Core_Model_Resource
  • Mage_Core_Model_Resource_Abstract
  • Mage_Core_Model_Resource_Cache
  • Mage_Core_Model_Resource_Config
  • Mage_Core_Model_Resource_Config_Data
  • Mage_Core_Model_Resource_Config_Data_Collection
  • Mage_Core_Model_Resource_Db_Abstract
  • Mage_Core_Model_Resource_Db_Collection_Abstract
  • Mage_Core_Model_Resource_Design
  • Mage_Core_Model_Resource_Design_Collection
  • Mage_Core_Model_Resource_Design_Package_Collection
  • Mage_Core_Model_Resource_Email_Template
  • Mage_Core_Model_Resource_Email_Template_Collection
  • Mage_Core_Model_Resource_Entity_Abstract
  • Mage_Core_Model_Resource_Entity_Table
  • Mage_Core_Model_Resource_File_Storage_Abstract
  • Mage_Core_Model_Resource_File_Storage_Database
  • Mage_Core_Model_Resource_File_Storage_Directory_Database
  • Mage_Core_Model_Resource_File_Storage_File
  • Mage_Core_Model_Resource_Flag
  • Mage_Core_Model_Resource_Helper_Abstract
  • Mage_Core_Model_Resource_Helper_Mysql4
  • Mage_Core_Model_Resource_Iterator
  • Mage_Core_Model_Resource_Language
  • Mage_Core_Model_Resource_Language_Collection
  • Mage_Core_Model_Resource_Layout
  • Mage_Core_Model_Resource_Resource
  • Mage_Core_Model_Resource_Session
  • Mage_Core_Model_Resource_Setup
  • Mage_Core_Model_Resource_Setup_Query_Modifier
  • Mage_Core_Model_Resource_Store
  • Mage_Core_Model_Resource_Store_Collection
  • Mage_Core_Model_Resource_Store_Group
  • Mage_Core_Model_Resource_Store_Group_Collection
  • Mage_Core_Model_Resource_Transaction
  • Mage_Core_Model_Resource_Translate
  • Mage_Core_Model_Resource_Translate_String
  • Mage_Core_Model_Resource_Type_Abstract
  • Mage_Core_Model_Resource_Type_Db
  • Mage_Core_Model_Resource_Type_Db_Mysqli
  • Mage_Core_Model_Resource_Type_Db_Mysqli_Setup
  • Mage_Core_Model_Resource_Type_Db_Pdo_Mysql
  • Mage_Core_Model_Resource_Url_Rewrite
  • Mage_Core_Model_Resource_Url_Rewrite_Collection
  • Mage_Core_Model_Resource_Variable
  • Mage_Core_Model_Resource_Variable_Collection
  • Mage_Core_Model_Resource_Website
  • Mage_Core_Model_Resource_Website_Collection
  • Mage_Core_Model_Session
  • Mage_Core_Model_Session_Abstract
  • Mage_Core_Model_Session_Abstract_Varien
  • Mage_Core_Model_Session_Abstract_Zend
  • Mage_Core_Model_Source_Email_Variables
  • Mage_Core_Model_Store
  • Mage_Core_Model_Store_Api
  • Mage_Core_Model_Store_Api_V2
  • Mage_Core_Model_Store_Group
  • Mage_Core_Model_Template
  • Mage_Core_Model_Translate
  • Mage_Core_Model_Translate_Expr
  • Mage_Core_Model_Translate_Inline
  • Mage_Core_Model_Translate_String
  • Mage_Core_Model_Url
  • Mage_Core_Model_Url_Rewrite
  • Mage_Core_Model_Url_Validator
  • Mage_Core_Model_Variable
  • Mage_Core_Model_Variable_Config
  • Mage_Core_Model_Variable_Observer
  • Mage_Core_Model_Website
  • Mage_Page_Block_Switch
  • Mage_Page_Block_Template_Container
  • Mage_Page_Block_Template_Links
  • Mage_Page_Block_Template_Links_Block
  • Mage_Sales_Model_Quote_Address_Total_Collector

Exceptions

  • Mage_Core_Controller_Varien_Exception
  • Mage_Core_Exception
  • Mage_Core_Model_Session_Exception
  • Mage_Core_Model_Store_Exception

Functions

  • destruct
  • is_dir_writeable
  • is_empty_date
  • mageCoreErrorHandler
  • mageDebugBacktrace
  • mageDelTree
  • mageFindClassFile
  • mageParseCsv
  • mageSendErrorFooter
  • mageSendErrorHeader
  • mageUndoMagicQuotes
  • now
  • sys_get_temp_dir
  • uc_words
  • Overview
  • Package
  • Class
  • Tree
   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_Core
  23:  * @copyright   Copyright (c) 2012 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: /**
  28:  * Resource Setup Model
  29:  *
  30:  * @category    Mage
  31:  * @package     Mage_Core
  32:  * @author      Magento Core Team <core@magentocommerce.com>
  33:  */
  34: class Mage_Core_Model_Resource_Setup
  35: {
  36:     const DEFAULT_SETUP_CONNECTION  = 'core_setup';
  37:     const VERSION_COMPARE_EQUAL     = 0;
  38:     const VERSION_COMPARE_LOWER     = -1;
  39:     const VERSION_COMPARE_GREATER   = 1;
  40: 
  41:     const TYPE_DB_INSTALL           = 'install';
  42:     const TYPE_DB_UPGRADE           = 'upgrade';
  43:     const TYPE_DB_ROLLBACK          = 'rollback';
  44:     const TYPE_DB_UNINSTALL         = 'uninstall';
  45:     const TYPE_DATA_INSTALL         = 'data-install';
  46:     const TYPE_DATA_UPGRADE         = 'data-upgrade';
  47: 
  48:     /**
  49:      * Setup resource name
  50:      * @var string
  51:      */
  52:     protected $_resourceName;
  53: 
  54:     /**
  55:      * Setup resource configuration object
  56:      *
  57:      * @var Varien_Simplexml_Object
  58:      */
  59:     protected $_resourceConfig;
  60: 
  61:     /**
  62:      * Connection configuration object
  63:      *
  64:      * @var Varien_Simplexml_Object
  65:      */
  66:     protected $_connectionConfig;
  67: 
  68:     /**
  69:      * Setup module configuration object
  70:      *
  71:      * @var Varien_Simplexml_Object
  72:      */
  73:     protected $_moduleConfig;
  74: 
  75:     /**
  76:      * Call afterApplyAllUpdates method flag
  77:      *
  78:      * @var boolean
  79:      */
  80:     protected $_callAfterApplyAllUpdates = false;
  81: 
  82:     /**
  83:      * Setup Connection
  84:      *
  85:      * @var Varien_Db_Adapter_Pdo_Mysql
  86:      */
  87:     protected $_conn;
  88:     /**
  89:      * Tables cache array
  90:      *
  91:      * @var array
  92:      */
  93:     protected $_tables = array();
  94:     /**
  95:      * Tables data cache array
  96:      *
  97:      * @var array
  98:      */
  99:     protected $_setupCache = array();
 100: 
 101:     /**
 102:      * Flag which shows, that setup has hooked queries from DB adapter
 103:      *
 104:      * @var bool
 105:      */
 106:     protected $_queriesHooked = false;
 107: 
 108:     /**
 109:      * Flag which allow to detect that some schema update was applied dueting request
 110:      *
 111:      * @var bool
 112:      */
 113:     protected static $_hadUpdates;
 114: 
 115:     /**
 116:      * Flag which allow run data install or upgrade
 117:      *
 118:      * @var bool
 119:      */
 120:     protected static $_schemaUpdatesChecked;
 121: 
 122:     /**
 123:      * Initialize resource configurations, setup connection, etc
 124:      *
 125:      * @param string $resourceName the setup resource name
 126:      */
 127:     public function __construct($resourceName)
 128:     {
 129:         $config = Mage::getConfig();
 130:         $this->_resourceName = $resourceName;
 131:         $this->_resourceConfig = $config->getResourceConfig($resourceName);
 132:         $connection = $config->getResourceConnectionConfig($resourceName);
 133:         if ($connection) {
 134:             $this->_connectionConfig = $connection;
 135:         } else {
 136:             $this->_connectionConfig = $config->getResourceConnectionConfig(self::DEFAULT_SETUP_CONNECTION);
 137:         }
 138: 
 139:         $modName = (string)$this->_resourceConfig->setup->module;
 140:         $this->_moduleConfig = $config->getModuleConfig($modName);
 141:         $connection = Mage::getSingleton('core/resource')->getConnection($this->_resourceName);
 142:         /**
 143:          * If module setup configuration wasn't loaded
 144:          */
 145:         if (!$connection) {
 146:             $connection = Mage::getSingleton('core/resource')->getConnection($this->_resourceName);
 147:         }
 148:         $this->_conn = $connection;
 149:     }
 150: 
 151:     /**
 152:      * Get connection object
 153:      *
 154:      * @return Varien_Db_Adapter_Interface
 155:      */
 156:     public function getConnection()
 157:     {
 158:         return $this->_conn;
 159:     }
 160: 
 161:     /**
 162:      * Add table placeholder/table name relation
 163:      *
 164:      * @param string $tableName
 165:      * @param string $realTableName
 166:      * @return Mage_Core_Model_Resource_Setup
 167:      */
 168:     public function setTable($tableName, $realTableName)
 169:     {
 170:         $this->_tables[$tableName] = $realTableName;
 171:         return $this;
 172:     }
 173: 
 174:     /**
 175:      * Get table name (validated by db adapter) by table placeholder
 176:      *
 177:      * @param string|array $tableName
 178:      * @return string
 179:      */
 180:     public function getTable($tableName)
 181:     {
 182:         $cacheKey = $this->_getTableCacheName($tableName);
 183:         if (!isset($this->_tables[$cacheKey])) {
 184:             $this->_tables[$cacheKey] = Mage::getSingleton('core/resource')->getTableName($tableName);
 185:         }
 186:         return $this->_tables[$cacheKey];
 187:     }
 188: 
 189:     /**
 190:      * Retrieve table name for cache
 191:      *
 192:      * @param string|array $tableName
 193:      * @return string
 194:      */
 195:     protected function _getTableCacheName($tableName)
 196:     {
 197:         if (is_array($tableName)) {
 198:             return join('_', $tableName);
 199: 
 200:         }
 201:         return $tableName;
 202:     }
 203: 
 204:     /**
 205:      * Get core resource resource model
 206:      *
 207:      * @return Mage_Core_Model_Resource_Resource
 208:      */
 209:     protected function _getResource()
 210:     {
 211:         return Mage::getResourceSingleton('core/resource');
 212:     }
 213: 
 214:     /**
 215:      * Apply database updates whenever needed
 216:      *
 217:      * @return boolean
 218:      */
 219:     static public function applyAllUpdates()
 220:     {
 221:         Mage::app()->setUpdateMode(true);
 222:         self::$_hadUpdates = false;
 223: 
 224:         $resources = Mage::getConfig()->getNode('global/resources')->children();
 225:         $afterApplyUpdates = array();
 226:         foreach ($resources as $resName => $resource) {
 227:             if (!$resource->setup) {
 228:                 continue;
 229:             }
 230:             $className = __CLASS__;
 231:             if (isset($resource->setup->class)) {
 232:                 $className = $resource->setup->getClassName();
 233:             }
 234:             $setupClass = new $className($resName);
 235:             $setupClass->applyUpdates();
 236:             if ($setupClass->getCallAfterApplyAllUpdates()) {
 237:                 $afterApplyUpdates[] = $setupClass;
 238:             }
 239:         }
 240: 
 241:         foreach ($afterApplyUpdates as $setupClass) {
 242:             $setupClass->afterApplyAllUpdates();
 243:         }
 244: 
 245:         Mage::app()->setUpdateMode(false);
 246:         self::$_schemaUpdatesChecked = true;
 247:         return true;
 248:     }
 249: 
 250:     /**
 251:      * Apply database data updates whenever needed
 252:      *
 253:      */
 254:     static public function applyAllDataUpdates()
 255:     {
 256:         if (!self::$_schemaUpdatesChecked) {
 257:             return;
 258:         }
 259:         $resources = Mage::getConfig()->getNode('global/resources')->children();
 260:         foreach ($resources as $resName => $resource) {
 261:             if (!$resource->setup) {
 262:                 continue;
 263:             }
 264:             $className = __CLASS__;
 265:             if (isset($resource->setup->class)) {
 266:                 $className = $resource->setup->getClassName();
 267:             }
 268:             $setupClass = new $className($resName);
 269:             $setupClass->applyDataUpdates();
 270:         }
 271:     }
 272: 
 273:     /**
 274:      * Apply data updates to the system after upgrading.
 275:      *
 276:      * @param string $fromVersion
 277:      * @return Mage_Core_Model_Resource_Setup
 278:      */
 279:     public function applyDataUpdates()
 280:     {
 281:         $dataVer= $this->_getResource()->getDataVersion($this->_resourceName);
 282:         $configVer = (string)$this->_moduleConfig->version;
 283:         if ($dataVer !== false) {
 284:              $status = version_compare($configVer, $dataVer);
 285:              if ($status == self::VERSION_COMPARE_GREATER) {
 286:                  $this->_upgradeData($dataVer, $configVer);
 287:              }
 288:         } elseif ($configVer) {
 289:             $this->_installData($configVer);
 290:         }
 291:         return $this;
 292:     }
 293: 
 294:     /**
 295:      * Apply module resource install, upgrade and data scripts
 296:      *
 297:      * @return Mage_Core_Model_Resource_Setup
 298:      */
 299:     public function applyUpdates()
 300:     {
 301:         $dbVer = $this->_getResource()->getDbVersion($this->_resourceName);
 302:         $configVer = (string)$this->_moduleConfig->version;
 303: 
 304:         /**
 305:          * Hook queries in adapter, so that in MySQL compatibility mode extensions and custom modules will avoid
 306:          * errors due to changes in database structure
 307:          */
 308:         if (((string)$this->_moduleConfig->codePool != 'core') && Mage::helper('core')->useDbCompatibleMode()) {
 309:             $this->_hookQueries();
 310:         }
 311: 
 312:         // Module is installed
 313:         if ($dbVer !== false) {
 314:              $status = version_compare($configVer, $dbVer);
 315:              switch ($status) {
 316:                 case self::VERSION_COMPARE_LOWER:
 317:                     $this->_rollbackResourceDb($configVer, $dbVer);
 318:                     break;
 319:                 case self::VERSION_COMPARE_GREATER:
 320:                     $this->_upgradeResourceDb($dbVer, $configVer);
 321:                     break;
 322:                 default:
 323:                     return true;
 324:                     break;
 325:              }
 326:         } elseif ($configVer) {
 327:             $this->_installResourceDb($configVer);
 328:         }
 329: 
 330:         $this->_unhookQueries();
 331: 
 332:         return $this;
 333:     }
 334: 
 335:     /**
 336:      * Hooks queries to strengthen backwards compatibility in MySQL.
 337:      * Currently - dynamically updates column types for foreign keys, when their targets were changed
 338:      * during MMDB development.
 339:      *
 340:      * @return Mage_Core_Model_Resource_Setup
 341:      */
 342:     protected function _hookQueries()
 343:     {
 344:         $this->_queriesHooked = true;
 345:         /* @var $adapter Varien_Db_Adapter_Pdo_Mysql */
 346:         $adapter = $this->getConnection();
 347:         $adapter->setQueryHook(array('object' => $this, 'method' => 'callbackQueryHook'));
 348:         return $this;
 349:     }
 350: 
 351:     /**
 352:      * Removes query hook
 353:      *
 354:      * @return Mage_Core_Model_Resource_Setup
 355:      */
 356:     protected function _unhookQueries()
 357:     {
 358:         if (!$this->_queriesHooked) {
 359:             return $this;
 360:         }
 361:         /* @var $adapter Varien_Db_Adapter_Pdo_Mysql */
 362:         $adapter = $this->getConnection();
 363:         $adapter->setQueryHook(null);
 364:         $this->_queriesHooked = false;
 365:         return $this;
 366:     }
 367: 
 368:     /**
 369:      * Callback function, called on every query adapter processes.
 370:      * Modifies SQL or tables, so that foreign keys will be set successfully
 371:      *
 372:      * @param string $sql
 373:      * @param array $bind
 374:      * @return Mage_Core_Model_Resource_Setup
 375:      */
 376:     public function callbackQueryHook(&$sql, &$bind)
 377:     {
 378:         Mage::getSingleton('core/resource_setup_query_modifier', array($this->getConnection()))
 379:             ->processQuery($sql, $bind);
 380:         return $this;
 381:     }
 382: 
 383:     /**
 384:      * Run data install scripts
 385:      *
 386:      * @param string $newVersion
 387:      * @return Mage_Core_Model_Resource_Setup
 388:      */
 389:     protected function _installData($newVersion)
 390:     {
 391:         $oldVersion = $this->_modifyResourceDb(self::TYPE_DATA_INSTALL, '', $newVersion);
 392:         $this->_modifyResourceDb(self::TYPE_DATA_UPGRADE, $oldVersion, $newVersion);
 393:         $this->_getResource()->setDataVersion($this->_resourceName, $newVersion);
 394: 
 395:         return $this;
 396:     }
 397: 
 398:     /**
 399:      * Run data upgrade scripts
 400:      *
 401:      * @param string $oldVersion
 402:      * @param string $newVersion
 403:      * @return Mage_Core_Model_Resource_Setup
 404:      */
 405:     protected function _upgradeData($oldVersion, $newVersion)
 406:     {
 407:         $this->_modifyResourceDb('data-upgrade', $oldVersion, $newVersion);
 408:         $this->_getResource()->setDataVersion($this->_resourceName, $newVersion);
 409: 
 410:         return $this;
 411:     }
 412: 
 413:     /**
 414:      * Run resource installation file
 415:      *
 416:      * @param string $newVersion
 417:      * @return Mage_Core_Model_Resource_Setup
 418:      */
 419:     protected function _installResourceDb($newVersion)
 420:     {
 421:         $oldVersion = $this->_modifyResourceDb(self::TYPE_DB_INSTALL, '', $newVersion);
 422:         $this->_modifyResourceDb(self::TYPE_DB_UPGRADE, $oldVersion, $newVersion);
 423:         $this->_getResource()->setDbVersion($this->_resourceName, $newVersion);
 424: 
 425:         return $this;
 426:     }
 427: 
 428:     /**
 429:      * Run resource upgrade files from $oldVersion to $newVersion
 430:      *
 431:      * @param string $oldVersion
 432:      * @param string $newVersion
 433:      * @return Mage_Core_Model_Resource_Setup
 434:      */
 435:     protected function _upgradeResourceDb($oldVersion, $newVersion)
 436:     {
 437:         $this->_modifyResourceDb(self::TYPE_DB_UPGRADE, $oldVersion, $newVersion);
 438:         $this->_getResource()->setDbVersion($this->_resourceName, $newVersion);
 439: 
 440:         return $this;
 441:     }
 442: 
 443:     /**
 444:      * Roll back resource
 445:      *
 446:      * @param string $newVersion
 447:      * @param string $oldVersion
 448:      * @return Mage_Core_Model_Resource_Setup
 449:      */
 450:     protected function _rollbackResourceDb($newVersion, $oldVersion)
 451:     {
 452:         $this->_modifyResourceDb(self::TYPE_DB_ROLLBACK, $newVersion, $oldVersion);
 453:         return $this;
 454:     }
 455: 
 456:     /**
 457:      * Uninstall resource
 458:      *
 459:      * @param string $version existing resource version
 460:      * @return Mage_Core_Model_Resource_Setup
 461:      */
 462:     protected function _uninstallResourceDb($version)
 463:     {
 464:         $this->_modifyResourceDb(self::TYPE_DB_UNINSTALL, $version, '');
 465:         return $this;
 466:     }
 467: 
 468:     /**
 469:      * Retrieve available Database install/upgrade files for current module
 470:      *
 471:      * @param string $actionType
 472:      * @param string $fromVersion
 473:      * @param string $toVersion
 474:      * @return array
 475:      */
 476:     protected function _getAvailableDbFiles($actionType, $fromVersion, $toVersion)
 477:     {
 478:         $resModel   = (string)$this->_connectionConfig->model;
 479:         $modName    = (string)$this->_moduleConfig[0]->getName();
 480: 
 481:         $filesDir   = Mage::getModuleDir('sql', $modName) . DS . $this->_resourceName;
 482:         if (!is_dir($filesDir) || !is_readable($filesDir)) {
 483:             return array();
 484:         }
 485: 
 486:         $dbFiles    = array();
 487:         $typeFiles  = array();
 488:         $regExpDb   = sprintf('#^%s-(.*)\.(php|sql)$#i', $actionType);
 489:         $regExpType = sprintf('#^%s-%s-(.*)\.(php|sql)$#i', $resModel, $actionType);
 490:         $handlerDir = dir($filesDir);
 491:         while (false !== ($file = $handlerDir->read())) {
 492:             $matches = array();
 493:             if (preg_match($regExpDb, $file, $matches)) {
 494:                 $dbFiles[$matches[1]] = $filesDir . DS . $file;
 495:             } else if (preg_match($regExpType, $file, $matches)) {
 496:                 $typeFiles[$matches[1]] = $filesDir . DS . $file;
 497:             }
 498:         }
 499:         $handlerDir->close();
 500: 
 501:         if (empty($typeFiles) && empty($dbFiles)) {
 502:             return array();
 503:         }
 504: 
 505:         foreach ($typeFiles as $version => $file) {
 506:             $dbFiles[$version] = $file;
 507:         }
 508: 
 509:         return $this->_getModifySqlFiles($actionType, $fromVersion, $toVersion, $dbFiles);
 510:     }
 511: 
 512:     /**
 513:      * Retrieve available Data install/upgrade files for current module
 514:      *
 515:      * @param string $actionType
 516:      * @param string $fromVersion
 517:      * @param string $toVersion
 518:      * @return array
 519:      */
 520:     protected function _getAvailableDataFiles($actionType, $fromVersion, $toVersion)
 521:     {
 522:         $modName    = (string)$this->_moduleConfig[0]->getName();
 523:         $files      = array();
 524: 
 525:         $filesDir   = Mage::getModuleDir('data', $modName) . DS . $this->_resourceName;
 526:         if (is_dir($filesDir) && is_readable($filesDir)) {
 527:             $regExp     = sprintf('#^%s-(.*)\.php$#i', $actionType);
 528:             $handlerDir = dir($filesDir);
 529:             while (false !== ($file = $handlerDir->read())) {
 530:                 $matches = array();
 531:                 if (preg_match($regExp, $file, $matches)) {
 532:                     $files[$matches[1]] = $filesDir . DS . $file;
 533:                 }
 534: 
 535:             }
 536:             $handlerDir->close();
 537:         }
 538: 
 539:         // search data files in old location
 540:         $filesDir   = Mage::getModuleDir('sql', $modName) . DS . $this->_resourceName;
 541:         if (is_dir($filesDir) && is_readable($filesDir)) {
 542:             $regExp     = sprintf('#^%s-%s-(.*)\.php$#i', $this->_connectionConfig->model, $actionType);
 543:             $handlerDir = dir($filesDir);
 544: 
 545:             while (false !== ($file = $handlerDir->read())) {
 546:                 $matches = array();
 547:                 if (preg_match($regExp, $file, $matches)) {
 548:                     $files[$matches[1]] = $filesDir . DS . $file;
 549:                 }
 550:             }
 551:             $handlerDir->close();
 552:         }
 553: 
 554:         if (empty($files)) {
 555:             return array();
 556:         }
 557: 
 558:         return $this->_getModifySqlFiles($actionType, $fromVersion, $toVersion, $files);
 559:     }
 560: 
 561:     /**
 562:      * Save resource version
 563:      *
 564:      * @param string $actionType
 565:      * @param string $version
 566:      * @return Mage_Core_Model_Resource_Setup
 567:      */
 568:     protected function _setResourceVersion($actionType, $version)
 569:     {
 570:         switch ($actionType) {
 571:             case self::TYPE_DB_INSTALL:
 572:             case self::TYPE_DB_UPGRADE:
 573:                 $this->_getResource()->setDbVersion($this->_resourceName, $version);
 574:                 break;
 575:             case self::TYPE_DATA_INSTALL:
 576:             case self::TYPE_DATA_UPGRADE:
 577:                 $this->_getResource()->setDataVersion($this->_resourceName, $version);
 578:                 break;
 579: 
 580:         }
 581: 
 582:         return $this;
 583:     }
 584: 
 585:     /**
 586:      * Run module modification files. Return version of last applied upgrade (false if no upgrades applied)
 587:      *
 588:      * @param string $actionType self::TYPE_*
 589:      * @param string $fromVersion
 590:      * @param string $toVersion
 591:      * @return string|false
 592:      * @throws Mage_Core_Exception
 593:      */
 594: 
 595:     protected function _modifyResourceDb($actionType, $fromVersion, $toVersion)
 596:     {
 597:         switch ($actionType) {
 598:             case self::TYPE_DB_INSTALL:
 599:             case self::TYPE_DB_UPGRADE:
 600:                 $files = $this->_getAvailableDbFiles($actionType, $fromVersion, $toVersion);
 601:                 break;
 602:             case self::TYPE_DATA_INSTALL:
 603:             case self::TYPE_DATA_UPGRADE:
 604:                 $files = $this->_getAvailableDataFiles($actionType, $fromVersion, $toVersion);
 605:                 break;
 606:             default:
 607:                 $files = array();
 608:                 break;
 609:         }
 610:         if (empty($files) || !$this->getConnection()) {
 611:             return false;
 612:         }
 613: 
 614:         $version = false;
 615: 
 616:         foreach ($files as $file) {
 617:             $fileName = $file['fileName'];
 618:             $fileType = pathinfo($fileName, PATHINFO_EXTENSION);
 619:             $this->getConnection()->disallowDdlCache();
 620:             try {
 621:                 switch ($fileType) {
 622:                     case 'php':
 623:                         $conn   = $this->getConnection();
 624:                         $result = include $fileName;
 625:                         break;
 626:                     case 'sql':
 627:                         $sql = file_get_contents($fileName);
 628:                         if (!empty($sql)) {
 629: 
 630:                             $result = $this->run($sql);
 631:                         } else {
 632:                             $result = true;
 633:                         }
 634:                         break;
 635:                     default:
 636:                         $result = false;
 637:                         break;
 638:                 }
 639: 
 640:                 if ($result) {
 641:                     $this->_setResourceVersion($actionType, $file['toVersion']);
 642:                 }
 643:             } catch (Exception $e) {
 644:                 printf('<pre>%s</pre>', print_r($e, true));
 645:                 throw Mage::exception('Mage_Core', Mage::helper('core')->__('Error in file: "%s" - %s', $fileName, $e->getMessage()));
 646:             }
 647:             $version = $file['toVersion'];
 648:             $this->getConnection()->allowDdlCache();
 649:         }
 650:         self::$_hadUpdates = true;
 651:         return $version;
 652:     }
 653: 
 654:     /**
 655:      * Get data files for modifications
 656:      *
 657:      * @param string $actionType
 658:      * @param string $fromVersion
 659:      * @param string $toVersion
 660:      * @param array $arrFiles
 661:      * @return array
 662:      */
 663:     protected function _getModifySqlFiles($actionType, $fromVersion, $toVersion, $arrFiles)
 664:     {
 665:         $arrRes = array();
 666:         switch ($actionType) {
 667:             case self::TYPE_DB_INSTALL:
 668:             case self::TYPE_DATA_INSTALL:
 669:                 uksort($arrFiles, 'version_compare');
 670:                 foreach ($arrFiles as $version => $file) {
 671:                     if (version_compare($version, $toVersion) !== self::VERSION_COMPARE_GREATER) {
 672:                         $arrRes[0] = array(
 673:                             'toVersion' => $version,
 674:                             'fileName'  => $file
 675:                         );
 676:                     }
 677:                 }
 678:                 break;
 679: 
 680:             case self::TYPE_DB_UPGRADE:
 681:             case self::TYPE_DATA_UPGRADE:
 682:                 uksort($arrFiles, 'version_compare');
 683:                 foreach ($arrFiles as $version => $file) {
 684:                     $versionInfo = explode('-', $version);
 685: 
 686:                     // In array must be 2 elements: 0 => version from, 1 => version to
 687:                     if (count($versionInfo)!=2) {
 688:                         break;
 689:                     }
 690:                     $infoFrom = $versionInfo[0];
 691:                     $infoTo   = $versionInfo[1];
 692:                     if (version_compare($infoFrom, $fromVersion) !== self::VERSION_COMPARE_LOWER
 693:                         && version_compare($infoTo, $toVersion) !== self::VERSION_COMPARE_GREATER) {
 694:                         $arrRes[] = array(
 695:                             'toVersion' => $infoTo,
 696:                             'fileName'  => $file
 697:                         );
 698:                     }
 699:                 }
 700:                 break;
 701: 
 702:             case self::TYPE_DB_ROLLBACK:
 703:                 break;
 704: 
 705:             case self::TYPE_DB_UNINSTALL:
 706:                 break;
 707:         }
 708:         return $arrRes;
 709:     }
 710: 
 711: 
 712: /******************* UTILITY METHODS *****************/
 713: 
 714:     /**
 715:      * Retrieve row or field from table by id or string and parent id
 716:      *
 717:      * @param string $table
 718:      * @param string $idField
 719:      * @param string|integer $id
 720:      * @param string $field
 721:      * @param string $parentField
 722:      * @param string|integer $parentId
 723:      * @return mixed|boolean
 724:      */
 725:     public function getTableRow($table, $idField, $id, $field=null, $parentField=null, $parentId=0)
 726:     {
 727:         if (strpos($table, '/') !== false) {
 728:             $table = $this->getTable($table);
 729:         }
 730: 
 731:         if (empty($this->_setupCache[$table][$parentId][$id])) {
 732:             $adapter = $this->getConnection();
 733:             $bind    = array('id_field' => $id);
 734:             $select  = $adapter->select()
 735:                 ->from($table)
 736:                 ->where($adapter->quoteIdentifier($idField) . '= :id_field');
 737:             if (!is_null($parentField)) {
 738:                 $select->where($adapter->quoteIdentifier($parentField) . '= :parent_id');
 739:                 $bind['parent_id'] = $parentId;
 740:             }
 741:             $this->_setupCache[$table][$parentId][$id] = $adapter->fetchRow($select, $bind);
 742:         }
 743: 
 744:         if (is_null($field)) {
 745:             return $this->_setupCache[$table][$parentId][$id];
 746:         }
 747:         return isset($this->_setupCache[$table][$parentId][$id][$field])
 748:             ? $this->_setupCache[$table][$parentId][$id][$field]
 749:             : false;
 750:     }
 751: 
 752: 
 753:      /**
 754:      * Delete table row
 755:      *
 756:      * @param string $table
 757:      * @param string $idField
 758:      * @param string|int $id
 759:      * @param null|string $parentField
 760:      * @param int|string $parentId
 761:      * @return Mage_Core_Model_Resource_Setup
 762:      */
 763:     public function deleteTableRow($table, $idField, $id, $parentField = null, $parentId = 0)
 764:     {
 765:         if (strpos($table, '/') !== false) {
 766:             $table = $this->getTable($table);
 767:         }
 768: 
 769:         $adapter = $this->getConnection();
 770:         $where = array($adapter->quoteIdentifier($idField) . '=?' => $id);
 771:         if (!is_null($parentField)) {
 772:             $where[$adapter->quoteIdentifier($parentField) . '=?'] = $parentId;
 773:         }
 774: 
 775:         $adapter->delete($table, $where);
 776: 
 777:         if (isset($this->_setupCache[$table][$parentId][$id])) {
 778:             unset($this->_setupCache[$table][$parentId][$id]);
 779:         }
 780: 
 781:         return $this;
 782:     }
 783: 
 784:     /**
 785:      * Update one or more fields of table row
 786:      *
 787:      * @param string $table
 788:      * @param string $idField
 789:      * @param string|integer $id
 790:      * @param string|array $field
 791:      * @param mixed|null $value
 792:      * @param string $parentField
 793:      * @param string|integer $parentId
 794:      * @return Mage_Eav_Model_Entity_Setup
 795:      */
 796:     public function updateTableRow($table, $idField, $id, $field, $value = null, $parentField = null, $parentId = 0)
 797:     {
 798:         if (strpos($table, '/') !== false) {
 799:             $table = $this->getTable($table);
 800:         }
 801: 
 802:         if (is_array($field)) {
 803:             $data = $field;
 804:         } else {
 805:             $data = array($field => $value);
 806:         }
 807: 
 808:         $adapter = $this->getConnection();
 809:         $where = array($adapter->quoteIdentifier($idField) . '=?' => $id);
 810:         $adapter->update($table, $data, $where);
 811: 
 812:         if (isset($this->_setupCache[$table][$parentId][$id])) {
 813:             if (is_array($field)) {
 814:                 $this->_setupCache[$table][$parentId][$id] =
 815:                     array_merge($this->_setupCache[$table][$parentId][$id], $field);
 816:             } else {
 817:                 $this->_setupCache[$table][$parentId][$id][$field] = $value;
 818:             }
 819:         }
 820: 
 821:         return $this;
 822:     }
 823: 
 824:     /**
 825:      * Update table data
 826:      *
 827:      * @param string $table
 828:      * @param Zend_Db_Expr $conditionExpr
 829:      * @param Zend_Db_Expr $valueExpr
 830:      * @return Mage_Core_Model_Resource_Setup
 831:      *
 832:      * @deprecated since 1.4.0.1
 833:      */
 834:     public function updateTable($table, $conditionExpr, $valueExpr)
 835:     {
 836:         if (strpos($table, '/') !== false) {
 837:             $table = $this->getTable($table);
 838:         }
 839:         $query = sprintf('UPDATE %s SET %s WHERE %s',
 840:             $this->getConnection()->quoteIdentifier($table),
 841:             $conditionExpr,
 842:             $valueExpr);
 843: 
 844:         $this->getConnection()->query($query);
 845: 
 846:         return $this;
 847:     }
 848: 
 849:     /**
 850:      * Check is table exists
 851:      *
 852:      * @param string $table
 853:      * @return bool
 854:      */
 855:     public function tableExists($table)
 856:     {
 857:         if (strpos($table, '/') !== false) {
 858:             $table = $this->getTable($table);
 859:         }
 860: 
 861:         return $this->getConnection()->isTableExists($table);
 862: 
 863:     }
 864: 
 865: /******************* CONFIG *****************/
 866: 
 867:     /**
 868:      * Undefined
 869:      *
 870:      * @param string $path
 871:      * @param string $label
 872:      * @param array $data
 873:      * @param string $default
 874:      * @return Mage_Core_Model_Resource_Setup
 875:      * @deprecated since 1.4.0.1
 876:      */
 877:     public function addConfigField($path, $label, array $data=array(), $default=null)
 878:     {
 879:         return $this;
 880:     }
 881: 
 882:     /**
 883:      * Save configuration data
 884:      *
 885:      * @param string $path
 886:      * @param string $value
 887:      * @param int|string $scope
 888:      * @param int $scopeId
 889:      * @param int $inherit
 890:      * @return Mage_Core_Model_Resource_Setup
 891:      */
 892:     public function setConfigData($path, $value, $scope = 'default', $scopeId = 0, $inherit=0)
 893:     {
 894:         $table = $this->getTable('core/config_data');
 895:         // this is a fix for mysql 4.1
 896:         $this->getConnection()->showTableStatus($table);
 897: 
 898:         $data  = array(
 899:             'scope'     => $scope,
 900:             'scope_id'  => $scopeId,
 901:             'path'      => $path,
 902:             'value'     => $value
 903:         );
 904:         $this->getConnection()->insertOnDuplicate($table, $data, array('value'));
 905:         return $this;
 906:     }
 907: 
 908:     /**
 909:      * Delete config field values
 910:      *
 911:      * @param string $path
 912:      * @param string $scope (default|stores|websites|config)
 913:      * @return Mage_Core_Model_Resource_Setup
 914:      */
 915:     public function deleteConfigData($path, $scope = null)
 916:     {
 917:         $where = array('path = ?' => $path);
 918:         if (!is_null($scope)) {
 919:             $where['scope = ?'] = $scope;
 920:         }
 921:         $this->getConnection()->delete($this->getTable('core/config_data'), $where);
 922:         return $this;
 923:     }
 924: 
 925:     /**
 926:      * Run plain SQL query(ies)
 927:      *
 928:      * @param string $sql
 929:      * @return Mage_Core_Model_Resource_Setup
 930:      */
 931:     public function run($sql)
 932:     {
 933:         $this->getConnection()->multiQuery($sql);
 934:         return $this;
 935:     }
 936: 
 937:     /**
 938:      * Prepare database before install/upgrade
 939:      *
 940:      * @return Mage_Core_Model_Resource_Setup
 941:      */
 942:     public function startSetup()
 943:     {
 944:         $this->getConnection()->startSetup();
 945:         return $this;
 946:     }
 947: 
 948:     /**
 949:      * Prepare database after install/upgrade
 950:      *
 951:      * @return Mage_Core_Model_Resource_Setup
 952:      */
 953:     public function endSetup()
 954:     {
 955:         $this->getConnection()->endSetup();
 956:         return $this;
 957:     }
 958: 
 959:     /**
 960:      * Retrieve 32bit UNIQUE HASH for a Table index
 961:      *
 962:      * @param string $tableName
 963:      * @param array|string $fields
 964:      * @param string $indexType
 965:      * @return string
 966:      */
 967:     public function getIdxName($tableName, $fields, $indexType = '')
 968:     {
 969:         return Mage::getSingleton('core/resource')->getIdxName($tableName, $fields, $indexType);
 970:     }
 971: 
 972:     /**
 973:      * Retrieve 32bit UNIQUE HASH for a Table foreign key
 974:      *
 975:      * @param string $priTableName  the target table name
 976:      * @param string $priColumnName the target table column name
 977:      * @param string $refTableName  the reference table name
 978:      * @param string $refColumnName the reference table column name
 979:      * @return string
 980:      */
 981:     public function getFkName($priTableName, $priColumnName, $refTableName, $refColumnName)
 982:     {
 983:         return Mage::getSingleton('core/resource')
 984:             ->getFkName($priTableName, $priColumnName, $refTableName, $refColumnName);
 985:     }
 986: 
 987:     /**
 988:      * Check call afterApplyAllUpdates method for setup class
 989:      *
 990:      * @return boolean
 991:      */
 992:     public function getCallAfterApplyAllUpdates()
 993:     {
 994:         return $this->_callAfterApplyAllUpdates;
 995:     }
 996: 
 997:     /**
 998:      * Run each time after applying of all updates,
 999:      * if setup model setted  $_callAfterApplyAllUpdates flag to true
1000:      *
1001:      * @return Mage_Core_Model_Resource_Setup
1002:      */
1003:     public function afterApplyAllUpdates()
1004:     {
1005:         return $this;
1006:     }
1007: }
1008: 
Magento 1.7.0.2 API documentation generated by ApiGen 2.8.0