Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Steur <thomas.steur@gmail.com>2015-12-02 01:10:03 +0300
committerThomas Steur <thomas.steur@gmail.com>2015-12-03 07:18:54 +0300
commit39f479f8c49c2ed71892ec149be51f2d92b663f4 (patch)
treedfe9368ebb82732ca64317725eee9031804d4af3 /plugins/SitesManager/SiteUrls.php
parentbf377e2c74f606682ddcfdb62d970dc28b625c0c (diff)
Added support to specify a path in a website URL and Piwik will recognize the path during tracking
Diffstat (limited to 'plugins/SitesManager/SiteUrls.php')
-rw-r--r--plugins/SitesManager/SiteUrls.php148
1 files changed, 141 insertions, 7 deletions
diff --git a/plugins/SitesManager/SiteUrls.php b/plugins/SitesManager/SiteUrls.php
index a1fac67a89..059521a4a9 100644
--- a/plugins/SitesManager/SiteUrls.php
+++ b/plugins/SitesManager/SiteUrls.php
@@ -9,6 +9,7 @@
namespace Piwik\Plugins\SitesManager;
use Piwik\Cache;
+use Piwik\Common;
class SiteUrls
{
@@ -19,6 +20,96 @@ class SiteUrls
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) {
+ $urlParsed = @parse_url($url);
+
+ if ($urlParsed === false || !isset($urlParsed['host'])) {
+ continue;
+ }
+
+ $host = $this->toCanonicalHost($urlParsed['host']);
+ $path = $this->getCanonicalPathFromParsedUrl($urlParsed);
+
+ if (!isset($allUrls[$host])) {
+ $allUrls[$host] = array();
+ }
+
+ if (!isset($allUrls[$host][$path])) {
+ $allUrls[$host][$path] = array();
+ }
+
+ if (!in_array($idSite, $allUrls[$host][$path])) {
+ $allUrls[$host][$path][] = $idSite;
+ }
+ }
+ }
+
+ foreach ($allUrls as $host => $paths) {
+ uksort($paths, array($this, 'sortByPathDepth'));
+ $allUrls[$host] = $paths;
+ }
+
+ return $allUrls;
+ }
+
+ 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 getAllCachedSiteUrls()
{
$cache = $this->getCache();
@@ -35,23 +126,66 @@ class SiteUrls
public function getAllSiteUrls()
{
$model = new Model();
- $siteIds = $model->getSitesId();
- $siteUrls = array();
+ $siteUrls = $model->getAllKnownUrlsForAllSites();
- if (empty($siteIds)) {
+ if (empty($siteUrls)) {
return array();
}
- foreach ($siteIds as $siteId) {
- $siteId = (int) $siteId;
- $siteUrls[$siteId] = $model->getSiteUrlsFromId($siteId);
+ $urls = array();
+ foreach ($siteUrls as $siteUrl) {
+ $siteId = (int) $siteUrl['idsite'];
+
+ if (!isset($urls[$siteId])) {
+ $urls[$siteId] = array();
+ }
+
+ $urls[$siteId][] = $siteUrl['url'];
}
- return $siteUrls;
+ return $urls;
}
private static function getCache()
{
return Cache::getLazyCache();
}
+
+ 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;
+ }
+
}