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
|
<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Plugins\SegmentEditor;
use Piwik\DataAccess\LogQueryBuilder;
use Piwik\Plugin\LogTablesProvider;
use Piwik\Plugins\SegmentEditor\Services\StoredSegmentService;
use Piwik\Segment\SegmentExpression;
use Piwik\SettingsServer;
/**
* Decorates segment sub-queries in archiving queries w/ the idSegment of the segment, if
* a stored segment exists.
*
* This class is configured for use in SegmentEditor's DI config.
*/
class SegmentQueryDecorator extends LogQueryBuilder
{
/**
* @var StoredSegmentService
*/
private $storedSegmentService;
public function __construct(StoredSegmentService $storedSegmentService, LogTablesProvider $logTablesProvider)
{
$this->storedSegmentService = $storedSegmentService;
parent::__construct($logTablesProvider);
}
public function getSelectQueryString(SegmentExpression $segmentExpression, $select, $from, $where, $bind, $groupBy,
$orderBy, $limit)
{
$result = parent::getSelectQueryString($segmentExpression, $select, $from, $where, $bind, $groupBy, $orderBy,
$limit);
$prefixParts = array();
if (SettingsServer::isArchivePhpTriggered()) {
$prefixParts[] = 'trigger = CronArchive';
}
$idSegments = $this->getSegmentIdOfExpression($segmentExpression);
if (!empty($idSegments)) {
$prefixParts[] = "idSegments = [" . implode(', ', $idSegments) . "]";
}
$select = 'SELECT';
if (!empty($prefixParts) && 0 === strpos(trim($result['sql']), $select)) {
$result['sql'] = trim($result['sql']);
$result['sql'] = 'SELECT /* ' . implode(', ', $prefixParts) . ' */' . substr($result['sql'], strlen($select));
}
return $result;
}
private function getSegmentIdOfExpression(SegmentExpression $segmentExpression)
{
$allSegments = $this->storedSegmentService->getAllSegmentsAndIgnoreVisibility();
$idSegments = array();
foreach ($allSegments as $segment) {
if ($segmentExpression->getSegmentDefinition() == $segment['definition']) {
$idSegments[] = $segment['idsegment'];
}
}
return $idSegments;
}
}
|