/**
* 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' );
}
private function getConfigSettings( $context ) {
$conf = $this->getConfig();
- // We can't use Title::newMainPage() if 'mainpage' is in
- // $wgForceUIMsgAsContentMsg because that will try to use the session
- // user's language and we have no session user. This does the
- // equivalent but falling back to our ResourceLoaderContext language
- // instead.
- $mainPage = Title::newFromText( $context->msg( 'mainpage' )->inContentLanguage()->text() );
- if ( !$mainPage ) {
- $mainPage = Title::newFromText( 'Main Page' );
- }
+ // Passing a context is important as Title::newMainPage() may otherwise
+ // try to intialise a session, which is not allowed on load.php requests.
+ $mainPage = Title::newMainPage( $context );
/**
* Namespace related preparation
use MediaWiki\Linker\LinkTarget;
use MediaWiki\MediaWikiServices;
+use Wikimedia\TestingAccessWrapper;
/**
* @group Database
$this->setContentLang( 'en' );
}
+ protected function tearDown() {
+ // For testNewMainPage
+ MessageCache::destroyInstance();
+ parent::tearDown();
+ }
+
/**
* @covers Title::legalChars
*/
$firstValue->equals( $secondValue )
);
}
+
+ /**
+ * @covers Title::newMainPage
+ */
+ public function testNewMainPage() {
+ $msgCache = TestingAccessWrapper::newFromClass( MessageCache::class );
+ $msgCache->instance = $this->createMock( MessageCache::class );
+ $msgCache->instance->method( 'get' )->willReturn( 'Foresheet' );
+ $msgCache->instance->method( 'transform' )->willReturn( 'Foresheet' );
+
+ $this->assertSame(
+ 'Foresheet',
+ Title::newMainPage()->getText()
+ );
+ }
+
+ /**
+ * @covers Title::newMainPage
+ */
+ public function testNewMainPageWithLocal() {
+ $local = $this->createMock( MessageLocalizer::class );
+ $local->method( 'msg' )->willReturn( new RawMessage( 'Prime Article' ) );
+
+ $this->assertSame(
+ 'Prime Article',
+ Title::newMainPage( $local )->getText()
+ );
+ }
}