00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 class Mage_Log_Model_Mysql4_Log extends Mage_Core_Model_Mysql4_Abstract
00036 {
00037
00038
00039
00040
00041 protected function _construct()
00042 {
00043 $this->_init('log/visitor', 'visitor_id');
00044 }
00045
00046
00047
00048
00049
00050
00051
00052 public function clean(Mage_Log_Model_Log $object)
00053 {
00054 $cleanTime = $object->getLogCleanTime();
00055
00056 Mage::dispatchEvent('log_log_clean_before', array(
00057 'log' => $object
00058 ));
00059
00060 $this->_cleanVisitors($cleanTime);
00061 $this->_cleanCustomers($cleanTime);
00062 $this->_cleanUrls();
00063
00064 Mage::dispatchEvent('log_log_clean_after', array(
00065 'log' => $object
00066 ));
00067
00068 return $this;
00069 }
00070
00071
00072
00073
00074
00075
00076
00077 protected function _cleanVisitors($time)
00078 {
00079 while (true) {
00080 $select = $this->_getReadAdapter()->select()
00081 ->from(
00082 array('visitor_table' => $this->getTable('log/visitor')),
00083 array('visitor_id' => 'visitor_table.visitor_id'))
00084 ->joinLeft(
00085 array('customer_table' => $this->getTable('log/customer')),
00086 'visitor_table.visitor_id = customer_table.visitor_id AND customer_table.log_id IS NULL',
00087 array())
00088 ->where('visitor_table.last_visit_at < ?', gmdate('Y-m-d H:i:s', time() - $time))
00089 ->limit(100);
00090
00091 $query = $this->_getReadAdapter()->query($select);
00092 $visitorIds = array();
00093 while ($row = $query->fetch()) {
00094 $visitorIds[] = $row['visitor_id'];
00095 }
00096
00097 if (!$visitorIds) {
00098 break;
00099 }
00100
00101
00102 $this->_getWriteAdapter()->delete(
00103 $this->getTable('log/quote_table'),
00104 $this->_getWriteAdapter()->quoteInto('visitor_id IN(?)', $visitorIds)
00105 );
00106
00107
00108 $this->_getWriteAdapter()->delete(
00109 $this->getTable('log/url_table'),
00110 $this->_getWriteAdapter()->quoteInto('visitor_id IN(?)', $visitorIds)
00111 );
00112
00113
00114 $this->_getWriteAdapter()->delete(
00115 $this->getTable('log/visitor_info'),
00116 $this->_getWriteAdapter()->quoteInto('visitor_id IN(?)', $visitorIds)
00117 );
00118
00119
00120 $this->_getWriteAdapter()->delete(
00121 $this->getTable('log/visitor'),
00122 $this->_getWriteAdapter()->quoteInto('visitor_id IN(?)', $visitorIds)
00123 );
00124 }
00125
00126 return $this;
00127 }
00128
00129
00130
00131
00132
00133
00134
00135 protected function _cleanCustomers($time)
00136 {
00137
00138 $row = $this->_getReadAdapter()->fetchRow(
00139 $this->_getReadAdapter()->select()
00140 ->from($this->getTable('log/customer'), 'log_id')
00141 ->where('login_at < ?', gmdate('Y-m-d H:i:s', time() - $time))
00142 ->order('log_id DESC')
00143 ->limit(1)
00144 );
00145
00146 if (!$row) {
00147 return $this;
00148 }
00149
00150 $lastLogId = $row['log_id'];
00151
00152
00153 $select = $this->_getReadAdapter()->select()
00154 ->from(
00155 array('log_customer_main' => $this->getTable('log/customer')),
00156 array('log_id'))
00157 ->joinLeft(
00158 array('log_customer' => $this->getTable('log/customer')),
00159 'log_customer_main.customer_id = log_customer.customer_id AND log_customer_main.log_id < log_customer.log_id',
00160 array())
00161 ->where('log_customer.customer_id IS NULL')
00162 ->where('log_customer_main.log_id<?', $lastLogId + 1);
00163
00164 $needLogIds = array();
00165 $query = $this->_getReadAdapter()->query($select);
00166 while ($row = $query->fetch()) {
00167 $needLogIds[$row['log_id']] = 1;
00168 }
00169
00170 $customerLogId = 0;
00171 while (true) {
00172 $visitorIds = array();
00173 $select = $this->_getReadAdapter()->select()
00174 ->from(
00175 $this->getTable('log/customer'),
00176 array('log_id', 'visitor_id'))
00177 ->where('log_id>?', $customerLogId)
00178 ->where('log_id<?', $lastLogId + 1)
00179 ->order('log_id')
00180 ->limit(100);
00181
00182 $query = $this->_getReadAdapter()->query($select);
00183 $count = 0;
00184 while ($row = $query->fetch()) {
00185 $count++;
00186 $customerLogId = $row['log_id'];
00187 if (!isset($needLogIds[$row['log_id']])) {
00188 $visitorIds[] = $row['visitor_id'];
00189 }
00190 }
00191
00192 if (!$count) {
00193 break;
00194 }
00195
00196 if ($visitorIds) {
00197
00198 $this->_getWriteAdapter()->delete(
00199 $this->getTable('log/quote_table'),
00200 $this->_getWriteAdapter()->quoteInto('visitor_id IN(?)', $visitorIds)
00201 );
00202
00203
00204 $this->_getWriteAdapter()->delete(
00205 $this->getTable('log/url_table'),
00206 $this->_getWriteAdapter()->quoteInto('visitor_id IN(?)', $visitorIds)
00207 );
00208
00209
00210 $this->_getWriteAdapter()->delete(
00211 $this->getTable('log/visitor_info'),
00212 $this->_getWriteAdapter()->quoteInto('visitor_id IN(?)', $visitorIds)
00213 );
00214
00215
00216 $this->_getWriteAdapter()->delete(
00217 $this->getTable('log/visitor'),
00218 $this->_getWriteAdapter()->quoteInto('visitor_id IN(?)', $visitorIds)
00219 );
00220
00221
00222 $this->_getWriteAdapter()->delete(
00223 $this->getTable('log/customer'),
00224 $this->_getWriteAdapter()->quoteInto('visitor_id IN(?)', $visitorIds)
00225 );
00226 }
00227
00228 if ($customerLogId == $lastLogId) {
00229 break;
00230 }
00231 }
00232
00233 return $this;
00234 }
00235
00236
00237
00238
00239
00240
00241 protected function _cleanUrls()
00242 {
00243 while (true) {
00244 $urlIds = array();
00245 $select = $this->_getReadAdapter()->select()
00246 ->from(
00247 array('url_info_table' => $this->getTable('log/url_info_table')),
00248 array('url_id'))
00249 ->joinLeft(
00250 array('url_table' => $this->getTable('log/url_table')),
00251 'url_info_table.url_id = url_table.url_id',
00252 array())
00253 ->where('url_table.url_id IS NULL')
00254 ->limit(100);
00255 $query = $this->_getReadAdapter()->query($select);
00256 while ($row = $query->fetch()) {
00257 $urlIds[] = $row['url_id'];
00258 }
00259
00260 if (!$urlIds) {
00261 break;
00262 }
00263
00264 $this->_getWriteAdapter()->delete(
00265 $this->getTable('log/url_info_table'),
00266 $this->_getWriteAdapter()->quoteInto('url_id IN(?)', $urlIds)
00267 );
00268 }
00269 }
00270 }