* @license GPL 2+
* @author Daniel Kinzler
*/
+use MediaWiki\Linker\LinkTarget;
+use Wikimedia\Assert\Assert;
/**
* Represents a page (or page fragment) title within %MediaWiki.
* @see https://www.mediawiki.org/wiki/Requests_for_comment/TitleValue
* @since 1.23
*/
-class TitleValue {
+class TitleValue implements LinkTarget {
/**
* @var int
*/
* @throws InvalidArgumentException
*/
public function __construct( $namespace, $dbkey, $fragment = '' ) {
- if ( !is_int( $namespace ) ) {
- throw new InvalidArgumentException( '$namespace must be an integer' );
- }
-
- if ( !is_string( $dbkey ) ) {
- throw new InvalidArgumentException( '$dbkey must be a string' );
- }
+ Assert::parameterType( 'integer', $namespace, '$namespace' );
+ Assert::parameterType( 'string', $dbkey, '$dbkey' );
+ Assert::parameterType( 'string', $fragment, '$fragment' );
// Sanity check, no full validation or normalization applied here!
- if ( preg_match( '/^_|[ \r\n\t]|_$/', $dbkey ) ) {
- throw new InvalidArgumentException( '$dbkey must be a valid DB key: ' . $dbkey );
- }
-
- if ( !is_string( $fragment ) ) {
- throw new InvalidArgumentException( '$fragment must be a string' );
- }
-
- if ( $dbkey === '' ) {
- throw new InvalidArgumentException( '$dbkey must not be empty' );
- }
+ Assert::parameter( !preg_match( '/^_|[ \r\n\t]|_$/', $dbkey ), '$dbkey', 'invalid DB key' );
+ Assert::parameter( $dbkey !== '', '$dbkey', 'should not be empty' );
$this->namespace = $namespace;
$this->dbkey = $dbkey;
return $this->fragment;
}
+ /**
+ * @since 1.27
+ * @return bool
+ */
+ public function hasFragment() {
+ return $this->fragment !== '';
+ }
+
/**
* Returns the title's DB key, as supplied to the constructor,
* without namespace prefix or fragment.
/**
* Creates a new TitleValue for a different fragment of the same page.
*
+ * @since 1.27
* @param string $fragment The fragment name, or "" for the entire page.
*
* @return TitleValue
*/
- public function createFragmentTitle( $fragment ) {
+ public function createFragmentTarget( $fragment ) {
return new TitleValue( $this->namespace, $this->dbkey, $fragment );
}