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

/app/code/core/Mage/Backup/Model/Db.php

https://github.com/cosmocommerce/magento-mirror
PHP | 180 lines | 93 code | 30 blank | 57 comment | 7 complexity | 1e7c1b24110ae14d3d4f5c4ed4ba1e87 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_Backup
  23. * @copyright Copyright (c) 2014 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. * Database backup model
  28. *
  29. * @category Mage
  30. * @package Mage_Backup
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Backup_Model_Db
  34. {
  35. /**
  36. * Buffer length for multi rows
  37. * default 100 Kb
  38. *
  39. */
  40. const BUFFER_LENGTH = 102400;
  41. /**
  42. * List of tables which data should not be backed up
  43. *
  44. * @var array
  45. */
  46. protected $_ignoreDataTablesList = array(
  47. 'importexport/importdata'
  48. );
  49. /**
  50. * Retrieve resource model
  51. *
  52. * @return Mage_Backup_Model_Mysql4_Db
  53. */
  54. public function getResource()
  55. {
  56. return Mage::getResourceSingleton('backup/db');
  57. }
  58. public function getTables()
  59. {
  60. return $this->getResource()->getTables();
  61. }
  62. public function getTableCreateScript($tableName, $addDropIfExists=false)
  63. {
  64. return $this->getResource()->getTableCreateScript($tableName, $addDropIfExists);
  65. }
  66. public function getTableDataDump($tableName)
  67. {
  68. return $this->getResource()->getTableDataDump($tableName);
  69. }
  70. public function getHeader()
  71. {
  72. return $this->getResource()->getHeader();
  73. }
  74. public function getFooter()
  75. {
  76. return $this->getResource()->getFooter();
  77. }
  78. public function renderSql()
  79. {
  80. ini_set('max_execution_time', 0);
  81. $sql = $this->getHeader();
  82. $tables = $this->getTables();
  83. foreach ($tables as $tableName) {
  84. $sql.= $this->getTableCreateScript($tableName, true);
  85. $sql.= $this->getTableDataDump($tableName);
  86. }
  87. $sql.= $this->getFooter();
  88. return $sql;
  89. }
  90. /**
  91. * Create backup and stream write to adapter
  92. *
  93. * @param Mage_Backup_Model_Backup $backup
  94. * @return Mage_Backup_Model_Db
  95. */
  96. public function createBackup(Mage_Backup_Model_Backup $backup)
  97. {
  98. $backup->open(true);
  99. $this->getResource()->beginTransaction();
  100. $tables = $this->getResource()->getTables();
  101. $backup->write($this->getResource()->getHeader());
  102. $ignoreDataTablesList = $this->getIgnoreDataTablesList();
  103. foreach ($tables as $table) {
  104. $backup->write($this->getResource()->getTableHeader($table)
  105. . $this->getResource()->getTableDropSql($table) . "\n");
  106. $backup->write($this->getResource()->getTableCreateSql($table, false) . "\n");
  107. $tableStatus = $this->getResource()->getTableStatus($table);
  108. if ($tableStatus->getRows() && !in_array($table, $ignoreDataTablesList)) {
  109. $backup->write($this->getResource()->getTableDataBeforeSql($table));
  110. if ($tableStatus->getDataLength() > self::BUFFER_LENGTH) {
  111. if ($tableStatus->getAvgRowLength() < self::BUFFER_LENGTH) {
  112. $limit = floor(self::BUFFER_LENGTH / $tableStatus->getAvgRowLength());
  113. $multiRowsLength = ceil($tableStatus->getRows() / $limit);
  114. }
  115. else {
  116. $limit = 1;
  117. $multiRowsLength = $tableStatus->getRows();
  118. }
  119. }
  120. else {
  121. $limit = $tableStatus->getRows();
  122. $multiRowsLength = 1;
  123. }
  124. for ($i = 0; $i < $multiRowsLength; $i ++) {
  125. $backup->write($this->getResource()->getTableDataSql($table, $limit, $i*$limit));
  126. }
  127. $backup->write($this->getResource()->getTableDataAfterSql($table));
  128. }
  129. }
  130. $backup->write($this->getResource()->getTableForeignKeysSql());
  131. $backup->write($this->getResource()->getFooter());
  132. $this->getResource()->commitTransaction();
  133. $backup->close();
  134. return $this;
  135. }
  136. /**.
  137. * Returns the list of tables which data should not be backed up
  138. *
  139. * @return array
  140. */
  141. public function getIgnoreDataTablesList()
  142. {
  143. $result = array();
  144. $resource = Mage::getSingleton('core/resource');
  145. foreach ($this->_ignoreDataTablesList as $table) {
  146. $result[] = $resource->getTableName($table);
  147. }
  148. return $result;
  149. }
  150. }