|
| 1 | +<?php |
| 2 | + |
| 3 | +class Kasterweb_Cleaner_Model_MagentoDatabase extends Mage_Core_Model_Abstract |
| 4 | +{ |
| 5 | + /** |
| 6 | + * Resource singleton |
| 7 | + * |
| 8 | + * @var \Mage_Core_Model_Resource |
| 9 | + */ |
| 10 | + protected $coreResource; |
| 11 | + |
| 12 | + /** |
| 13 | + * Write connection to DB |
| 14 | + * |
| 15 | + * @var \Varien_Db_Adapter_Interface |
| 16 | + */ |
| 17 | + protected $dbWrite; |
| 18 | + |
| 19 | + /** |
| 20 | + * Read connection to DB |
| 21 | + * |
| 22 | + * @var \Varien_Db_Adapter_Interface |
| 23 | + */ |
| 24 | + protected $dbRead; |
| 25 | + |
| 26 | + /** |
| 27 | + * List of tables in DB |
| 28 | + * |
| 29 | + * @var string[] |
| 30 | + */ |
| 31 | + protected $tables; |
| 32 | + |
| 33 | + /** |
| 34 | + * Resources to truncate |
| 35 | + * |
| 36 | + * @var array |
| 37 | + */ |
| 38 | + protected $resources = array( |
| 39 | + // Cache |
| 40 | + 'core/cache', |
| 41 | + 'core/cache_tag', |
| 42 | + |
| 43 | + // Catalog |
| 44 | + 'catalog_index/aggregation', |
| 45 | + 'catalog_index/aggregation/tag', |
| 46 | + 'catalog_index/aggregation_to_tag', |
| 47 | + |
| 48 | + // Session |
| 49 | + 'core/session', |
| 50 | + |
| 51 | + // Dataflow |
| 52 | + 'dataflow/batch_export', |
| 53 | + 'dataflow/batch_import', |
| 54 | + |
| 55 | + // Enterprise Admin Logs |
| 56 | + 'enterprise_logging/event', |
| 57 | + 'enterprise_logging/event_changes', |
| 58 | + |
| 59 | + // Index |
| 60 | + 'index/event', |
| 61 | + 'index/process_event', |
| 62 | + |
| 63 | + // Logs |
| 64 | + 'log/customer', |
| 65 | + 'log/quote_table', |
| 66 | + 'log/summary_table', |
| 67 | + 'log/summary_type_table', |
| 68 | + 'log/url_table', |
| 69 | + 'log/url_info_table', |
| 70 | + 'log/visitor', |
| 71 | + 'log/visitor_info', |
| 72 | + 'log/visitor_online', |
| 73 | + |
| 74 | + // Reports |
| 75 | + 'reports/event', |
| 76 | + 'reports/viewed_product_index', |
| 77 | + 'reports/viewed_aggregated_daily', |
| 78 | + 'reports/viewed_aggregated_monthly', |
| 79 | + 'reports/viewed_aggregated_yearly', |
| 80 | + ); |
| 81 | + |
| 82 | + protected function _construct() |
| 83 | + { |
| 84 | + $this->_init('cleaner/magentoDatabase'); |
| 85 | + |
| 86 | + $this->coreResource = Mage::getSingleton('core/resource'); |
| 87 | + $this->dbRead = $this->coreResource->getConnection('core_read'); |
| 88 | + $this->dbWrite = $this->coreResource->getConnection('core_write'); |
| 89 | + $this->tables = $this->getTables(); |
| 90 | + } |
| 91 | + |
| 92 | + public function truncate() |
| 93 | + { |
| 94 | + $truncatedResources = array(); |
| 95 | + $this->withoutForeignKeyChecks(array($this, 'truncateResources')); |
| 96 | + return $truncatedResources; |
| 97 | + } |
| 98 | + |
| 99 | + protected function truncateResources() |
| 100 | + { |
| 101 | + foreach ($this->prepareResources($this->resources) as $resourceTable) { |
| 102 | + $this->dbWrite->truncateTable($resourceTable); |
| 103 | + $truncatedResources[] = $resourceTable; |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + protected function withoutForeignKeyChecks($closure) |
| 108 | + { |
| 109 | + $this->setForeignKeyCheck(false); |
| 110 | + $return = call_user_func($closure); |
| 111 | + $this->setForeignKeyCheck(true); |
| 112 | + return $return; |
| 113 | + } |
| 114 | + |
| 115 | + protected function setForeignKeyCheck($value) |
| 116 | + { |
| 117 | + return $this->dbWrite->query(sprintf('SET foreign_key_checks = %s', (int) $value)); |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Extract table names and prepare for cli rendering |
| 122 | + * |
| 123 | + * @param array $resources |
| 124 | + * |
| 125 | + * @return void |
| 126 | + */ |
| 127 | + protected function prepareResources($resources) |
| 128 | + { |
| 129 | + $preparedResources = array(); |
| 130 | + foreach ($resources as $resource) { |
| 131 | + try { |
| 132 | + $preparedResources[] = $this->coreResource->getTableName($resource); |
| 133 | + } catch (Mage_Core_Exception $e) { } |
| 134 | + } |
| 135 | + return $preparedResources; |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * Get tables in DB |
| 140 | + * |
| 141 | + * @return string[] |
| 142 | + */ |
| 143 | + protected function getTables() |
| 144 | + { |
| 145 | + $stmt = $this->dbRead->query('SHOW TABLES'); |
| 146 | + $stmt->execute(); |
| 147 | + return $stmt->fetchAll(PDO::FETCH_COLUMN); |
| 148 | + } |
| 149 | +} |
0 commit comments