diff options
-rw-r--r-- | RELEASE-NOTES-1.37 | 2 | ||||
-rw-r--r-- | includes/api/ApiHelpParamValueMessage.php | 2 | ||||
-rw-r--r-- | includes/cache/MessageCache.php | 25 | ||||
-rw-r--r-- | includes/language/Message.php | 45 | ||||
-rw-r--r-- | tests/phpunit/includes/MessageTest.php | 7 | ||||
-rw-r--r-- | tests/phpunit/includes/api/ApiMessageTest.php | 14 |
6 files changed, 64 insertions, 31 deletions
diff --git a/RELEASE-NOTES-1.37 b/RELEASE-NOTES-1.37 index 3815e2e5cea3..8dc12d866e4a 100644 --- a/RELEASE-NOTES-1.37 +++ b/RELEASE-NOTES-1.37 @@ -398,7 +398,7 @@ because of Phabricator reports. override getSizeAndMetadata(). * Deprecated File::getMetadata(). Instead use ::getMetadataArray(), ::getMetadataItem() and ::getMetadataItems(). - +* Message::title was deprecated, use Message::page instead. * … === Other changes in 1.37 === diff --git a/includes/api/ApiHelpParamValueMessage.php b/includes/api/ApiHelpParamValueMessage.php index 01975a61f124..5fa988fb5e21 100644 --- a/includes/api/ApiHelpParamValueMessage.php +++ b/includes/api/ApiHelpParamValueMessage.php @@ -118,7 +118,7 @@ class ApiHelpParamValueMessage extends Message { $msg->interface = $this->interface; $msg->language = $this->language; $msg->useDatabase = $this->useDatabase; - $msg->title = $this->title; + $msg->contextPage = $this->contextPage; return $msg->fetchMessage(); } diff --git a/includes/cache/MessageCache.php b/includes/cache/MessageCache.php index 64dc429cc733..4a0e3f0a5f67 100644 --- a/includes/cache/MessageCache.php +++ b/includes/cache/MessageCache.php @@ -29,6 +29,8 @@ use MediaWiki\Languages\LanguageNameUtils; use MediaWiki\Linker\LinkTarget; use MediaWiki\Logger\LoggerFactory; use MediaWiki\MediaWikiServices; +use MediaWiki\Page\PageReference; +use MediaWiki\Page\PageReferenceValue; use MediaWiki\Revision\SlotRecord; use Psr\Log\LoggerAwareInterface; use Psr\Log\LoggerInterface; @@ -1234,10 +1236,10 @@ class MessageCache implements LoggerAwareInterface { * @param string $message * @param bool $interface * @param Language|null $language - * @param Title|null $title + * @param PageReference|null $page * @return string */ - public function transform( $message, $interface = false, $language = null, $title = null ) { + public function transform( $message, $interface = false, $language = null, PageReference $page = null ) { // Avoid creating parser if nothing to transform if ( strpos( $message, '{{' ) === false ) { return $message; @@ -1255,7 +1257,7 @@ class MessageCache implements LoggerAwareInterface { $userlang = $popts->setUserLang( $language ); $this->mInParser = true; - $message = $parser->transformMsg( $message, $popts, $title ); + $message = $parser->transformMsg( $message, $popts, $page ); $this->mInParser = false; $popts->setUserLang( $userlang ); } @@ -1278,13 +1280,13 @@ class MessageCache implements LoggerAwareInterface { /** * @param string $text - * @param Title|null $title + * @param PageReference|null $page * @param bool $linestart Whether or not this is at the start of a line * @param bool $interface Whether this is an interface message * @param Language|string|null $language Language code * @return ParserOutput|string */ - public function parse( $text, $title = null, $linestart = true, + public function parse( $text, PageReference $page = null, $linestart = true, $interface = false, $language = null ) { global $wgTitle; @@ -1302,23 +1304,26 @@ class MessageCache implements LoggerAwareInterface { } $popts->setTargetLanguage( $language ); - if ( !$title || !$title instanceof Title ) { + if ( !$page ) { $logger = LoggerFactory::getInstance( 'GlobalTitleFail' ); $logger->info( __METHOD__ . ' called with no title set.', [ 'exception' => new Exception ] ); - $title = $wgTitle; + $page = $wgTitle; } // Sometimes $wgTitle isn't set either... - if ( !$title ) { + if ( !$page ) { # It's not uncommon having a null $wgTitle in scripts. See r80898 # Create a ghost title in such case - $title = Title::makeTitle( NS_SPECIAL, 'Badtitle/title not set in ' . __METHOD__ ); + $page = PageReferenceValue::localReference( + NS_SPECIAL, + 'Badtitle/title not set in ' . __METHOD__ + ); } $this->mInParser = true; - $res = $parser->parse( $text, $title, $popts, $linestart ); + $res = $parser->parse( $text, $page, $popts, $linestart ); $this->mInParser = false; return $res; diff --git a/includes/language/Message.php b/includes/language/Message.php index 8988cc18299e..7403f854043b 100644 --- a/includes/language/Message.php +++ b/includes/language/Message.php @@ -21,6 +21,8 @@ use MediaWiki\Logger\LoggerFactory; use MediaWiki\MediaWikiServices; +use MediaWiki\Page\PageReference; +use MediaWiki\Page\PageReferenceValue; /** * The Message class deals with fetching and processing of interface message @@ -199,9 +201,9 @@ class Message implements MessageSpecifier, Serializable { protected $useDatabase = true; /** - * @var Title Title object to use as context. + * @var ?PageReference page object to use as context. */ - protected $title = null; + protected $contextPage = null; /** * @var Content Content object representing the message. @@ -269,8 +271,8 @@ class Message implements MessageSpecifier, Serializable { // Optimisation: Avoid cost of TitleFormatter on serialize, // and especially cost of TitleParser (via Title::newFromText) // on retrieval. - 'titlevalue' => ( $this->title - ? [ 0 => $this->title->getNamespace(), 1 => $this->title->getDBkey() ] + 'titlevalue' => ( $this->contextPage + ? [ 0 => $this->contextPage->getNamespace(), 1 => $this->contextPage->getDBkey() ] : null ), ] ); @@ -300,11 +302,16 @@ class Message implements MessageSpecifier, Serializable { // Since 1.35, the key 'titlevalue' is set, instead of 'titlestr'. if ( isset( $data['titlevalue'] ) ) { - $this->title = Title::makeTitle( $data['titlevalue'][0], $data['titlevalue'][1] ); + $this->contextPage = new PageReferenceValue( + $data['titlevalue'][0], + $data['titlevalue'][1], + PageReference::LOCAL + ); } elseif ( isset( $data['titlestr'] ) ) { - $this->title = Title::newFromText( $data['titlestr'] ); + // TODO: figure out what's needed to remove this codepath + $this->contextPage = Title::newFromText( $data['titlestr'] ); } else { - $this->title = null; // Explicit for sanity + $this->contextPage = null; // Explicit for sanity } } @@ -748,7 +755,7 @@ class Message implements MessageSpecifier, Serializable { */ public function setContext( IContextSource $context ) { $this->inLanguage( $context->getLanguage() ); - $this->title( $context->getTitle() ); + $this->page( $context->getTitle() ); $this->interface = true; return $this; @@ -845,13 +852,27 @@ class Message implements MessageSpecifier, Serializable { * Set the Title object to use as context when transforming the message * * @since 1.18 + * @deprecated since 1.37. Use ::page instead * * @param Title $title * * @return Message $this */ public function title( $title ) { - $this->title = $title; + return $this->page( $title ); + } + + /** + * Set the page object to use as context when transforming the message + * + * @since 1.37 + * + * @param ?PageReference $page + * + * @return Message $this + */ + public function page( ?PageReference $page ) { + $this->contextPage = $page; return $this; } @@ -1296,7 +1317,7 @@ class Message implements MessageSpecifier, Serializable { $msg->interface = $this->interface; $msg->language = $this->language; $msg->useDatabase = $this->useDatabase; - $msg->title = $this->title; + $msg->contextPage = $this->contextPage; // DWIM if ( $format === 'block-parse' ) { @@ -1325,7 +1346,7 @@ class Message implements MessageSpecifier, Serializable { protected function parseText( $string ) { $out = MediaWikiServices::getInstance()->getMessageCache()->parse( $string, - $this->title, + $this->contextPage, /*linestart*/true, $this->interface, $this->getLanguage() @@ -1357,7 +1378,7 @@ class Message implements MessageSpecifier, Serializable { $string, $this->interface, $this->getLanguage(), - $this->title + $this->contextPage ); } diff --git a/tests/phpunit/includes/MessageTest.php b/tests/phpunit/includes/MessageTest.php index 758cf167f343..d6dfccce590c 100644 --- a/tests/phpunit/includes/MessageTest.php +++ b/tests/phpunit/includes/MessageTest.php @@ -1,6 +1,7 @@ <?php use MediaWiki\MediaWikiServices; +use MediaWiki\Page\PageReference; use Wikimedia\TestingAccessWrapper; /** @@ -867,9 +868,9 @@ class MessageTest extends MediaWikiLangTestCase { $this->assertSame( '(<a>foo</a>)', $msg->parse(), 'Sanity check' ); $msg = unserialize( serialize( $msg ) ); $this->assertSame( '(<a>foo</a>)', $msg->parse() ); - $title = TestingAccessWrapper::newFromObject( $msg )->title; - $this->assertInstanceOf( Title::class, $title ); - $this->assertSame( 'Testing', $title->getFullText() ); + $title = TestingAccessWrapper::newFromObject( $msg )->contextPage; + $this->assertInstanceOf( PageReference::class, $title ); + $this->assertSame( 'Testing', $title->getDbKey() ); $msg = new Message( 'mainpage' ); $msg->inLanguage( 'de' ); diff --git a/tests/phpunit/includes/api/ApiMessageTest.php b/tests/phpunit/includes/api/ApiMessageTest.php index fafcd92b815d..227313be5606 100644 --- a/tests/phpunit/includes/api/ApiMessageTest.php +++ b/tests/phpunit/includes/api/ApiMessageTest.php @@ -1,5 +1,6 @@ <?php +use MediaWiki\Page\PageReferenceValue; use Wikimedia\TestingAccessWrapper; /** @@ -19,8 +20,8 @@ class ApiMessageTest extends MediaWikiIntegrationTestCase { $this->assertSame( $msg->useDatabase, $msg2->useDatabase, 'useDatabase' ); $this->assertSame( $msg->format, $msg2->format, 'format' ); $this->assertSame( - $msg->title ? $msg->title->getFullText() : null, - $msg2->title ? $msg2->title->getFullText() : null, + $msg->contextPage ? "{$msg->contextPage->getNamespace()}:{$msg->contextPage->getDbKey()}" : null, + $msg2->contextPage ? "{$msg->contextPage->getNamespace()}:{$msg->contextPage->getDbKey()}" : null, 'title' ); } @@ -81,7 +82,10 @@ class ApiMessageTest extends MediaWikiIntegrationTestCase { */ public function testApiMessage() { $msg = new Message( [ 'foo', 'bar' ], [ 'baz' ] ); - $msg->inLanguage( 'de' )->title( Title::newMainPage() ); + $msg->inLanguage( 'de' ) + ->page( + PageReferenceValue::localReference( NS_MAIN, 'Main_Page' ) + ); $msg2 = new ApiMessage( $msg, 'code', [ 'data' ] ); $this->compareMessages( $msg, $msg2 ); $this->assertEquals( 'code', $msg2->getApiCode() ); @@ -120,7 +124,9 @@ class ApiMessageTest extends MediaWikiIntegrationTestCase { */ public function testApiRawMessage() { $msg = new RawMessage( 'foo', [ 'baz' ] ); - $msg->inLanguage( 'de' )->title( Title::newMainPage() ); + $msg->inLanguage( 'de' )->page( + PageReferenceValue::localReference( NS_MAIN, 'Main_Page' ) + ); $msg2 = new ApiRawMessage( $msg, 'code', [ 'data' ] ); $this->compareMessages( $msg, $msg2 ); $this->assertEquals( 'code', $msg2->getApiCode() ); |