r69790 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r69789‎ | r69790 | r69791 >
Date:14:14, 23 July 2010
Author:jeroendedauw
Status:ok
Tags:
Comment:
Stylized
Modified paths:
  • /trunk/extensions/RSS/RSS.php (modified) (history)

Diff [purge]

Index: trunk/extensions/RSS/RSS.php
@@ -3,24 +3,23 @@
44 /**
55 * RSS-Feed MediaWiki extension.
66 * @link http://www.mediawiki.org/wiki/Extension:RSS Documentation
7 - *
 7+ *
88 * @file RSS.php
99 * @ingroup Extensions
1010 *
11 - * TODO: stylize
1211 * TODO: replace all @ by wfSurpressWarnings and wfResumeWarnings
1312 */
14 -
15 -if( !defined( 'MEDIAWIKI' ) ) {
 13+
 14+if ( !defined( 'MEDIAWIKI' ) ) {
1615 die( "This is not a valid entry point.\n" );
1716 }
18 -
 17+
1918 define( 'RSS_VERSION', '1.7 alpha' );
2019
2120 $wgExtensionCredits['parserhook'][] = array(
2221 'path' => __FILE__,
2322 'name' => 'RSS feed',
24 - 'author' => array(
 23+ 'author' => array(
2524 'mutante',
2625 'Duesentrieb',
2726 'Rdb',
@@ -35,112 +34,112 @@
3635 'url' => 'http://www.mediawiki.org/wiki/Extension:RSS',
3736 'descriptionmsg' => 'rss-desc',
3837 );
39 -
 38+
4039 $dir = dirname( __FILE__ );
4140 $wgExtensionMessagesFiles['RSS'] = "$dir/RSS.i18n.php";
4241
4342 define( 'MAGPIE_OUTPUT_ENCODING', 'UTF-8' );
44 -
45 -#change this according to your magpie installation!
 43+
 44+# change this according to your magpie installation!
4645 require_once( dirname( __FILE__ ) . '/magpierss/rss_fetch.inc' );
47 -
 46+
4847 // Avoid unstubbing $wgParser too early on modern (1.12+) MW versions, as per r35980
4948 if ( defined( 'MW_SUPPORTS_PARSERFIRSTCALLINIT' ) ) {
5049 $wgHooks['ParserFirstCallInit'][] = 'wfRssExtension';
5150 } else {
5251 $wgExtensionFunctions[] = 'wfRssExtension';
5352 }
54 -
55 -#Extension hook callback function
56 -function wfRssExtension() {
 53+
 54+# Extension hook callback function
 55+function wfRssExtension() {
5756 global $wgParser;
58 -
59 - #Install parser hook for <rss> tags
 57+
 58+ # Install parser hook for <rss> tags
6059 $wgParser->setHook( 'rss', 'renderRss' );
6160 return true;
6261 }
63 -
64 -#Parser hook callback function
 62+
 63+# Parser hook callback function
6564 function renderRss( $input ) {
6665 global $wgOutputEncoding, $wgParser;
67 -
 66+
6867 // Kill parser cache
6968 $wgParser->disableCache();
70 -
71 - if ( !$input ) return ''; #if <rss>-section is empty, return nothing
7269
73 - #Parse fields in rss-section
 70+ if ( !$input ) return ''; # if <rss>-section is empty, return nothing
 71+
 72+ # Parse fields in rss-section
7473 $fields = explode( '|', $input );
7574 $url = @$fields[0];
76 -
 75+
7776 $args = array();
7877 for ( $i = 1; $i < sizeof( $fields ); $i++ ) {
7978 $f = $fields[$i];
80 -
81 - if ( strpos( $f, '=' ) === false ) $args[strtolower(trim($f))] = false;
 79+
 80+ if ( strpos( $f, '=' ) === false ) $args[strtolower( trim( $f ) )] = false;
8281 else {
8382 list( $k, $v ) = explode( '=', $f, 2 );
84 - if ( trim( $v ) == false ) $args[strtolower(trim($k))] = false;
85 - else $args[strtolower(trim($k))] = trim($v);
 83+ if ( trim( $v ) == false ) $args[strtolower( trim( $k ) )] = false;
 84+ else $args[strtolower( trim( $k ) )] = trim( $v );
8685 }
8786 }
88 -
89 - #Get charset from argument-array
 87+
 88+ # Get charset from argument-array
9089 $charset = @$args['charset'];
91 - if( !$charset ) $charset = $wgOutputEncoding;
92 - #Get max number of headlines from argument-array
 90+ if ( !$charset ) $charset = $wgOutputEncoding;
 91+ # Get max number of headlines from argument-array
9392 $maxheads = @$args['max'];
9493 $headcnt = 0;
95 -
96 - #Get short-flag from argument-array
97 - #If short is set, no description text is printed
98 - if( isset( $args['short'] ) ) $short = true; else $short = false;
99 - #Get reverse-flag from argument-array
100 - if( isset( $args['reverse'] ) ) $reverse = true; else $reverse = false;
101 -
 94+
 95+ # Get short-flag from argument-array
 96+ # If short is set, no description text is printed
 97+ if ( isset( $args['short'] ) ) $short = true; else $short = false;
 98+ # Get reverse-flag from argument-array
 99+ if ( isset( $args['reverse'] ) ) $reverse = true; else $reverse = false;
 100+
102101 # Get date format from argument-array
103 - if (isset($args["date"])) {
 102+ if ( isset( $args["date"] ) ) {
104103 $date = @$args["date"];
105 - if ($date == '')
 104+ if ( $date == '' )
106105 $date = 'd M Y H:i';
107106 }
108107 else
109108 $date = false;
110 -
111 - #Get highlight terms from argument array
 109+
 110+ # Get highlight terms from argument array
112111 $rssHighlight = @$args['highlight'];
113112 $rssHighlight = str_replace( ' ', ' ', $rssHighlight );
114113 $rssHighlight = explode( ' ', trim( $rssHighlight ) );
115 -
116 - #Get filter terms from argument-array
 114+
 115+ # Get filter terms from argument-array
117116 $rssFilter = @$args['filter'];
118117 $rssFilter = str_replace( ' ', ' ', $rssFilter );
119118 $rssFilter = explode( ' ', trim( $rssFilter ) );
120 -
121 - #Filterout terms
 119+
 120+ # Filterout terms
122121 $rssFilterout = @$args['filterout'];
123122 $rssFilterout = str_replace( ' ', ' ', $rssFilterout );
124123 $rssFilterout = explode( ' ', trim( $rssFilterout ) );
125 -
126 - #Fetch RSS. May be cached locally.
127 - #Refer to the documentation of magpie for details.
 124+
 125+ # Fetch RSS. May be cached locally.
 126+ # Refer to the documentation of magpie for details.
128127 $rss = @fetch_rss( $url );
129 -
130 - #Check for errors.
 128+
 129+ # Check for errors.
131130 if ( $rss->ERROR ) {
132 - return "<div>Failed to load RSS feed from $url: ".$rss->ERROR."</div>"; #localize…
 131+ return "<div>Failed to load RSS feed from $url: " . $rss->ERROR . "</div>"; # localize…
133132 }
134 -
 133+
135134 if ( !is_array( $rss->items ) ) {
136 - return "<div>Failed to load RSS feed from $url!</div>"; #localize…
 135+ return "<div>Failed to load RSS feed from $url!</div>"; # localize…
137136 }
138 -
139 - #Build title line
140 - #$title = iconv($charset, $wgOutputEncoding, $rss->channel['title']);
141 - #if( $rss->channel['link'] ) $title = "<a href='".$rss->channel['link']."'>$title</a>";
142137
 138+ # Build title line
 139+ # $title = iconv($charset, $wgOutputEncoding, $rss->channel['title']);
 140+ # if( $rss->channel['link'] ) $title = "<a href='".$rss->channel['link']."'>$title</a>";
 141+
143142 $output = '';
144 - if( $reverse ) $rss->items = array_reverse( $rss->items );
 143+ if ( $reverse ) $rss->items = array_reverse( $rss->items );
145144 $description = false;
146145 foreach ( $rss->items as $item ) {
147146 if ( $item['description'] ) {
@@ -148,36 +147,36 @@
149148 break;
150149 }
151150 }
152 -
153 - #Build items
154 - if ( !$short and $description ) { #full item list
155 - $output.= '<dl>';
156 -
 151+
 152+ # Build items
 153+ if ( !$short and $description ) { # full item list
 154+ $output .= '<dl>';
 155+
157156 foreach ( $rss->items as $item ) {
158157 $d_text = true;
159158 $d_title = true;
160 -
 159+
161160 $href = htmlspecialchars( trim( iconv( $charset, $wgOutputEncoding, $item['link'] ) ) );
162161 $title = htmlspecialchars( trim( iconv( $charset, $wgOutputEncoding, $item['title'] ) ) );
163 -
164 - if ($date) {
 162+
 163+ if ( $date ) {
165164 $pubdate = trim( iconv( $charset, $wgOutputEncoding, $item['pubdate'] ) );
166165 $pubdate = date( $date, strtotime( $pubdate ) );
167166 }
168 -
 167+
169168 $d_title = wfRssFilter( $title, $rssFilter );
170169 $d_title = wfRssFilterout( $title, $rssFilterout );
171170 $title = wfRssHighlight( $title, $rssHighlight );
172 -
173 - #Build description text if desired
 171+
 172+ # Build description text if desired
174173 if ( $item['description'] ) {
175174 $text = trim( iconv( $charset, $wgOutputEncoding, $item['description'] ) );
176 - #Avoid pre-tags
 175+ # Avoid pre-tags
177176 $text = str_replace( "\r", ' ', $text );
178177 $text = str_replace( "\n", ' ', $text );
179178 $text = str_replace( "\t", ' ', $text );
180179 $text = str_replace( '<br>', '', $text );
181 -
 180+
182181 $d_text = wfRssFilter( $text, $rssFilter );
183182 $d_text = wfRssFilterout( $text, $rssFilterout );
184183 $text = wfRssHighlight( $text, $rssHighlight );
@@ -187,25 +186,25 @@
188187 $display = $d_title;
189188 }
190189 if ( $display ) {
191 - $output.= "<dt><a href='$href'><b>$title</b></a></dt>";
192 - if ( $date ) $output.= " ($pubdate)";
193 - if ( $text ) $output.= "<dd>$text <b>[<a href='$href'>?</a>]</b></dd>";
 190+ $output .= "<dt><a href='$href'><b>$title</b></a></dt>";
 191+ if ( $date ) $output .= " ($pubdate)";
 192+ if ( $text ) $output .= "<dd>$text <b>[<a href='$href'>?</a>]</b></dd>";
194193 }
195 - #Cut off output when maxheads is reached:
 194+ # Cut off output when maxheads is reached:
196195 if ( ++$headcnt == $maxheads ) break;
197196 }
198 -
199 - $output.= '</dl>';
200 - } else { #short item list
201 - ## HACKY HACKY HACKY
202 - $output.= '<ul>';
 197+
 198+ $output .= '</dl>';
 199+ } else { # short item list
 200+ # # HACKY HACKY HACKY
 201+ $output .= '<ul>';
203202 $displayed = array();
204203 foreach ( $rss->items as $item ) {
205204 $href = htmlspecialchars( trim( iconv( $charset, $wgOutputEncoding, $item['link'] ) ) );
206205 $title = htmlspecialchars( trim( iconv( $charset, $wgOutputEncoding, $item['title'] ) ) );
207206 $d_title = wfRssFilter( $title, $rssFilter ) && wfRssFilterout( $title, $rssFilterout );
208207 $title = wfRssHighlight( $title, $rssHighlight );
209 - if ($date) {
 208+ if ( $date ) {
210209 $pubdate = trim( iconv( $charset, $wgOutputEncoding, $item['pubdate'] ) );
211210 if ( $pubdate == '' ) {
212211 $pubdate = trim( iconv( $charset, $wgOutputEncoding, $item['dc']['date'] ) );
@@ -214,27 +213,27 @@
215214 }
216215 if ( $d_title && !in_array( $title, $displayed ) ) {
217216 // Add date to ouput if specified
218 - $output.= '<li><a href="'.$href.'" title="'.$title.'">'.$title.'</a>';
219 - if( $date ) {
220 - $output.= " ($pubdate)";
 217+ $output .= '<li><a href="' . $href . '" title="' . $title . '">' . $title . '</a>';
 218+ if ( $date ) {
 219+ $output .= " ($pubdate)";
221220 }
222 - $output.= '</li>';
223 -
 221+ $output .= '</li>';
 222+
224223 $displayed[] = $title;
225 - #Cut off output when maxheads is reached:
 224+ # Cut off output when maxheads is reached:
226225 if ( ++$headcnt == $maxheads ) break;
227226 }
228227 }
229 - $output.= '</ul>';
 228+ $output .= '</ul>';
230229 }
231 -
 230+
232231 return $output;
233232 }
234 -
 233+
235234 function wfRssFilter( $text, $rssFilter ) {
236235 $display = true;
237236 if ( is_array( $rssFilter ) ) {
238 - foreach( $rssFilter as $term ) {
 237+ foreach ( $rssFilter as $term ) {
239238 if ( $term ) {
240239 $display = false;
241240 if ( preg_match( "|$term|i", $text, $a ) ) {
@@ -247,7 +246,7 @@
248247 }
249248 return $display;
250249 }
251 -
 250+
252251 function wfRssFilterout( $text, $rssFilterout ) {
253252 $display = true;
254253 if ( is_array( $rssFilterout ) ) {
@@ -262,35 +261,35 @@
263262 }
264263 return $display;
265264 }
266 -
 265+
267266 function wfRssHighlight( $text, $rssHighlight ) {
268267 $i = 0;
269268 $starttag = 'v8x5u3t3u8h';
270269 $endtag = 'q8n4f6n4n4x';
271 -
 270+
272271 $color[] = 'coral';
273272 $color[] = 'greenyellow';
274273 $color[] = 'lightskyblue';
275274 $color[] = 'gold';
276275 $color[] = 'violet';
277276 $count_color = count( $color );
278 -
 277+
279278 if ( is_array( $rssHighlight ) ) {
280 - foreach( $rssHighlight as $term ) {
 279+ foreach ( $rssHighlight as $term ) {
281280 if ( $term ) {
282 - $text = preg_replace("|\b(\w*?".$term."\w*?)\b|i", "$starttag"."_".$i."\\1$endtag", $text);
 281+ $text = preg_replace( "|\b(\w*?" . $term . "\w*?)\b|i", "$starttag" . "_" . $i . "\\1$endtag", $text );
283282 $i++;
284283 if ( $i == $count_color ) $i = 0;
285284 }
286285 }
287286 }
288 -
289 - #To avoid trouble should someone wants to highlight the terms "span", "style", …
 287+
 288+ # To avoid trouble should someone wants to highlight the terms "span", "style", …
290289 for ( $i = 0; $i < 5; $i++ ) {
291 - $text = preg_replace( "|$starttag"."_".$i."|", "<span style=\"background-color:".$color[$i]."; font-weight: bold;\">", $text );
 290+ $text = preg_replace( "|$starttag" . "_" . $i . "|", "<span style=\"background-color:" . $color[$i] . "; font-weight: bold;\">", $text );
292291 $text = preg_replace( "|$endtag|", '</span>', $text );
293292 }
294 -
 293+
295294 return $text;
296295 }
297 -#PHP closing tag intentionally left blank
\ No newline at end of file
 296+# PHP closing tag intentionally left blank
\ No newline at end of file

Status & tagging log