r111350 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r111349‎ | r111350 | r111351 >
Date:07:23, 13 February 2012
Author:wikinaut
Status:reverted (Comments)
Tags:gerritmigration 
Comment:
fix for bug30377 : add a new parameter to limit the number of characters when rendering the channel item <description>
Modified paths:
  • /trunk/extensions/RSS/RELEASE-NOTES (modified) (history)
  • /trunk/extensions/RSS/RSS.php (modified) (history)
  • /trunk/extensions/RSS/RSSParser.php (modified) (history)

Diff [purge]

Index: trunk/extensions/RSS/RELEASE-NOTES
@@ -2,8 +2,6 @@
33 http://www.mediawiki.org/wiki/Extension:RSS
44
55 === TO DO ===
6 -* bug 30377 add a new parameter to limit the number of characters when rendering
7 - the channel item <description>
86 * set an upper default limit for HttpRequest request size when fetching feeds
97 doing a HEAD request first to ask for the size but that value may not be
108 available. Check how much data is returned as its coming back
@@ -15,8 +13,11 @@
1614 * bug 30028 "Error parsing XML for RSS" - improve and harden Extension:RSS when
1715 parsing differently flavoured RSS feeds
1816
19 -=== Version 1.91 2012-02-13 ===
 17+=== Version 1.92 2012-02-13 ===
2018 * added optional date= attribute and $wgRSSDateDefaultFormat parameter
 19+* added optional item-max-length= attribute and $wgRSSItemMaxLength parameter
 20+ fixes bug 30377 add a new parameter to limit the number of characters when
 21+ rendering the channel item <description>
2122
2223 === Version 1.90 2011-08-15 ===
2324 * removed parsing of each single channel subelement (item)
Index: trunk/extensions/RSS/RSSParser.php
@@ -2,6 +2,8 @@
33
44 class RSSParser {
55 protected $maxheads = 32;
 6+ protected $date = "Y-m-d H:i:s";
 7+ protected $ItemMaxLength = 200;
68 protected $reversed = false;
79 protected $highlight = array();
810 protected $filter = array();
@@ -37,7 +39,7 @@
3840 * and return an object that can produce rendered output.
3941 */
4042 function __construct( $url, $args ) {
41 - global $wgRSSDateDefaultFormat;
 43+ global $wgRSSDateDefaultFormat,$wgRSSItemMaxLength;
4244
4345 $this->url = $url;
4446
@@ -60,8 +62,6 @@
6163 case ( isset( $wgRSSDateDefaultFormat ) ):
6264 $this->date = $wgRSSDateDefaultFormat;
6365 break;
64 - default:
65 - $this->date = "Y-m-d H:i:s";
6666 }
6767
6868 # Get highlight terms from argument array
@@ -75,6 +75,16 @@
7676 $this->filter = self::explodeOnSpaces( $args['filter'] );
7777 }
7878
 79+ # Get a maximal length for item texts
 80+ switch ( true ) {
 81+ case ( isset( $args['item-max-length'] ) ):
 82+ $this->ItemMaxLength = $args['item-max-length'];
 83+ break;
 84+ case ( isset( $wgRSSItemMaxLength ) ):
 85+ $this->ItemMaxLength = $wgRSSItemMaxLength;
 86+ break;
 87+ }
 88+
7989 if ( isset( $args['filterout'] ) ) {
8090 $this->filterOut = self::explodeOnSpaces( $args['filterout'] );
8191 }
@@ -298,6 +308,7 @@
299309 // and that means bad RSS with stuff like
300310 // <description><script>alert("hi")</script></description> will find its
301311 // rogue <script> tags neutered.
 312+ // use the overloaded multi byte wrapper functions in GlobalFunctions.php
302313
303314 foreach ( array_keys( $item ) as $info ) {
304315 switch ( $info ) {
@@ -311,8 +322,12 @@
312323 $txt = date( $this->date, strtotime( $this->escapeTemplateParameter( $item[ $info ] ) ) );
313324 date_default_timezone_set( $tempTimezone );
314325 break;
315 - default:
316 - $txt = $this->highlightTerms( $this->escapeTemplateParameter( $item[ $info ] ) );
 326+ default:
 327+ $str = $this->escapeTemplateParameter( $item[ $info ] );
 328+ if ( mb_strlen( $str ) > $this->ItemMaxLength ) {
 329+ $str = mb_substr( $str, 0, $this->ItemMaxLength ) . " ...";
 330+ }
 331+ $txt = $this->highlightTerms( $str );
317332 }
318333
319334 $renderedItem = str_replace( '{{{' . $info . '}}}', $txt, $renderedItem );
Index: trunk/extensions/RSS/RSS.php
@@ -26,7 +26,7 @@
2727 'Rdb', 'Mafs', 'Alxndr', 'Thomas Gries', 'Chris Reigrut',
2828 'K001', 'Jack Phoenix', 'Jeroen De Dauw', 'Mark A. Hershberger'
2929 ),
30 - 'version' => '1.91 20120213',
 30+ 'version' => '1.92 20120213',
3131 'url' => 'https://www.mediawiki.org/wiki/Extension:RSS',
3232 'descriptionmsg' => 'rss-desc',
3333 );
@@ -68,3 +68,7 @@
6969
7070 // default date format of item publication dates see http://www.php.net/date
7171 $wgRSSDateDefaultFormat = "(Y-m-d H:i:s)";
 72+
 73+// limit the number of characters in the item description
 74+// or set to false for unlimited length.
 75+// $wgRSSItemMaxLength = 100;

Follow-up revisions

RevisionCommit summaryAuthorDate
r111351follow-up r111350 . check if optional parameter isset and is_numeric, otherw...wikinaut07:36, 13 February 2012
r111816follow-up r111350 r111351 . switch replaced by if elseif construct.wikinaut07:35, 18 February 2012
r113297fix for bug34763 'RSS feed items (HTML) are not rendered as HTML but htmlesca...wikinaut21:06, 7 March 2012
r114390Revert r111347, r111348, r111350, r111351, r111515, r111816, r112243, r112251......catrope18:40, 21 March 2012

Comments

#Comment by Awjrichards (talk | contribs)   23:20, 17 February 2012
if ( mb_strlen( $str ) > $this->ItemMaxLength ) {
					$str = mb_substr( $str, 0, $this->ItemMaxLength ) . " ...";
			}

Be careful here - PHP may not necessarily be compiled with mb string support as it is a non-default extension. Before relying on mb functions, perhaps ensure they exist with function_exists().

Also, nitpicky style note:

+		# Get a maximal length for item texts
 	80	+		switch ( true ) {
 	81	+		case ( isset( $args['item-max-length'] ) ):
 	82	+			$this->ItemMaxLength = $args['item-max-length'];
 	83	+			break;
 	84	+		case ( isset( $wgRSSItemMaxLength ) ):
 	85	+			$this->ItemMaxLength = $wgRSSItemMaxLength;
 	86	+			break;
 	87	+		}
 	88	+		

Indentation is messed up here. Also, this seems like an unnecessarily more difficult to read version of using if() {} elseif(){}.

#Comment by Wikinaut (talk | contribs)   00:10, 18 February 2012

"Be careful here - PHP may not necessarily be compiled with mb string support as it is a non-default extension. Before relying on mb functions, perhaps ensure they exist with function_exists(). "

No, it is not. As already mentioned in the comment, GlobalFunctions.php automatically wraps the mb_ function if it does not exists. The indentation is perfect. The switch is a very fine layout here.

Reset to NEW

#Comment by Awjrichards (talk | contribs)   00:28, 18 February 2012

Ah indeed - I missed the comment line and didn't know we had those functions overloaded in GlobalFunctions.php.

As for the indentation, I think convention dictates that the case() statements should be indented an additional level from switch().

And as for even using the switch() statement this way, I think it's just a matter of style - personally I find this much more confusing when reading through code quickly than using if() {} elseif().

#Comment by Wikinaut (talk | contribs)   00:37, 18 February 2012

Source / and my Reference:

https://www.mediawiki.org/w/index.php?title=Manual:Coding_conventions&oldid=426301


There are some exceptions, such as switch statements, where the indentation of the cases are optional, so both of the below are fine.

        switch ( $mimetype ) {
        case 'text/xml':
        case 'application/xhtml+xml':
        case 'application/xml':
                return true;
        default:
                return false;
        }

        switch ( $mimetype ) {
                case 'text/xml':
                case 'application/xhtml+xml':
                case 'application/xml':
                        return true;
                default:
                        return false;
        }

#Comment by Johnduhart (talk | contribs)   00:52, 18 February 2012

The usage in those examples is vastly differently from how you are doing that.

#Comment by Wikinaut (talk | contribs)   07:36, 18 February 2012

The usage in those examples is vastly differently from how you are doing that."

fixed in r111816 .

#Comment by Bawolff (talk | contribs)   02:20, 8 March 2012

Note however, the Language class provides a truncate method which can localize the '...' message ( $parser->getFunctionLang()->truncate( $str, $length ); ), which may be a better choice. OTOH that truncate method is more meant for byte limits, so would handle multibyte characters slightly differently, so I suppose mb_substr is ok if we want the length to be the number of "code points" so to speak.

#Comment by Bawolff (talk | contribs)   02:23, 8 March 2012

One of the lines of the case statement:

84 + case ( isset( $wgRSSItemMaxLength ) ):

is a little confusing at first glance, since isset( $wgRSSItemMaxLength ) should always return true. (Not a major issue, and is probably ok, just a bit confusing)

#Comment by Wikinaut (talk | contribs)   07:01, 8 March 2012

Indeed, you are right, because $wgRSSItemMaxLength is always set in RSS.php it should be existent, but I wanted to be sure for cases where admins set or, worse, unset $wgRSSItemMaxLength in their LocalSettings.

BTW: this line was further changed in follow-up r111351 to

  • case ( isset( $wgRSSItemMaxLength ) && is_numeric( $wgRSSItemMaxLength ) )
#Comment by Wikinaut (talk | contribs)   00:24, 18 February 2012

GlobalFunctions:

if ( !function_exists( 'mb_strlen' ) ) { 51 /** 52 * @codeCoverageIgnore 53 * @return int 54 */ 55 function mb_strlen( $str, $enc = ) { 56 return Fallback::mb_strlen( $str, $enc ); 57 } 58 }


Fallback:

/** 129 * Fallback implementation of mb_strlen, hardcoded to UTF-8. 130 * @param string $str 131 * @param string $enc optional encoding; ignored 132 * @return int 133 */ 134 public static function mb_strlen( $str, $enc = ) { 135 $counts = count_chars( $str ); 136 $total = 0; 137 138

#Comment by Wikinaut (talk | contribs)   00:26, 18 February 2012

[CORR] using preformatted

GlobalFunctions.php:

50 	if ( !function_exists( 'mb_strlen' ) ) {
51 	/**
52 	* @codeCoverageIgnore
53 	* @return int
54 	*/
55 	function mb_strlen( $str, $enc = '' ) {
56 	return Fallback::mb_strlen( $str, $enc );
57 	}
58 	}


Fallback.php

/**
129 	* Fallback implementation of mb_strlen, hardcoded to UTF-8.
130 	* @param string $str
131 	* @param string $enc optional encoding; ignored
132 	* @return int
133 	*/
134 	public static function mb_strlen( $str, $enc = '' ) {
135 	$counts = count_chars( $str );
136 	$total = 0;
137 	
138 	

Status & tagging log