specials: Avoid the use of global variables in Special:Version
[lhc/web/wiklou.git] / includes / Title.php
index 3891c82..27baeb2 100644 (file)
@@ -246,6 +246,8 @@ class Title implements LinkTarget, IDBAccessObject {
         * unless $forceClone is "clone". If $forceClone is "clone" and the given TitleValue
         * is already a Title instance, that instance is copied using the clone operator.
         *
+        * @deprecated since 1.34, use newFromLinkTarget or castFromLinkTarget
+        *
         * @param TitleValue $titleValue Assumed to be safe.
         * @param string $forceClone set to NEW_CLONE to ensure a fresh instance is returned.
         *
@@ -283,6 +285,17 @@ class Title implements LinkTarget, IDBAccessObject {
                );
        }
 
+       /**
+        * Same as newFromLinkTarget, but if passed null, returns null.
+        *
+        * @param LinkTarget|null $linkTarget Assumed to be safe (if not null).
+        *
+        * @return Title|null
+        */
+       public static function castFromLinkTarget( $linkTarget ) {
+               return $linkTarget ? self::newFromLinkTarget( $linkTarget ) : null;
+       }
+
        /**
         * Create a new Title from text, such as what one would find in a link. De-
         * codes any HTML entities in the text.
@@ -623,15 +636,33 @@ class Title implements LinkTarget, IDBAccessObject {
        /**
         * Create a new Title for the Main Page
         *
+        * This uses the 'mainpage' interface message, which could be specified in
+        * `$wgForceUIMsgAsContentMsg`. If that is the case, then calling this method
+        * will use the user language, which would involve initialising the session
+        * via `RequestContext::getMain()->getLanguage()`. For session-less endpoints,
+        * be sure to pass in a MessageLocalizer (such as your own RequestContext,
+        * or ResourceloaderContext) to prevent an error.
+        *
         * @note The Title instance returned by this method is not guaranteed to be a fresh instance.
         * It may instead be a cached instance created previously, with references to it remaining
         * elsewhere.
         *
-        * @return Title The new object
+        * @param MessageLocalizer|null $localizer An optional context to use (since 1.34)
+        * @return Title
         */
-       public static function newMainPage() {
-               $title = self::newFromText( wfMessage( 'mainpage' )->inContentLanguage()->text() );
-               // Don't give fatal errors if the message is broken
+       public static function newMainPage( MessageLocalizer $localizer = null ) {
+               if ( $localizer ) {
+                       $msg = $localizer->msg( 'mainpage' );
+               } else {
+                       $msg = wfMessage( 'mainpage' );
+               }
+
+               $title = self::newFromText( $msg->inContentLanguage()->text() );
+
+               // Every page renders at least one link to the Main Page (e.g. sidebar).
+               // If the localised value is invalid, don't produce fatal errors that
+               // would make the wiki inaccessible (and hard to fix the invalid message).
+               // Gracefully fallback...
                if ( !$title ) {
                        $title = self::newFromText( 'Main Page' );
                }