* @license GPL 2+
* @author Daniel Kinzler
*/
+use MediaWiki\Linker\LinkTarget;
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
*/
*/
protected $fragment;
+ /**
+ * @var string
+ */
+ protected $interwiki;
+
/**
* Constructs a TitleValue.
*
* @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' );
+ Assert::parameter( !preg_match( '/^_|[ \r\n\t]|_$/', $dbkey ), '$dbkey',
+ "invalid DB key '$dbkey'" );
Assert::parameter( $dbkey !== '', '$dbkey', 'should not be empty' );
$this->namespace = $namespace;
$this->dbkey = $dbkey;
$this->fragment = $fragment;
+ $this->interwiki = $interwiki;
}
/**
return $this->namespace;
}
+ /**
+ * @since 1.27
+ * @param int $ns
+ * @return bool
+ */
+ public function inNamespace( $ns ) {
+ return $this->namespace == $ns;
+ }
+
/**
* @return string
*/
* @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;
}
/**
$name .= '#' . $this->fragment;
}
+ if ( $this->interwiki !== '' ) {
+ $name = $this->interwiki . ':' . $name;
+ }
+
return $name;
}
}