Merge "Enforce load.php's no-session constraint"
[lhc/web/wiklou.git] / includes / title / MediaWikiTitleCodec.php
index 1de4247..0e291ed 100644 (file)
@@ -21,6 +21,7 @@
  * @license GPL 2+
  * @author Daniel Kinzler
  */
+use MediaWiki\Linker\LinkTarget;
 
 /**
  * A codec for %MediaWiki page titles.
@@ -55,7 +56,7 @@ class MediaWikiTitleCodec implements TitleFormatter, TitleParser {
         * @param string[]|string $localInterwikis
         */
        public function __construct( Language $language, GenderCache $genderCache,
-               $localInterwikis = array()
+               $localInterwikis = []
        ) {
                $this->language = $language;
                $this->genderCache = $genderCache;
@@ -97,11 +98,12 @@ class MediaWikiTitleCodec implements TitleFormatter, TitleParser {
         * @param string $text The page title. Should be valid. Only minimal normalization is applied.
         *        Underscores will be replaced.
         * @param string $fragment The fragment name (may be empty).
+        * @param string $interwiki The interwiki name (may be empty).
         *
         * @throws InvalidArgumentException If the namespace is invalid
         * @return string
         */
-       public function formatTitle( $namespace, $text, $fragment = '' ) {
+       public function formatTitle( $namespace, $text, $fragment = '', $interwiki = '' ) {
                if ( $namespace !== false ) {
                        $namespace = $this->getNamespaceName( $namespace, $text );
 
@@ -114,6 +116,10 @@ class MediaWikiTitleCodec implements TitleFormatter, TitleParser {
                        $text = $text . '#' . $fragment;
                }
 
+               if ( $interwiki !== '' ) {
+                       $text = $interwiki . ':' . $text;
+               }
+
                $text = str_replace( '_', ' ', $text );
 
                return $text;
@@ -135,17 +141,17 @@ class MediaWikiTitleCodec implements TitleFormatter, TitleParser {
                // be refactored to avoid this.
                $parts = $this->splitTitleString( $text, $defaultNamespace );
 
-               // Interwiki links are not supported by TitleValue
-               if ( $parts['interwiki'] !== '' ) {
-                       throw new MalformedTitleException( 'title-invalid-interwiki', $text );
-               }
-
                // Relative fragment links are not supported by TitleValue
                if ( $parts['dbkey'] === '' ) {
                        throw new MalformedTitleException( 'title-invalid-empty', $text );
                }
 
-               return new TitleValue( $parts['namespace'], $parts['dbkey'], $parts['fragment'] );
+               return new TitleValue(
+                       $parts['namespace'],
+                       $parts['dbkey'],
+                       $parts['fragment'],
+                       $parts['interwiki']
+               );
        }
 
        /**
@@ -167,7 +173,12 @@ class MediaWikiTitleCodec implements TitleFormatter, TitleParser {
         * @return string
         */
        public function getPrefixedText( LinkTarget $title ) {
-               return $this->formatTitle( $title->getNamespace(), $title->getText(), '' );
+               return $this->formatTitle(
+                       $title->getNamespace(),
+                       $title->getText(),
+                       '',
+                       $title->getInterwiki()
+               );
        }
 
        /**
@@ -178,7 +189,12 @@ class MediaWikiTitleCodec implements TitleFormatter, TitleParser {
         * @return string
         */
        public function getFullText( LinkTarget $title ) {
-               return $this->formatTitle( $title->getNamespace(), $title->getText(), $title->getFragment() );
+               return $this->formatTitle(
+                       $title->getNamespace(),
+                       $title->getText(),
+                       $title->getFragment(),
+                       $title->getInterwiki()
+               );
        }
 
        /**
@@ -205,14 +221,14 @@ class MediaWikiTitleCodec implements TitleFormatter, TitleParser {
                $dbkey = str_replace( ' ', '_', $text );
 
                # Initialisation
-               $parts = array(
+               $parts = [
                        'interwiki' => '',
                        'local_interwiki' => false,
                        'fragment' => '',
                        'namespace' => $defaultNamespace,
                        'dbkey' => $dbkey,
                        'user_case_dbkey' => $dbkey,
-               );
+               ];
 
                # Strip Unicode bidi override characters.
                # Sometimes they slip into cut-n-pasted page titles, where the
@@ -252,7 +268,7 @@ class MediaWikiTitleCodec implements TitleFormatter, TitleParser {
                # Namespace or interwiki prefix
                $prefixRegexp = "/^(.+?)_*:_*(.*)$/S";
                do {
-                       $m = array();
+                       $m = [];
                        if ( preg_match( $prefixRegexp, $dbkey, $m ) ) {
                                $p = $m[1];
                                $ns = $this->language->getNsIndex( $p );
@@ -283,14 +299,14 @@ class MediaWikiTitleCodec implements TitleFormatter, TitleParser {
                                                                # Empty self-links should point to the Main Page, to ensure
                                                                # compatibility with cross-wiki transclusions and the like.
                                                                $mainPage = Title::newMainPage();
-                                                               return array(
+                                                               return [
                                                                        'interwiki' => $mainPage->getInterwiki(),
                                                                        'local_interwiki' => true,
                                                                        'fragment' => $mainPage->getFragment(),
                                                                        'namespace' => $mainPage->getNamespace(),
                                                                        'dbkey' => $mainPage->getDBkey(),
                                                                        'user_case_dbkey' => $mainPage->getUserCaseDBKey()
-                                                               );
+                                                               ];
                                                        }
                                                        $parts['interwiki'] = '';
                                                        # local interwikis should behave like initial-colon links
@@ -325,9 +341,9 @@ class MediaWikiTitleCodec implements TitleFormatter, TitleParser {
 
                # Reject illegal characters.
                $rxTc = self::getTitleInvalidRegex();
-               $matches = array();
+               $matches = [];
                if ( preg_match( $rxTc, $dbkey, $matches ) ) {
-                       throw new MalformedTitleException( 'title-invalid-characters', $text, array( $matches[0] ) );
+                       throw new MalformedTitleException( 'title-invalid-characters', $text, [ $matches[0] ] );
                }
 
                # Pages with "/./" or "/../" appearing in the URLs will often be un-
@@ -360,7 +376,7 @@ class MediaWikiTitleCodec implements TitleFormatter, TitleParser {
                $maxLength = ( $parts['namespace'] != NS_SPECIAL ) ? 255 : 512;
                if ( strlen( $dbkey ) > $maxLength ) {
                        throw new MalformedTitleException( 'title-invalid-too-long', $text,
-                               array( Message::numParam( $maxLength ) ) );
+                               [ Message::numParam( $maxLength ) ] );
                }
 
                # Normally, all wiki links are forced to have an initial capital letter so [[foo]]