Merge "Replace uses of deprecated Language::truncate()"

This commit is contained in:
jenkins-bot 2018-06-17 19:14:08 +00:00 committed by Gerrit Code Review
commit bc156ccb26
4 changed files with 28 additions and 20 deletions

View file

@ -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 );
}
}

View file

@ -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;
}

View file

@ -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,

View file

@ -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,
'...',