From 7271ac0dcd001b514dbd9753a3d5a461fc527a9d Mon Sep 17 00:00:00 2001 From: daniel Date: Mon, 3 Jun 2019 23:29:55 +0200 Subject: [PATCH] Tolerate invalid titles in some ChangesFeed and LogFormatter code Bug: T224811 Change-Id: If134e20cc14d80f9186611606df0b860889bd2cf --- includes/changes/ChangesFeed.php | 2 +- includes/logging/BlockLogFormatter.php | 2 +- .../logging/BlockLogFormatterTest.php | 24 +++++++++++++++++++ .../includes/logging/LogFormatterTestCase.php | 17 +++++++++++++ 4 files changed, 43 insertions(+), 2 deletions(-) diff --git a/includes/changes/ChangesFeed.php b/includes/changes/ChangesFeed.php index bb9114aa62..69c709c258 100644 --- a/includes/changes/ChangesFeed.php +++ b/includes/changes/ChangesFeed.php @@ -93,7 +93,7 @@ class ChangesFeed { $nsInfo = MediaWikiServices::getInstance()->getNamespaceInfo(); foreach ( $sorted as $obj ) { $title = Title::makeTitle( $obj->rc_namespace, $obj->rc_title ); - $talkpage = $nsInfo->hasTalkNamespace( $obj->rc_namespace ) + $talkpage = $nsInfo->hasTalkNamespace( $obj->rc_namespace ) && $title->isValid() ? $title->getTalkPage()->getFullURL() : ''; diff --git a/includes/logging/BlockLogFormatter.php b/includes/logging/BlockLogFormatter.php index ddecf9ead5..ead290f062 100644 --- a/includes/logging/BlockLogFormatter.php +++ b/includes/logging/BlockLogFormatter.php @@ -127,7 +127,7 @@ class BlockLogFormatter extends LogFormatter { public function getPreloadTitles() { $title = $this->entry->getTarget(); // Preload user page for non-autoblocks - if ( substr( $title->getText(), 0, 1 ) !== '#' ) { + if ( substr( $title->getText(), 0, 1 ) !== '#' && $title->isValid() ) { return [ $title->getTalkPage() ]; } return []; diff --git a/tests/phpunit/includes/logging/BlockLogFormatterTest.php b/tests/phpunit/includes/logging/BlockLogFormatterTest.php index bc0ca2ad85..b6f8f9cc37 100644 --- a/tests/phpunit/includes/logging/BlockLogFormatterTest.php +++ b/tests/phpunit/includes/logging/BlockLogFormatterTest.php @@ -34,6 +34,30 @@ class BlockLogFormatterTest extends LogFormatterTestCase { 'duration' => 'infinite', 'flags' => [ 'anononly' ], ], + 'preload' => [ new TitleValue( NS_USER_TALK, 'Logtestuser' ) ], + ], + ], + + // With blank page title (T224811) + [ + [ + 'type' => 'block', + 'action' => 'block', + 'comment' => 'Block comment', + 'user' => 0, + 'user_text' => 'Sysop', + 'namespace' => NS_USER, + 'title' => '', + 'params' => [], + ], + [ + 'text' => 'Sysop blocked (no username available) ' + . 'with an expiration time of indefinite', + 'api' => [ + 'duration' => 'infinite', + 'flags' => [], + ], + 'preload' => [], ], ], diff --git a/tests/phpunit/includes/logging/LogFormatterTestCase.php b/tests/phpunit/includes/logging/LogFormatterTestCase.php index 883af71240..fc2ab916cb 100644 --- a/tests/phpunit/includes/logging/LogFormatterTestCase.php +++ b/tests/phpunit/includes/logging/LogFormatterTestCase.php @@ -1,4 +1,5 @@ formatParametersForApi() ), 'Api log params is equal to expected array' ); + + if ( isset( $extra['preload'] ) ) { + $this->assertArrayEquals( + $this->getLinkTargetsAsStrings( $extra['preload'] ), + $this->getLinkTargetsAsStrings( + $formatter->getPreloadTitles() + ) + ); + } + } + + private function getLinkTargetsAsStrings( array $linkTargets ) { + return array_map( function ( LinkTarget $t ) { + return $t->getInterwiki() . ':' . $t->getNamespace() . ':' + . $t->getDBkey() . '#' . $t->getFragment(); + }, $linkTargets ); } protected function isLegacy( $extra ) { -- 2.20.1