Handle missing namespace prefix in XML dumps more gracefully
[lhc/web/wiklou.git] / includes / title / TitleValue.php
index a0a7b1d..597bf2f 100644 (file)
@@ -21,6 +21,7 @@
  * @license GPL 2+
  * @author Daniel Kinzler
  */
+use MediaWiki\Linker\LinkTarget;
 use Wikimedia\Assert\Assert;
 
 /**
@@ -29,9 +30,6 @@ use Wikimedia\Assert\Assert;
  * @note In contrast to Title, this is designed to be a plain value object. That is,
  * it is immutable, does not use global state, and causes no side effects.
  *
- * @note TitleValue represents the title of a local page (or fragment of a page).
- * It does not represent a link, and does not support interwiki prefixes etc.
- *
  * @see https://www.mediawiki.org/wiki/Requests_for_comment/TitleValue
  * @since 1.23
  */
@@ -51,6 +49,11 @@ class TitleValue implements LinkTarget {
         */
        protected $fragment;
 
+       /**
+        * @var string
+        */
+       protected $interwiki;
+
        /**
         * Constructs a TitleValue.
         *
@@ -64,13 +67,15 @@ class TitleValue implements LinkTarget {
         * @param string $dbkey The page title in valid DBkey form. No normalization is applied.
         * @param string $fragment The fragment title. Use '' to represent the whole page.
         *   No validation or normalization is applied.
+        * @param string $interwiki The interwiki component
         *
         * @throws InvalidArgumentException
         */
-       public function __construct( $namespace, $dbkey, $fragment = '' ) {
+       public function __construct( $namespace, $dbkey, $fragment = '', $interwiki = '' ) {
                Assert::parameterType( 'integer', $namespace, '$namespace' );
                Assert::parameterType( 'string', $dbkey, '$dbkey' );
                Assert::parameterType( 'string', $fragment, '$fragment' );
+               Assert::parameterType( 'string', $interwiki, '$interwiki' );
 
                // Sanity check, no full validation or normalization applied here!
                Assert::parameter( !preg_match( '/^_|[ \r\n\t]|_$/', $dbkey ), '$dbkey', 'invalid DB key' );
@@ -79,6 +84,7 @@ class TitleValue implements LinkTarget {
                $this->namespace = $namespace;
                $this->dbkey = $dbkey;
                $this->fragment = $fragment;
+               $this->interwiki = $interwiki;
        }
 
        /**
@@ -88,6 +94,15 @@ class TitleValue implements LinkTarget {
                return $this->namespace;
        }
 
+       /**
+        * @since 1.27
+        * @param int $ns
+        * @return bool
+        */
+       public function inNamespace( $ns ) {
+               return $this->namespace == $ns;
+       }
+
        /**
         * @return string
         */
@@ -137,7 +152,32 @@ class TitleValue implements LinkTarget {
         * @return TitleValue
         */
        public function createFragmentTarget( $fragment ) {
-               return new TitleValue( $this->namespace, $this->dbkey, $fragment );
+               return new TitleValue(
+                       $this->namespace,
+                       $this->dbkey,
+                       $fragment,
+                       $this->interwiki
+               );
+       }
+
+       /**
+        * Whether it has an interwiki part
+        *
+        * @since 1.27
+        * @return bool
+        */
+       public function isExternal() {
+               return $this->interwiki !== '';
+       }
+
+       /**
+        * Returns the interwiki part
+        *
+        * @since 1.27
+        * @return string
+        */
+       public function getInterwiki() {
+               return $this->interwiki;
        }
 
        /**
@@ -154,6 +194,10 @@ class TitleValue implements LinkTarget {
                        $name .= '#' . $this->fragment;
                }
 
+               if ( $this->interwiki !== '' ) {
+                       $name = $this->interwiki . ':' . $name;
+               }
+
                return $name;
        }
 }