From 1b40b69481e73169b48ec4f0969460daa1cfc0b4 Mon Sep 17 00:00:00 2001 From: Ed Sanders Date: Wed, 22 Jul 2020 23:03:44 +0100 Subject: [PATCH] Make default snippet length (150) a constant Change-Id: Idb53b226618fa0d625c7e4713c780538bd6a76ea --- includes/DiscussionParser.php | 22 ++++++++++++------- .../formatters/MentionPresentationModel.php | 2 +- .../formatters/PresentationModelSection.php | 2 +- tests/phpunit/DiscussionParserTest.php | 2 +- 4 files changed, 17 insertions(+), 11 deletions(-) diff --git a/includes/DiscussionParser.php b/includes/DiscussionParser.php index 1c2f7a7ed..bda507ff1 100644 --- a/includes/DiscussionParser.php +++ b/includes/DiscussionParser.php @@ -7,6 +7,8 @@ use MediaWiki\Revision\SlotRecord; abstract class EchoDiscussionParser { const HEADER_REGEX = '^(==+)\h*([^=].*)\h*\1$'; + const DEFAULT_SNIPPET_LENGTH = 150; + protected static $timestampRegex; protected static $revisionInterpretationCache = []; protected static $diffParser; @@ -161,7 +163,7 @@ abstract class EchoDiscussionParser { $snippet = self::getTextSnippet( self::stripSignature( self::stripHeader( $action['content'] ), $title ), RequestContext::getMain()->getLanguage(), - 150, + self::DEFAULT_SNIPPET_LENGTH, $title ); break; case 'new-section-with-comment': @@ -169,7 +171,7 @@ abstract class EchoDiscussionParser { $snippet = self::getTextSnippet( self::stripSignature( self::stripHeader( $action['content'] ), $title ), RequestContext::getMain()->getLanguage(), - 150, + self::DEFAULT_SNIPPET_LENGTH, $title ); break; } @@ -1180,11 +1182,13 @@ abstract class EchoDiscussionParser { * Parse wikitext into truncated plain text. * @param string $text * @param Language $lang - * @param int $length Length in characters (not bytes); default 150 + * @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 * @return string */ - public static function getTextSnippet( $text, Language $lang, $length = 150, $title = null ) { + public static function getTextSnippet( + $text, Language $lang, $length = self::DEFAULT_SNIPPET_LENGTH, $title = null + ) { // Parse wikitext $html = MediaWikiServices::getInstance()->getMessageCache()->parse( $text, $title )->getText( [ 'enableSectionEditLinks' => false @@ -1197,10 +1201,10 @@ abstract class EchoDiscussionParser { * Parse an edit summary into truncated plain text. * @param string $text * @param Language $lang - * @param int $length Length in characters (not bytes); default 150 + * @param int $length Length in characters (not bytes); default DEFAULT_SNIPPET_LENGTH * @return string */ - public static function getTextSnippetFromSummary( $text, Language $lang, $length = 150 ) { + public static function getTextSnippetFromSummary( $text, Language $lang, $length = self::DEFAULT_SNIPPET_LENGTH ) { // Parse wikitext with summary parser $html = Linker::formatLinksInComment( Sanitizer::escapeHtmlAllowEntities( $text ) ); $plaintext = trim( Sanitizer::stripAllTags( $html ) ); @@ -1212,10 +1216,12 @@ abstract class EchoDiscussionParser { * * @param RevisionRecord $revision * @param Language $lang - * @param int $length Length in characters (not bytes); default 150 + * @param int $length Length in characters (not bytes); default DEFAULT_SNIPPET_LENGTH * @return string */ - public static function getEditExcerpt( RevisionRecord $revision, Language $lang, $length = 150 ) { + public static function getEditExcerpt( + RevisionRecord $revision, Language $lang, $length = self::DEFAULT_SNIPPET_LENGTH + ) { $interpretation = self::getChangeInterpretationForRevision( $revision ); $section = self::detectSectionTitleAndText( $interpretation ); return $lang->truncateForVisual( $section['section-title'] . ' ' . $section['section-text'], $length ); diff --git a/includes/formatters/MentionPresentationModel.php b/includes/formatters/MentionPresentationModel.php index c407b3503..740619594 100644 --- a/includes/formatters/MentionPresentationModel.php +++ b/includes/formatters/MentionPresentationModel.php @@ -79,7 +79,7 @@ class EchoMentionPresentationModel extends EchoEventPresentationModel { EchoDiscussionParser::getTextSnippet( $content, $this->language, - 150, + EchoDiscussionParser::DEFAULT_SNIPPET_LENGTH, $this->event->getTitle() ) ); diff --git a/includes/formatters/PresentationModelSection.php b/includes/formatters/PresentationModelSection.php index 0a93bc530..f6376ff10 100644 --- a/includes/formatters/PresentationModelSection.php +++ b/includes/formatters/PresentationModelSection.php @@ -82,7 +82,7 @@ class EchoPresentationModelSection { $this->parsedSectionTitle = EchoDiscussionParser::getTextSnippet( $rawSectionTitle, $this->language, - 150, + EchoDiscussionParser::DEFAULT_SNIPPET_LENGTH, $this->event->getTitle() ); return $this->parsedSectionTitle; diff --git a/tests/phpunit/DiscussionParserTest.php b/tests/phpunit/DiscussionParserTest.php index a161cd07a..0b29da25a 100644 --- a/tests/phpunit/DiscussionParserTest.php +++ b/tests/phpunit/DiscussionParserTest.php @@ -1795,7 +1795,7 @@ TEXT EchoDiscussionParser::getTextSnippet( '[[:{{BASEPAGENAME}}]]', Language::factory( 'en' ), - 150, + EchoDiscussionParser::DEFAULT_SNIPPET_LENGTH, Title::newFromText( 'Page001' ) ) );