Index: trunk/extensions/RSS/RSSHooks.php |
— | — | @@ -1,4 +1,5 @@ |
2 | 2 | <?php |
| 3 | + |
3 | 4 | class RSSHooks { |
4 | 5 | /** |
5 | 6 | * Tell the parser how to handle <rss> elements |
— | — | @@ -12,9 +13,10 @@ |
13 | 14 | |
14 | 15 | /** |
15 | 16 | * Static function wrapping RSSParser to handle rendering of RSS elements |
16 | | - * @param String Text inside the tags. |
17 | | - * @param Array value associative list of the element attributes and their values. |
18 | | - * @param Frame parser context |
| 17 | + * @param $input String: text inside the tags. |
| 18 | + * @param $args Array: value associative list of the element attributes and |
| 19 | + * their values. |
| 20 | + * @param $frame Frame parser context |
19 | 21 | */ |
20 | 22 | static function renderRss( $input, $args, $parser, $frame ) { |
21 | 23 | global $wgRSSCacheAge, $wgRSSCacheCompare; |
— | — | @@ -35,11 +37,13 @@ |
36 | 38 | $status = $rss->fetch(); |
37 | 39 | |
38 | 40 | # Check for errors. |
39 | | - if ( !$status->isGood() ) |
40 | | - return wfMsg( 'rss-error', htmlspecialchars( $input), $status->getWikiText() ); |
| 41 | + if ( !$status->isGood() ) { |
| 42 | + return wfMsg( 'rss-error', htmlspecialchars( $input ), $status->getWikiText() ); |
| 43 | + } |
41 | 44 | |
42 | | - if ( !is_object( $rss->rss ) || !is_array( $rss->rss->items ) ) |
| 45 | + if ( !is_object( $rss->rss ) || !is_array( $rss->rss->items ) ) { |
43 | 46 | return wfMsg( 'rss-empty', htmlspecialchars( $input ) ); |
| 47 | + } |
44 | 48 | |
45 | 49 | return $rss->renderFeed( $parser, $frame ); |
46 | 50 | } |
Index: trunk/extensions/RSS/RSSData.php |
— | — | @@ -6,8 +6,8 @@ |
7 | 7 | |
8 | 8 | /** |
9 | 9 | * Constructor, takes a DOMDocument and returns an array of parsed items. |
10 | | - * @param DOMDocument The pre-parsed XML Document |
11 | | - * @returns Object RSSData object with a member items that is an array of parsed items, |
| 10 | + * @param $xml DOMDocument: the pre-parsed XML Document |
| 11 | + * @return Object RSSData object with a member items that is an array of parsed items, |
12 | 12 | */ |
13 | 13 | function __construct( $xml ) { |
14 | 14 | if ( !( $xml instanceOf DOMDocument ) ) { |
— | — | @@ -17,25 +17,27 @@ |
18 | 18 | $xpath = new DOMXPath( $xml ); |
19 | 19 | $items = $xpath->query( '/rss/channel/item' ); |
20 | 20 | |
21 | | - if($items->length !== 0) { |
| 21 | + if( $items->length !== 0 ) { |
22 | 22 | foreach ( $items as $item ) { |
23 | 23 | $bit = array(); |
24 | 24 | foreach ( $item->childNodes as $n ) { |
25 | 25 | $name = $this->rssTokenToName( $n->nodeName ); |
26 | 26 | if ( $name != null ) { |
27 | | - /* Because for DOMElements the nodeValue is just |
| 27 | + /** |
| 28 | + * Because for DOMElements the nodeValue is just |
28 | 29 | * the text of the containing element, without any |
29 | 30 | * tags, it makes this a safe, if unattractive, |
30 | 31 | * value to use. If you want to allow people to |
31 | 32 | * mark up their RSS, some more precautions are |
32 | | - * needed. */ |
| 33 | + * needed. |
| 34 | + */ |
33 | 35 | $bit[$name] = $n->nodeValue; |
34 | 36 | } |
35 | 37 | } |
36 | 38 | $this->items[] = $bit; |
37 | 39 | } |
38 | 40 | } else { |
39 | | - $this->ERROR = "No RSS items found."; |
| 41 | + $this->ERROR = 'No RSS items found.'; |
40 | 42 | return; |
41 | 43 | } |
42 | 44 | } |
— | — | @@ -46,8 +48,9 @@ |
47 | 49 | * same array key. This works on WordPress feeds as-is, but it |
48 | 50 | * probably needs a way to concert dc:date format dates to be the |
49 | 51 | * same as pubDate. |
50 | | - * @param String $elementName Name of the element we have |
51 | | - * @returns String Name to map it to |
| 52 | + * |
| 53 | + * @param $n String: name of the element we have |
| 54 | + * @return String Name to map it to |
52 | 55 | */ |
53 | 56 | protected function rssTokenToName( $n ) { |
54 | 57 | switch( $n ) { |
Index: trunk/extensions/RSS/RSSParser.php |
— | — | @@ -19,8 +19,8 @@ |
20 | 20 | |
21 | 21 | /** |
22 | 22 | * Convenience function that takes a space-separated string and returns an array of words |
23 | | - * @param String list of words |
24 | | - * @returns Array words found |
| 23 | + * @param $str String: list of words |
| 24 | + * @return Array words found |
25 | 25 | */ |
26 | 26 | private static function explodeOnSpaces( $str ) { |
27 | 27 | $found = preg_split( '# +#', $str ); |
— | — | @@ -33,7 +33,6 @@ |
34 | 34 | * and return an object that can produce rendered output. |
35 | 35 | */ |
36 | 36 | function __construct( $url, $args ) { |
37 | | - |
38 | 37 | $this->url = $url; |
39 | 38 | |
40 | 39 | # Get charset from argument array |
— | — | @@ -74,32 +73,30 @@ |
75 | 74 | |
76 | 75 | if ( isset( $args['filterout'] ) ) { |
77 | 76 | $this->filterOut = self::explodeOnSpaces( $args['filterout'] ); |
78 | | - |
79 | 77 | } |
80 | 78 | |
81 | 79 | if ( isset( $args['template'] ) ) { |
82 | 80 | $titleObject = Title::newFromText( $args['template'], NS_TEMPLATE ); |
83 | 81 | $article = new Article( $titleObject, 0 ); |
84 | | - $this->itemTemplate = $article->fetchContent( ); |
| 82 | + $this->itemTemplate = $article->fetchContent(); |
85 | 83 | } else { |
86 | 84 | $this->itemTemplate = wfMsgNoTrans( 'rss-item' ); |
87 | 85 | } |
88 | 86 | } |
89 | 87 | |
90 | 88 | /** |
91 | | - * Return RSS object for the given URL, maintaining caching. |
92 | | - * |
93 | | - * NOTES ON RETRIEVING REMOTE FILES: |
94 | | - * No attempt will be made to fetch remote files if there is something in cache. |
95 | | - * |
96 | | - * NOTES ON FAILED REQUESTS: |
97 | | - * If there is an HTTP error while fetching an RSS object, the cached version |
98 | | - * will be returned, if it exists (and if $wgRSSCacheFreshOnly is false) |
99 | | - * |
100 | | - * @param $url String: URL of RSS file |
101 | | - * @return boolean Status object |
102 | | - */ |
103 | | - function fetch( ) { |
| 89 | + * Return RSS object for the given URL, maintaining caching. |
| 90 | + * |
| 91 | + * NOTES ON RETRIEVING REMOTE FILES: |
| 92 | + * No attempt will be made to fetch remote files if there is something in cache. |
| 93 | + * |
| 94 | + * NOTES ON FAILED REQUESTS: |
| 95 | + * If there is an HTTP error while fetching an RSS object, the cached version |
| 96 | + * will be returned, if it exists (and if $wgRSSCacheFreshOnly is false) |
| 97 | + * |
| 98 | + * @return boolean Status object |
| 99 | + */ |
| 100 | + function fetch() { |
104 | 101 | global $wgRSSCacheAge, $wgRSSCacheFreshOnly; |
105 | 102 | global $wgRSSCacheDirectory, $wgRSSFetchTimeout; |
106 | 103 | global $wgRSSOutputEncoding, $wgRSSInputEncoding; |
— | — | @@ -120,7 +117,7 @@ |
121 | 118 | wfDebugLog( 'RSS', 'Outputting cached feed for ' . $this->url ); |
122 | 119 | return Status::newGood(); |
123 | 120 | } |
124 | | - wfDebugLog( 'RSS', 'Cache Failed, fetching ' . $this->url. ' from remote.' ); |
| 121 | + wfDebugLog( 'RSS', 'Cache Failed, fetching ' . $this->url . ' from remote.' ); |
125 | 122 | |
126 | 123 | $status = $this->fetchRemote( $key ); |
127 | 124 | return $status; |
— | — | @@ -128,8 +125,8 @@ |
129 | 126 | |
130 | 127 | /** |
131 | 128 | * Retrieve the URL from the cache |
132 | | - * @param string $key lookup key to associate with this item |
133 | | - * @returns boolean |
| 129 | + * @param $key String: lookup key to associate with this item |
| 130 | + * @return boolean |
134 | 131 | */ |
135 | 132 | protected function loadFromCache( $key ) { |
136 | 133 | global $wgMemc, $wgRSSCacheCompare; |
— | — | @@ -155,7 +152,7 @@ |
156 | 153 | |
157 | 154 | // We only care if $wgRSSCacheCompare is > 0 |
158 | 155 | if ( $wgRSSCacheCompare && time() - $wgRSSCacheCompare > $lastModified ) { |
159 | | - wfDebugLog( 'RSS', "Content is old enough that we need to check cached content"); |
| 156 | + wfDebugLog( 'RSS', 'Content is old enough that we need to check cached content' ); |
160 | 157 | return false; |
161 | 158 | } |
162 | 159 | |
— | — | @@ -164,8 +161,8 @@ |
165 | 162 | |
166 | 163 | /** |
167 | 164 | * Store this objects (e.g. etag, lastModified, and RSS) in the cache. |
168 | | - * @param string $key lookup key to associate with this item |
169 | | - * @returns boolean |
| 165 | + * @param $key String: lookup key to associate with this item |
| 166 | + * @return boolean |
170 | 167 | */ |
171 | 168 | protected function storeInCache( $key ) { |
172 | 169 | global $wgMemc, $wgRSSCacheAge; |
— | — | @@ -183,20 +180,19 @@ |
184 | 181 | |
185 | 182 | /** |
186 | 183 | * Retrieve a feed. |
187 | | - * @param $url String: URL of the feed. |
| 184 | + * @param $key String: |
188 | 185 | * @param $headers Array: headers to send along with the request |
189 | 186 | * @return Status object |
190 | 187 | */ |
191 | 188 | protected function fetchRemote( $key, array $headers = array()) { |
192 | | - global $wgRSSFetchTimeout; |
193 | | - global $wgRSSUserAgent; |
| 189 | + global $wgRSSFetchTimeout, $wgRSSUserAgent; |
194 | 190 | |
195 | 191 | if ( $this->etag ) { |
196 | 192 | wfDebugLog( 'RSS', 'Used etag: ' . $this->etag ); |
197 | 193 | $headers['If-None-Match'] = $this->etag; |
198 | 194 | } |
199 | 195 | if ( $this->lastModified ) { |
200 | | - $lm = gmdate('r', $this->lastModified); |
| 196 | + $lm = gmdate( 'r', $this->lastModified ); |
201 | 197 | wfDebugLog( 'RSS', "Used last modified: $lm" ); |
202 | 198 | $headers['If-Modified-Since'] = $lm; |
203 | 199 | } |
— | — | @@ -228,7 +224,7 @@ |
229 | 225 | * @param $frame the frame param to pass to recursiveTagParse() |
230 | 226 | */ |
231 | 227 | function renderFeed( $parser, $frame ) { |
232 | | - $output = ""; |
| 228 | + $output = ''; |
233 | 229 | if ( isset( $this->itemTemplate ) ) { |
234 | 230 | $headcnt = 0; |
235 | 231 | if ( $this->reversed ) { |
— | — | @@ -250,15 +246,17 @@ |
251 | 247 | } |
252 | 248 | |
253 | 249 | /** |
254 | | - * Render each item, filtering it out if necessary, applying any highlighting, |
255 | | - * @param $item an array produced by RSSData where keys are the names of the RSS elements |
| 250 | + * Render each item, filtering it out if necessary, applying any highlighting. |
| 251 | + * |
| 252 | + * @param $item Array: an array produced by RSSData where keys are the |
| 253 | + * names of the RSS elements |
256 | 254 | * @param $parser the parser param to pass to recursiveTagParse() |
257 | 255 | * @param $frame the frame param to pass to recursiveTagParse() |
258 | 256 | */ |
259 | 257 | protected function renderItem( $item, $parser, $frame ) { |
260 | 258 | $parts = explode( '|', $this->itemTemplate ); |
261 | 259 | |
262 | | - $output = ""; |
| 260 | + $output = ''; |
263 | 261 | if ( count( $parts ) > 1 && isset( $parser ) && isset( $frame ) ) { |
264 | 262 | $rendered = array(); |
265 | 263 | foreach ( $this->displayFields as $field ) { |
— | — | @@ -272,7 +270,7 @@ |
273 | 271 | $left = null; |
274 | 272 | |
275 | 273 | if ( count( $bits ) == 2 ) { |
276 | | - $left = trim( $bits[0] ); |
| 274 | + $left = trim( $bits[0] ); |
277 | 275 | } |
278 | 276 | |
279 | 277 | if ( isset( $item[$left] ) ) { |
— | — | @@ -282,18 +280,19 @@ |
283 | 281 | $rendered[] = $part; |
284 | 282 | } |
285 | 283 | } |
286 | | - $output .= $parser->recursiveTagParse( implode( " | ", $rendered ), $frame ); |
| 284 | + $output .= $parser->recursiveTagParse( implode( ' | ', $rendered ), $frame ); |
287 | 285 | } |
288 | 286 | return $output; |
289 | 287 | } |
290 | 288 | |
291 | 289 | /** |
292 | 290 | * Parse an HTTP response object into an array of relevant RSS data |
293 | | - * @param $key the to use to store the parsaed response in the cache |
| 291 | + * |
| 292 | + * @param $key String: the key to use to store the parsed response in the cache |
294 | 293 | * @return parsed RSS object (see RSSParse) or false |
295 | 294 | */ |
296 | 295 | protected function responseToXML( $key ) { |
297 | | - wfDebugLog( 'RSS', "Got '".$this->client->getStatus()."', updating cache for $key" ); |
| 296 | + wfDebugLog( 'RSS', "Got '" . $this->client->getStatus() . "', updating cache for $key" ); |
298 | 297 | if ( $this->client->getStatus() === 304 ) { |
299 | 298 | # Not modified, update cache |
300 | 299 | wfDebugLog( 'RSS', "Got 304, updating cache for $key" ); |
— | — | @@ -302,8 +301,8 @@ |
303 | 302 | $this->xml = new DOMDocument; |
304 | 303 | $raw_xml = $this->client->getContent(); |
305 | 304 | |
306 | | - if( $raw_xml == "" ) { |
307 | | - return Status::newFatal( 'rss-parse-error', "No XML content" ); |
| 305 | + if( $raw_xml == '' ) { |
| 306 | + return Status::newFatal( 'rss-parse-error', 'No XML content' ); |
308 | 307 | } |
309 | 308 | |
310 | 309 | wfSuppressWarnings(); |
— | — | @@ -331,11 +330,12 @@ |
332 | 331 | |
333 | 332 | /** |
334 | 333 | * Determine if a given item should or should not be displayed |
335 | | - * @param associative array that RSSData produced for an <item> |
336 | | - * @returns boolean |
| 334 | + * |
| 335 | + * @param $item Array: associative array that RSSData produced for an <item> |
| 336 | + * @return boolean |
337 | 337 | */ |
338 | 338 | protected function canDisplay( array $item ) { |
339 | | - $check = ""; |
| 339 | + $check = ''; |
340 | 340 | |
341 | 341 | /* We're only going to check the displayable fields */ |
342 | 342 | foreach ( $this->displayFields as $field ) { |
— | — | @@ -355,9 +355,12 @@ |
356 | 356 | |
357 | 357 | /** |
358 | 358 | * Filters items in or out if the match a string we're looking for. |
359 | | - * @param String the text to examine |
360 | | - * @param String "filterOut" to check for matches in the filterOut member list. Otherwise, uses the filter member list. |
361 | | - * @returns boolean decision to filter or not. |
| 359 | + * |
| 360 | + * @param $text String: the text to examine |
| 361 | + * @param $filterType String: "filterOut" to check for matches in the |
| 362 | + * filterOut member list. |
| 363 | + * Otherwise, uses the filter member list. |
| 364 | + * @return Boolean: decision to filter or not. |
362 | 365 | */ |
363 | 366 | protected function filter( $text, $filterType ) { |
364 | 367 | if ( $filterType === 'filterOut' ) { |
— | — | @@ -366,10 +369,12 @@ |
367 | 370 | $filter = $this->filter; |
368 | 371 | } |
369 | 372 | |
370 | | - if ( count( $filter ) == 0 ) return $filterType !== 'filterOut'; |
| 373 | + if ( count( $filter ) == 0 ) { |
| 374 | + return $filterType !== 'filterOut'; |
| 375 | + } |
371 | 376 | |
372 | 377 | /* Using : for delimiter here since it'll be quoted automatically. */ |
373 | | - $match = preg_match( ':(' . implode( "|", array_map('preg_quote', $filter ) ) . '):i', $text ) ; |
| 378 | + $match = preg_match( ':(' . implode( '|', array_map( 'preg_quote', $filter ) ) . '):i', $text ) ; |
374 | 379 | if ( $match ) { |
375 | 380 | return true; |
376 | 381 | } |
— | — | @@ -378,8 +383,9 @@ |
379 | 384 | |
380 | 385 | /** |
381 | 386 | * Highlight the words we're supposed to be looking for |
382 | | - * @param String the text to look in. |
383 | | - * @returns String with matched text highlighted in a <span> element |
| 387 | + * |
| 388 | + * @param $text String: the text to look in. |
| 389 | + * @return String with matched text highlighted in a <span> element |
384 | 390 | */ |
385 | 391 | protected function highlightTerms( $text ) { |
386 | 392 | if ( count( $this->highlight ) === 0 ) { |
— | — | @@ -387,7 +393,7 @@ |
388 | 394 | } |
389 | 395 | |
390 | 396 | RSSHighlighter::setTerms( $this->highlight ); |
391 | | - $highlight = ':'. implode( "|", array_map( 'preg_quote', array_values( $this->highlight ) ) ) . ':i'; |
| 397 | + $highlight = ':'. implode( '|', array_map( 'preg_quote', array_values( $this->highlight ) ) ) . ':i'; |
392 | 398 | return preg_replace_callback( $highlight, 'RSSHighlighter::highlightThis', $text ); |
393 | 399 | } |
394 | 400 | } |
— | — | @@ -398,7 +404,7 @@ |
399 | 405 | |
400 | 406 | /** |
401 | 407 | * Set the list of terms to match for the next highlighting session |
402 | | - * @param List of words to match. |
| 408 | + * @param $terms Array: list of words to match. |
403 | 409 | */ |
404 | 410 | static function setTerms( array $terms ) { |
405 | 411 | self::$terms = array_flip( array_map( 'strtolower', $terms ) ); |
— | — | @@ -406,23 +412,25 @@ |
407 | 413 | |
408 | 414 | /** |
409 | 415 | * Actually replace the supplied list of words with HTML code to highlight the words. |
410 | | - * @param List of matched words to highlight. The words are assigned colors based upon the order they were supplied in setTerms() |
411 | | - * @returns String word wrapped in HTML code. |
| 416 | + * @param $match Array: list of matched words to highlight. |
| 417 | + * The words are assigned colors based upon the order |
| 418 | + * they were supplied in setTerms() |
| 419 | + * @return String word wrapped in HTML code. |
412 | 420 | */ |
413 | 421 | static function highlightThis( $match ) { |
414 | 422 | $styleStart = "<span style='font-weight: bold; background: none repeat scroll 0%% 0%% rgb(%s); color: %s;'>"; |
415 | | - $styleEnd = "</span>"; |
| 423 | + $styleEnd = '</span>'; |
416 | 424 | |
417 | 425 | # bg colors cribbed from Google's highlighting of search teerms |
418 | 426 | $bgcolor = array( '255, 255, 102', '160, 255, 255', '153, 255, 153', |
419 | 427 | '255, 153, 153', '255, 102, 255', '136, 0, 0', '0, 170, 0', '136, 104, 0', |
420 | 428 | '0, 70, 153', '153, 0, 153' ); |
421 | 429 | # Spelling out the fg colors instead of using processing time to create this list |
422 | | - $color = array("black", "black", "black", "black", "black", |
423 | | - "white", "white", "white", "white", "white" ); |
| 430 | + $color = array( 'black', 'black', 'black', 'black', 'black', |
| 431 | + 'white', 'white', 'white', 'white', 'white' ); |
424 | 432 | |
425 | | - $index = self::$terms[strtolower($match[0])] % count( $bgcolor ); |
| 433 | + $index = self::$terms[strtolower( $match[0] )] % count( $bgcolor ); |
426 | 434 | |
427 | | - return sprintf($styleStart, $bgcolor[$index], $color[$index]). $match[0] .$styleEnd; |
| 435 | + return sprintf( $styleStart, $bgcolor[$index], $color[$index] ) . $match[0] . $styleEnd; |
428 | 436 | } |
429 | 437 | } |
Index: trunk/extensions/RSS/RSS.php |
— | — | @@ -4,7 +4,7 @@ |
5 | 5 | * |
6 | 6 | * @file |
7 | 7 | * @ingroup Extensions |
8 | | - * @version 1.7 |
| 8 | + * @version 1.8 |
9 | 9 | * @author mutante, Daniel Kinzler, Rdb, Mafs, Alxndr, Chris Reigrut, K001 |
10 | 10 | * @author Kellan Elliott-McCrea <kellan@protest.net> -- author of MagpieRSS |
11 | 11 | * @author Jeroen De Dauw |
— | — | @@ -18,9 +18,6 @@ |
19 | 19 | die( "This is not a valid entry point.\n" ); |
20 | 20 | } |
21 | 21 | |
22 | | -// Agent to use for fetching feeds |
23 | | -$wgRSSUserAgent='MediaWikiRSS/0.01 (+http://www.mediawiki.org/wiki/Extension:RSS) / MediaWiki RSS extension'; |
24 | | - |
25 | 22 | // Extension credits that will show up on Special:Version |
26 | 23 | $wgExtensionCredits['parserhook'][] = array( |
27 | 24 | 'name' => 'RSS feed', |
— | — | @@ -62,3 +59,6 @@ |
63 | 60 | $wgRSSInputEncoding = null; |
64 | 61 | $wgRSSDetectEncoding = true; |
65 | 62 | $wgRSSFetchTimeout = 5; // 5 second timeout |
| 63 | + |
| 64 | +// Agent to use for fetching feeds |
| 65 | +$wgRSSUserAgent = 'MediaWikiRSS/0.01 (+http://www.mediawiki.org/wiki/Extension:RSS) / MediaWiki RSS extension'; |
\ No newline at end of file |