Index: trunk/extensions/RSS/RSS.php |
— | — | @@ -3,24 +3,23 @@ |
4 | 4 | /** |
5 | 5 | * RSS-Feed MediaWiki extension. |
6 | 6 | * @link http://www.mediawiki.org/wiki/Extension:RSS Documentation |
7 | | - * |
| 7 | + * |
8 | 8 | * @file RSS.php |
9 | 9 | * @ingroup Extensions |
10 | 10 | * |
11 | | - * TODO: stylize |
12 | 11 | * TODO: replace all @ by wfSurpressWarnings and wfResumeWarnings |
13 | 12 | */ |
14 | | - |
15 | | -if( !defined( 'MEDIAWIKI' ) ) { |
| 13 | + |
| 14 | +if ( !defined( 'MEDIAWIKI' ) ) { |
16 | 15 | die( "This is not a valid entry point.\n" ); |
17 | 16 | } |
18 | | - |
| 17 | + |
19 | 18 | define( 'RSS_VERSION', '1.7 alpha' ); |
20 | 19 | |
21 | 20 | $wgExtensionCredits['parserhook'][] = array( |
22 | 21 | 'path' => __FILE__, |
23 | 22 | 'name' => 'RSS feed', |
24 | | - 'author' => array( |
| 23 | + 'author' => array( |
25 | 24 | 'mutante', |
26 | 25 | 'Duesentrieb', |
27 | 26 | 'Rdb', |
— | — | @@ -35,112 +34,112 @@ |
36 | 35 | 'url' => 'http://www.mediawiki.org/wiki/Extension:RSS', |
37 | 36 | 'descriptionmsg' => 'rss-desc', |
38 | 37 | ); |
39 | | - |
| 38 | + |
40 | 39 | $dir = dirname( __FILE__ ); |
41 | 40 | $wgExtensionMessagesFiles['RSS'] = "$dir/RSS.i18n.php"; |
42 | 41 | |
43 | 42 | define( 'MAGPIE_OUTPUT_ENCODING', 'UTF-8' ); |
44 | | - |
45 | | -#change this according to your magpie installation! |
| 43 | + |
| 44 | +# change this according to your magpie installation! |
46 | 45 | require_once( dirname( __FILE__ ) . '/magpierss/rss_fetch.inc' ); |
47 | | - |
| 46 | + |
48 | 47 | // Avoid unstubbing $wgParser too early on modern (1.12+) MW versions, as per r35980 |
49 | 48 | if ( defined( 'MW_SUPPORTS_PARSERFIRSTCALLINIT' ) ) { |
50 | 49 | $wgHooks['ParserFirstCallInit'][] = 'wfRssExtension'; |
51 | 50 | } else { |
52 | 51 | $wgExtensionFunctions[] = 'wfRssExtension'; |
53 | 52 | } |
54 | | - |
55 | | -#Extension hook callback function |
56 | | -function wfRssExtension() { |
| 53 | + |
| 54 | +# Extension hook callback function |
| 55 | +function wfRssExtension() { |
57 | 56 | global $wgParser; |
58 | | - |
59 | | - #Install parser hook for <rss> tags |
| 57 | + |
| 58 | + # Install parser hook for <rss> tags |
60 | 59 | $wgParser->setHook( 'rss', 'renderRss' ); |
61 | 60 | return true; |
62 | 61 | } |
63 | | - |
64 | | -#Parser hook callback function |
| 62 | + |
| 63 | +# Parser hook callback function |
65 | 64 | function renderRss( $input ) { |
66 | 65 | global $wgOutputEncoding, $wgParser; |
67 | | - |
| 66 | + |
68 | 67 | // Kill parser cache |
69 | 68 | $wgParser->disableCache(); |
70 | | - |
71 | | - if ( !$input ) return ''; #if <rss>-section is empty, return nothing |
72 | 69 | |
73 | | - #Parse fields in rss-section |
| 70 | + if ( !$input ) return ''; # if <rss>-section is empty, return nothing |
| 71 | + |
| 72 | + # Parse fields in rss-section |
74 | 73 | $fields = explode( '|', $input ); |
75 | 74 | $url = @$fields[0]; |
76 | | - |
| 75 | + |
77 | 76 | $args = array(); |
78 | 77 | for ( $i = 1; $i < sizeof( $fields ); $i++ ) { |
79 | 78 | $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; |
82 | 81 | else { |
83 | 82 | 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 ); |
86 | 85 | } |
87 | 86 | } |
88 | | - |
89 | | - #Get charset from argument-array |
| 87 | + |
| 88 | + # Get charset from argument-array |
90 | 89 | $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 |
93 | 92 | $maxheads = @$args['max']; |
94 | 93 | $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 | + |
102 | 101 | # Get date format from argument-array |
103 | | - if (isset($args["date"])) { |
| 102 | + if ( isset( $args["date"] ) ) { |
104 | 103 | $date = @$args["date"]; |
105 | | - if ($date == '') |
| 104 | + if ( $date == '' ) |
106 | 105 | $date = 'd M Y H:i'; |
107 | 106 | } |
108 | 107 | else |
109 | 108 | $date = false; |
110 | | - |
111 | | - #Get highlight terms from argument array |
| 109 | + |
| 110 | + # Get highlight terms from argument array |
112 | 111 | $rssHighlight = @$args['highlight']; |
113 | 112 | $rssHighlight = str_replace( ' ', ' ', $rssHighlight ); |
114 | 113 | $rssHighlight = explode( ' ', trim( $rssHighlight ) ); |
115 | | - |
116 | | - #Get filter terms from argument-array |
| 114 | + |
| 115 | + # Get filter terms from argument-array |
117 | 116 | $rssFilter = @$args['filter']; |
118 | 117 | $rssFilter = str_replace( ' ', ' ', $rssFilter ); |
119 | 118 | $rssFilter = explode( ' ', trim( $rssFilter ) ); |
120 | | - |
121 | | - #Filterout terms |
| 119 | + |
| 120 | + # Filterout terms |
122 | 121 | $rssFilterout = @$args['filterout']; |
123 | 122 | $rssFilterout = str_replace( ' ', ' ', $rssFilterout ); |
124 | 123 | $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. |
128 | 127 | $rss = @fetch_rss( $url ); |
129 | | - |
130 | | - #Check for errors. |
| 128 | + |
| 129 | + # Check for errors. |
131 | 130 | 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… |
133 | 132 | } |
134 | | - |
| 133 | + |
135 | 134 | 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… |
137 | 136 | } |
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>"; |
142 | 137 | |
| 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 | + |
143 | 142 | $output = ''; |
144 | | - if( $reverse ) $rss->items = array_reverse( $rss->items ); |
| 143 | + if ( $reverse ) $rss->items = array_reverse( $rss->items ); |
145 | 144 | $description = false; |
146 | 145 | foreach ( $rss->items as $item ) { |
147 | 146 | if ( $item['description'] ) { |
— | — | @@ -148,36 +147,36 @@ |
149 | 148 | break; |
150 | 149 | } |
151 | 150 | } |
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 | + |
157 | 156 | foreach ( $rss->items as $item ) { |
158 | 157 | $d_text = true; |
159 | 158 | $d_title = true; |
160 | | - |
| 159 | + |
161 | 160 | $href = htmlspecialchars( trim( iconv( $charset, $wgOutputEncoding, $item['link'] ) ) ); |
162 | 161 | $title = htmlspecialchars( trim( iconv( $charset, $wgOutputEncoding, $item['title'] ) ) ); |
163 | | - |
164 | | - if ($date) { |
| 162 | + |
| 163 | + if ( $date ) { |
165 | 164 | $pubdate = trim( iconv( $charset, $wgOutputEncoding, $item['pubdate'] ) ); |
166 | 165 | $pubdate = date( $date, strtotime( $pubdate ) ); |
167 | 166 | } |
168 | | - |
| 167 | + |
169 | 168 | $d_title = wfRssFilter( $title, $rssFilter ); |
170 | 169 | $d_title = wfRssFilterout( $title, $rssFilterout ); |
171 | 170 | $title = wfRssHighlight( $title, $rssHighlight ); |
172 | | - |
173 | | - #Build description text if desired |
| 171 | + |
| 172 | + # Build description text if desired |
174 | 173 | if ( $item['description'] ) { |
175 | 174 | $text = trim( iconv( $charset, $wgOutputEncoding, $item['description'] ) ); |
176 | | - #Avoid pre-tags |
| 175 | + # Avoid pre-tags |
177 | 176 | $text = str_replace( "\r", ' ', $text ); |
178 | 177 | $text = str_replace( "\n", ' ', $text ); |
179 | 178 | $text = str_replace( "\t", ' ', $text ); |
180 | 179 | $text = str_replace( '<br>', '', $text ); |
181 | | - |
| 180 | + |
182 | 181 | $d_text = wfRssFilter( $text, $rssFilter ); |
183 | 182 | $d_text = wfRssFilterout( $text, $rssFilterout ); |
184 | 183 | $text = wfRssHighlight( $text, $rssHighlight ); |
— | — | @@ -187,25 +186,25 @@ |
188 | 187 | $display = $d_title; |
189 | 188 | } |
190 | 189 | 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>"; |
194 | 193 | } |
195 | | - #Cut off output when maxheads is reached: |
| 194 | + # Cut off output when maxheads is reached: |
196 | 195 | if ( ++$headcnt == $maxheads ) break; |
197 | 196 | } |
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>'; |
203 | 202 | $displayed = array(); |
204 | 203 | foreach ( $rss->items as $item ) { |
205 | 204 | $href = htmlspecialchars( trim( iconv( $charset, $wgOutputEncoding, $item['link'] ) ) ); |
206 | 205 | $title = htmlspecialchars( trim( iconv( $charset, $wgOutputEncoding, $item['title'] ) ) ); |
207 | 206 | $d_title = wfRssFilter( $title, $rssFilter ) && wfRssFilterout( $title, $rssFilterout ); |
208 | 207 | $title = wfRssHighlight( $title, $rssHighlight ); |
209 | | - if ($date) { |
| 208 | + if ( $date ) { |
210 | 209 | $pubdate = trim( iconv( $charset, $wgOutputEncoding, $item['pubdate'] ) ); |
211 | 210 | if ( $pubdate == '' ) { |
212 | 211 | $pubdate = trim( iconv( $charset, $wgOutputEncoding, $item['dc']['date'] ) ); |
— | — | @@ -214,27 +213,27 @@ |
215 | 214 | } |
216 | 215 | if ( $d_title && !in_array( $title, $displayed ) ) { |
217 | 216 | // 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)"; |
221 | 220 | } |
222 | | - $output.= '</li>'; |
223 | | - |
| 221 | + $output .= '</li>'; |
| 222 | + |
224 | 223 | $displayed[] = $title; |
225 | | - #Cut off output when maxheads is reached: |
| 224 | + # Cut off output when maxheads is reached: |
226 | 225 | if ( ++$headcnt == $maxheads ) break; |
227 | 226 | } |
228 | 227 | } |
229 | | - $output.= '</ul>'; |
| 228 | + $output .= '</ul>'; |
230 | 229 | } |
231 | | - |
| 230 | + |
232 | 231 | return $output; |
233 | 232 | } |
234 | | - |
| 233 | + |
235 | 234 | function wfRssFilter( $text, $rssFilter ) { |
236 | 235 | $display = true; |
237 | 236 | if ( is_array( $rssFilter ) ) { |
238 | | - foreach( $rssFilter as $term ) { |
| 237 | + foreach ( $rssFilter as $term ) { |
239 | 238 | if ( $term ) { |
240 | 239 | $display = false; |
241 | 240 | if ( preg_match( "|$term|i", $text, $a ) ) { |
— | — | @@ -247,7 +246,7 @@ |
248 | 247 | } |
249 | 248 | return $display; |
250 | 249 | } |
251 | | - |
| 250 | + |
252 | 251 | function wfRssFilterout( $text, $rssFilterout ) { |
253 | 252 | $display = true; |
254 | 253 | if ( is_array( $rssFilterout ) ) { |
— | — | @@ -262,35 +261,35 @@ |
263 | 262 | } |
264 | 263 | return $display; |
265 | 264 | } |
266 | | - |
| 265 | + |
267 | 266 | function wfRssHighlight( $text, $rssHighlight ) { |
268 | 267 | $i = 0; |
269 | 268 | $starttag = 'v8x5u3t3u8h'; |
270 | 269 | $endtag = 'q8n4f6n4n4x'; |
271 | | - |
| 270 | + |
272 | 271 | $color[] = 'coral'; |
273 | 272 | $color[] = 'greenyellow'; |
274 | 273 | $color[] = 'lightskyblue'; |
275 | 274 | $color[] = 'gold'; |
276 | 275 | $color[] = 'violet'; |
277 | 276 | $count_color = count( $color ); |
278 | | - |
| 277 | + |
279 | 278 | if ( is_array( $rssHighlight ) ) { |
280 | | - foreach( $rssHighlight as $term ) { |
| 279 | + foreach ( $rssHighlight as $term ) { |
281 | 280 | 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 ); |
283 | 282 | $i++; |
284 | 283 | if ( $i == $count_color ) $i = 0; |
285 | 284 | } |
286 | 285 | } |
287 | 286 | } |
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", … |
290 | 289 | 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 ); |
292 | 291 | $text = preg_replace( "|$endtag|", '</span>', $text ); |
293 | 292 | } |
294 | | - |
| 293 | + |
295 | 294 | return $text; |
296 | 295 | } |
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 |