Merge "MimeAnalyzer: Add testcases for mp3 detection"
[lhc/web/wiklou.git] / includes / title / MediaWikiTitleCodec.php
index a937e75..dd8b975 100644 (file)
@@ -21,6 +21,8 @@
  * @license GPL 2+
  * @author Daniel Kinzler
  */
+use MediaWiki\Interwiki\InterwikiLookup;
+use MediaWiki\MediaWikiServices;
 use MediaWiki\Linker\LinkTarget;
 
 /**
@@ -50,17 +52,25 @@ class MediaWikiTitleCodec implements TitleFormatter, TitleParser {
         */
        protected $localInterwikis;
 
+       /**
+        * @var InterwikiLookup
+        */
+       protected $interwikiLookup;
+
        /**
         * @param Language $language The language object to use for localizing namespace names.
         * @param GenderCache $genderCache The gender cache for generating gendered namespace names
         * @param string[]|string $localInterwikis
+        * @param InterwikiLookup|null $interwikiLookup
         */
        public function __construct( Language $language, GenderCache $genderCache,
-               $localInterwikis = []
+               $localInterwikis = [], $interwikiLookup = null
        ) {
                $this->language = $language;
                $this->genderCache = $genderCache;
                $this->localInterwikis = (array)$localInterwikis;
+               $this->interwikiLookup = $interwikiLookup ?:
+                       MediaWikiServices::getInstance()->getInterwikiLookup();
        }
 
        /**
@@ -76,7 +86,6 @@ class MediaWikiTitleCodec implements TitleFormatter, TitleParser {
                if ( $this->language->needsGenderDistinction() &&
                        MWNamespace::hasGenderDistinction( $namespace )
                ) {
-
                        // NOTE: we are assuming here that the title text is a user name!
                        $gender = $this->genderCache->getGenderOf( $text, __METHOD__ );
                        $name = $this->language->getGenderNsText( $namespace, $gender );
@@ -292,7 +301,7 @@ class MediaWikiTitleCodec implements TitleFormatter, TitleParser {
 
                # Initial colon indicates main namespace rather than specified default
                # but should not create invalid {ns,title} pairs such as {0,Project:Foo}
-               if ( $dbkey !== '' && ':' == $dbkey[0] ) {
+               if ( $dbkey !== '' && $dbkey[0] == ':' ) {
                        $parts['namespace'] = NS_MAIN;
                        $dbkey = substr( $dbkey, 1 ); # remove the colon but continue processing
                        $dbkey = trim( $dbkey, '_' ); # remove any subsequent whitespace
@@ -318,13 +327,13 @@ class MediaWikiTitleCodec implements TitleFormatter, TitleParser {
                                                if ( $this->language->getNsIndex( $x[1] ) ) {
                                                        # Disallow Talk:File:x type titles...
                                                        throw new MalformedTitleException( 'title-invalid-talk-namespace', $text );
-                                               } elseif ( Interwiki::isValidInterwiki( $x[1] ) ) {
+                                               } elseif ( $this->interwikiLookup->isValidInterwiki( $x[1] ) ) {
                                                        // TODO: get rid of global state!
                                                        # Disallow Talk:Interwiki:x type titles...
                                                        throw new MalformedTitleException( 'title-invalid-talk-namespace', $text );
                                                }
                                        }
-                               } elseif ( Interwiki::isValidInterwiki( $p ) ) {
+                               } elseif ( $this->interwikiLookup->isValidInterwiki( $p ) ) {
                                        # Interwiki link
                                        $dbkey = $m[2];
                                        $parts['interwiki'] = $this->language->lc( $p );
@@ -359,6 +368,7 @@ class MediaWikiTitleCodec implements TitleFormatter, TitleParser {
                                        if ( $dbkey !== '' && $dbkey[0] == ':' ) {
                                                $parts['namespace'] = NS_MAIN;
                                                $dbkey = substr( $dbkey, 1 );
+                                               $dbkey = trim( $dbkey, '_' );
                                        }
                                }
                                # If there's no recognized interwiki or namespace,