Joomla! has a function called isMobile.
It's created in the JBrowser class in libraries/joomla/environment/browser.php
With this function you can separate mobile devices from other devices.
The JBrowser class lacks the function to detect tablet.
Creating and improving the current php file is an option... but don't invent the wheel when it's already there... https://github.com/serbanghita/Mobile-Detect
This lightweight PHP class is used for detecting mobile devices (including tablets). It uses the User-Agent string combined with specific HTTP headers to detect the mobile environment.
https://github.com/serbanghita/Mobile-Detect/blob/master/Mobile_Detect.php
It's accurate and it's updated frequently.
(it's also used in a very popular User Agent Detector Joomla plugin -> https://github.com/renekreijveld/UserAgentDetector)
What if we use that file as a base for our JBrowser?
I support also this idea and I also want to propose to take it even further and introduce also varnish detection in case this is available in the server set up. You are welcome to take the code from here https://github.com/dgt41/varnishua This is also based on the excellent code of @renekreijveld for the php Mobile Detect but also has an option to use the varnish version. Varnish mobile detection is a one to one translation of the php Mobile Detect. Using varnish for the mobile detection the code runs very early, before even the Joomla is called!
Here is the code for the translation of php to varnish https://github.com/willemk/varnish-mobiletranslate
I know varnish is kinda exotic thing for many low end users but this needs only few lines of code (already sitting there) and will make happier people that support high traffic sites!
We've actually got two classes which are supposed to be doing this environment detection stuff. There's the older JBrowser and JApplicationWebClient, the latter being integrated directly into our web application classes. Personally, I would suggest deprecating JBrowser in favor of the newer class as it's more thoroughly tested, lives in the Framework, and is integrated into the application platform. Then comes the issue of B/C; so long as we don't break the public API of JApplicationWebClient (or JBrowser) I don't see any reason we couldn't integrate an external package into those classes to improve their capabilities.
Category | ⇒ | External Library Feature Request Libraries |
I have seen a few Joomla extensions including Mobile-Detect in their distribution (myself included),
because JApplicationWebClient is in J3.x, and they need to support J2.5 too
e.g. i see it tries to do:
$this->platform = self::IPAD;
and
$this->platform = self::ANDROIDTABLET;
etc
can JApplicationWebClient use Mobile_Detect and still identify all existing cases of '$this->platform' exactly as it does now ?
Hi you created this issue sometime ago but have not provided any code for people to evaluate. As no one else has shown any interest in providing the code and you have not then I am closing this issue at this time. If code is provided (a pull request) it can always be re-examined.
Status | New | ⇒ | Closed |
Closed_Date | 0000-00-00 00:00:00 | ⇒ | 2016-05-08 10:09:22 |
Closed_By | ⇒ | brianteeman |
Hi
This feature is very important because it improve the cache management of Joomla
I've propose the follwing code to modify the file /libraries/src/Cache/Cache.php
public static function getPlatformPrefix()
{
// No prefix when Global Config is set to no platfom specific prefix
if (!\JFactory::getConfig()->get('cache_platformprefix', '0'))
{
return '';
}
$detect = new Mobile_Detect;
// Any tablet device.
if( $detect->isTablet()
{
return 'T-';
}else if ($detect->isMobile() && !$detect->isTablet() )
{
return 'M-';
}
return '';
}
Is it possible Add this feature in the next release of Joomla?
It would be nice to have the joomla cache be able to distinguish between tablets and mobiles
that is either by using Mobile_Detect or by using Joomla own library
right now i have a warning to my users to enable platform specific cache
but to note that Tablets will be treated as mobiles and if they need display / different modules shown in tablets then to disable caching in module parameters
Joomla handler/storage like memcache(-d), redis have a problem with prefix, it should be suffix.
If there are testers, then I can write code in cache files based on WebClient.
Status | Discussion | ⇒ | New |
Closed_Date | 0000-00-00 00:00:00 | ⇒ |
Status | Closed | ⇒ | Discussion |
Closed_Date | 2016-05-08 10:09:22 | ⇒ | |
Closed_By | brianteeman | ⇒ |
Set to "open" on behalf of @franz-wohlkoenig by The JTracker Application at issues.joomla.org/joomla-cms/6859
Reopened so we don't have to comment on closed Issue.
Status | New | ⇒ | Discussion |
The patch could look like:
diff --git a/libraries/src/Cache/Cache.php b/libraries/src/Cache/Cache.php
index f9888bdb12..5bfd67c5a6 100644
--- a/libraries/src/Cache/Cache.php
+++ b/libraries/src/Cache/Cache.php
@@ -59,6 +59,24 @@ class Cache
'caching' => ($conf->get('caching') >= 1) ? true : false,
);
+ if ($conf->get('cache_platformprefix', '0'))
+ {
+ $webclient = new WebClient;
+
+ // Add a platform key to cache key if device calls for separate caching
+ if ($webclient->mobile)
+ {
+ if ($webclient->platform === WebClient::ANDROIDTABLET)
+ {
+ $this->_options['platformKey'] = 'T';
+ }
+ else
+ {
+ $this->_options['platformKey'] = 'M';
+ }
+ }
+ }
+
// Overwrite default options with given options
foreach ($options as $option => $value)
{
@@ -758,6 +776,7 @@ class Cache
* @return string
*
* @since 3.5
+ * @deprecated 4.0
*/
public static function getPlatformPrefix()
{
@@ -771,7 +790,7 @@ class Cache
if ($webclient->mobile)
{
- return 'M-';
+ return $webclient->platform === WebClient::ANDROIDTABLET ? 'T-' : 'M-';
}
return '';
diff --git a/libraries/src/Cache/CacheStorage.php b/libraries/src/Cache/CacheStorage.php
index d704f71f23..8787de71df 100644
--- a/libraries/src/Cache/CacheStorage.php
+++ b/libraries/src/Cache/CacheStorage.php
@@ -77,6 +77,22 @@ class CacheStorage
*/
public $_hash;
+ /**
+ * Platform key used as prefix or suffix
+ *
+ * @var string
+ * @since __DEPLOY_VERSION__
+ */
+ public $_platformKey;
+
+ /**
+ * Whether platform key is used as suffix
+ *
+ * @var string
+ * @since __DEPLOY_VERSION__
+ */
+ protected $_platformKeyAsSuffix = false;
+
/**
* Constructor
*
@@ -89,11 +105,12 @@ class CacheStorage
$config = \JFactory::getConfig();
$this->_hash = md5($config->get('secret'));
- $this->_application = (isset($options['application'])) ? $options['application'] : null;
- $this->_language = (isset($options['language'])) ? $options['language'] : 'en-GB';
- $this->_locking = (isset($options['locking'])) ? $options['locking'] : true;
- $this->_lifetime = (isset($options['lifetime'])) ? $options['lifetime'] * 60 : $config->get('cachetime') * 60;
- $this->_now = (isset($options['now'])) ? $options['now'] : time();
+ $this->_application = isset($options['application']) ? $options['application'] : null;
+ $this->_language = isset($options['language']) ? $options['language'] : 'en-GB';
+ $this->_locking = isset($options['locking']) ? $options['locking'] : true;
+ $this->_lifetime = isset($options['lifetime']) ? $options['lifetime'] * 60 : $config->get('cachetime') * 60;
+ $this->_now = isset($options['now']) ? $options['now'] : time();
+ $this->_platformKey = isset($options['platformKey']) ? $options['platformKey'] : '';
// Set time threshold value. If the lifetime is not set, default to 60 (0 is BAD)
// _threshold is now available ONLY as a legacy (it's deprecated). It's no longer used in the core.
@@ -374,7 +391,19 @@ class CacheStorage
$name = md5($this->_application . '-' . $id . '-' . $this->_language);
$this->rawname = $this->_hash . '-' . $name;
- return Cache::getPlatformPrefix() . $this->_hash . '-cache-' . $group . '-' . $name;
+ $cacheId = $this->_hash . '-cache-' . $group . '-' . $name;
+
+ if ($this->_platformKey !== '')
+ {
+ if ($this->_platformKeyAsSuffix)
+ {
+ return $cacheId . '-' . $this->_platformKey;
+ }
+
+ return $this->_platformKey . '-' . $cacheId;
+ }
+
+ return $cacheId;
}
/**
diff --git a/libraries/src/Cache/Storage/MemcacheStorage.php b/libraries/src/Cache/Storage/MemcacheStorage.php
index ebd3c59f10..eadd3abcee 100644
--- a/libraries/src/Cache/Storage/MemcacheStorage.php
+++ b/libraries/src/Cache/Storage/MemcacheStorage.php
@@ -38,6 +38,14 @@ class MemcacheStorage extends CacheStorage
*/
protected $_compress = 0;
+ /**
+ * Whether platform key is used as suffix
+ *
+ * @var string
+ * @since __DEPLOY_VERSION__
+ */
+ protected $_platformKeyAsSuffix = true;
+
/**
* Constructor
*
@@ -98,31 +106,6 @@ class MemcacheStorage extends CacheStorage
}
}
- /**
- * Get a cache_id string from an id/group pair
- *
- * @param string $id The cache data id
- * @param string $group The cache data group
- *
- * @return string The cache_id string
- *
- * @since 11.1
- */
- protected function _getCacheId($id, $group)
- {
- $prefix = Cache::getPlatformPrefix();
- $length = strlen($prefix);
- $cache_id = parent::_getCacheId($id, $group);
-
- if ($length)
- {
- // Memcache use suffix instead of prefix
- $cache_id = substr($cache_id, $length) . strrev($prefix);
- }
-
- return $cache_id;
- }
-
/**
* Check if the cache contains data stored by ID and group
*
diff --git a/libraries/src/Cache/Storage/MemcachedStorage.php b/libraries/src/Cache/Storage/MemcachedStorage.php
index 2f16b79132..fa4b7cfb27 100644
--- a/libraries/src/Cache/Storage/MemcachedStorage.php
+++ b/libraries/src/Cache/Storage/MemcachedStorage.php
@@ -38,6 +38,14 @@ class MemcachedStorage extends CacheStorage
*/
protected $_compress = 0;
+ /**
+ * Whether platform key is used as suffix
+ *
+ * @var string
+ * @since __DEPLOY_VERSION__
+ */
+ protected $_platformKeyAsSuffix = true;
+
/**
* Constructor
*
diff --git a/libraries/src/Cache/Storage/RedisStorage.php b/libraries/src/Cache/Storage/RedisStorage.php
index a06578ca3d..40c4beee59 100644
--- a/libraries/src/Cache/Storage/RedisStorage.php
+++ b/libraries/src/Cache/Storage/RedisStorage.php
@@ -36,6 +36,14 @@ class RedisStorage extends CacheStorage
*/
protected $_persistent = false;
+ /**
+ * Whether platform key is used as suffix
+ *
+ * @var string
+ * @since __DEPLOY_VERSION__
+ */
+ protected $_platformKeyAsSuffix = true;
+
/**
* Constructor
*
Your patch seems achieve the goal but not have all tablet platforms
I should try add a little piece of code
diff --git a/libraries/src/Cache/Cache.php b/libraries/src/Cache/Cache.php
index f9888bdb12..5bfd67c5a6 100644
--- a/libraries/src/Cache/Cache.php
+++ b/libraries/src/Cache/Cache.php
@@ -59,6 +59,24 @@ class Cache
'caching' => ($conf->get('caching') >= 1) ? true : false,
);
+ if ($conf->get('cache_platformprefix', '0'))
+ {
+ $webclient = new WebClient;
+
+ // Add a platform key to cache key if device calls for separate caching
+ if ($webclient->mobile)
+ {
+ if ($webclient->platform === WebClient::ANDROIDTABLET || $webclient->platform === WebClient::IPAD)
+ {
+ $this->_options['platformKey'] = 'T';
+ }
+ else
+ {
+ $this->_options['platformKey'] = 'M';
+ }
+ }
+ }
+
// Overwrite default options with given options
foreach ($options as $option => $value)
{
@@ -758,6 +776,7 @@ class Cache
* @return string
*
* @since 3.5
+ * @deprecated 4.0
*/
public static function getPlatformPrefix()
{
@@ -771,7 +790,7 @@ class Cache
if ($webclient->mobile)
{
- if ($webclient->platform === WebClient::ANDROIDTABLET || $webclient->platform === WebClient::IPAD)
{
return 'T-';
}
else
{
return 'M-';
}
}
return '';`
The best method is Mobile Detect library http://mobiledetect.net/
Is there a way to integrate in joomla core files?
My already implemented patch is based on the code available in Joomla itself. I'm a big fan of mobiledetect too and I would like to see that being integrated in Joomla too some day. But... since there are already two different mobile detection mechanism implemented in Joomla 3 I don't recommend implementing MobileDetect.net as well.
On the other hand... implementing it into Joomla 4 would be nice.
Status | Discussion | ⇒ | Closed |
Closed_Date | 0000-00-00 00:00:00 | ⇒ | 2017-11-20 06:24:28 |
Closed_By | ⇒ | franz-wohlkoenig |
Set to "closed" on behalf of @franz-wohlkoenig by The JTracker Application at issues.joomla.org/joomla-cms/6859
Is the writer of the User Agent Detector plugin I fully support this proposal! I've been using the Mobile Detect for quite some time now and it's stable and updated regularly. The sooner I can do without my plugins and use the core library the better.