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.
* (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
// 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 );
'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
);
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();
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];
* 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;
*
* 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
*/
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