blob: db368a315e2503c060d0cbc55f5d9bb9e803c47c (
plain)
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
|
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Plugins\BulkTracking\Tracker;
use Piwik\Exception\UnexpectedWebsiteFoundException;
use Piwik\Tracker;
use Piwik\Tracker\RequestSet;
use Piwik\Tracker\TrackerConfig;
use Exception;
class Handler extends Tracker\Handler
{
private $transactionId = null;
public function __construct()
{
$this->setResponse(new Response());
}
public function onStartTrackRequests(Tracker $tracker, RequestSet $requestSet)
{
if ($this->isTransactionSupported()) {
$this->beginTransaction();
}
}
public function onAllRequestsTracked(Tracker $tracker, RequestSet $requestSet)
{
$this->commitTransaction();
// Do not run schedule task if we are importing logs or doing custom tracking (as it could slow down)
}
public function process(Tracker $tracker, RequestSet $requestSet)
{
$invalidRequests = 0;
foreach ($requestSet->getRequests() as $request) {
try {
$tracker->trackRequest($request);
} catch (UnexpectedWebsiteFoundException $ex) {
$invalidRequests += 1;
}
}
/** @var Response $response */
$response = $this->getResponse();
$response->setInvalidCount($invalidRequests);
}
public function onException(Tracker $tracker, RequestSet $requestSet, Exception $e)
{
$this->rollbackTransaction();
parent::onException($tracker, $requestSet, $e);
}
private function beginTransaction()
{
if (empty($this->transactionId)) {
$this->transactionId = $this->getDb()->beginTransaction();
}
}
private function commitTransaction()
{
if (!empty($this->transactionId)) {
$this->getDb()->commit($this->transactionId);
$this->transactionId = null;
}
}
private function rollbackTransaction()
{
if (!empty($this->transactionId)) {
$this->getDb()->rollback($this->transactionId);
$this->transactionId = null;
}
}
private function getDb()
{
return Tracker::getDatabase();
}
/**
* @return bool
*/
private function isTransactionSupported()
{
return (bool) TrackerConfig::getConfigValue('bulk_requests_use_transaction');
}
}
|