MaxSem has uploaded a new change for review.

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

Change subject: Make sure canonical URLs are always in UTF-8
......................................................................

Make sure canonical URLs are always in UTF-8

Bug: 69026
Change-Id: Ia37dcd35965b3506252f96ffd25ef1404c5ac3db
---
M includes/OutputPage.php
M includes/WebRequest.php
M tests/phpunit/includes/OutputPageTest.php
3 files changed, 74 insertions(+), 2 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/21/151221/1

diff --git a/includes/OutputPage.php b/includes/OutputPage.php
index 0393425..23ebb8d 100644
--- a/includes/OutputPage.php
+++ b/includes/OutputPage.php
@@ -3236,7 +3236,7 @@
                        $wgSitename, $wgVersion,
                        $wgFeed, $wgOverrideSiteFeed, $wgAdvertisedFeedTypes,
                        $wgDisableLangConversion, $wgCanonicalLanguageLinks,
-                       $wgRightsPage, $wgRightsUrl;
+                       $wgRightsPage, $wgRightsUrl, $wgContLang;
 
                $tags = array();
 
@@ -3442,6 +3442,24 @@
                        } else {
                                $reqUrl = $this->getRequest()->getRequestURL();
                                $canonicalUrl = wfExpandUrl( $reqUrl, 
PROTO_CANONICAL );
+                               // Convert the URL from fallback encoding
+                               $parts = wfParseUrl( $canonicalUrl );
+                               $pathParts = explode( '/', $parts['path'] );
+                               $fixed = array();
+                               foreach ( $pathParts as $part ) {
+                                       $fixed[] = urlencode( 
$wgContLang->checkTitleEncoding( urldecode( $part ) ) );
+                               }
+                               $parts['path'] = implode( '/', $fixed );
+                               if ( isset( $parts['query'] ) ) {
+                                       $query = wfCgiToArray( $parts['query'] 
);
+                                       $newQuery = array();
+                                       foreach ( $query as $key => $value ) {
+                                               
$newQuery[$wgContLang->checkTitleEncoding( $key )] =
+                                                       
$wgContLang->checkTitleEncoding( $value );
+                                       }
+                                       $parts['query'] = wfArrayToCgi( 
$newQuery );
+                               }
+                               $canonicalUrl = wfAssembleUrl( $parts );
                        }
                }
                if ( $canonicalUrl !== false ) {
diff --git a/includes/WebRequest.php b/includes/WebRequest.php
index a1fa0eb..9927c4d 100644
--- a/includes/WebRequest.php
+++ b/includes/WebRequest.php
@@ -1250,6 +1250,7 @@
 class FauxRequest extends WebRequest {
        private $wasPosted = false;
        private $session = array();
+       private $requestUrl;
 
        /**
         * @param array $data Array of *non*-urlencoded key => value pairs, the
@@ -1329,8 +1330,15 @@
                return false;
        }
 
+       public function setRequestURL( $url ) {
+               $this->requestUrl = $url;
+       }
+
        public function getRequestURL() {
-               $this->notImplemented( __METHOD__ );
+               if ( $this->requestUrl === null ) {
+                       throw new MWException( 'Request URL not set' );
+               }
+               return $this->requestUrl;
        }
 
        public function getProtocol() {
diff --git a/tests/phpunit/includes/OutputPageTest.php 
b/tests/phpunit/includes/OutputPageTest.php
index 2cfdfcd..2256ae4 100644
--- a/tests/phpunit/includes/OutputPageTest.php
+++ b/tests/phpunit/includes/OutputPageTest.php
@@ -221,4 +221,50 @@
                $links = $method->invokeArgs( $out, $args );
                $this->assertEquals( $expectedHtml, $links['html'] );
        }
+
+       /**
+        * @dataProvider provideBug69026
+        * @param $langCode
+        * @param $url
+        * @param $expectedCanonicalUrl
+        */
+       public function testBug69026( $langCode, $url, $expectedCanonicalUrl ) {
+               $this->setMwGlobals(
+                       array(
+                               'wgContLang' => Language::factory( $langCode ),
+                               'wgServer' => 'http://localhost',
+                               'wgCanonicalServer' => 'http://localhost',
+                               'wgEnableCanonicalServerLink' => true,
+                       )
+               );
+               $req = new FauxRequest();
+               $req->setRequestURL( $url );
+               $context = new DerivativeContext( new RequestContext() );
+               $context->setRequest( $req );
+               $context->setUser( new User() );
+               $context->setTitle( Title::newFromText( 'Test' ) );
+               $out = new OutputPage( $context );
+               $items = $out->getHeadLinksArray();
+               foreach ( $items as $item ) {
+                       if ( strpos( '<link rel="canonical"', $item ) === 0 ) {
+                               break;
+                       }
+               }
+               $this->assertEquals( Html::element( 'link', array(
+                               'rel' => 'canonical',
+                               'href' => $expectedCanonicalUrl
+                       ) ),
+                       $item
+               );
+       }
+
+       public function provideBug69026() {
+               return array(
+                       array( 'en', '/wiki/Foo', 'http://localhost/wiki/Foo' ),
+                       array( 'ru', 
'/wiki/%D0%EE%F1%F2_%F7%E5%EB%EE%E2%E5%EA%E0',
+                               
'http://localhost/wiki/%D0%A0%D0%BE%D1%81%D1%82_%D1%87%D0%B5%D0%BB%D0%BE%D0%B2%D0%B5%D0%BA%D0%B0'
 ),
+                       array( 'ru', 
'/wiki/%D0%EE%F1%F2_%F7%E5%EB%EE%E2%E5%EA%E0?test=%D0%EE%F1%F2_%F7%E5%EB%EE%E2%E5%EA%E0',
+                                  
'http://localhost/wiki/%D0%A0%D0%BE%D1%81%D1%82_%D1%87%D0%B5%D0%BB%D0%BE%D0%B2%D0%B5%D0%BA%D0%B0?test=%D0%A0%D0%BE%D1%81%D1%82_%D1%87%D0%B5%D0%BB%D0%BE%D0%B2%D0%B5%D0%BA%D0%B0'
 ),
+               );
+       }
 }

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ia37dcd35965b3506252f96ffd25ef1404c5ac3db
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: MaxSem <maxsem.w...@gmail.com>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to