(follow-up 79778) Make $wgLang->truncate function consider the length of the ......
authorBrian Wolff <bawolff@users.mediawiki.org>
Thu, 24 Mar 2011 02:54:11 +0000 (02:54 +0000)
committerBrian Wolff <bawolff@users.mediawiki.org>
Thu, 24 Mar 2011 02:54:11 +0000 (02:54 +0000)
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
includes/Article.php
includes/DefaultSettings.php
includes/Title.php
includes/search/SearchEngine.php
languages/Language.php

index 003b677..b1b62fc 100644 (file)
@@ -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
index e950a03..cc352e1 100644 (file)
@@ -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 );
index 13d443a..2a959fb 100644 (file)
@@ -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
 );
 
index d21ed35..4f0000b 100644 (file)
@@ -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();
index 379b292..141f672 100644 (file)
@@ -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];
index ae8f07e..a8b59f5 100644 (file)
@@ -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