Skip to content

Commit

Permalink
Merge branch '0.6'
Browse files Browse the repository at this point in the history
  • Loading branch information
dhensby committed Feb 1, 2017
2 parents 2782281 + fadf65c commit d21fea3
Show file tree
Hide file tree
Showing 31 changed files with 106 additions and 629 deletions.
25 changes: 14 additions & 11 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
# See https://github.com/silverstripe-labs/silverstripe-travis-support for setup details

language: php
php:
- 5.3
language: php

sudo: false

env:
- DB=MYSQL CORE_RELEASE=3.1
- DB=MYSQL CORE_RELEASE=master
- DB=PGSQL CORE_RELEASE=3.1

matrix:
include:
- php: 5.3
env: DB=MYSQL CORE_RELEASE=3.1
- php: 5.4
env: DB=MYSQL CORE_RELEASE=master
env: DB=PGSQL CORE_RELEASE=3.2
- php: 5.5
env: DB=SQLITE CORE_RELEASE=3.3
- php: 5.6
env: DB=MYSQL CORE_RELEASE=3.4
- php: 5.6
env: DB=MYSQL CORE_RELEASE=3.5
- php: 5.6
env: DB=MYSQL CORE_RELEASE=3

before_script:
- git clone git://github.com/silverstripe-labs/silverstripe-travis-support.git ~/travis-support
- php ~/travis-support/travis_setup.php --source `pwd` --target ~/builds/ss --require silverstripe/cms
- cd ~/builds/ss

script:
- phpunit mobile/tests/
script:
- vendor/bin/phpunit mobile/tests/
25 changes: 18 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ The mobile module provides detection for mobile devices,
and can serve a different SilverStripe theme to them.
The module can either use redirection to a separate mobile
domain, or serve mobile optimised content under the same URLs.
It includes a default "blackcandymobile" theme to demonstrate the effects.

