PageRenderTime 50ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/app/code/core/Mage/Log/Model/Resource/Log.php

https://bitbucket.org/acidel/buykoala
PHP | 254 lines | 144 code | 41 blank | 69 comment | 13 complexity | 772000fa6c575c0a3a1453fbf09952ee 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_Log
  23. * @copyright Copyright (c) 2011 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. * Log Resource Model
  28. *
  29. * @category Mage
  30. * @package Mage_Log
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Log_Model_Resource_Log extends Mage_Core_Model_Resource_Db_Abstract
  34. {
  35. /**
  36. * Init Resource model and connection
  37. *
  38. */
  39. protected function _construct()
  40. {
  41. $this->_init('log/visitor', 'visitor_id');
  42. }
  43. /**
  44. * Clean logs
  45. *
  46. * @param Mage_Log_Model_Log $object
  47. * @return Mage_Log_Model_Resource_Log
  48. */
  49. public function clean(Mage_Log_Model_Log $object)
  50. {
  51. $cleanTime = $object->getLogCleanTime();
  52. Mage::dispatchEvent('log_log_clean_before', array(
  53. 'log' => $object
  54. ));
  55. $this->_cleanVisitors($cleanTime);
  56. $this->_cleanCustomers($cleanTime);
  57. $this->_cleanUrls();
  58. Mage::dispatchEvent('log_log_clean_after', array(
  59. 'log' => $object
  60. ));
  61. return $this;
  62. }
  63. /**
  64. * Clean visitors table
  65. *
  66. * @param int $time
  67. * @return Mage_Log_Model_Resource_Log
  68. */
  69. protected function _cleanVisitors($time)
  70. {
  71. $readAdapter = $this->_getReadAdapter();
  72. $writeAdapter = $this->_getWriteAdapter();
  73. $timeLimit = $this->formatDate(Mage::getModel('core/date')->gmtTimestamp() - $time);
  74. while (true) {
  75. $select = $readAdapter->select()
  76. ->from(
  77. array('visitor_table' => $this->getTable('log/visitor')),
  78. array('visitor_id' => 'visitor_table.visitor_id'))
  79. ->joinLeft(
  80. array('customer_table' => $this->getTable('log/customer')),
  81. 'visitor_table.visitor_id = customer_table.visitor_id AND customer_table.log_id IS NULL',
  82. array())
  83. ->where('visitor_table.last_visit_at < ?', $timeLimit)
  84. ->limit(100);
  85. $visitorIds = $readAdapter->fetchCol($select);
  86. if (!$visitorIds) {
  87. break;
  88. }
  89. $condition = array('visitor_id IN (?)' => $visitorIds);
  90. // remove visitors from log/quote
  91. $writeAdapter->delete($this->getTable('log/quote_table'), $condition);
  92. // remove visitors from log/url
  93. $writeAdapter->delete($this->getTable('log/url_table'), $condition);
  94. // remove visitors from log/visitor_info
  95. $writeAdapter->delete($this->getTable('log/visitor_info'), $condition);
  96. // remove visitors from log/visitor
  97. $writeAdapter->delete($this->getTable('log/visitor'), $condition);
  98. }
  99. return $this;
  100. }
  101. /**
  102. * Clean customer table
  103. *
  104. * @param int $time
  105. * @return Mage_Log_Model_Resource_Log
  106. */
  107. protected function _cleanCustomers($time)
  108. {
  109. $readAdapter = $this->_getReadAdapter();
  110. $writeAdapter = $this->_getWriteAdapter();
  111. $timeLimit = $this->formatDate(Mage::getModel('core/date')->gmtTimestamp() - $time);
  112. // retrieve last active customer log id
  113. $lastLogId = $readAdapter->fetchOne(
  114. $readAdapter->select()
  115. ->from($this->getTable('log/customer'), 'log_id')
  116. ->where('login_at < ?', $timeLimit)
  117. ->order('log_id DESC')
  118. ->limit(1)
  119. );
  120. if (!$lastLogId) {
  121. return $this;
  122. }
  123. // Order by desc log_id before grouping (within-group aggregates query pattern)
  124. $select = $readAdapter->select()
  125. ->from(
  126. array('log_customer_main' => $this->getTable('log/customer')),
  127. array('log_id'))
  128. ->joinLeft(
  129. array('log_customer' => $this->getTable('log/customer')),
  130. 'log_customer_main.customer_id = log_customer.customer_id '
  131. . 'AND log_customer_main.log_id < log_customer.log_id',
  132. array())
  133. ->where('log_customer.customer_id IS NULL')
  134. ->where('log_customer_main.log_id < ?', $lastLogId + 1);
  135. $needLogIds = array();
  136. $query = $readAdapter->query($select);
  137. while ($row = $query->fetch()) {
  138. $needLogIds[$row['log_id']] = 1;
  139. }
  140. $customerLogId = 0;
  141. while (true) {
  142. $visitorIds = array();
  143. $select = $readAdapter->select()
  144. ->from(
  145. $this->getTable('log/customer'),
  146. array('log_id', 'visitor_id'))
  147. ->where('log_id > ?', $customerLogId)
  148. ->where('log_id < ?', $lastLogId + 1)
  149. ->order('log_id')
  150. ->limit(100);
  151. $query = $readAdapter->query($select);
  152. $count = 0;
  153. while ($row = $query->fetch()) {
  154. $count++;
  155. $customerLogId = $row['log_id'];
  156. if (!isset($needLogIds[$row['log_id']])) {
  157. $visitorIds[] = $row['visitor_id'];
  158. }
  159. }
  160. if (!$count) {
  161. break;
  162. }
  163. if ($visitorIds) {
  164. $condition = array('visitor_id IN (?)' => $visitorIds);
  165. // remove visitors from log/quote
  166. $writeAdapter->delete($this->getTable('log/quote_table'), $condition);
  167. // remove visitors from log/url
  168. $writeAdapter->delete($this->getTable('log/url_table'), $condition);
  169. // remove visitors from log/visitor_info
  170. $writeAdapter->delete($this->getTable('log/visitor_info'), $condition);
  171. // remove visitors from log/visitor
  172. $writeAdapter->delete($this->getTable('log/visitor'), $condition);
  173. // remove customers from log/customer
  174. $writeAdapter->delete($this->getTable('log/customer'), $condition);
  175. }
  176. if ($customerLogId == $lastLogId) {
  177. break;
  178. }
  179. }
  180. return $this;
  181. }
  182. /**
  183. * Clean url table
  184. *
  185. * @return Mage_Log_Model_Resource_Log
  186. */
  187. protected function _cleanUrls()
  188. {
  189. $readAdapter = $this->_getReadAdapter();
  190. $writeAdapter = $this->_getWriteAdapter();
  191. while (true) {
  192. $select = $readAdapter->select()
  193. ->from(
  194. array('url_info_table' => $this->getTable('log/url_info_table')),
  195. array('url_id'))
  196. ->joinLeft(
  197. array('url_table' => $this->getTable('log/url_table')),
  198. 'url_info_table.url_id = url_table.url_id',
  199. array())
  200. ->where('url_table.url_id IS NULL')
  201. ->limit(100);
  202. $urlIds = $readAdapter->fetchCol($select);
  203. if (!$urlIds) {
  204. break;
  205. }
  206. $writeAdapter->delete(
  207. $this->getTable('log/url_info_table'),
  208. array('url_id IN (?)' => $urlIds)
  209. );
  210. }
  211. return $this;
  212. }
  213. }