From 3493317d79c62adb2f2e38f1f3c2ee1dcbb46805 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartosz=20Dziewo=C5=84ski?= Date: Thu, 20 Jan 2022 23:52:01 +0100 Subject: [PATCH] Parse section titles in notifs as if they weren't at the start of line MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The section titles are wikitext extracted from inside heading markup like `== … ==`, so start-of-line markup like `*` should not be parsed. Bug: T299572 Change-Id: Ie3995b943e5fe20ad86041d6be755f14f32eb01e --- includes/DiscussionParser.php | 5 +++-- .../formatters/PresentationModelSection.php | 5 ++++- tests/phpunit/DiscussionParserTest.php | 20 +++++++++++++++++++ 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/includes/DiscussionParser.php b/includes/DiscussionParser.php index a8f1a54ee..86bbd46fe 100644 --- a/includes/DiscussionParser.php +++ b/includes/DiscussionParser.php @@ -1239,13 +1239,14 @@ abstract class EchoDiscussionParser { * @param Language $lang * @param int $length Length in characters (not bytes); default DEFAULT_SNIPPET_LENGTH * @param Title|null $title Page from which the text snippet is being extracted + * @param bool $linestart Whether or not this is at the start of a line * @return string */ public static function getTextSnippet( - $text, Language $lang, $length = self::DEFAULT_SNIPPET_LENGTH, $title = null + $text, Language $lang, $length = self::DEFAULT_SNIPPET_LENGTH, $title = null, $linestart = true ) { // Parse wikitext - $html = MediaWikiServices::getInstance()->getMessageCache()->parse( $text, $title )->getText( [ + $html = MediaWikiServices::getInstance()->getMessageCache()->parse( $text, $title, $linestart )->getText( [ 'enableSectionEditLinks' => false ] ); $plaintext = trim( Sanitizer::stripAllTags( $html ) ); diff --git a/includes/formatters/PresentationModelSection.php b/includes/formatters/PresentationModelSection.php index 94ca5948e..4d55c001c 100644 --- a/includes/formatters/PresentationModelSection.php +++ b/includes/formatters/PresentationModelSection.php @@ -83,7 +83,10 @@ class EchoPresentationModelSection { $rawSectionTitle, $this->language, EchoDiscussionParser::DEFAULT_SNIPPET_LENGTH, - $this->event->getTitle() + $this->event->getTitle(), + // linestart=false, because this wikitext was inside a heading like `== … ==`, + // so start-of-line markup like `*` should not be parsed (T299572) + false ); return $this->parsedSectionTitle; } diff --git a/tests/phpunit/DiscussionParserTest.php b/tests/phpunit/DiscussionParserTest.php index 97c4dced5..a2076ee93 100644 --- a/tests/phpunit/DiscussionParserTest.php +++ b/tests/phpunit/DiscussionParserTest.php @@ -1816,5 +1816,25 @@ TEXT Title::newFromText( 'Page001' ) ) ); + $this->assertEquals( + 'Hello', + EchoDiscussionParser::getTextSnippet( + '* Hello', + Language::factory( 'en' ), + EchoDiscussionParser::DEFAULT_SNIPPET_LENGTH, + null, + true + ) + ); + $this->assertEquals( + '* Hello', + EchoDiscussionParser::getTextSnippet( + '* Hello', + Language::factory( 'en' ), + EchoDiscussionParser::DEFAULT_SNIPPET_LENGTH, + null, + false + ) + ); } }