magento-ce-1.5.1.0 /app/code/core/Mage/Log/Model/Mysql4/Log.php

Language PHP Lines 271
MD5 Hash d4aee356ee64928309664219157b1480 Estimated Cost $4,126 (why?)
Repository https://bitbucket.org/MXWest/magento-ce-1.5.1.0.git View Raw File
  1
  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
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
<?php
/**
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Open Software License (OSL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/osl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magentocommerce.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magentocommerce.com for more information.
 *
 * @category    Mage
 * @package     Mage_Log
 * @copyright   Copyright (c) 2010 Magento Inc. (http://www.magentocommerce.com)
 * @license     http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */


/**
 * Log Resource Model
 *
 * @category   Mage
 * @package    Mage_Log
 * @author     Magento Core Team <core@magentocommerce.com>
 */
class Mage_Log_Model_Mysql4_Log extends Mage_Core_Model_Mysql4_Abstract
{
    /**
     * Init Resource model and connection
     *
     */
    protected function _construct()
    {
        $this->_init('log/visitor', 'visitor_id');
    }

    /**
     * Clean logs
     *
     * @param Mage_Log_Model_Log $object
     * @return Mage_Log_Model_Mysql4_Log
     */
    public function clean(Mage_Log_Model_Log $object)
    {
        $cleanTime = $object->getLogCleanTime();

        Mage::dispatchEvent('log_log_clean_before', array(
            'log'   => $object
        ));

        $this->_cleanVisitors($cleanTime);
        $this->_cleanCustomers($cleanTime);
        $this->_cleanUrls();

        Mage::dispatchEvent('log_log_clean_after', array(
            'log'   => $object
        ));

        return $this;
    }

    /**
     * Clean visitors table
     *
     * @param int $time
     * @return Mage_Log_Model_Mysql4_Log
     */
    protected function _cleanVisitors($time)
    {
        while (true) {
            $select = $this->_getReadAdapter()->select()
            ->from(
                array('visitor_table' => $this->getTable('log/visitor')),
                array('visitor_id' => 'visitor_table.visitor_id'))
            ->joinLeft(
                array('customer_table' => $this->getTable('log/customer')),
                'visitor_table.visitor_id = customer_table.visitor_id AND customer_table.log_id IS NULL',
                array())
            ->where('visitor_table.last_visit_at < ?', gmdate('Y-m-d H:i:s', time() - $time))
            ->limit(100);

            $query = $this->_getReadAdapter()->query($select);
            $visitorIds = array();
            while ($row = $query->fetch()) {
                $visitorIds[] = $row['visitor_id'];
            }

            if (!$visitorIds) {
                break;
            }

            // remove visitors from log/quote
            $this->_getWriteAdapter()->delete(
                $this->getTable('log/quote_table'),
                $this->_getWriteAdapter()->quoteInto('visitor_id IN(?)', $visitorIds)
            );

            // remove visitors from log/url
            $this->_getWriteAdapter()->delete(
                $this->getTable('log/url_table'),
                $this->_getWriteAdapter()->quoteInto('visitor_id IN(?)', $visitorIds)
            );

            // remove visitors from log/visitor_info
            $this->_getWriteAdapter()->delete(
                $this->getTable('log/visitor_info'),
                $this->_getWriteAdapter()->quoteInto('visitor_id IN(?)', $visitorIds)
            );

            // remove visitors from log/visitor
            $this->_getWriteAdapter()->delete(
                $this->getTable('log/visitor'),
                $this->_getWriteAdapter()->quoteInto('visitor_id IN(?)', $visitorIds)
            );
        }

        return $this;
    }

