diff options
author | Aryeh Gregor <ayg@aryeh.name> | 2019-04-29 19:29:31 +0300 |
---|---|---|
committer | James D. Forrester <jforrester@wikimedia.org> | 2019-05-06 08:29:28 -0700 |
commit | e30cb5ba908609f33bcabd59114065e10dbdbdd2 (patch) | |
tree | f9590fbcedc2749c7333606b19f9e15bd967b8e5 /includes/title/NamespaceInfo.php | |
parent | 8d9d8c8bb319482919d3d62732b9a0f689ede052 (diff) | |
download | mediawikicore-e30cb5ba908609f33bcabd59114065e10dbdbdd2.tar.gz mediawikicore-e30cb5ba908609f33bcabd59114065e10dbdbdd2.zip |
Move Title::getSubject/Talk/OtherPage to NamespaceInfo
This allows converting some more code to LinkTarget. 100% test coverage.
Change-Id: I28903af6a41d02755f37f31561a524547445821e
Diffstat (limited to 'includes/title/NamespaceInfo.php')
-rw-r--r-- | includes/title/NamespaceInfo.php | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/includes/title/NamespaceInfo.php b/includes/title/NamespaceInfo.php index e3ab3a3abaf5..7cfadc01441e 100644 --- a/includes/title/NamespaceInfo.php +++ b/includes/title/NamespaceInfo.php @@ -21,6 +21,7 @@ */ use MediaWiki\Config\ServiceOptions; +use MediaWiki\Linker\LinkTarget; /** * This is a utility class for dealing with namespaces that encodes all the "magic" behaviors of @@ -150,6 +151,18 @@ class NamespaceInfo { } /** + * @param LinkTarget $target + * @return LinkTarget Talk page for $target + * @throws MWException if $target's namespace doesn't have talk pages (e.g., NS_SPECIAL) + */ + public function getTalkPage( LinkTarget $target ) : LinkTarget { + if ( $this->isTalk( $target->getNamespace() ) ) { + return $target; + } + return new TitleValue( $this->getTalk( $target->getNamespace() ), $target->getDbKey() ); + } + + /** * Get the subject namespace index for a given namespace * Special namespaces (NS_MEDIA, NS_SPECIAL) are always the subject. * @@ -168,12 +181,24 @@ class NamespaceInfo { } /** + * @param LinkTarget $target + * @return LinkTarget Subject page for $target + */ + public function getSubjectPage( LinkTarget $target ) : LinkTarget { + if ( $this->isSubject( $target->getNamespace() ) ) { + return $target; + } + return new TitleValue( $this->getSubject( $target->getNamespace() ), $target->getDbKey() ); + } + + /** * Get the associated namespace. * For talk namespaces, returns the subject (non-talk) namespace * For subject (non-talk) namespaces, returns the talk namespace * * @param int $index Namespace index * @return int + * @throws MWException if called on a namespace that has no talk pages (e.g., NS_SPECIAL) */ public function getAssociated( $index ) { $this->isMethodValidFor( $index, __METHOD__ ); @@ -185,6 +210,17 @@ class NamespaceInfo { } /** + * @param LinkTarget $target + * @return LinkTarget Talk page for $target if it's a subject page, subject page if it's a talk + * page + * @throws MWException if $target's namespace doesn't have talk pages (e.g., NS_SPECIAL) + */ + public function getAssociatedPage( LinkTarget $target ) : LinkTarget { + return new TitleValue( + $this->getAssociated( $target->getNamespace() ), $target->getDbKey() ); + } + + /** * Returns whether the specified namespace exists * * @param int $index |