diff options
author | Kate Butler <kate@innocraft.com> | 2019-08-26 01:03:51 +0300 |
---|---|---|
committer | Thomas Steur <tsteur@users.noreply.github.com> | 2019-08-26 01:03:51 +0300 |
commit | 444e8415ed5e0771213be972bd96249f091fdbf1 (patch) | |
tree | 3214e4ccf3e822d45d104f7a3ab48064f3f602ef | |
parent | 332598bdcea6d4e9e1692547f7f15c64edb5e810 (diff) |
Cache parsed device detection info to file (#14751)
* Create a file cache for parsed device info
* Tests for device detection file cache
* Move device detector cache into separate plugin
* Move WarmDeviceDetectorCache to separate plugin
* Use DI to get the DeviceDetectorFactory
* add device detector cache as a submodule
* update submodule
* update submodule
* fix BC break
* System test, deal with useragent strings > 500 characters long
* update submodule
* Update submodule
-rw-r--r-- | .gitmodules | 6 | ||||
-rw-r--r-- | core/DeviceDetector/DeviceDetectorCache.php (renamed from core/DeviceDetectorCache.php) | 4 | ||||
-rw-r--r-- | core/DeviceDetector/DeviceDetectorFactory.php | 64 | ||||
-rw-r--r-- | core/DeviceDetectorFactory.php | 25 | ||||
-rw-r--r-- | core/Tracker/Settings.php | 5 | ||||
-rw-r--r-- | core/Tracker/VisitExcluded.php | 5 | ||||
-rw-r--r-- | plugins/API/Menu.php | 2 | ||||
m--------- | plugins/DeviceDetectorCache | 0 | ||||
-rw-r--r-- | plugins/DevicesDetection/Columns/Base.php | 5 | ||||
-rw-r--r-- | tests/PHPUnit/Framework/TestingEnvironmentVariables.php | 1 |
10 files changed, 88 insertions, 29 deletions
diff --git a/.gitmodules b/.gitmodules index 4d30b7ac0b..6c9c8b56a8 100644 --- a/.gitmodules +++ b/.gitmodules @@ -58,6 +58,10 @@ path = plugins/TagManager url = https://github.com/matomo-org/tag-manager.git branch = master +[submodule "plugins/DeviceDetectorCache"] + path = plugins/DeviceDetectorCache + url = https://github.com/matomo-org/plugin-DeviceDetectorCache.git + # Add new Plugin submodule above this line ^^ # @@ -72,4 +76,4 @@ path = plugins/Morpheus/icons url = https://github.com/matomo-org/matomo-icons.git -# Note: do not add new plugin submodules here, but a few lines above +# Note: do not add new plugin submodules here, but a few lines above
\ No newline at end of file diff --git a/core/DeviceDetectorCache.php b/core/DeviceDetector/DeviceDetectorCache.php index fe4815136b..ad2d8573ec 100644 --- a/core/DeviceDetectorCache.php +++ b/core/DeviceDetector/DeviceDetectorCache.php @@ -6,7 +6,7 @@ * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later * */ -namespace Piwik; +namespace Piwik\DeviceDetector; use Piwik\Cache as PiwikCache; @@ -27,7 +27,7 @@ class DeviceDetectorCache implements \DeviceDetector\Cache\Cache public function __construct($ttl = 300) { $this->ttl = (int) $ttl; - $this->cache = PiwikCache::getEagerCache(); + $this->cache = PiwikCache::getLazyCache(); } /** diff --git a/core/DeviceDetector/DeviceDetectorFactory.php b/core/DeviceDetector/DeviceDetectorFactory.php new file mode 100644 index 0000000000..ce8b98bbd7 --- /dev/null +++ b/core/DeviceDetector/DeviceDetectorFactory.php @@ -0,0 +1,64 @@ +<?php +/** + * Piwik - free/libre analytics platform + * + * @link https://matomo.org + * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later + * + */ +namespace Piwik\DeviceDetector; + +use DeviceDetector\DeviceDetector; +use Piwik\Common; +use Piwik\Container\StaticContainer; + +class DeviceDetectorFactory +{ + protected static $deviceDetectorInstances = array(); + + /** + * Returns an instance of DeviceDetector for the given user agent. Uses template method pattern + * and calls getDeviceDetectionInfo() when it doesn't find a matching instance in the cache. + * @param string $userAgent + * @return DeviceDetector|mixed + */ + public function makeInstance($userAgent) + { + $userAgent = self::getNormalizedUserAgent($userAgent); + + if (array_key_exists($userAgent, self::$deviceDetectorInstances)) { + return self::$deviceDetectorInstances[$userAgent]; + } + + $deviceDetector = $this->getDeviceDetectionInfo($userAgent); + + self::$deviceDetectorInstances[$userAgent] = $deviceDetector; + + return $deviceDetector; + } + + public static function getNormalizedUserAgent($userAgent) + { + return Common::mb_substr(trim($userAgent), 0, 500); + } + + /** + * Creates a new DeviceDetector for the user agent. Called by makeInstance() when no matching instance + * was found in the cache. + * @param $userAgent + * @return DeviceDetector + */ + protected function getDeviceDetectionInfo($userAgent) + { + $deviceDetector = new DeviceDetector($userAgent); + $deviceDetector->discardBotInformation(); + $deviceDetector->setCache(new DeviceDetectorCache(86400)); + $deviceDetector->parse(); + return $deviceDetector; + } + + public static function clearInstancesCache() + { + self::$deviceDetectorInstances = array(); + } +}
\ No newline at end of file diff --git a/core/DeviceDetectorFactory.php b/core/DeviceDetectorFactory.php index 2217dbe7c0..1d392cf955 100644 --- a/core/DeviceDetectorFactory.php +++ b/core/DeviceDetectorFactory.php @@ -9,32 +9,19 @@ namespace Piwik; use DeviceDetector\DeviceDetector; -use Piwik\Common; +use Piwik\Container\StaticContainer; class DeviceDetectorFactory { - protected static $deviceDetectorInstances = array(); - /** - * Returns a Singleton instance of DeviceDetector for the given user agent + * Returns a Singleton instance of DeviceDetector for the given user agent. * @param string $userAgent * @return DeviceDetector + * @deprecated Should get a factory via StaticContainer and call makeInstance() on it instead */ public static function getInstance($userAgent) { - $userAgent = Common::mb_substr($userAgent, 0, 500); - - if (array_key_exists($userAgent, self::$deviceDetectorInstances)) { - return self::$deviceDetectorInstances[$userAgent]; - } - - $deviceDetector = new DeviceDetector($userAgent); - $deviceDetector->discardBotInformation(); - $deviceDetector->setCache(new DeviceDetectorCache(86400)); - $deviceDetector->parse(); - - self::$deviceDetectorInstances[$userAgent] = $deviceDetector; - - return $deviceDetector; + $factory = StaticContainer::get(\Piwik\DeviceDetector\DeviceDetectorFactory::class); + return $factory->makeInstance($userAgent); } -} +}
\ No newline at end of file diff --git a/core/Tracker/Settings.php b/core/Tracker/Settings.php index 06657d097a..6db7b998d3 100644 --- a/core/Tracker/Settings.php +++ b/core/Tracker/Settings.php @@ -9,8 +9,9 @@ namespace Piwik\Tracker; use Piwik\Config; +use Piwik\Container\StaticContainer; use Piwik\Tracker; -use Piwik\DeviceDetectorFactory; +use Piwik\DeviceDetector\DeviceDetectorFactory; use Piwik\SettingsPiwik; class Settings // TODO: merge w/ visitor recognizer or make it it's own service. the class name is required for BC. @@ -37,7 +38,7 @@ class Settings // TODO: merge w/ visitor recognizer or make it it's own service. $userAgent = $request->getUserAgent(); - $deviceDetector = DeviceDetectorFactory::getInstance($userAgent); + $deviceDetector = StaticContainer::get(DeviceDetectorFactory::class)->makeInstance($userAgent); $aBrowserInfo = $deviceDetector->getClient(); if ($aBrowserInfo['type'] != 'browser') { diff --git a/core/Tracker/VisitExcluded.php b/core/Tracker/VisitExcluded.php index bd0e390ea8..838f0cf2a7 100644 --- a/core/Tracker/VisitExcluded.php +++ b/core/Tracker/VisitExcluded.php @@ -10,7 +10,8 @@ namespace Piwik\Tracker; use Piwik\Cache as PiwikCache; use Piwik\Common; -use Piwik\DeviceDetectorFactory; +use Piwik\Container\StaticContainer; +use Piwik\DeviceDetector\DeviceDetectorFactory; use Piwik\Exception\UnexpectedWebsiteFoundException; use Piwik\Network\IP; use Piwik\Piwik; @@ -175,7 +176,7 @@ class VisitExcluded { $allowBots = $this->request->getParam('bots'); - $deviceDetector = DeviceDetectorFactory::getInstance($this->userAgent); + $deviceDetector = StaticContainer::get(DeviceDetectorFactory::class)->makeInstance($this->userAgent ); return !$allowBots && ($deviceDetector->isBot() || $this->isIpInRange()); diff --git a/plugins/API/Menu.php b/plugins/API/Menu.php index 67e2029926..71e5f5dd9f 100644 --- a/plugins/API/Menu.php +++ b/plugins/API/Menu.php @@ -8,7 +8,7 @@ */ namespace Piwik\Plugins\API; -use Piwik\DeviceDetectorCache; +use Piwik\DeviceDetector\DeviceDetectorCache; use Piwik\Menu\MenuAdmin; use Piwik\Menu\MenuTop; use Piwik\Piwik; diff --git a/plugins/DeviceDetectorCache b/plugins/DeviceDetectorCache new file mode 160000 +Subproject c4e27702032e0f02a80deae591e99f724c31500 diff --git a/plugins/DevicesDetection/Columns/Base.php b/plugins/DevicesDetection/Columns/Base.php index c837c2e0ba..efc3a6a0d7 100644 --- a/plugins/DevicesDetection/Columns/Base.php +++ b/plugins/DevicesDetection/Columns/Base.php @@ -8,13 +8,14 @@ */ namespace Piwik\Plugins\DevicesDetection\Columns; -use Piwik\DeviceDetectorFactory; +use Piwik\Container\StaticContainer; +use Piwik\DeviceDetector\DeviceDetectorFactory; use Piwik\Plugin\Dimension\VisitDimension; abstract class Base extends VisitDimension { protected function getUAParser($userAgent) { - return DeviceDetectorFactory::getInstance($userAgent); + return StaticContainer::get(DeviceDetectorFactory::class)->makeInstance($userAgent); } } diff --git a/tests/PHPUnit/Framework/TestingEnvironmentVariables.php b/tests/PHPUnit/Framework/TestingEnvironmentVariables.php index a4a875bcfb..6c611dd624 100644 --- a/tests/PHPUnit/Framework/TestingEnvironmentVariables.php +++ b/tests/PHPUnit/Framework/TestingEnvironmentVariables.php @@ -108,6 +108,7 @@ class TestingEnvironmentVariables $disabledPlugins[] = 'LoginLdap'; $disabledPlugins[] = 'MarketingCampaignsReporting'; $disabledPlugins[] = 'ExampleVisualization'; + $disabledPlugins[] = 'DeviceDetectorCache'; $disabledPlugins = array_diff($disabledPlugins, array( 'DBStats', 'ExampleUI', 'ExampleCommand', 'ExampleSettingsPlugin' |