Merge "Migration of browsertests* Jenkins jobs to selenium* jobs"
[lhc/web/wiklou.git] / includes / title / TitleValue.php
index 5cac347..c23d698 100644 (file)
@@ -21,6 +21,8 @@
  * @license GPL 2+
  * @author Daniel Kinzler
  */
+use MediaWiki\Linker\LinkTarget;
+use Wikimedia\Assert\Assert;
 
 /**
  * Represents a page (or page fragment) title within %MediaWiki.
@@ -34,7 +36,7 @@
  * @see https://www.mediawiki.org/wiki/Requests_for_comment/TitleValue
  * @since 1.23
  */
-class TitleValue {
+class TitleValue implements LinkTarget {
        /**
         * @var int
         */
@@ -67,26 +69,13 @@ class TitleValue {
         * @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;
@@ -107,6 +96,14 @@ class TitleValue {
                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.
@@ -135,11 +132,12 @@ class TitleValue {
        /**
         * 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 );
        }