1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27:
28: 29: 30: 31: 32: 33: 34:
35: class Mage_Log_Model_Resource_Log extends Mage_Core_Model_Resource_Db_Abstract
36: {
37: 38: 39: 40:
41: protected function _construct()
42: {
43: $this->_init('log/visitor', 'visitor_id');
44: }
45:
46: 47: 48: 49: 50: 51:
52: public function clean(Mage_Log_Model_Log $object)
53: {
54: $cleanTime = $object->getLogCleanTime();
55:
56: Mage::dispatchEvent('log_log_clean_before', array(
57: 'log' => $object
58: ));
59:
60: $this->_cleanVisitors($cleanTime);
61: $this->_cleanCustomers($cleanTime);
62: $this->_cleanUrls();
63:
64: Mage::dispatchEvent('log_log_clean_after', array(
65: 'log' => $object
66: ));
67:
68: return $this;
69: }
70:
71: 72: 73: 74: 75: 76:
77: protected function _cleanVisitors($time)
78: {
79: $readAdapter = $this->_getReadAdapter();
80: $writeAdapter = $this->_getWriteAdapter();
81:
82: $timeLimit = $this->formatDate(Mage::getModel('core/date')->gmtTimestamp() - $time);
83:
84: while (true) {
85: $select = $readAdapter->select()
86: ->from(
87: array('visitor_table' => $this->getTable('log/visitor')),
88: array('visitor_id' => 'visitor_table.visitor_id'))
89: ->joinLeft(
90: array('customer_table' => $this->getTable('log/customer')),
91: 'visitor_table.visitor_id = customer_table.visitor_id AND customer_table.log_id IS NULL',
92: array())
93: ->where('visitor_table.last_visit_at < ?', $timeLimit)
94: ->limit(100);
95:
96: $visitorIds = $readAdapter->fetchCol($select);
97:
98: if (!$visitorIds) {
99: break;
100: }
101:
102: $condition = array('visitor_id IN (?)' => $visitorIds);
103:
104:
105: $writeAdapter->delete($this->getTable('log/quote_table'), $condition);
106:
107:
108: $writeAdapter->delete($this->getTable('log/url_table'), $condition);
109:
110:
111: $writeAdapter->delete($this->getTable('log/visitor_info'), $condition);
112:
113:
114: $writeAdapter->delete($this->getTable('log/visitor'), $condition);
115: }
116:
117: return $this;
118: }
119:
120: 121: 122: 123: 124: 125:
126: protected function _cleanCustomers($time)
127: {
128: $readAdapter = $this->_getReadAdapter();
129: $writeAdapter = $this->_getWriteAdapter();
130:
131: $timeLimit = $this->formatDate(Mage::getModel('core/date')->gmtTimestamp() - $time);
132:
133:
134: $lastLogId = $readAdapter->fetchOne(
135: $readAdapter->select()
136: ->from($this->getTable('log/customer'), 'log_id')
137: ->where('login_at < ?', $timeLimit)
138: ->order('log_id DESC')
139: ->limit(1)
140: );
141:
142: if (!$lastLogId) {
143: return $this;
144: }
145:
146:
147: $select = $readAdapter->select()
148: ->from(
149: array('log_customer_main' => $this->getTable('log/customer')),
150: array('log_id'))
151: ->joinLeft(
152: array('log_customer' => $this->getTable('log/customer')),
153: 'log_customer_main.customer_id = log_customer.customer_id '
154: . 'AND log_customer_main.log_id < log_customer.log_id',
155: array())
156: ->where('log_customer.customer_id IS NULL')
157: ->where('log_customer_main.log_id < ?', $lastLogId + 1);
158:
159: $needLogIds = array();
160: $query = $readAdapter->query($select);
161: while ($row = $query->fetch()) {
162: $needLogIds[$row['log_id']] = 1;
163: }
164:
165: $customerLogId = 0;
166: while (true) {
167: $visitorIds = array();
168: $select = $readAdapter->select()
169: ->from(
170: $this->getTable('log/customer'),
171: array('log_id', 'visitor_id'))
172: ->where('log_id > ?', $customerLogId)
173: ->where('log_id < ?', $lastLogId + 1)
174: ->order('log_id')
175: ->limit(100);
176:
177: $query = $readAdapter->query($select);
178: $count = 0;
179: while ($row = $query->fetch()) {
180: $count++;
181: $customerLogId = $row['log_id'];
182: if (!isset($needLogIds[$row['log_id']])) {
183: $visitorIds[] = $row['visitor_id'];
184: }
185: }
186:
187: if (!$count) {
188: break;
189: }
190:
191: if ($visitorIds) {
192: $condition = array('visitor_id IN (?)' => $visitorIds);
193:
194:
195: $writeAdapter->delete($this->getTable('log/quote_table'), $condition);
196:
197:
198: $writeAdapter->delete($this->getTable('log/url_table'), $condition);
199:
200:
201: $writeAdapter->delete($this->getTable('log/visitor_info'), $condition);
202:
203:
204: $writeAdapter->delete($this->getTable('log/visitor'), $condition);
205:
206:
207: $writeAdapter->delete($this->getTable('log/customer'), $condition);
208: }
209:
210: if ($customerLogId == $lastLogId) {
211: break;
212: }
213: }
214:
215: return $this;
216: }
217:
218: 219: 220: 221: 222:
223: protected function _cleanUrls()
224: {
225: $readAdapter = $this->_getReadAdapter();
226: $writeAdapter = $this->_getWriteAdapter();
227:
228: while (true) {
229: $select = $readAdapter->select()
230: ->from(
231: array('url_info_table' => $this->getTable('log/url_info_table')),
232: array('url_id'))
233: ->joinLeft(
234: array('url_table' => $this->getTable('log/url_table')),
235: 'url_info_table.url_id = url_table.url_id',
236: array())
237: ->where('url_table.url_id IS NULL')
238: ->limit(100);
239:
240: $urlIds = $readAdapter->fetchCol($select);
241:
242: if (!$urlIds) {
243: break;
244: }
245:
246: $writeAdapter->delete(
247: $this->getTable('log/url_info_table'),
248: array('url_id IN (?)' => $urlIds)
249: );
250: }
251:
252: return $this;
253: }
254: }
255: