http://www.mediawiki.org/wiki/Special:Code/MediaWiki/84660
Revision: 84660
Author: bawolff
Date: 2011-03-24 02:54:11 +0000 (Thu, 24 Mar 2011)
Log Message:
-----------
(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.
Modified Paths:
--------------
trunk/phase3/RELEASE-NOTES
trunk/phase3/includes/Article.php
trunk/phase3/includes/DefaultSettings.php
trunk/phase3/includes/Title.php
trunk/phase3/includes/search/SearchEngine.php
trunk/phase3/languages/Language.php
Modified: trunk/phase3/RELEASE-NOTES
===================================================================
--- trunk/phase3/RELEASE-NOTES 2011-03-24 01:44:48 UTC (rev 84659)
+++ trunk/phase3/RELEASE-NOTES 2011-03-24 02:54:11 UTC (rev 84660)
@@ -204,6 +204,8 @@
* (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
Modified: trunk/phase3/includes/Article.php
===================================================================
--- trunk/phase3/includes/Article.php 2011-03-24 01:44:48 UTC (rev 84659)
+++ trunk/phase3/includes/Article.php 2011-03-24 02:54:11 UTC (rev 84660)
@@ -2726,8 +2726,8 @@
// 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
);
Modified: trunk/phase3/includes/DefaultSettings.php
===================================================================
--- trunk/phase3/includes/DefaultSettings.php 2011-03-24 01:44:48 UTC (rev
84659)
+++ trunk/phase3/includes/DefaultSettings.php 2011-03-24 02:54:11 UTC (rev
84660)
@@ -928,7 +928,7 @@
'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
);
Modified: trunk/phase3/includes/Title.php
===================================================================
--- trunk/phase3/includes/Title.php 2011-03-24 01:44:48 UTC (rev 84659)
+++ trunk/phase3/includes/Title.php 2011-03-24 02:54:11 UTC (rev 84660)
@@ -3231,8 +3231,8 @@
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();
Modified: trunk/phase3/includes/search/SearchEngine.php
===================================================================
--- trunk/phase3/includes/search/SearchEngine.php 2011-03-24 01:44:48 UTC
(rev 84659)
+++ trunk/phase3/includes/search/SearchEngine.php 2011-03-24 02:54:11 UTC
(rev 84660)
@@ -1323,12 +1323,13 @@
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];
Modified: trunk/phase3/languages/Language.php
===================================================================
--- trunk/phase3/languages/Language.php 2011-03-24 01:44:48 UTC (rev 84659)
+++ trunk/phase3/languages/Language.php 2011-03-24 02:54:11 UTC (rev 84660)
@@ -2382,27 +2382,32 @@
* 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 @@
*
* 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 @@
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
_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs