PageRenderTime 49ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/MXWest/magento-ce-1.5.1.0
PHP | 270 lines | 168 code | 33 blank | 69 comment | 15 complexity | d4aee356ee64928309664219157b1480 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-2.1, GPL-2.0, WTFPL
  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) 2010 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_Mysql4_Log extends Mage_Core_Model_Mysql4_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_Mysql4_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_Mysql4_Log
  68. */
  69. protected function _cleanVisitors($time)
  70. {
  71. while (true) {
  72. $select = $this->_getReadAdapter()->select()
  73. ->from(
  74. array('visitor_table' => $this->getTable('log/visitor')),
  75. array('visitor_id' => 'visitor_table.visitor_id'))
  76. ->joinLeft(
  77. array('customer_table' => $this->getTable('log/customer')),
  78. 'visitor_table.visitor_id = customer_table.visitor_id AND customer_table.log_id IS NULL',
  79. array())
  80. ->where('visitor_table.last_visit_at < ?', gmdate('Y-m-d H:i:s', time() - $time))
  81. ->limit(100);
  82. $query = $this->_getReadAdapter()->query($select);
  83. $visitorIds = array();
  84. while ($row = $query->fetch()) {
  85. $visitorIds[] = $row['visitor_id'];
  86. }
  87. if (!$visitorIds) {
  88. break;
  89. }
  90. // remove visitors from log/quote
  91. $this->_getWriteAdapter()->delete(
  92. $this->getTable('log/quote_table'),
  93. $this->_getWriteAdapter()->quoteInto('visitor_id IN(?)', $visitorIds)
  94. );
  95. // remove visitors from log/url
  96. $this->_getWriteAdapter()->delete(
  97. $this->getTable('log/url_table'),
  98. $this->_getWriteAdapter()->quoteInto('visitor_id IN(?)', $visitorIds)
  99. );
  100. // remove visitors from log/visitor_info
  101. $this->_getWriteAdapter()->delete(
  102. $this->getTable('log/visitor_info'),
  103. $this->_getWriteAdapter()->quoteInto('visitor_id IN(?)', $visitorIds)
  104. );
  105. // remove visitors from log/visitor
  106. $this->_getWriteAdapter()->delete(
  107. $this->getTable('log/visitor'),
  108. $this->_getWriteAdapter()->quoteInto('visitor_id IN(?)', $visitorIds)
  109. );
  110. }
  111. return $this;
  112. }
  113. /**
  114. * Clean customer table
  115. *
  116. * @param int $time
  117. * @return Mage_Log_Model_Mysql4_Log
  118. */
  119. protected function _cleanCustomers($time)
  120. {
  121. // retrieve last active customer log id
  122. $row = $this->_getReadAdapter()->fetchRow(
  123. $this->_getReadAdapter()->select()
  124. ->from($this->getTable('log/customer'), 'log_id')
  125. ->where('login_at < ?', gmdate('Y-m-d H:i:s', time() - $time))
  126. ->order('log_id DESC')
  127. ->limit(1)
  128. );
  129. if (!$row) {
  130. return $this;
  131. }
  132. $lastLogId = $row['log_id'];
  133. // Order by desc log_id before grouping (within-group aggregates query pattern)
  134. $select = $this->_getReadAdapter()->select()
  135. ->from(
  136. array('log_customer_main' => $this->getTable('log/customer')),
  137. array('log_id'))
  138. ->joinLeft(
  139. array('log_customer' => $this->getTable('log/customer')),
  140. 'log_customer_main.customer_id = log_customer.customer_id AND log_customer_main.log_id < log_customer.log_id',
  141. array())
  142. ->where('log_customer.customer_id IS NULL')
  143. ->where('log_customer_main.log_id<?', $lastLogId + 1);
  144. $needLogIds = array();
  145. $query = $this->_getReadAdapter()->query($select);
  146. while ($row = $query->fetch()) {
  147. $needLogIds[$row['log_id']] = 1;
  148. }
  149. $customerLogId = 0;
  150. while (true) {
  151. $visitorIds = array();
  152. $select = $this->_getReadAdapter()->select()
  153. ->from(
  154. $this->getTable('log/customer'),
  155. array('log_id', 'visitor_id'))
  156. ->where('log_id>?', $customerLogId)
  157. ->where('log_id<?', $lastLogId + 1)
  158. ->order('log_id')
  159. ->limit(100);
  160. $query = $this->_getReadAdapter()->query($select);
  161. $count = 0;
  162. while ($row = $query->fetch()) {
  163. $count++;
  164. $customerLogId = $row['log_id'];
  165. if (!isset($needLogIds[$row['log_id']])) {
  166. $visitorIds[] = $row['visitor_id'];
  167. }
  168. }
  169. if (!$count) {
  170. break;
  171. }
  172. if ($visitorIds) {
  173. // remove visitors from log/quote
  174. $this->_getWriteAdapter()->delete(
  175. $this->getTable('log/quote_table'),
  176. $this->_getWriteAdapter()->quoteInto('visitor_id IN(?)', $visitorIds)
  177. );
  178. // remove visitors from log/url
  179. $this->_getWriteAdapter()->delete(
  180. $this->getTable('log/url_table'),
  181. $this->_getWriteAdapter()->quoteInto('visitor_id IN(?)', $visitorIds)
  182. );
  183. // remove visitors from log/visitor_info
  184. $this->_getWriteAdapter()->delete(
  185. $this->getTable('log/visitor_info'),
  186. $this->_getWriteAdapter()->quoteInto('visitor_id IN(?)', $visitorIds)
  187. );
  188. // remove visitors from log/visitor
  189. $this->_getWriteAdapter()->delete(
  190. $this->getTable('log/visitor'),
  191. $this->_getWriteAdapter()->quoteInto('visitor_id IN(?)', $visitorIds)
  192. );
  193. // remove customers from log/customer
  194. $this->_getWriteAdapter()->delete(
  195. $this->getTable('log/customer'),
  196. $this->_getWriteAdapter()->quoteInto('visitor_id IN(?)', $visitorIds)
  197. );
  198. }
  199. if ($customerLogId == $lastLogId) {
  200. break;
  201. }
  202. }
  203. return $this;
  204. }
  205. /**
  206. * Clean url table
  207. *
  208. * @return Mage_Log_Model_Mysql4_Log
  209. */
  210. protected function _cleanUrls()
  211. {
  212. while (true) {
  213. $urlIds = array();
  214. $select = $this->_getReadAdapter()->select()
  215. ->from(
  216. array('url_info_table' => $this->getTable('log/url_info_table')),
  217. array('url_id'))
  218. ->joinLeft(
  219. array('url_table' => $this->getTable('log/url_table')),
  220. 'url_info_table.url_id = url_table.url_id',
  221. array())
  222. ->where('url_table.url_id IS NULL')
  223. ->limit(100);
  224. $query = $this->_getReadAdapter()->query($select);
  225. while ($row = $query->fetch()) {
  226. $urlIds[] = $row['url_id'];
  227. }
  228. if (!$urlIds) {
  229. break;
  230. }
  231. $this->_getWriteAdapter()->delete(
  232. $this->getTable('log/url_info_table'),
  233. $this->_getWriteAdapter()->quoteInto('url_id IN(?)', $urlIds)
  234. );
  235. }
  236. }
  237. }