Move Title::getSubject/Talk/OtherPage to NamespaceInfo
[lhc/web/wiklou.git] / includes / title / NamespaceInfo.php
index 4a7545a..7cfadc0 100644 (file)
@@ -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
@@ -104,8 +105,8 @@ class NamespaceInfo {
         * @return bool
         */
        public function isMovable( $index ) {
-               $result = !( $index < NS_MAIN ||
-                       ( $index == NS_FILE && !$this->options->get( 'AllowImageMoving' ) ) );
+               $result = $index >= NS_MAIN &&
+                       ( $index != NS_FILE || $this->options->get( 'AllowImageMoving' ) );
 
                /**
                 * @since 1.20
@@ -149,6 +150,18 @@ class NamespaceInfo {
                        : $index + 1;
        }
 
+       /**
+        * @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.
@@ -167,24 +180,44 @@ class NamespaceInfo {
                        : $index;
        }
 
+       /**
+        * @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|null If no associated namespace could be found
+        * @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__ );
 
                if ( $this->isSubject( $index ) ) {
                        return $this->getTalk( $index );
-               } elseif ( $this->isTalk( $index ) ) {
-                       return $this->getSubject( $index );
-               } else {
-                       return null;
                }
+               return $this->getSubject( $index );
+       }
+
+       /**
+        * @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() );
        }
 
        /**
@@ -266,7 +299,7 @@ class NamespaceInfo {
         * The input *must* be converted to lower case first
         *
         * @param string $name Namespace name
-        * @return int
+        * @return int|null
         */
        public function getCanonicalIndex( $name ) {
                if ( $this->namespaceIndexes === false ) {
@@ -283,8 +316,8 @@ class NamespaceInfo {
        }
 
        /**
-        * Returns an array of the namespaces (by integer id) that exist on the
-        * wiki. Used primarily by the api in help documentation.
+        * Returns an array of the namespaces (by integer id) that exist on the wiki. Used primarily by
+        * the API in help documentation. The array is sorted numerically and omits negative namespaces.
         * @return array
         */
        public function getValidNamespaces() {
@@ -464,6 +497,9 @@ class NamespaceInfo {
         * Determine which restriction levels it makes sense to use in a namespace,
         * optionally filtered by a user's rights.
         *
+        * @todo Move this to PermissionManager and remove the dependency here on permissions-related
+        * config settings.
+        *
         * @param int $index Index to check
         * @param User|null $user User to check
         * @return array