From ab623befc5ee0d9b0cf98ad99771f18e3a875834 Mon Sep 17 00:00:00 2001 From: Kunal Mehta Date: Wed, 11 May 2016 14:01:48 -0700 Subject: [PATCH] Add LinkTarget::inNamespace() helper function And implementation for TitleValue, plus tests Change-Id: I42c301e28938dcd55580e20b7c1ddefcbf5a2bef --- includes/linker/LinkTarget.php | 8 ++++++++ includes/title/TitleValue.php | 9 +++++++++ tests/phpunit/includes/title/TitleValueTest.php | 1 + 3 files changed, 18 insertions(+) diff --git a/includes/linker/LinkTarget.php b/includes/linker/LinkTarget.php index 7b59751de6..da48e00a99 100644 --- a/includes/linker/LinkTarget.php +++ b/includes/linker/LinkTarget.php @@ -33,6 +33,14 @@ interface LinkTarget { */ public function getNamespace(); + /** + * Convenience function to test if it is in the namespace + * + * @param int $ns + * @return bool + */ + public function inNamespace( $ns ); + /** * Get the link fragment (i.e. the bit after the #) in text form. * diff --git a/includes/title/TitleValue.php b/includes/title/TitleValue.php index 63c075f1c5..597bf2f4ee 100644 --- a/includes/title/TitleValue.php +++ b/includes/title/TitleValue.php @@ -94,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 */ diff --git a/tests/phpunit/includes/title/TitleValueTest.php b/tests/phpunit/includes/title/TitleValueTest.php index 792255339f..4dbda74ae0 100644 --- a/tests/phpunit/includes/title/TitleValueTest.php +++ b/tests/phpunit/includes/title/TitleValueTest.php @@ -42,6 +42,7 @@ class TitleValueTest extends MediaWikiTestCase { $title = new TitleValue( $ns, $text, $fragment, $interwiki ); $this->assertEquals( $ns, $title->getNamespace() ); + $this->assertTrue( $title->inNamespace( $ns ) ); $this->assertEquals( $text, $title->getText() ); $this->assertEquals( $fragment, $title->getFragment() ); $this->assertEquals( $hasFragment, $title->hasFragment() ); -- 2.20.1