Merge "Allow local interwiki links with an empty title part"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Thu, 3 Jul 2014 17:46:48 +0000 (17:46 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Thu, 3 Jul 2014 17:46:48 +0000 (17:46 +0000)
1  2 
includes/title/MediaWikiTitleCodec.php
tests/phpunit/includes/TitleTest.php

@@@ -33,6 -33,7 +33,6 @@@
   * @see https://www.mediawiki.org/wiki/Requests_for_comment/TitleValue
   */
  class MediaWikiTitleCodec implements TitleFormatter, TitleParser {
 -
        /**
         * @var Language
         */
@@@ -53,9 -54,7 +53,9 @@@
         * @param GenderCache $genderCache the gender cache for generating gendered namespace names
         * @param string[]|string $localInterwikis
         */
 -      public function __construct( Language $language, GenderCache $genderCache, $localInterwikis = array() ) {
 +      public function __construct( Language $language, GenderCache $genderCache,
 +              $localInterwikis = array()
 +      ) {
                $this->language = $language;
                $this->genderCache = $genderCache;
                $this->localInterwikis = (array)$localInterwikis;
@@@ -72,8 -71,7 +72,8 @@@
         */
        public function getNamespaceName( $namespace, $text ) {
                if ( $this->language->needsGenderDistinction() &&
 -                      MWNamespace::hasGenderDistinction( $namespace ) ) {
 +                      MWNamespace::hasGenderDistinction( $namespace )
 +              ) {
  
                        //NOTE: we are assuming here that the title text is a user name!
                        $gender = $this->genderCache->getGenderOf( $text, __METHOD__ );
                # Note: use of the /u option on preg_replace here will cause
                # input with invalid UTF-8 sequences to be nullified out in PHP 5.2.x,
                # conveniently disabling them.
 -              $dbkey = preg_replace( '/[ _\xA0\x{1680}\x{180E}\x{2000}-\x{200A}\x{2028}\x{2029}\x{202F}\x{205F}\x{3000}]+/u', '_', $dbkey );
 +              $dbkey = preg_replace(
 +                      '/[ _\xA0\x{1680}\x{180E}\x{2000}-\x{200A}\x{2028}\x{2029}\x{202F}\x{205F}\x{3000}]+/u',
 +                      '_',
 +                      $dbkey
 +              );
                $dbkey = trim( $dbkey, '_' );
  
                if ( strpos( $dbkey, UTF8_REPLACEMENT ) !== false ) {
                                        foreach ( $this->localInterwikis as $localIW ) {
                                                if ( 0 == strcasecmp( $parts['interwiki'], $localIW ) ) {
                                                        if ( $dbkey == '' ) {
-                                                               # Can't have an empty self-link
-                                                               throw new MalformedTitleException( 'Local interwiki with empty title: ' . $text );
+                                                               # Empty self-links should point to the Main Page, to ensure
+                                                               # compatibility with cross-wiki transclusions and the like.
+                                                               $mainPage = Title::newMainPage();
+                                                               return array(
+                                                                       'interwiki' => $mainPage->getInterwiki(),
+                                                                       'fragment' => $mainPage->getFragment(),
+                                                                       'namespace' => $mainPage->getNamespace(),
+                                                                       'dbkey' => $mainPage->getDBkey(),
+                                                                       'user_case_dbkey' => $mainPage->getUserCaseDBKey()
+                                                               );
                                                        }
                                                        $parts['interwiki'] = '';
  
  
                # Fill fields
                $parts['dbkey'] = $dbkey;
 +
                return $parts;
        }
 -
  }
@@@ -29,15 -29,9 +29,15 @@@ class TitleTest extends MediaWikiTestCa
                foreach ( range( 1, 255 ) as $num ) {
                        $chr = chr( $num );
                        if ( strpos( "#[]{}<>|", $chr ) !== false || preg_match( "/[\\x00-\\x1f\\x7f]/", $chr ) ) {
 -                              $this->assertFalse( (bool)preg_match( "/[$titlechars]/", $chr ), "chr($num) = $chr is not a valid titlechar" );
 +                              $this->assertFalse(
 +                                      (bool)preg_match( "/[$titlechars]/", $chr ),
 +                                      "chr($num) = $chr is not a valid titlechar"
 +                              );
                        } else {
 -                              $this->assertTrue( (bool)preg_match( "/[$titlechars]/", $chr ), "chr($num) = $chr is a valid titlechar" );
 +                              $this->assertTrue(
 +                                      (bool)preg_match( "/[$titlechars]/", $chr ),
 +                                      "chr($num) = $chr is a valid titlechar"
 +                              );
                        }
                }
        }
                        'Foo/.../Sandbox',
                        'Sandbox/...',
                        'A~~',
 +                      ':A',
                        // Length is 256 total, but only title part matters
                        'Category:' . str_repeat( 'x', 248 ),
                        str_repeat( 'x', 252 ),
                        // interwiki prefix
                        'localtestiw: #anchor',
+                       'localtestiw:',
                        'localtestiw:foo',
                        'localtestiw: foo # anchor',
                        'localtestiw: Talk: Sandbox # anchor',
                        'Category: ',
                        'Category: #bar',
                        // interwiki prefix
-                       'localtestiw:',
                        'localtestiw: Talk: # anchor',
                        'localtestiw: Talk:'
                ) as $text ) {
                } else {
                        $par = null;
                }
 -              $this->assertEquals( $expectedParam, $par, "Bug 31100 regression check: Title->fixSpecialName() should preserve parameter" );
 +              $this->assertEquals(
 +                      $expectedParam,
 +                      $par,
 +                      "Bug 31100 regression check: Title->fixSpecialName() should preserve parameter"
 +              );
        }
  
        public static function provideBug31100() {
                        $allowableness = $expected
                                ? " should be allowed"
                                : " should NOT be allowed";
 -                      $this->assertEquals( $expected, $errors, "User action '$action' on [[$source]] $allowableness." );
 +                      $this->assertEquals(
 +                              $expected,
 +                              $errors,
 +                              "User action '$action' on [[$source]] $allowableness."
 +                      );
                } else {
                        $errors = $this->flattenErrorsArray( $errors );
                        foreach ( (array)$expected as $error ) {
         * @dataProvider provideGetPageViewLanguage
         * @covers Title::getPageViewLanguage
         */
 -      public function testGetPageViewLanguage( $expected, $titleText, $contLang, $lang, $variant, $msg = '' ) {
 +      public function testGetPageViewLanguage( $expected, $titleText, $contLang,
 +              $lang, $variant, $msg = ''
 +      ) {
                global $wgLanguageCode, $wgContLang, $wgLang, $wgDefaultLanguageVariant, $wgAllowUserJs;
  
                // Setup environnement for this test