Skip to content

Commit

Permalink
starting to implement new DeviceDetector structure
Browse files Browse the repository at this point in the history
  • Loading branch information
sgiehl committed May 24, 2014
1 parent cbde699 commit cef2d69
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 71 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"symfony/console": ">=v2.3.5",
"tedivm/jshrink": "v0.5.1",
"mustangostang/spyc": "0.5.*",
"piwik/device-detector": "1.0"
"piwik/device-detector": "dev-master"
},
"require-dev": {
"phpunit/phpunit": "4.*"
Expand Down
8 changes: 6 additions & 2 deletions core/Tracker/Visit.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace Piwik\Tracker;

use DeviceDetector;
use DeviceDetector\DeviceDetector;
use Piwik\Common;
use Piwik\Config;
use Piwik\IP;
Expand Down Expand Up @@ -602,7 +602,11 @@ protected function getUserSettingsInformation()

$deviceDetector = new DeviceDetector($userAgent);
$deviceDetector->parse();
$aBrowserInfo = $deviceDetector->getBrowser();
$aBrowserInfo = $deviceDetector->getClient();
if ($aBrowserInfo['type'] != 'browser') {
// for now only track browsers
unset($aBrowserInfo);
}

$browserName = !empty($aBrowserInfo['short_name']) ? $aBrowserInfo['short_name'] : 'UNK';
$browserVersion = !empty($aBrowserInfo['version']) ? $aBrowserInfo['version'] : '';
Expand Down
10 changes: 5 additions & 5 deletions libs/UserAgentParser/UserAgentParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -655,9 +655,9 @@ static public function getBrowserNameFromId($browserId)
if (isset(self::$browserIdToName[$browserId])) {
return self::$browserIdToName[$browserId];
}
if(class_exists('DeviceDetector')) {
if( !empty(DeviceDetector::$browsers[$browserId])) {
return DeviceDetector::$browsers[$browserId];
if(class_exists('DeviceDetector\\Parser\\Client\\Browser')) {
if( array_key_exists($browserId, DeviceDetector\Parser\Client\Browser::getAvailableBrowsers())) {
return DeviceDetector\Parser\Client\Browser::getAvailableBrowsers()[$browserId];
}
}
return false;
Expand Down Expand Up @@ -692,8 +692,8 @@ static public function getOperatingSystemNameFromId($osId)
return self::$operatingSystemsIdToName[$osId];
}

if(class_exists('DeviceDetector')) {
return DeviceDetector::getOsNameFromId($osId);
if(class_exists('DeviceDetector\\Parser\\OperatingSystem')) {
return DeviceDetector\Parser\OperatingSystem::getNameFromId($osId);
}
return false;
}
Expand Down
2 changes: 0 additions & 2 deletions piwik.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,6 @@
$vendorDirectory = PIWIK_INCLUDE_PATH . '/../..';
}
require_once $vendorDirectory . '/autoload.php';
require_once $vendorDirectory . '/mustangostang/spyc/Spyc.php';
require_once $vendorDirectory . '/piwik/device-detector/DeviceDetector.php';

session_cache_limiter('nocache');
@date_default_timezone_set('UTC');
Expand Down
4 changes: 2 additions & 2 deletions plugins/API/API.php
Original file line number Diff line number Diff line change
Expand Up @@ -714,11 +714,11 @@ protected function addTopMenuMobileApp()
if (empty($_SERVER['HTTP_USER_AGENT'])) {
return;
}
if (!class_exists("DeviceDetector")) {
if (!class_exists("DeviceDetector\\DeviceDetector")) {
throw new \Exception("DeviceDetector could not be found, maybe you are using Piwik from git and need to have update Composer. <br>php composer.phar update");
}

$ua = new \DeviceDetector($_SERVER['HTTP_USER_AGENT']);
$ua = new \DeviceDetector\DeviceDetector($_SERVER['HTTP_USER_AGENT']);
$ua->parse();
$os = $ua->getOs('short_name');
if ($os && in_array($os, array('AND', 'IOS'))) {
Expand Down
55 changes: 28 additions & 27 deletions plugins/DevicesDetection/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/
namespace Piwik\Plugins\DevicesDetection;

use DeviceDetector;
use DeviceDetector\DeviceDetector;
use Piwik\Common;
use Piwik\Db;
use Piwik\Piwik;
Expand Down Expand Up @@ -74,26 +74,27 @@ public function deviceDetection()

$userAgent = Common::getRequestVar('ua', $_SERVER['HTTP_USER_AGENT'], 'string');

$parsedUA = DeviceDetector::getInfoFromUserAgent($userAgent);
$uaParser = new DeviceDetector($userAgent);
$uaParser->parse();

$view->userAgent = $userAgent;
$view->browser_name = $parsedUA['browser']['name'];
$view->browser_short_name = $parsedUA['browser']['short_name'];
$view->browser_version = $parsedUA['browser']['version'];
$view->browser_logo = getBrowserLogoExtended($parsedUA['browser']['short_name']);
$view->browser_family = $parsedUA['browser_family'];
$view->browser_family_logo = getBrowserFamilyLogoExtended($parsedUA['browser_family']);
$view->os_name = $parsedUA['os']['name'];
$view->os_logo = getOsLogoExtended($parsedUA['os']['short_name']);
$view->os_short_name = $parsedUA['os']['short_name'];
$view->os_family = $parsedUA['os_family'];
$view->os_family_logo = getOsFamilyLogoExtended($parsedUA['os_family']);
$view->os_version = $parsedUA['os']['version'];
$view->device_type = getDeviceTypeLabel($parsedUA['device']['type']);
$view->device_type_logo = getDeviceTypeLogo($parsedUA['device']['type']);
$view->device_model = $parsedUA['device']['model'];
$view->device_brand = getDeviceBrandLabel($parsedUA['device']['brand']);
$view->device_brand_logo = getBrandLogo($view->device_brand);
$view->browser_name = $uaParser->getClient('name');
$view->browser_short_name = $uaParser->getClient('short_name');
$view->browser_version = $uaParser->getClient('version');
$view->browser_logo = getBrowserLogoExtended($uaParser->getClient('short_name'));
$view->browser_family = \DeviceDetector\Parser\Client\Browser::getBrowserFamily($uaParser->getClient('short_name'));
$view->browser_family_logo = getBrowserFamilyLogoExtended($view->browser_family);
$view->os_name = $uaParser->getOs('name');
$view->os_logo = getOsLogoExtended($uaParser->getOs('short_name'));
$view->os_short_name = $uaParser->getOs('short_name');
$view->os_family = \DeviceDetector\Parser\OperatingSystem::getOsFamily($uaParser->getOs('short_name'));
$view->os_family_logo = getOsFamilyLogoExtended($view->os_family);
$view->os_version = $uaParser->getOs('version');
$view->device_type = getDeviceTypeLabel($uaParser->getDeviceName());
$view->device_type_logo = getDeviceTypeLogo($uaParser->getDeviceName());
$view->device_model = $uaParser->getModel();
$view->device_brand = getDeviceBrandLabel($uaParser->getBrand());
$view->device_brand_logo = getBrandLogo($uaParser->getBrand());

return $view->render();
}
Expand All @@ -110,7 +111,7 @@ public function showList()

switch ($type) {
case 'brands':
$availableBrands = DeviceDetector::$deviceBrands;
$availableBrands = \DeviceDetector\Parser\Device\DeviceParserAbstract::$deviceBrands;

foreach ($availableBrands AS $short => $name) {
if ($name != 'Unknown') {
Expand All @@ -120,33 +121,33 @@ public function showList()
break;

case 'browsers':
$availableBrowsers = DeviceDetector::$browsers;
$availableBrowsers = \DeviceDetector\Parser\Client\Browser::getAvailableBrowsers();

foreach ($availableBrowsers AS $short => $name) {
$list[$name] = getBrowserLogoExtended($short);
}
break;

case 'browserfamilies':
$availableBrowserFamilies = DeviceDetector::$browserFamilies;
$availableBrowserFamilies = \DeviceDetector\Parser\Client\Browser::getAvailableBrowserFamilies();

foreach ($availableBrowserFamilies AS $name => $browsers) {
$list[$name] = getBrowserFamilyLogoExtended($name);
}
break;

case 'os':
$availableOSs = DeviceDetector::$osShorts;
$availableOSs = \DeviceDetector\Parser\OperatingSystem::getAvailableOperatingSystems();

foreach ($availableOSs AS $name => $short) {
foreach ($availableOSs AS $short => $name) {
if ($name != 'Bot') {
$list[$name] = getOsLogoExtended($short);
}
}
break;

case 'osfamilies':
$osFamilies = DeviceDetector::$osFamilies;
$osFamilies = \DeviceDetector\Parser\OperatingSystem::getAvailableOperatingSystemFamilies();

foreach ($osFamilies AS $name => $oss) {
if ($name != 'Bot') {
Expand All @@ -156,9 +157,9 @@ public function showList()
break;

case 'devicetypes':
$deviceTypes = DeviceDetector::$deviceTypes;
$deviceTypes = \DeviceDetector\Parser\Device\DeviceParserAbstract::getAvailableDeviceTypes();

foreach ($deviceTypes AS $name) {
foreach ($deviceTypes AS $name => $id) {
$list[$name] = getDeviceTypeLogo($name);
}
break;
Expand Down
13 changes: 7 additions & 6 deletions plugins/DevicesDetection/DevicesDetection.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@

namespace Piwik\Plugins\DevicesDetection;

use DeviceDetector;
use DeviceDetector\Parser\Device\DeviceParserAbstract AS DeviceParser;
use DeviceDetector\DeviceDetector;
use Exception;
use Piwik\ArchiveProcessor;
use Piwik\CacheFile;
Expand Down Expand Up @@ -58,10 +59,10 @@ public function __construct()

protected function getRawMetadataDeviceType()
{
$deviceTypeList = implode(", ", DeviceDetector::$deviceTypes);
$deviceTypeList = implode(", ", DeviceParser::getAvailableDeviceTypeNames());

$deviceTypeLabelToCode = function ($type) use ($deviceTypeList) {
$index = array_search(strtolower(trim(urldecode($type))), DeviceDetector::$deviceTypes);
$index = array_search(strtolower(trim(urldecode($type))), DeviceParser::getAvailableDeviceTypeNames());
if ($index === false) {
throw new Exception("deviceType segment must be one of: $deviceTypeList");
}
Expand Down Expand Up @@ -257,10 +258,10 @@ public function parseMobileVisitData(&$visitorInfo, \Piwik\Tracker\Request $requ
$userAgent = $request->getUserAgent();

$UAParser = new DeviceDetector($userAgent);
$UAParser->setCache(new CacheFile('tracker', 86400));
#$UAParser->setCache(new CacheFile('tracker', 86400));
$UAParser->parse();
$deviceInfo['config_browser_name'] = $UAParser->getBrowser("short_name");
$deviceInfo['config_browser_version'] = $UAParser->getBrowser("version");
$deviceInfo['config_browser_name'] = $UAParser->getClient("type") == 'browser' ? $UAParser->getClient("short_name") : 'UNK';
$deviceInfo['config_browser_version'] = $UAParser->getClient("type") == 'browser' ? $UAParser->getClient("version") : 'UNK';
$deviceInfo['config_os'] = $UAParser->getOs("short_name");
$deviceInfo['config_os_version'] = $UAParser->getOs("version");
$deviceInfo['config_device_type'] = $UAParser->getDevice();
Expand Down
54 changes: 28 additions & 26 deletions plugins/DevicesDetection/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@

namespace Piwik\Plugins\DevicesDetection;

use DeviceDetector;
use Piwik\Piwik;
use DeviceDetector\Parser\OperatingSystem AS OperatingSystemParser;
use DeviceDetector\Parser\Device\DeviceParserAbstract AS DeviceParser;
use DeviceDetector\Parser\Client\Browser AS BrowserParser;

function getBrandLogo($label)
{
Expand All @@ -25,7 +27,7 @@ function getBrandLogo($label)

function getBrowserFamilyFullNameExtended($label)
{
foreach (DeviceDetector::$browserFamilies as $name => $family) {
foreach (BrowserParser::getAvailableBrowserFamilies() as $name => $family) {
if (in_array($label, $family)) {
return $name;
}
Expand All @@ -35,8 +37,8 @@ function getBrowserFamilyFullNameExtended($label)

function getBrowserFamilyLogoExtended($label)
{
if (array_key_exists($label, DeviceDetector::$browserFamilies)) {
return getBrowserLogoExtended(DeviceDetector::$browserFamilies[$label][0]);
if (!empty($label) && array_key_exists($label, BrowserParser::getAvailableBrowserFamilies())) {
return getBrowserLogoExtended(BrowserParser::getAvailableBrowserFamilies()[$label][0]);
}
return getBrowserLogoExtended($label);
}
Expand All @@ -45,8 +47,8 @@ function getBrowserNameExtended($label)
{
$short = substr($label, 0, 2);
$ver = substr($label, 3, 10);
if (array_key_exists($short, DeviceDetector::$browsers)) {
return trim(ucfirst(DeviceDetector::$browsers[$short]) . ' ' . $ver);
if (array_key_exists($short, BrowserParser::getAvailableBrowsers())) {
return trim(ucfirst(BrowserParser::getAvailableBrowsers()[$short]) . ' ' . $ver);
} else {
return Piwik::translate('General_Unknown');
}
Expand All @@ -70,8 +72,8 @@ function getBrowserLogoExtended($short)
// If name is given instead of short code, try to find matching shortcode
if (strlen($short) > 2) {

if (in_array($short, DeviceDetector::$browsers)) {
$flippedBrowsers = array_flip(DeviceDetector::$browsers);
if (in_array($short, BrowserParser::getAvailableBrowsers())) {
$flippedBrowsers = array_flip(BrowserParser::getAvailableBrowsers());
$short = $flippedBrowsers[$short];
} else {
$short = substr($short, 0, 2);
Expand All @@ -80,18 +82,18 @@ function getBrowserLogoExtended($short)

$family = getBrowserFamilyFullNameExtended($short);

if (array_key_exists($short, DeviceDetector::$browsers) && file_exists(PIWIK_INCLUDE_PATH.'/'.sprintf($path, $short))) {
if (!empty($short) && array_key_exists($short, BrowserParser::getAvailableBrowsers()) && file_exists(PIWIK_INCLUDE_PATH.'/'.sprintf($path, $short))) {
return sprintf($path, $short);
} elseif (array_key_exists($family, DeviceDetector::$browserFamilies) && file_exists(PIWIK_INCLUDE_PATH.'/'.sprintf($path, DeviceDetector::$browserFamilies[$family][0]))) {
return sprintf($path, DeviceDetector::$browserFamilies[$family][0]);
} elseif (!empty($short) && array_key_exists($family, BrowserParser::getAvailableBrowserFamilies()) && file_exists(PIWIK_INCLUDE_PATH.'/'.sprintf($path, BrowserParser::getAvailableBrowserFamilies()[$family][0]))) {
return sprintf($path, BrowserParser::getAvailableBrowserFamilies()[$family][0]);
}
return sprintf($path, 'UNK');
}

function getDeviceBrandLabel($label)
{
if (array_key_exists($label, DeviceDetector::$deviceBrands)) {
return ucfirst(DeviceDetector::$deviceBrands[$label]);
if (array_key_exists($label, DeviceParser::$deviceBrands)) {
return ucfirst(DeviceParser::$deviceBrands[$label]);
} else {
return Piwik::translate('General_Unknown');
}
Expand All @@ -110,8 +112,8 @@ function getDeviceTypeLabel($label)
'smart display' => 'DevicesDetection_SmartDisplay',
'camera' => 'DevicesDetection_Camera'
);
if (isset(DeviceDetector::$deviceTypes[$label]) && isset($translations[DeviceDetector::$deviceTypes[$label]])) {
return Piwik::translate($translations[DeviceDetector::$deviceTypes[$label]]);
if (in_array($label, DeviceParser::getAvailableDeviceTypes()) && isset($translations[array_search($label, DeviceParser::getAvailableDeviceTypes())])) {
return Piwik::translate($translations[array_search($label, DeviceParser::getAvailableDeviceTypes())]);
} else if (isset($translations[$label])) {
return Piwik::translate($translations[$label]);
} else {
Expand All @@ -121,8 +123,8 @@ function getDeviceTypeLabel($label)

function getDeviceTypeLogo($label)
{
if (is_numeric($label) && isset(DeviceDetector::$deviceTypes[$label])) {
$label = DeviceDetector::$deviceTypes[$label];
if (is_numeric($label) && in_array($label, DeviceParser::getAvailableDeviceTypes())) {
$label = array_search($label, DeviceParser::getAvailableDeviceTypes());
}

$label = strtolower($label);
Expand Down Expand Up @@ -156,7 +158,7 @@ function getModelName($label)

function getOSFamilyFullNameExtended($label)
{
$label = DeviceDetector::getOsFamily($label);
$label = OperatingSystemParser::getOsFamily($label);
if($label !== false) {
return $label;
}
Expand All @@ -165,8 +167,8 @@ function getOSFamilyFullNameExtended($label)

function getOsFamilyLogoExtended($label)
{
if (array_key_exists($label, DeviceDetector::$osFamilies)) {
return getOsLogoExtended(DeviceDetector::$osFamilies[$label][0]);
if (!empty($label) && array_key_exists($label, OperatingSystemParser::getAvailableOperatingSystemFamilies())) {
return getOsLogoExtended(OperatingSystemParser::getAvailableOperatingSystemFamilies()[$label][0]);
}
return getOsLogoExtended($label);
}
Expand All @@ -176,7 +178,7 @@ function getOsFullNameExtended($label)
if (!empty($label) && $label != ";") {
$os = substr($label, 0, 3);
$ver = substr($label, 4, 15);
$name = DeviceDetector::getOsNameFromId($os, $ver);
$name = OperatingSystemParser::getNameFromId($os, $ver);
if (!empty($name)) {
return $name;
}
Expand All @@ -202,19 +204,19 @@ function getOsLogoExtended($short)
// If name is given instead of short code, try to find matching shortcode
if (strlen($short) > 3) {

if (array_key_exists($short, DeviceDetector::$osShorts)) {
$short = DeviceDetector::$osShorts[$short];
if (in_array($short, OperatingSystemParser::getAvailableOperatingSystems())) {
$short = array_search($short, OperatingSystemParser::getAvailableOperatingSystems());
} else {
$short = substr($short, 0, 3);
}
}

$family = getOsFamilyFullNameExtended($short);

if (in_array($short, DeviceDetector::$osShorts) && file_exists(PIWIK_INCLUDE_PATH.'/'.sprintf($path, $short))) {
if (!empty($short) && array_key_exists($short, OperatingSystemParser::getAvailableOperatingSystems()) && file_exists(PIWIK_INCLUDE_PATH.'/'.sprintf($path, $short))) {
return sprintf($path, $short);
} elseif (array_key_exists($family, DeviceDetector::$osFamilies) && file_exists(PIWIK_INCLUDE_PATH.'/'.sprintf($path, DeviceDetector::$osFamilies[$family][0]))) {
return sprintf($path, DeviceDetector::$osFamilies[$family][0]);
} elseif (!empty($family) && array_key_exists($family, OperatingSystemParser::getAvailableOperatingSystemFamilies()) && file_exists(PIWIK_INCLUDE_PATH.'/'.sprintf($path, OperatingSystemParser::getAvailableOperatingSystemFamilies()[$family][0]))) {
return sprintf($path, OperatingSystemParser::getAvailableOperatingSystemFamilies()[$family][0]);
}
return sprintf($path, 'UNK');
}

0 comments on commit cef2d69

Please sign in to comment.