The codebase relies on server-side device detection based on user agent strings,
which is an [unreliable](http://www.brettjankord.com/2013/01/10/active-development-on-categorizr-has-come-to-an-end/) way to determine if a device is considered to be "mobile".
Expand Down Expand Up @@ -47,13 +46,22 @@ A new theme called "blackcandymobile" will be created in your themes folder afte
invoking the database (dev) build. This theme is a good starting point which you can modify
to create your own mobile theme.

If the themes folder can't be written to by the web server during dev/build, please
manually copy "blackcandymobile" into your themes folder from the mobile folder.
## Themes

Alternatively, the module contains a "jquerymobile" sample theme
which creates a basic navigation interface through [jQuery Mobile](http://jquerymobile.com).
To use this theme, copy it to `/themes` in the same way,
and set it in your `SiteConfig` as described below.
We provide two themes as a starting point which you can modify to create your own mobile theme:

* ["blackcandymobile"](https://github.com/silverstripe-themes/silverstripe-blackcandymobile):
Modelled to work with the "blackcandy" theme which comes with the SilverStripe 2.x default installation.
* ["jquerymobile"](https://github.com/silverstripe-themes/silverstripe-jquerymobile):
Creates a basic navigation interface through [jQuery Mobile](http://jquerymobile.com)

Either download the themes from github, or add them via [composer](http://getcomposer.org):

{
"require": {"silverstripe-themes/blackcandymobile": "*"}
}

Alternatively, you can start your own mobile theme of course.

## Configuration

Expand All @@ -74,6 +82,9 @@ the one that mobile users of your site will see.

Please keep in mind that the mobile domain must point to your site before it will work.

In order to force extended tablet device detection, set `MobileBrowserDetection::$tablet_is_mobile`
either to `TRUE` (forces mobile template) or `FALSE` (forces desktop template).

### Search Engine Optimization ###

The module follows [Google's recommendations](http://googlewebmastercentral.blogspot.com/2011/02/making-websites-mobile-friendly.html)
Expand Down
45 changes: 28 additions & 17 deletions code/MobileBrowserDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,18 @@
*
* CAUTION: Does NOT detect Windows 8 tablets, since there's no user-agent distinction between
* tablets and desktops in Windows 8.
*
*
* @package mobile
*/
class MobileBrowserDetector {

/**
* @var Boolean Consider tablet devices as "mobile" devices in {@link is_mobile()}.
* Set to FALSE to force non-mobile display if a tablet is detected,
* or to NULL to fall back to the standard mobile detection.
*/
public static $tablet_is_mobile = null;

/**
* List of known mobiles, found in the HTTP_USER_AGENT variable
* @see MobileBrowserDetector::is_mobile() for how they're used.
Expand Down Expand Up @@ -55,7 +62,7 @@ public static function is_win_phone() {
*
* CAUTION: Does NOT detect Windows 8 tablets, since there's no user-agent distinction between
* tablets and desktops in Windows 8.
*
*
* @see http://mobiforge.com/developing/story/setting-http-headers-advise-transcoding-proxies
*
* @param String User agent (defaults to $_SERVER)
Expand All @@ -66,7 +73,12 @@ public static function is_mobile($agent = null) {
if(!$agent) $agent = $_SERVER['HTTP_USER_AGENT'];
$accept = isset($_SERVER['HTTP_ACCEPT']) ? $_SERVER['HTTP_ACCEPT'] : '';

switch(true) {
$isTablet = self::is_tablet($agent);
if(self::$tablet_is_mobile === TRUE && $isTablet) {
$isMobile = true;
} else if(self::$tablet_is_mobile === FALSE && $isTablet) {
$isMobile = false;
} else {switch(true) {
case(self::is_iphone()):
$isMobile = true;
break;
Expand All @@ -84,8 +96,7 @@ public static function is_mobile($agent = null) {
break;
case(self::is_win_phone()):
$isMobile = true;
break;
case(self::is_windows()):
break;case(self::is_windows()):
$isMobile = true;
break;
case(preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|vodafone|o2|pocket|kindle|mobile|pda|psp|treo)/i', $agent)):
Expand All @@ -99,7 +110,7 @@ public static function is_mobile($agent = null) {
break;
case(in_array(strtolower(substr($agent, 0, 4)), self::mobile_index_list())):
$isMobile = true;
break;
break;}
}

if(!headers_sent()) {
Expand All @@ -112,10 +123,10 @@ public static function is_mobile($agent = null) {

/**
* Rough detection of "tablet" user agents, based on their user agent string.
*
*
* CAUTION: Does NOT detect Windows 8 tablets, since there's no user-agent distinction between
* tablets and desktops in Windows 8.
*
*
* Loosely based off the (now discontinued) Categorizr library:
* http://www.brettjankord.com/2012/01/16/categorizr-a-modern-device-detection-script/
*
Expand All @@ -127,38 +138,38 @@ public static function is_tablet($agent = null) {

// Check if user agent is a Tablet
if(
(preg_match('/iP(a|ro)d/i', $agent))
(preg_match('/iP(a|ro)d/i', $agent))
|| (preg_match('/tablet/i', $agent)) && (!preg_match('/RX-34/i', $agent)) || (preg_match('/FOLIO/i', $agent))
) {
return true;
}
// Check if user agent is an Android Tablet
else if (
(preg_match('/Linux/i', $agent))
&& (preg_match('/Android/i', $agent))
(preg_match('/Linux/i', $agent))
&& (preg_match('/Android/i', $agent))
&& (!preg_match('/Fennec|mobi|HTC.Magic|HTCX06HT|Nexus.One|SC-02B|fone.945/i', $agent))
&& (!preg_match('/Mobile/i', $agent))
) {
// see http://googlewebmastercentral.blogspot.de/2011/03/mo-better-to-also-detect-mobile-user.html
// see http://googlewebmastercentral.blogspot.de/2011/03/mo-better-to-also-detect-mobile-user.html
// see http://googlewebmastercentral.blogspot.de/2012/11/giving-tablet-users-full-sized-web.html
return true;
}
// Check if user agent is a Kindle or Kindle Fire
else if (
(preg_match('/Kindle/i', $agent))
|| (preg_match('/Mac.OS/i', $agent))
(preg_match('/Kindle/i', $agent))
|| (preg_match('/Mac.OS/i', $agent))
&& (preg_match('/Silk/i', $agent))
) {
return true;
}
// Check if user agent is a pre Android 3.0 Tablet
else if (
(preg_match('/GT-P10|SC-01C|SHW-M180S|SGH-T849|SCH-I800|SHW-M180L|SPH-P100|SGH-I987|zt180|HTC(.Flyer|\_Flyer)|Sprint.ATP51|ViewPad7|pandigital(sprnova|nova)|Ideos.S7|Dell.Streak.7|Advent.Vega|A101IT|A70BHT|MID7015|Next2|nook/i', $agent))
|| (preg_match('/MB511/i', $agent))
(preg_match('/GT-P10|SC-01C|SHW-M180S|SGH-T849|SCH-I800|SHW-M180L|SPH-P100|SGH-I987|zt180|HTC(.Flyer|\_Flyer)|Sprint.ATP51|ViewPad7|pandigital(sprnova|nova)|Ideos.S7|Dell.Streak.7|Advent.Vega|A101IT|A70BHT|MID7015|Next2|nook/i', $agent))
|| (preg_match('/MB511/i', $agent))
&& (preg_match('/RUTEM/i', $agent))
) {
return true;
}
}
// Browser is either a mobile handset or desktop.
else {
return false;
Expand Down
8 changes: 4 additions & 4 deletions code/MobileSiteConfigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ class MobileSiteConfigExtension extends DataExtension {
/**
* The path the default mobile theme should be copied
* to when {@link SiteConfig} is first created in the database.
*
*
* @see MobileSiteConfigExtension::requireDefaultRecords()
* @var string
*/
protected static $theme_copy_path;

public static $theme_copy_enabled = true;

public static function set_theme_copy_path($path) {
Expand Down Expand Up @@ -172,9 +172,9 @@ public function updateCMSFields(FieldList $fields) {
* Copies a directory from source to destination
* completely by recursively copying each
* individual file.
*
*
* Note: This will ignore ".svn" directories.
*
*
* @param string $src Source path
* @param string $dst Destination path
*/
Expand Down
2 changes: 1 addition & 1 deletion code/MobileSiteControllerExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function onAfterInit() {
else {
$fullSiteCookie = Cookie::get('fullSite');
}

if(is_numeric($fullSiteCookie)) {
// Full site requested
if($fullSiteCookie) {
Expand Down
14 changes: 13 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,17 @@
"require": {
"silverstripe/framework": "~3.1"
},
"license": "BSD-3-Clause"
"require-dev": {
"phpunit/phpunit": "^3.7"
},
"license": "BSD-3-Clause",
"extra": {
"branch-alias": {
"dev-master": "0.7.x-dev"
}
},
"suggests": {
"silverstripe-themes/blackcandymobile": "*",
"silverstripe-themes/jquerymobile": "*"
}
}
26 changes: 26 additions & 0 deletions tests/MobileBrowserDetectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,30 @@ public function testIsTablet() {
'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; ARM; Trident/6.0; Touch)'
));
}

public function testTabletIsMobile() {
$tabletUa = 'Mozilla/5.0 (Linux; U; Android 3.0; en-us; Xoom Build/HRI39) AppleWebKit/534.13 (KHTML, like Gecko) Version/4.0 Safari/534.13';
$mobileUa = 'Mozilla/5.0 (Linux; U; Android 2.2.1; en-us; Nexus One Build/FRG83) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1';
$desktopUa = 'Mozilla/5.0 (Windows; U; MSIE 9.0; WIndows NT 9.0; en-US))';

$orig = MobileBrowserDetector::$tablet_is_mobile;

MobileBrowserDetector::$tablet_is_mobile = true;
$this->assertTrue(MobileBrowserDetector::is_mobile($tabletUa));
$this->assertTrue(MobileBrowserDetector::is_mobile($mobileUa));
$this->assertFalse(MobileBrowserDetector::is_mobile($desktopUa));

MobileBrowserDetector::$tablet_is_mobile = false;
$this->assertFalse(MobileBrowserDetector::is_mobile($tabletUa));
$this->assertTrue(MobileBrowserDetector::is_mobile($mobileUa));
$this->assertFalse(MobileBrowserDetector::is_mobile($desktopUa));

MobileBrowserDetector::$tablet_is_mobile = null;
// Technically this is a bug, but its the standard module check
$this->assertFalse(MobileBrowserDetector::is_mobile($tabletUa));
$this->assertTrue(MobileBrowserDetector::is_mobile($mobileUa));
$this->assertFalse(MobileBrowserDetector::is_mobile($desktopUa));

MobileBrowserDetector::$tablet_is_mobile = $orig;
}
}
2 changes: 2 additions & 0 deletions tests/MobileSiteConfigExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
*/
class MobileSiteConfigExtensionTest extends SapphireTest {

protected $usesDatabase = true;

public function setUp() {
MobileSiteConfigExtension::set_theme_copy_path(TEMP_FOLDER . '/mobile-test-copy-theme/');
MobileSiteConfigExtension::copyDefaultTheme();
Expand Down
5 changes: 0 additions & 5 deletions tests/MobileSiteTreeExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@ class MobileSiteTreeExtensionTest extends FunctionalTest {
'SiteConfig' => array('MobileSiteConfigExtension'),
);

public function setUp() {
parent::setUp();
MobileSiteConfigExtension::set_theme_copy_path(TEMP_FOLDER . '/mobile-test-copy-theme/');
}

public function testShowsCanonicalLinkOnMobile() {
$page = $this->objFromFixture('Page', 'page');

Expand Down
Loading

0 comments on commit d21fea3

Please sign in to comment.