From 2d64c69910873334f9e1370894cc7b15431ab709 Mon Sep 17 00:00:00 2001 From: Brian Wolff Date: Thu, 24 Mar 2011 02:54:11 +0000 Subject: [PATCH] (follow-up 79778) Make $wgLang->truncate function consider the length of the ... (ellipsis message) in the truncation length. The length of this message varries by localization, so the previous solution of telling truncate to truncate 5 bytes less than needed is not good since this will be too little or too much. Updated places where its used. Some places I left as is, as it looked like the new behaviour would work fine for them to. (for example, the autosummary feature - it was cutting off at 200 bytes, which is no where near 250 limit, so I presume that was for asethic reasons rather then to fit as much in before the db limit). Will do another commit for extension callers in a moment. --- RELEASE-NOTES | 2 ++ includes/Article.php | 4 ++-- includes/DefaultSettings.php | 2 +- includes/Title.php | 4 ++-- includes/search/SearchEngine.php | 5 +++-- languages/Language.php | 16 ++++++++++++---- 6 files changed, 22 insertions(+), 11 deletions(-) diff --git a/RELEASE-NOTES b/RELEASE-NOTES index 003b6779fc..b1b62fc5c0 100644 --- a/RELEASE-NOTES +++ b/RELEASE-NOTES @@ -204,6 +204,8 @@ PHP if you have not done so prior to upgrading MediaWiki. * (bug 26939) Installer does not set $wgMetaNamespace * (bug 28166) UploadBase assumes that 'edit' and 'upload' rights are not per page restrictions +* Make truncate function automatically consider length of '...' string, + since length can vary by localization. === API changes in 1.18 === * (bug 26339) Throw warning when truncating an overlarge API result diff --git a/includes/Article.php b/includes/Article.php index e950a031c9..cc352e1446 100644 --- a/includes/Article.php +++ b/includes/Article.php @@ -2726,8 +2726,8 @@ class Article { // Replace newlines with spaces to prevent uglyness $contents = preg_replace( "/[\n\r]/", ' ', $contents ); // Calculate the maximum amount of chars to get - // Max content length = max comment length - length of the comment (excl. $1) - '...' - $maxLength = 255 - ( strlen( $reason ) - 2 ) - 3; + // Max content length = max comment length - length of the comment (excl. $1) + $maxLength = 255 - ( strlen( $reason ) - 2 ); $contents = $wgContLang->truncate( $contents, $maxLength ); // Remove possible unfinished links $contents = preg_replace( '/\[\[([^\]]*)\]?$/', '$1', $contents ); diff --git a/includes/DefaultSettings.php b/includes/DefaultSettings.php index 13d443aaa2..2a959fbac7 100644 --- a/includes/DefaultSettings.php +++ b/includes/DefaultSettings.php @@ -928,7 +928,7 @@ $wgGalleryOptions = array ( 'imagesPerRow' => 0, // Default number of images per-row in the gallery. 0 -> Adapt to screensize 'imageWidth' => 120, // Width of the cells containing images in galleries (in "px") 'imageHeight' => 120, // Height of the cells containing images in galleries (in "px") - 'captionLength' => 20, // Length of caption to truncate (in characters) + 'captionLength' => 25, // Length of caption to truncate (in characters) 'showBytes' => true, // Show the filesize in bytes in categories ); diff --git a/includes/Title.php b/includes/Title.php index d21ed35695..4f0000b016 100644 --- a/includes/Title.php +++ b/includes/Title.php @@ -3231,8 +3231,8 @@ class Title { if ( $reason ) { $comment .= wfMsgForContent( 'colon-separator' ) . $reason; } - # Truncate for whole multibyte characters. +5 bytes for ellipsis - $comment = $wgContLang->truncate( $comment, 250 ); + # Truncate for whole multibyte characters. + $comment = $wgContLang->truncate( $comment, 255 ); $oldid = $this->getArticleID(); $latest = $this->getLatestRevID(); diff --git a/includes/search/SearchEngine.php b/includes/search/SearchEngine.php index 379b292682..141f67278d 100644 --- a/includes/search/SearchEngine.php +++ b/includes/search/SearchEngine.php @@ -1323,12 +1323,13 @@ class SearchHighlighter { continue; } --$contextlines; - $pre = $wgContLang->truncate( $m[1], - $contextchars ); + // truncate function changes ... to relevant i18n message. + $pre = $wgContLang->truncate( $m[1], - $contextchars, '...', false ); if ( count( $m ) < 3 ) { $post = ''; } else { - $post = $wgContLang->truncate( $m[3], $contextchars ); + $post = $wgContLang->truncate( $m[3], $contextchars, '...', false ); } $found = $m[2]; diff --git a/languages/Language.php b/languages/Language.php index ae8f07eb7c..a8b59f5190 100644 --- a/languages/Language.php +++ b/languages/Language.php @@ -2382,27 +2382,32 @@ class Language { * If $length is negative, the string will be truncated from the beginning * * @param $string String to truncate - * @param $length Int: maximum length (excluding ellipses) + * @param $length Int: maximum length (including ellipses) * @param $ellipsis String to append to the truncated text + * @param $adjustLength Boolean: Subtract length of ellipsis from $length. + * $adjustLength was introduced in 1.18, before that behaved as if false. * @return string */ - function truncate( $string, $length, $ellipsis = '...' ) { + function truncate( $string, $length, $ellipsis = '...', $adjustLength = true ) { # Use the localized ellipsis character if ( $ellipsis == '...' ) { $ellipsis = wfMsgExt( 'ellipsis', array( 'escapenoentities', 'language' => $this ) ); } + $eLength = $adjustLength ? strlen( $ellipsis ) : 0; # Check if there is no need to truncate - if ( $length == 0 ) { + if ( $length == 0 || strlen( $ellipsis ) >= abs( $length ) ) { return $ellipsis; } elseif ( strlen( $string ) <= abs( $length ) ) { return $string; } $stringOriginal = $string; if ( $length > 0 ) { + $length -= $eLength; $string = substr( $string, 0, $length ); // xyz... $string = $this->removeBadCharLast( $string ); $string = $string . $ellipsis; } else { + $length += $eLength; $string = substr( $string, $length ); // ...xyz $string = $this->removeBadCharFirst( $string ); $string = $ellipsis . $string; @@ -2463,8 +2468,10 @@ class Language { * * Note: tries to fix broken HTML with MWTidy * + * Note: since 1.18 you do not need to leave extra room in $length for ellipses. + * * @param string $text HTML string to truncate - * @param int $length (zero/positive) Maximum length (excluding ellipses) + * @param int $length (zero/positive) Maximum length (including ellipses) * @param string $ellipsis String to append to the truncated text * @returns string */ @@ -2473,6 +2480,7 @@ class Language { if ( $ellipsis == '...' ) { $ellipsis = wfMsgExt( 'ellipsis', array( 'escapenoentities', 'language' => $this ) ); } + $length -= strlen( $ellipsis ); # Check if there is no need to truncate if ( $length <= 0 ) { return $ellipsis; // no text shown, nothing to format -- 2.20.1