Ori.livneh has uploaded a new change for review.
https://gerrit.wikimedia.org/r/257681
Change subject: Vary HTML output by NetSpeed designation
......................................................................
Vary HTML output by NetSpeed designation
Vary MobileFrontend's HTML output based on connection speed, as indicated by
'NetSpeed' cookie. 'NetSpeed' may be set to either 'A' (for fast connections)
or 'B' (for slow connections). If cookie is not set or has an invalid value,
the default designation is 'A' (fast), simply because that is the current
implicit default.
When the NetSpeed designation is 'B', omit the `srcset` attribute from image
tags, and set image quality to 'low'. Currently this affects only the JPEG
encoder.
Bug: T119797
Change-Id: I8a1fb69724b2581c2efc1f5a9eddf72d4c10a6d2
(cherry picked from commit e74e620b04b2ddd4a5f06e33ae9fed3f4cd7b0c0)
---
M MobileFrontend.php
M includes/MobileContext.php
M includes/MobileFrontend.hooks.php
M tests/phpunit/MobileContextTest.php
4 files changed, 102 insertions(+), 0 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/MobileFrontend
refs/changes/81/257681/1
diff --git a/MobileFrontend.php b/MobileFrontend.php
index 83eb2ea..53d6db9 100644
--- a/MobileFrontend.php
+++ b/MobileFrontend.php
@@ -145,6 +145,8 @@
$wgHooks['LoginFormValidErrorMessages'][] =
'MobileFrontendHooks::onLoginFormValidErrorMessages';
$wgHooks['ResourceLoaderGetLessVars'][] =
'MobileFrontendHooks::onResourceLoaderGetLessVars';
$wgHooks['SkinPreloadExistence'][] =
'MobileFrontendHooks::onSkinPreloadExistence';
+$wgHooks['ThumbnailBeforeProduceHTML'][] =
'MobileFrontendHooks::onThumbnailBeforeProduceHTML';
+$wgHooks['ImageBeforeProduceHTML'][] =
'MobileFrontendHooks::onImageBeforeProduceHTML';
$wgSpecialPages += array(
'History' => 'SpecialMobileHistory',
diff --git a/includes/MobileContext.php b/includes/MobileContext.php
index 5950881..e8b113e 100644
--- a/includes/MobileContext.php
+++ b/includes/MobileContext.php
@@ -9,6 +9,10 @@
class MobileContext extends ContextSource {
const USEFORMAT_COOKIE_NAME = 'mf_useformat';
const USER_MODE_PREFERENCE_NAME = 'mfMode';
+
+ const NETSPEED_FAST = 'A';
+ const NETSPEED_SLOW = 'B';
+
/**
* Saves the testing mode user has opted in: 'beta' or 'stable'
* @var string $mobileMode
@@ -19,6 +23,11 @@
* @var boolean $disableImages
*/
protected $disableImages;
+ /**
+ * NetSpeed designation
+ * @var string $netSpeed
+ */
+ protected $netSpeed;
/**
* Save explicitly requested format
* @var string $useFormat
@@ -172,6 +181,23 @@
}
/**
+ * Get NetSpeed designation.
+ * @return string MobileContext::NETSPEED_FAST or
MobileContext::NETSPEED_SLOW.
+ */
+ public function getNetSpeed() {
+ if ( is_null( $this->netSpeed ) ) {
+ $this->netSpeed = $this->getRequest()->getCookie(
'NetSpeed', '' );
+ if ( $this->netSpeed !== self::NETSPEED_FAST &&
$this->netSpeed !== self::NETSPEED_SLOW ) {
+ // Since we are currently delivering the
richest experience
+ // to all users, make the default netSpeed
'fast'.
+ $this->netSpeed = self::NETSPEED_FAST;
+ }
+ }
+
+ return $this->netSpeed;
+ }
+
+ /**
* Whether the user is allowed to upload
* @return boolean
*/
diff --git a/includes/MobileFrontend.hooks.php
b/includes/MobileFrontend.hooks.php
index f027069..cca00a0 100644
--- a/includes/MobileFrontend.hooks.php
+++ b/includes/MobileFrontend.hooks.php
@@ -1075,6 +1075,49 @@
}
/**
+ * If the NetSpeed designation is not MobileContext::NETSPEED_FAST, omit
+ * srcset attributes from image tags.
+ *
+ * @param ThumbnailImage $thumbnail
+ * @param array &$attribs
+ * @param array &$linkAttribs
+ */
+ public static function onThumbnailBeforeProduceHTML( $thumbnail,
&$attribs, &$linkAttribs ) {
+ $context = MobileContext::singleton();
+
+ if (
+ $context->shouldDisplayMobileView() &&
+ $context->getNetSpeed() !== MobileContext::NETSPEED_FAST
+ ) {
+ unset( $attribs['srcset'] );
+ }
+ }
+
+ /**
+ * If the NetSpeed designation is not MobileContext::NETSPEED_FAST, set
+ * image quality to 'low'. Currently this only influences JPEG encoding.
+ *
+ * @param Skin &$skin
+ * @param Title &$title
+ * @param File &$file
+ * @param array &$frameParams
+ * @param array &$handlerParams
+ * @param string &$time
+ * @param string &$res
+ */
+ public static function onImageBeforeProduceHTML( &$skin, &$title,
&$file,
+ &$frameParams, &$handlerParams, &$time, &$res ) {
+ $context = MobileContext::singleton();
+
+ if (
+ $context->shouldDisplayMobileView() &&
+ $context->getNetSpeed() !== MobileContext::NETSPEED_FAST
+ ) {
+ $handlerParams['quality'] = 'low';
+ }
+ }
+
+ /**
* LoginFormValidErrorMessages hook handler to promote MF specific
error message be valid.
*
* @param array $messages Array of already added messages
diff --git a/tests/phpunit/MobileContextTest.php
b/tests/phpunit/MobileContextTest.php
index 4dfc4fd..dbe226d 100644
--- a/tests/phpunit/MobileContextTest.php
+++ b/tests/phpunit/MobileContextTest.php
@@ -611,6 +611,37 @@
SpecialPage::getTitleFor( 'Search' );
$this->assertTrue( true, 'In case of failure this test just
crashes' );
}
+
+ public function getNetSpeedProvider() {
+ return array(
+ array(
+ array( 'NetSpeed' =>
MobileContext::NETSPEED_FAST ),
+ MobileContext::NETSPEED_FAST
+ ),
+ array(
+ array( 'NetSpeed' =>
MobileContext::NETSPEED_SLOW ),
+ MobileContext::NETSPEED_SLOW
+ ),
+ array(
+ array(),
+ MobileContext::NETSPEED_FAST,
+ 'If NetSpeed cookie is absent, default to
MobileContext::NETSPEED_FAST'
+ ),
+ array(
+ array( 'NetSpeed' => 'Bogus value' ),
+ MobileContext::NETSPEED_FAST,
+ 'If NetSpeed cookie value is invalid, default
to MobileContext::NETSPEED_FAST'
+ ),
+ );
+ }
+
+ /**
+ * @dataProvider getNetSpeedProvider
+ */
+ public function testGetNetSpeed( $cookies, $netSpeed, $message = null )
{
+ $context = $this->makeContext( '/', $cookies );
+ $this->assertEquals( $context->getNetSpeed(), $netSpeed,
$message );
+ }
}
class BogusMobileContext {
--
To view, visit https://gerrit.wikimedia.org/r/257681
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I8a1fb69724b2581c2efc1f5a9eddf72d4c10a6d2
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/MobileFrontend
Gerrit-Branch: wmf/1.27.0-wmf.7
Gerrit-Owner: Ori.livneh <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits