Add SpecialPage::getTitleValueFor()
authorKunal Mehta <legoktm@member.fsf.org>
Tue, 9 Aug 2016 22:27:29 +0000 (15:27 -0700)
committerKunal Mehta <legoktm@member.fsf.org>
Thu, 11 Aug 2016 02:51:21 +0000 (19:51 -0700)
One of the most frequent instances of Title being used where TitleValue
could be used instead is for special pages, since there is no
convenience function that returns a TitleValue object. Hopefully this
will make it easier.

Because TitleValue is stricer about validation, we need to ensure that
$subpage is in dbkey form.

Change-Id: I50457620e25dfe03eb44d6fda5aabebcbf314863

includes/specialpage/SpecialPage.php
includes/specialpage/SpecialPageFactory.php

index 35ecc6e..44b8fff 100644 (file)
@@ -67,6 +67,8 @@ class SpecialPage {
 
        /**
         * Get a localised Title object for a specified special page name
+        * If you don't need a full Title object, consider using TitleValue through
+        * getTitleValueFor() below.
         *
         * @since 1.9
         * @since 1.21 $fragment parameter added
@@ -78,9 +80,24 @@ class SpecialPage {
         * @throws MWException
         */
        public static function getTitleFor( $name, $subpage = false, $fragment = '' ) {
+               return Title::newFromTitleValue(
+                       self::getTitleValueFor( $name, $subpage, $fragment )
+               );
+       }
+
+       /**
+        * Get a localised TitleValue object for a specified special page name
+        *
+        * @since 1.28
+        * @param string $name
+        * @param string|bool $subpage Subpage string, or false to not use a subpage
+        * @param string $fragment The link fragment (after the "#")
+        * @return TitleValue
+        */
+       public static function getTitleValueFor( $name, $subpage = false, $fragment = '' ) {
                $name = SpecialPageFactory::getLocalNameFor( $name, $subpage );
 
-               return Title::makeTitle( NS_SPECIAL, $name, $fragment );
+               return new TitleValue( NS_SPECIAL, $name, $fragment );
        }
 
        /**
index b69b28a..68c817c 100644 (file)
@@ -714,6 +714,8 @@ class SpecialPageFactory {
                }
 
                if ( $subpage !== false && !is_null( $subpage ) ) {
+                       // Make sure it's in dbkey form
+                       $subpage = str_replace( ' ', '_', $subpage );
                        $name = "$name/$subpage";
                }