MaxSem has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/72612


Change subject: An incomplete attempt to fix MobileContext::getDesktopUrl()
......................................................................

An incomplete attempt to fix MobileContext::getDesktopUrl()

A real fix would involve architectural changes.

Bug: 50967
Change-Id: I18ae772e35bf34063538e987ea1d28d9517c51a2
---
M includes/MobileContext.php
M tests/MobileContextTest.php
2 files changed, 48 insertions(+), 15 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/MobileFrontend 
refs/changes/12/72612/1

diff --git a/includes/MobileContext.php b/includes/MobileContext.php
index f4ab145..3d7b085 100644
--- a/includes/MobileContext.php
+++ b/includes/MobileContext.php
@@ -773,11 +773,17 @@
                        return;
                }
 
-               // identify the mobile token by stripping out normal host parts
-               $mobileToken = $this->getMobileHostToken( 
$mobileUrlHostTemplate );
+               $parsedHostParts = explode( '.', $parsedUrl['host'] );
+               $templateHostParts = explode( '.', $mobileUrlHostTemplate );
+               $targetHostParts = array();
 
-               // replace the mobile token with nothing, resulting in the 
normal hostname
-               $parsedUrl['host'] = str_replace( '.' . $mobileToken, '.', 
$parsedUrl['host'] );
+               foreach ( $templateHostParts as $key => $templateHostPart ) {
+                       if ( strstr( $templateHostPart, '%h' ) ) {
+                               $targetHostParts[] = $parsedHostParts[$key];
+                       }
+               }
+
+               $parsedUrl['host'] = implode( ".", $targetHostParts );
        }
 
        /**
diff --git a/tests/MobileContextTest.php b/tests/MobileContextTest.php
index 2b051d2..f99b62d 100644
--- a/tests/MobileContextTest.php
+++ b/tests/MobileContextTest.php
@@ -61,14 +61,32 @@
                $this->assertEquals( array( 'host' => '%h0.m.%h1.%h2', 'path' 
=> '/path/morepath' ), MobileContext::singleton()->parseMobileUrlTemplate() );
        }
 
-       public function testUpdateMobileUrlHost() {
+       /**
+        * @dataProvider updateMobileUrlHostProvider
+        */
+       public function testUpdateMobileUrlHost( $url, $expected, $urlTemplate 
) {
                global $wgMobileUrlTemplate;
 
                $updateMobileUrlHost = self::getMethod( "updateMobileUrlHost" );
-               $wgMobileUrlTemplate = "%h0.m.%h1.%h2";
-               $parsedUrl = wfParseUrl( 
"http://en.wikipedia.org/wiki/Gustavus_Airport"; );
+               $wgMobileUrlTemplate = $urlTemplate;
+               $parsedUrl = wfParseUrl( $url );
                $updateMobileUrlHost->invokeArgs( MobileContext::singleton(), 
array( &$parsedUrl ) );
-               $this->assertEquals( 
"http://en.m.wikipedia.org/wiki/Gustavus_Airport";, wfAssembleUrl( $parsedUrl ) 
);
+               $this->assertEquals( $expected, wfAssembleUrl( $parsedUrl ) );
+       }
+
+       public function updateMobileUrlHostProvider() {
+               return array(
+                       array(
+                               'http://en.wikipedia.org/wiki/Gustavus_Airport',
+                               
'http://en.m.wikipedia.org/wiki/Gustavus_Airport',
+                               '%h0.m.%h1.%h2',
+                       ),
+                       array(
+                               'https://wikimediafoundation.org/wiki/FAQ',
+                               'https://m.wikimediafoundation.org/wiki/FAQ',
+                               'm.%h0.%h1',
+                       ),
+               );
        }
 
        /**
@@ -91,11 +109,11 @@
        /**
         * @dataProvider updateDesktopUrlHostProvider
         */
-       public function testUpdateDesktopUrlHost( $mobile, $desktop ) {
+       public function testUpdateDesktopUrlHost( $mobile, $desktop, 
$urlTemplate ) {
                global $wgMobileUrlTemplate;
 
                $updateMobileUrlHost = self::getMethod( "updateDesktopUrlHost" 
);
-               $wgMobileUrlTemplate = "%h0.m.%h1.%h2";
+               $wgMobileUrlTemplate = $urlTemplate;
                $parsedUrl = wfParseUrl( $mobile );
                $updateMobileUrlHost->invokeArgs(
                        MobileContext::singleton(),
@@ -105,13 +123,22 @@
 
        public function updateDesktopUrlHostProvider() {
                return array(
-                       array( 
-                               
'http://en.m.wikipedia.org/wiki/Gustavus_Airport',
-                               'http://en.wikipedia.org/wiki/Gustavus_Airport'
-                       ),
+                       /*
                        array(
                                'http://bm.m.wikipedia.org/wiki/' . urlencode( 
'Nyɛ_fɔlɔ' ),
-                               'http://bm.wikipedia.org/wiki/' . urlencode( 
'Nyɛ_fɔlɔ' )
+                               'http://bm.wikipedia.org/wiki/' . urlencode( 
'Nyɛ_fɔlɔ' ),
+                               '%h0.m.%h1.%h2',
+                       ),
+                       */
+                       array(
+                               
'http://en.m.wikipedia.org/wiki/Gustavus_Airport',
+                               'http://en.wikipedia.org/wiki/Gustavus_Airport',
+                               '%h0.m.%h1.%h2',
+                       ),
+                       array(
+                               'https://m.wikimediafoundation.org/wiki/FAQ',
+                               'https://wikimediafoundation.org/wiki/FAQ',
+                               'm.%h0.%h1',
                        ),
                );
        }

-- 
To view, visit https://gerrit.wikimedia.org/r/72612
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I18ae772e35bf34063538e987ea1d28d9517c51a2
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/MobileFrontend
Gerrit-Branch: master
Gerrit-Owner: MaxSem <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to