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
|
<?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\SitesManager;
use Piwik\Cache;
use Piwik\Common;
use Piwik\Tracker\Request;
use Piwik\Tracker\Visitor;
class SiteUrls
{
private static $cacheId = 'allSiteUrlsPerSite';
public static function clearSitesCache()
{
self::getCache()->delete(self::$cacheId);
}
/**
* Groups all URLs by host, path and idsite.
*
* @param array $urls An array containing URLs by idsite,
* eg array(array($idSite = 1 => array('apache.piwik', 'apache2.piwik'), 2 => array(), ...))
* as returned by {@link getAllCachedSiteUrls()} and {@link getAllSiteUrls}
* @return array All urls grouped by host => path => idSites. Path having the most '/' will be listed first
* array(
'apache.piwik' => array(
'/test/two' => $idsite = array(3),
'/test' => $idsite = array(1),
'/' => $idsite = array(2),
),
'test.apache.piwik' => array(
'/test/two' => $idsite = array(3),
'/test' => $idsite = array(1),
'/' => $idsite = array(2, 3),
),
);
*/
public function groupUrlsByHost($siteUrls)
{
if (empty($siteUrls)) {
return array();
}
$allUrls = array();
foreach ($siteUrls as $idSite => $urls) {
$idSite = (int) $idSite;
foreach ($urls as $url) {
$this->addUrlByHost($allUrls, $idSite, $url);
}
}
$this->sortUrlsByHost($allUrls);
return $allUrls;
}
private function addUrlByHost(&$allUrls, $idSite, $url, $addPath = true)
{
$urlParsed = @parse_url($url);
if ($urlParsed === false || !isset($urlParsed['host'])) {
return;
}
$host = $this->toCanonicalHost($urlParsed['host']);
$path = $this->getCanonicalPathFromParsedUrl($urlParsed);
if (!isset($allUrls[$host])) {
$allUrls[$host] = array();
}
if (!$addPath) {
$path = '/';
}
if (!isset($allUrls[$host][$path])) {
$allUrls[$host][$path] = array();
}
if (!in_array($idSite, $allUrls[$host][$path])) {
$allUrls[$host][$path][] = $idSite;
}
}
private function sortUrlsByHost(&$allUrls)
{
foreach ($allUrls as $host => $paths) {
uksort($paths, array($this, 'sortByPathDepth'));
$allUrls[$host] = $paths;
}
}
public function getIdSitesMatchingUrl($parsedUrl, $urlsGroupedByHost)
{
if (empty($parsedUrl['host'])) {
return null;
}
$urlHost = $this->toCanonicalHost($parsedUrl['host']);
$urlPath = $this->getCanonicalPathFromParsedUrl($parsedUrl);
$matchingSites = null;
if (isset($urlsGroupedByHost[$urlHost])) {
$paths = $urlsGroupedByHost[$urlHost];
foreach ($paths as $path => $idSites) {
if (0 === strpos($urlPath, $path)) {
$matchingSites = $idSites;
break;
}
}
if (!isset($matchingSites) && isset($paths['/'])) {
$matchingSites = $paths['/'];
}
}
return $matchingSites;
}
public function getPathMatchingUrl($parsedUrl, $urlsGroupedByHost)
{
if (empty($parsedUrl['host'])) {
return null;
}
$urlHost = $this->toCanonicalHost($parsedUrl['host']);
$urlPath = $this->getCanonicalPathFromParsedUrl($parsedUrl);
$matchingSites = null;
if (isset($urlsGroupedByHost[$urlHost])) {
$paths = $urlsGroupedByHost[$urlHost];
foreach ($paths as $path => $idSites) {
if (0 === strpos($urlPath, $path)) {
return $path;
}
}
}
}
public function getAllCachedSiteUrls()
{
$cache = $this->getCache();
$siteUrls = $cache->fetch(self::$cacheId);
if (empty($siteUrls)) {
$siteUrls = $this->getAllSiteUrls();
$cache->save(self::$cacheId, $siteUrls, 1800);
}
return $siteUrls;
}
public function getAllSiteUrls()
{
$model = new Model();
$siteUrls = $model->getAllKnownUrlsForAllSites();
if (empty($siteUrls)) {
return array();
}
$urls = array();
foreach ($siteUrls as $siteUrl) {
$siteId = (int) $siteUrl['idsite'];
if (!isset($urls[$siteId])) {
$urls[$siteId] = array();
}
$urls[$siteId][] = $siteUrl['url'];
}
return $urls;
}
private static function getCache()
{
return Cache::getLazyCache();
}
public function addRequestUrlToSiteUrls(&$allUrls, Request $request)
{
$idSite = $request->getIdSite();
$url = $request->getParam('url');
$this->addUrlByHost($allUrls, $idSite, $url, $addPath = false);
}
private function sortByPathDepth($pathA, $pathB)
{
// list first the paths with most '/' , and list path = '/' last
$numSlashA = substr_count($pathA, '/');
$numSlashB = substr_count($pathB, '/');
if ($numSlashA === $numSlashB) {
return -1 * strcmp($pathA, $pathB);
}
return $numSlashA > $numSlashB ? -1 : 1;
}
private function toCanonicalHost($host)
{
$host = Common::mb_strtolower($host);
if (strpos($host, 'www.') === 0) {
$host = substr($host, 4);
}
return $host;
}
private function getCanonicalPathFromParsedUrl($urlParsed)
{
$path = '/';
if (isset($urlParsed['path'])) {
$path = Common::mb_strtolower($urlParsed['path']);
if (!Common::stringEndsWith($path, '/')) {
$path .= '/';
}
}
return $path;
}
}
|