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
|
<?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\DataAccess\LogQueryBuilder;
use Exception;
use Piwik\Plugin\LogTablesProvider;
class JoinTables extends \ArrayObject
{
/**
* @var LogTablesProvider
*/
private $logTableProvider;
/**
* Tables constructor.
* @param LogTablesProvider $logTablesProvider
* @param array $tables
*/
public function __construct(LogTablesProvider $logTablesProvider, $tables)
{
$this->logTableProvider = $logTablesProvider;
foreach ($tables as $table) {
$this->checkTableCanBeUsedForSegmentation($table);
}
$this->exchangeArray(array_values($tables));
}
public function getTables()
{
return $this->getArrayCopy();
}
public function addTableToJoin($tableName)
{
$this->checkTableCanBeUsedForSegmentation($tableName);
$this->append($tableName);
}
public function hasJoinedTable($tableName)
{
return in_array($tableName, $this->getTables());
}
public function hasJoinedTableManually($tableToFind, $joinToFind)
{
foreach ($this as $table) {
if (is_array($table)
&& !empty($table['table'])
&& $table['table'] === $tableToFind
&& (!isset($table['tableAlias']) || $table['tableAlias'] === $tableToFind)
&& (!isset($table['join']) || strtolower($table['join']) === 'left join')
&& isset($table['joinOn']) && $table['joinOn'] === $joinToFind) {
return true;
}
}
return false;
}
public function getLogTable($tableName)
{
return $this->logTableProvider->getLogTable($tableName);
}
public function findIndexOfManuallyAddedTable($tableNameToFind)
{
foreach ($this as $index => $table) {
if (is_array($table)
&& !empty($table['table'])
&& $table['table'] === $tableNameToFind
&& (!isset($table['join']) || strtolower($table['join']) === 'left join')
&& (!isset($table['tableAlias']) || $table['tableAlias'] === $tableNameToFind)) {
return $index;
}
}
}
public function hasAddedTableManually($tableToFind)
{
$table = $this->findIndexOfManuallyAddedTable($tableToFind);
return isset($table);
}
public function sort($cmpFunction)
{
// we do not use $this->uasort as we do not want to maintain keys
$tables = $this->getTables();
// we need to make sure first table always comes first, only sort tables after the first table
$firstTable = array_shift($tables);
usort($tables, function ($ta, $tb) use ($tables, $cmpFunction) {
$return = call_user_func($cmpFunction, $ta, $tb);
if ($return === 0) {
$indexA = array_search($ta, $tables);
$indexB = array_search($tb, $tables);
return $indexA - $indexB;
}
return $return;
});
array_unshift($tables, $firstTable);
$this->exchangeArray($tables);
}
private function checkTableCanBeUsedForSegmentation($tableName)
{
if (!is_array($tableName) && !$this->getLogTable($tableName)) {
throw new Exception("Table '$tableName' can't be used for segmentation");
}
}
}
|