    /**
     * Clean customer table
     *
     * @param int $time
     * @return Mage_Log_Model_Mysql4_Log
     */
    protected function _cleanCustomers($time)
    {
        // retrieve last active customer log id
        $row = $this->_getReadAdapter()->fetchRow(
            $this->_getReadAdapter()->select()
                ->from($this->getTable('log/customer'), 'log_id')
                ->where('login_at < ?', gmdate('Y-m-d H:i:s', time() - $time))
                ->order('log_id DESC')
                ->limit(1)
        );

        if (!$row) {
            return $this;
        }

        $lastLogId = $row['log_id'];

        // Order by desc log_id before grouping (within-group aggregates query pattern)
        $select = $this->_getReadAdapter()->select()
            ->from(
                array('log_customer_main' => $this->getTable('log/customer')),
                array('log_id'))
            ->joinLeft(
                array('log_customer' => $this->getTable('log/customer')),
                'log_customer_main.customer_id = log_customer.customer_id AND log_customer_main.log_id < log_customer.log_id',
                array())
            ->where('log_customer.customer_id IS NULL')
            ->where('log_customer_main.log_id<?', $lastLogId + 1);

        $needLogIds = array();
        $query = $this->_getReadAdapter()->query($select);
        while ($row = $query->fetch()) {
            $needLogIds[$row['log_id']] = 1;
        }

        $customerLogId = 0;
        while (true) {
            $visitorIds = array();
            $select = $this->_getReadAdapter()->select()
                ->from(
                    $this->getTable('log/customer'),
                    array('log_id', 'visitor_id'))
                ->where('log_id>?', $customerLogId)
                ->where('log_id<?', $lastLogId + 1)
                ->order('log_id')
                ->limit(100);

            $query = $this->_getReadAdapter()->query($select);
            $count = 0;
            while ($row = $query->fetch()) {
                $count++;
                $customerLogId = $row['log_id'];
                if (!isset($needLogIds[$row['log_id']])) {
                    $visitorIds[] = $row['visitor_id'];
                }
            }

            if (!$count) {
                break;
            }

            if ($visitorIds) {
                // remove visitors from log/quote
                $this->_getWriteAdapter()->delete(
                    $this->getTable('log/quote_table'),
                    $this->_getWriteAdapter()->quoteInto('visitor_id IN(?)', $visitorIds)
                );

                // remove visitors from log/url
                $this->_getWriteAdapter()->delete(
                    $this->getTable('log/url_table'),
                    $this->_getWriteAdapter()->quoteInto('visitor_id IN(?)', $visitorIds)
                );

                // remove visitors from log/visitor_info
                $this->_getWriteAdapter()->delete(
                    $this->getTable('log/visitor_info'),
                    $this->_getWriteAdapter()->quoteInto('visitor_id IN(?)', $visitorIds)
                );

                // remove visitors from log/visitor
                $this->_getWriteAdapter()->delete(
                    $this->getTable('log/visitor'),
                    $this->_getWriteAdapter()->quoteInto('visitor_id IN(?)', $visitorIds)
                );

                // remove customers from log/customer
                $this->_getWriteAdapter()->delete(
                    $this->getTable('log/customer'),
                    $this->_getWriteAdapter()->quoteInto('visitor_id IN(?)', $visitorIds)
                );
            }

            if ($customerLogId == $lastLogId) {
                break;
            }
        }

        return $this;
    }

    /**
     * Clean url table
     *
     * @return Mage_Log_Model_Mysql4_Log
     */
    protected function _cleanUrls()
    {
        while (true) {
            $urlIds = array();
            $select = $this->_getReadAdapter()->select()
                ->from(
                    array('url_info_table' => $this->getTable('log/url_info_table')),
                    array('url_id'))
                ->joinLeft(
                    array('url_table' => $this->getTable('log/url_table')),
                    'url_info_table.url_id = url_table.url_id',
                    array())
                ->where('url_table.url_id IS NULL')
                ->limit(100);
            $query = $this->_getReadAdapter()->query($select);
            while ($row = $query->fetch()) {
                $urlIds[] = $row['url_id'];
            }

            if (!$urlIds) {
                break;
            }

            $this->_getWriteAdapter()->delete(
                $this->getTable('log/url_info_table'),
                $this->_getWriteAdapter()->quoteInto('url_id IN(?)', $urlIds)
            );
        }
    }
}
Back to Top