From 190377dbbc83afda06729bd1a9f5560b1109a20e Mon Sep 17 00:00:00 2001 From: "C. Scott Ananian" Date: Fri, 15 Jun 2018 16:01:16 -0400 Subject: [PATCH] Replace uses of deprecated Language::truncate() See I2291c69d9df17c1a9e4ab1b7d4cbc73bc51d3ebb for the anticipated hard-deprecation of this method in core. Bug: T197492 Change-Id: I4687db09c27480147cfa7a648a886b1670812deb --- includes/DiscussionParser.php | 21 +++++++++-------- includes/EchoHooks.php | 2 +- .../formatters/EventPresentationModel.php | 23 +++++++++++-------- .../PresentationModelSectionTrait.php | 2 +- 4 files changed, 28 insertions(+), 20 deletions(-) diff --git a/includes/DiscussionParser.php b/includes/DiscussionParser.php index 3a61f0443..0fce4515e 100644 --- a/includes/DiscussionParser.php +++ b/includes/DiscussionParser.php @@ -820,11 +820,14 @@ abstract class EchoDiscussionParser { return substr( $text, 0, $timestampPos ); } - // Use truncate() instead of truncateHTML() because truncateHTML() - // would not strip signature if the text contains < or & + // Use truncateForDatabase() instead of truncateHTML() because + // truncateHTML() would not strip signature if the text contains + // < or &. (And we can't use truncateForVisual() because + // self::getUserFromLine() returns byte offsets, not character + // offsets.) global $wgContLang; - return $wgContLang->truncate( $text, $output[0], '' ); + return $wgContLang->truncateForDatabase( $text, $output[0], '' ); } /** @@ -1189,7 +1192,7 @@ abstract class EchoDiscussionParser { * Parse wikitext into truncated plain text. * @param string $text * @param Language $lang - * @param int $length default 150 + * @param int $length Length in characters (not bytes); default 150 * @param Title|null $title Page from which the text snippet is being extracted * @return string */ @@ -1199,21 +1202,21 @@ abstract class EchoDiscussionParser { 'enableSectionEditLinks' => false ] ); $plaintext = trim( Sanitizer::stripAllTags( $html ) ); - return $lang->truncate( $plaintext, $length ); + return $lang->truncateForVisual( $plaintext, $length ); } /** * Parse an edit summary into truncated plain text. * @param string $text * @param Language $lang - * @param int $length default 150 + * @param int $length Length in characters (not bytes); default 150 * @return string */ static function getTextSnippetFromSummary( $text, Language $lang, $length = 150 ) { // Parse wikitext with summary parser $html = Linker::formatLinksInComment( Sanitizer::escapeHtmlAllowEntities( $text ) ); $plaintext = trim( Sanitizer::stripAllTags( $html ) ); - return $lang->truncate( $plaintext, $length ); + return $lang->truncateForVisual( $plaintext, $length ); } /** @@ -1221,12 +1224,12 @@ abstract class EchoDiscussionParser { * * @param Revision $revision * @param Language $lang - * @param int $length + * @param int $length Length in characters (not bytes); default 150 * @return string */ public static function getEditExcerpt( Revision $revision, Language $lang, $length = 150 ) { $interpretation = self::getChangeInterpretationForRevision( $revision ); $section = self::detectSectionTitleAndText( $interpretation ); - return $lang->truncate( $section['section-title'] . ' ' . $section['section-text'], $length ); + return $lang->truncateForVisual( $section['section-title'] . ' ' . $section['section-text'], $length ); } } diff --git a/includes/EchoHooks.php b/includes/EchoHooks.php index 2745dde95..9caf5cf4a 100644 --- a/includes/EchoHooks.php +++ b/includes/EchoHooks.php @@ -1382,7 +1382,7 @@ class EchoHooks { if ( $subject === $autoSubject ) { $autoFooter = "\n\n-- \n" . wfMessage( 'emailuserfooter', $from->name, $address->name )->inContentLanguage()->text(); $textWithoutFooter = preg_replace( '/' . preg_quote( $autoFooter, '/' ) . '$/', '', $text ); - $preview = $wgContLang->truncate( $textWithoutFooter, 125 ); + $preview = $wgContLang->truncateForVisual( $textWithoutFooter, 125 ); } else { $preview = $subject; } diff --git a/includes/formatters/EventPresentationModel.php b/includes/formatters/EventPresentationModel.php index 77605d419..148fbb3d8 100644 --- a/includes/formatters/EventPresentationModel.php +++ b/includes/formatters/EventPresentationModel.php @@ -9,27 +9,32 @@ use Wikimedia\Timestamp\TimestampException; abstract class EchoEventPresentationModel implements JsonSerializable { /** - * Recommended length of usernames included in messages + * Recommended length of usernames included in messages, in + * characters (not bytes). */ const USERNAME_RECOMMENDED_LENGTH = 20; /** - * Recommended length of usernames used as link label + * Recommended length of usernames used as link label, in + * characters (not bytes). */ const USERNAME_AS_LABEL_RECOMMENDED_LENGTH = 15; /** - * Recommended length of page names included in messages + * Recommended length of page names included in messages, in + * characters (not bytes). */ const PAGE_NAME_RECOMMENDED_LENGTH = 50; /** - * Recommended length of page names used as link label + * Recommended length of page names used as link label, in + * characters (not bytes). */ const PAGE_NAME_AS_LABEL_RECOMMENDED_LENGTH = 15; /** - * Recommended length of section titles included in messages + * Recommended length of section titles included in messages, in + * characters (not bytes). */ const SECTION_TITLE_RECOMMENDED_LENGTH = 50; @@ -506,7 +511,7 @@ abstract class EchoEventPresentationModel implements JsonSerializable { * @return string */ protected function getTruncatedUsername( User $user ) { - return $this->language->embedBidi( $this->language->truncate( $user->getName(), self::USERNAME_RECOMMENDED_LENGTH, '...', false ) ); + return $this->language->embedBidi( $this->language->truncateForVisual( $user->getName(), self::USERNAME_RECOMMENDED_LENGTH, '...', false ) ); } /** @@ -516,7 +521,7 @@ abstract class EchoEventPresentationModel implements JsonSerializable { */ protected function getTruncatedTitleText( Title $title, $includeNamespace = false ) { $text = $includeNamespace ? $title->getPrefixedText() : $title->getText(); - return $this->language->embedBidi( $this->language->truncate( $text, self::PAGE_NAME_RECOMMENDED_LENGTH, '...', false ) ); + return $this->language->embedBidi( $this->language->truncateForVisual( $text, self::PAGE_NAME_RECOMMENDED_LENGTH, '...', false ) ); } /** @@ -537,7 +542,7 @@ abstract class EchoEventPresentationModel implements JsonSerializable { : $user->getUserPage()->getFullURL(); $label = $user->getName(); - $truncatedLabel = $this->language->truncate( $label, self::USERNAME_AS_LABEL_RECOMMENDED_LENGTH, '...', false ); + $truncatedLabel = $this->language->truncateForVisual( $label, self::USERNAME_AS_LABEL_RECOMMENDED_LENGTH, '...', false ); $isTruncated = $label !== $truncatedLabel; return [ @@ -569,7 +574,7 @@ abstract class EchoEventPresentationModel implements JsonSerializable { return [ 'url' => $title->getFullURL( $query ), 'label' => $this->language->embedBidi( - $this->language->truncate( $title->getText(), self::PAGE_NAME_AS_LABEL_RECOMMENDED_LENGTH, '...', false ) + $this->language->truncateForVisual( $title->getText(), self::PAGE_NAME_AS_LABEL_RECOMMENDED_LENGTH, '...', false ) ), 'tooltip' => $title->getPrefixedText(), 'description' => $description, diff --git a/includes/formatters/PresentationModelSectionTrait.php b/includes/formatters/PresentationModelSectionTrait.php index ebf949b86..00f9e3181 100644 --- a/includes/formatters/PresentationModelSectionTrait.php +++ b/includes/formatters/PresentationModelSectionTrait.php @@ -82,7 +82,7 @@ trait EchoPresentationModelSectionTrait { } protected function getTruncatedSectionTitle() { - return $this->language->embedBidi( $this->language->truncate( + return $this->language->embedBidi( $this->language->truncateForVisual( $this->getParsedSectionTitle(), self::SECTION_TITLE_RECOMMENDED_LENGTH, '...',