1 | <?php |
---|
2 | /** |
---|
3 | * RSS-Feed MediaWiki extension |
---|
4 | * |
---|
5 | * @file |
---|
6 | * @ingroup Extensions |
---|
7 | * @version 1.5 |
---|
8 | * @author mutante, Duesentrieb, Rdb, Mafs, Alxndr, Cmreigrut |
---|
9 | * @copyright © mutante, Duesentrieb, Rdb, Mafs, Alxndr, Cmreigrut |
---|
10 | * @link http://www.mediawiki.org/wiki/Extension:RSS Documentation |
---|
11 | * |
---|
12 | * Requires: |
---|
13 | * # magpie rss parser <http://magpierss.sourceforge.net/> |
---|
14 | * # iconv <http://www.gnu.org/software/libiconv/>, see also <http://www.php.net/iconv> |
---|
15 | * |
---|
16 | * 07.05.2008 compatible/checked with MediaWiki 1.12 |
---|
17 | */ |
---|
18 | |
---|
19 | if( !defined( 'MEDIAWIKI' ) ) { |
---|
20 | die( "This is not a valid entry point.\n" ); |
---|
21 | } |
---|
22 | |
---|
23 | $wgExtensionCredits['parserhook'][] = array( |
---|
24 | 'name' => 'RSS feed', |
---|
25 | 'author' => array('mutante', 'Duesentrieb', 'Rdb', 'Mafs', 'Alxndr', 'Wikinaut', 'Cmreigrut'), |
---|
26 | 'version' => '1.5', |
---|
27 | 'url' => 'http://www.mediawiki.org/wiki/Extension:RSS', |
---|
28 | 'description' => 'Displays an RSS feed on a wiki page' |
---|
29 | ); |
---|
30 | |
---|
31 | define('MAGPIE_OUTPUT_ENCODING', 'UTF-8'); |
---|
32 | |
---|
33 | #change this according to your magpie installation! |
---|
34 | #require_once('/var/alternc/html/a/admin/redcta.org.ar/www/phase3/extensions/magpierss/rss_fetch.inc'); |
---|
35 | if ( ! @include_once( '/var/alternc/html/a/admin/redcta.org.ar/www/phase3/extensions/magpierss/rss_fetch.inc' ) ) { |
---|
36 | return "<br /><b>Error: Missing magpierss-0.71.1. Download from <a href=\"http://magpierss.sourceforge.net/\">Sourceforge</a> ". |
---|
37 | "and unpack to extensions/rss</b><br />"; |
---|
38 | } |
---|
39 | |
---|
40 | // Avoid unstubbing $wgParser too early on modern (1.12+) MW versions, as per r35980 |
---|
41 | if ( defined( 'MW_SUPPORTS_PARSERFIRSTCALLINIT' ) ) { |
---|
42 | $wgHooks['ParserFirstCallInit'][] = 'wfRssExtension'; |
---|
43 | } else { |
---|
44 | $wgExtensionFunctions[] = 'wfRssExtension'; |
---|
45 | } |
---|
46 | |
---|
47 | #Extension hook callback function |
---|
48 | function wfRssExtension() { |
---|
49 | global $wgParser; |
---|
50 | |
---|
51 | #Install parser hook for <rss> tags |
---|
52 | $wgParser->setHook( 'rss', 'renderRss' ); |
---|
53 | return true; |
---|
54 | } |
---|
55 | |
---|
56 | #Parser hook callback function |
---|
57 | function renderRss( $input ) { |
---|
58 | global $wgOutputEncoding, $wgParser; |
---|
59 | |
---|
60 | // Kill parser cache |
---|
61 | $wgParser->disableCache(); |
---|
62 | |
---|
63 | if ( !$input ) return ''; #if <rss>-section is empty, return nothing |
---|
64 | |
---|
65 | #Parse fields in rss-section |
---|
66 | $fields = explode( "|", $input ); |
---|
67 | $url = @$fields[0]; |
---|
68 | |
---|
69 | $args = array(); |
---|
70 | for ( $i = 1; $i < sizeof( $fields ); $i++ ) { |
---|
71 | $f = $fields[$i]; |
---|
72 | |
---|
73 | if ( strpos( $f, "=" ) === false ) $args[strtolower(trim($f))] = false; |
---|
74 | else { |
---|
75 | list( $k, $v ) = explode( "=", $f, 2 ); |
---|
76 | if ( trim( $v ) == false ) $args[strtolower(trim($k))] = false; |
---|
77 | else $args[strtolower(trim($k))] = trim($v); |
---|
78 | } |
---|
79 | } |
---|
80 | |
---|
81 | #Get charset from argument-array |
---|
82 | $charset = @$args['charset']; |
---|
83 | if( !$charset ) $charset = $wgOutputEncoding; |
---|
84 | #Get max number of headlines from argument-array |
---|
85 | $maxheads = @$args['max']; |
---|
86 | $headcnt = 0; |
---|
87 | |
---|
88 | #Get short-flag from argument-array |
---|
89 | #If short is set, no description text is printed |
---|
90 | if( isset( $args['short'] ) ) $short = true; else $short = false; |
---|
91 | #Get reverse-flag from argument-array |
---|
92 | if( isset( $args['reverse'] ) ) $reverse = true; else $reverse = false; |
---|
93 | #Get date-flag from argument-array |
---|
94 | if( isset( $args['date'] ) ) $date = true; else $date = false; |
---|
95 | #Get highlight terms from argument array |
---|
96 | $rssHighlight = @$args['highlight']; |
---|
97 | $rssHighlight = str_replace( ' ', ' ', $rssHighlight ); |
---|
98 | $rssHighlight = explode( ' ', trim( $rssHighlight ) ); |
---|
99 | |
---|
100 | #Get filter terms from argument-array |
---|
101 | $rssFilter = @$args['filter']; |
---|
102 | $rssFilter = str_replace( ' ', ' ', $rssFilter ); |
---|
103 | $rssFilter = explode( ' ', trim( $rssFilter ) ); |
---|
104 | |
---|
105 | #Filterout terms |
---|
106 | $rssFilterout = @$args['filterout']; |
---|
107 | $rssFilterout = str_replace( ' ', ' ', $rssFilterout ); |
---|
108 | $rssFilterout = explode( ' ', trim( $rssFilterout ) ); |
---|
109 | |
---|
110 | #Fetch RSS. May be cached locally. |
---|
111 | #Refer to the documentation of magpie for details. |
---|
112 | $rss = @fetch_rss( $url ); |
---|
113 | |
---|
114 | #Check for errors. |
---|
115 | if ( $rss->ERROR ) { |
---|
116 | return "<div>Failed to load RSS feed from $url: ".$rss->ERROR."</div>"; #localize
|
---|
117 | } |
---|
118 | |
---|
119 | if ( !is_array( $rss->items ) ) { |
---|
120 | return "<div>Failed to load RSS feed from $url!</div>"; #localize
|
---|
121 | } |
---|
122 | |
---|
123 | #Build title line |
---|
124 | #$title = iconv($charset, $wgOutputEncoding, $rss->channel['title']); |
---|
125 | #if( $rss->channel['link'] ) $title = "<a href='".$rss->channel['link']."'>$title</a>"; |
---|
126 | |
---|
127 | $output = ''; |
---|
128 | if( $reverse ) $rss->items = array_reverse( $rss->items ); |
---|
129 | $description = false; |
---|
130 | foreach ( $rss->items as $item ) { |
---|
131 | if ( $item['description'] ) { |
---|
132 | $description = true; |
---|
133 | break; |
---|
134 | } |
---|
135 | } |
---|
136 | |
---|
137 | #Build items |
---|
138 | if ( !$short and $description ) { #full item list |
---|
139 | $output.= '<dl>'; |
---|
140 | |
---|
141 | foreach ( $rss->items as $item ) { |
---|
142 | $d_text = true; |
---|
143 | $d_title = true; |
---|
144 | |
---|
145 | $href = trim( iconv( $charset, $wgOutputEncoding, $item['link'] ) ); |
---|
146 | $title = trim( iconv( $charset, $wgOutputEncoding, $item['title'] ) ); |
---|
147 | |
---|
148 | $pubdate = trim( iconv( $charset, $wgOutputEncoding, $item['pubdate'] ) ); |
---|
149 | $pubdate = date( "d.M. H:i", strtotime( $pubdate ) ); |
---|
150 | |
---|
151 | $d_title = wfRssFilter( $title, $rssFilter ); |
---|
152 | $d_title = wfRssFilterout( $title, $rssFilterout ); |
---|
153 | $title = wfRssHighlight( $title, $rssHighlight ); |
---|
154 | |
---|
155 | #Build description text if desired |
---|
156 | if ( $item['description'] ) { |
---|
157 | $text = trim( iconv( $charset, $wgOutputEncoding, $item['description'] ) ); |
---|
158 | #Avoid pre-tags |
---|
159 | $text = str_replace( "\r", ' ', $text ); |
---|
160 | $text = str_replace( "\n", ' ', $text ); |
---|
161 | $text = str_replace( "\t", ' ', $text ); |
---|
162 | $text = str_replace( '<br>', '', $text ); |
---|
163 | |
---|
164 | $d_text = wfRssFilter( $text, $rssFilter ); |
---|
165 | $d_text = wfRssFilterout( $text, $rssFilterout ); |
---|
166 | $text = wfRssHighlight( $text, $rssHighlight ); |
---|
167 | $display = $d_text or $d_title; |
---|
168 | } else { |
---|
169 | $text = ''; |
---|
170 | $display = $d_title; |
---|
171 | } |
---|
172 | if ( $display ) { |
---|
173 | $output.= "<dt><a href='$href'><b>$title</b></a></dt>"; |
---|
174 | if ( $date ) $output.= " ($pubdate)"; |
---|
175 | if ( $text ) $output.= "<dd>$text <b>[<a href='$href'>?</a>]</b></dd>"; |
---|
176 | } |
---|
177 | #Cut off output when maxheads is reached: |
---|
178 | if ( ++$headcnt == $maxheads ) break; |
---|
179 | } |
---|
180 | |
---|
181 | $output.= '</dl>'; |
---|
182 | } else { #short item list |
---|
183 | ## HACKY HACKY HACKY |
---|
184 | $output.= '<ul>'; |
---|
185 | $displayed = array(); |
---|
186 | foreach ( $rss->items as $item ) { |
---|
187 | $href = trim( iconv( $charset, $wgOutputEncoding, $item['link'] ) ); |
---|
188 | $title = trim( iconv( $charset, $wgOutputEncoding, $item['title'] ) ); |
---|
189 | $d_title = wfRssFilter( $title, $rssFilter ) && wfRssFilterout( $title, $rssFilterout ); |
---|
190 | $title = wfRssHighlight( $title, $rssHighlight ); |
---|
191 | $pubdate = trim( iconv( $charset, $wgOutputEncoding, $item['pubdate'] ) ); |
---|
192 | if ( $pubdate == '' ) { |
---|
193 | $pubdate = trim( iconv( $charset, $wgOutputEncoding, $item['dc']['date'] ) ); |
---|
194 | } |
---|
195 | $pubdate = date( "F d, Y H:i", strtotime( $pubdate ) ); |
---|
196 | |
---|
197 | if ( $d_title && !in_array( $title, $displayed ) ) { |
---|
198 | // Add date to ouput if specified |
---|
199 | $output.= '<li><a href="'.$href.'" title="'.$title.'">'.$title.'</a>'; |
---|
200 | if( $date ) { |
---|
201 | $output.= " ($pubdate)"; |
---|
202 | } |
---|
203 | $output.= '</li>'; |
---|
204 | |
---|
205 | $displayed[] = $title; |
---|
206 | #Cut off output when maxheads is reached: |
---|
207 | if ( ++$headcnt == $maxheads ) break; |
---|
208 | } |
---|
209 | } |
---|
210 | $output.= '</ul>'; |
---|
211 | } |
---|
212 | |
---|
213 | return $output; |
---|
214 | } |
---|
215 | |
---|
216 | function wfRssFilter( $text, $rssFilter ) { |
---|
217 | $display = true; |
---|
218 | if ( is_array( $rssFilter ) ) { |
---|
219 | foreach( $rssFilter as $term ) { |
---|
220 | if ( $term ) { |
---|
221 | $display = false; |
---|
222 | if ( preg_match( "|$term|i", $text, $a ) ) { |
---|
223 | $display = true; |
---|
224 | return $display; |
---|
225 | } |
---|
226 | } |
---|
227 | if ( $display ) break; |
---|
228 | } |
---|
229 | } |
---|
230 | return $display; |
---|
231 | } |
---|
232 | |
---|
233 | function wfRssFilterout( $text, $rssFilterout ) { |
---|
234 | $display = true; |
---|
235 | if ( is_array( $rssFilterout ) ) { |
---|
236 | foreach ( $rssFilterout as $term ) { |
---|
237 | if ( $term ) { |
---|
238 | if ( preg_match( "|$term|i", $text, $a ) ) { |
---|
239 | $display = false; |
---|
240 | return $display; |
---|
241 | } |
---|
242 | } |
---|
243 | } |
---|
244 | } |
---|
245 | return $display; |
---|
246 | } |
---|
247 | |
---|
248 | function wfRssHighlight( $text, $rssHighlight ) { |
---|
249 | $i = 0; |
---|
250 | $starttag = 'v8x5u3t3u8h'; |
---|
251 | $endtag = 'q8n4f6n4n4x'; |
---|
252 | |
---|
253 | $color[] = 'coral'; |
---|
254 | $color[] = 'greenyellow'; |
---|
255 | $color[] = 'lightskyblue'; |
---|
256 | $color[] = 'gold'; |
---|
257 | $color[] = 'violet'; |
---|
258 | $count_color = count( $color ); |
---|
259 | |
---|
260 | if ( is_array( $rssHighlight ) ) { |
---|
261 | foreach( $rssHighlight as $term ) { |
---|
262 | if ( $term ) { |
---|
263 | $text = preg_replace("|\b(\w*?".$term."\w*?)\b|i", "$starttag"."_".$i."\\1$endtag", $text); |
---|
264 | $i++; |
---|
265 | if ( $i == $count_color ) $i = 0; |
---|
266 | } |
---|
267 | } |
---|
268 | } |
---|
269 | |
---|
270 | #To avoid trouble should someone wants to highlight the terms "span", "style",
|
---|
271 | for ( $i = 0; $i < 5; $i++ ) { |
---|
272 | $text = preg_replace( "|$starttag"."_".$i."|", "<span style=\"background-color:".$color[$i]."; font-weight: bold;\">", $text ); |
---|
273 | $text = preg_replace( "|$endtag|", '</span>', $text ); |
---|
274 | } |
---|
275 | |
---|
276 | return $text; |
---|
277 | } |
---|
278 | ?> |
---|