source: documentation/trunk/packages/dokuwiki-2011-05-25a/feed.php@ 25031

Last change on this file since 25031 was 25027, checked in by jmt12, 12 years ago

Adding the packages directory, and within it a configured version of dokuwiki all ready to run

File size: 12.5 KB
Line 
1<?php
2/**
3 * XML feed export
4 *
5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author Andreas Gohr <[email protected]>
7 */
8
9if(!defined('DOKU_INC')) define('DOKU_INC',dirname(__FILE__).'/');
10require_once(DOKU_INC.'inc/init.php');
11
12//close session
13session_write_close();
14
15// get params
16$opt = rss_parseOptions();
17
18// the feed is dynamic - we need a cache for each combo
19// (but most people just use the default feed so it's still effective)
20$cache = getCacheName(join('',array_values($opt)).$_SERVER['REMOTE_USER'],'.feed');
21$key = join('', array_values($opt)) . $_SERVER['REMOTE_USER'];
22$cache = new cache($key, '.feed');
23
24// prepare cache depends
25$depends['files'] = getConfigFiles('main');
26$depends['age'] = $conf['rss_update'];
27$depends['purge'] = isset($_REQUEST['purge']);
28
29// check cacheage and deliver if nothing has changed since last
30// time or the update interval has not passed, also handles conditional requests
31header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
32header('Pragma: public');
33header('Content-Type: application/xml; charset=utf-8');
34header('X-Robots-Tag: noindex');
35if($cache->useCache($depends)) {
36 http_conditionalRequest($cache->_time);
37 if($conf['allowdebug']) header("X-CacheUsed: $cache->cache");
38 print $cache->retrieveCache();
39 exit;
40} else {
41 http_conditionalRequest(time());
42 }
43
44// create new feed
45$rss = new DokuWikiFeedCreator();
46$rss->title = $conf['title'].(($opt['namespace']) ? ' '.$opt['namespace'] : '');
47$rss->link = DOKU_URL;
48$rss->syndicationURL = DOKU_URL.'feed.php';
49$rss->cssStyleSheet = DOKU_URL.'lib/exe/css.php?s=feed';
50
51$image = new FeedImage();
52$image->title = $conf['title'];
53$image->url = tpl_getFavicon(true);
54$image->link = DOKU_URL;
55$rss->image = $image;
56
57$data = null;
58$modes = array('list' => 'rssListNamespace',
59 'search' => 'rssSearch',
60 'recent' => 'rssRecentChanges');
61if (isset($modes[$opt['feed_mode']])) {
62 $data = $modes[$opt['feed_mode']]($opt);
63} else {
64 $eventData = array(
65 'opt' => &$opt,
66 'data' => &$data,
67 );
68 $event = new Doku_Event('FEED_MODE_UNKNOWN', $eventData);
69 if ($event->advise_before(true)) {
70 echo sprintf('<error>Unknown feed mode %s</error>', hsc($opt['feed_mode']));
71 exit;
72 }
73 $event->advise_after();
74}
75
76rss_buildItems($rss, $data, $opt);
77$feed = $rss->createFeed($opt['feed_type'],'utf-8');
78
79// save cachefile
80$cache->storeCache($feed);
81
82// finally deliver
83print $feed;
84
85// ---------------------------------------------------------------- //
86
87/**
88 * Get URL parameters and config options and return an initialized option array
89 *
90 * @author Andreas Gohr <[email protected]>
91 */
92function rss_parseOptions(){
93 global $conf;
94
95 $opt = array();
96
97 foreach(array(
98 // Basic feed properties
99 // Plugins may probably want to add new values to these
100 // properties for implementing own feeds
101
102 // One of: list, search, recent
103 'feed_mode' => array('mode', 'recent'),
104 // One of: diff, page, rev, current
105 'link_to' => array('linkto', $conf['rss_linkto']),
106 // One of: abstract, diff, htmldiff, html
107 'item_content' => array('content', $conf['rss_content']),
108
109 // Special feed properties
110 // These are only used by certain feed_modes
111
112 // String, used for feed title, in list and rc mode
113 'namespace' => array('ns', null),
114 // Positive integer, only used in rc mode
115 'items' => array('num', $conf['recent']),
116 // Boolean, only used in rc mode
117 'show_minor' => array('minor', false),
118 // String, only used in search mode
119 'search_query' => array('q', null),
120
121 ) as $name => $val) {
122 $opt[$name] = (isset($_REQUEST[$val[0]]) && !empty($_REQUEST[$val[0]]))
123 ? $_REQUEST[$val[0]] : $val[1];
124 }
125
126 $opt['items'] = max(0, (int) $opt['items']);
127 $opt['show_minor'] = (bool) $opt['show_minor'];
128
129 $opt['guardmail'] = ($conf['mailguard'] != '' && $conf['mailguard'] != 'none');
130
131 $type = valid_input_set('type', array('rss','rss2','atom','atom1','rss1',
132 'default' => $conf['rss_type']),
133 $_REQUEST);
134 switch ($type){
135 case 'rss':
136 $opt['feed_type'] = 'RSS0.91';
137 $opt['mime_type'] = 'text/xml';
138 break;
139 case 'rss2':
140 $opt['feed_type'] = 'RSS2.0';
141 $opt['mime_type'] = 'text/xml';
142 break;
143 case 'atom':
144 $opt['feed_type'] = 'ATOM0.3';
145 $opt['mime_type'] = 'application/xml';
146 break;
147 case 'atom1':
148 $opt['feed_type'] = 'ATOM1.0';
149 $opt['mime_type'] = 'application/atom+xml';
150 break;
151 default:
152 $opt['feed_type'] = 'RSS1.0';
153 $opt['mime_type'] = 'application/xml';
154 }
155
156 $eventData = array(
157 'opt' => &$opt,
158 );
159 trigger_event('FEED_OPTS_POSTPROCESS', $eventData);
160 return $opt;
161}
162
163/**
164 * Add recent changed pages to a feed object
165 *
166 * @author Andreas Gohr <[email protected]>
167 * @param object $rss - the FeedCreator Object
168 * @param array $data - the items to add
169 * @param array $opt - the feed options
170 */
171function rss_buildItems(&$rss,&$data,$opt){
172 global $conf;
173 global $lang;
174 global $auth;
175
176 $eventData = array(
177 'rss' => &$rss,
178 'data' => &$data,
179 'opt' => &$opt,
180 );
181 $event = new Doku_Event('FEED_DATA_PROCESS', $eventData);
182 if ($event->advise_before(false)){
183 foreach($data as $ditem){
184 if(!is_array($ditem)){
185 // not an array? then only a list of IDs was given
186 $ditem = array( 'id' => $ditem );
187 }
188
189 $item = new FeedItem();
190 $id = $ditem['id'];
191 $meta = p_get_metadata($id);
192
193 // add date
194 if($ditem['date']){
195 $date = $ditem['date'];
196 }elseif($meta['date']['modified']){
197 $date = $meta['date']['modified'];
198 }else{
199 $date = @filemtime(wikiFN($id));
200 }
201 if($date) $item->date = date('r',$date);
202
203 // add title
204 if($conf['useheading'] && $meta['title']){
205 $item->title = $meta['title'];
206 }else{
207 $item->title = $ditem['id'];
208 }
209 if($conf['rss_show_summary'] && !empty($ditem['sum'])){
210 $item->title .= ' - '.strip_tags($ditem['sum']);
211 }
212
213 // add item link
214 switch ($opt['link_to']){
215 case 'page':
216 $item->link = wl($id,'rev='.$date,true,'&');
217 break;
218 case 'rev':
219 $item->link = wl($id,'do=revisions&rev='.$date,true,'&');
220 break;
221 case 'current':
222 $item->link = wl($id, '', true,'&');
223 break;
224 case 'diff':
225 default:
226 $item->link = wl($id,'rev='.$date.'&do=diff',true,'&');
227 }
228
229 // add item content
230 switch ($opt['item_content']){
231 case 'diff':
232 case 'htmldiff':
233 require_once(DOKU_INC.'inc/DifferenceEngine.php');
234 $revs = getRevisions($id, 0, 1);
235 $rev = $revs[0];
236
237 if($rev){
238 $df = new Diff(explode("\n",htmlspecialchars(rawWiki($id,$rev))),
239 explode("\n",htmlspecialchars(rawWiki($id,''))));
240 }else{
241 $df = new Diff(array(''),
242 explode("\n",htmlspecialchars(rawWiki($id,''))));
243 }
244
245 if($opt['item_content'] == 'htmldiff'){
246 $tdf = new TableDiffFormatter();
247 $content = '<table>';
248 $content .= '<tr><th colspan="2" width="50%">'.$rev.'</th>';
249 $content .= '<th colspan="2" width="50%">'.$lang['current'].'</th></tr>';
250 $content .= $tdf->format($df);
251 $content .= '</table>';
252 }else{
253 $udf = new UnifiedDiffFormatter();
254 $content = "<pre>\n".$udf->format($df)."\n</pre>";
255 }
256 break;
257 case 'html':
258 $content = p_wiki_xhtml($id,$date,false);
259 // no TOC in feeds
260 $content = preg_replace('/(<!-- TOC START -->).*(<!-- TOC END -->)/s','',$content);
261
262 // make URLs work when canonical is not set, regexp instead of rerendering!
263 if(!$conf['canonical']){
264 $base = preg_quote(DOKU_REL,'/');
265 $content = preg_replace('/(<a href|<img src)="('.$base.')/s','$1="'.DOKU_URL,$content);
266 }
267
268 break;
269 case 'abstract':
270 default:
271 $content = $meta['description']['abstract'];
272 }
273 $item->description = $content; //FIXME a plugin hook here could be senseful
274
275 // add user
276 # FIXME should the user be pulled from metadata as well?
277 $user = @$ditem['user']; // the @ spares time repeating lookup
278 $item->author = '';
279 if($user && $conf['useacl'] && $auth){
280 $userInfo = $auth->getUserData($user);
281 if ($userInfo){
282 switch ($conf['showuseras']){
283 case 'username':
284 $item->author = $userInfo['name'];
285 break;
286 default:
287 $item->author = $user;
288 break;
289 }
290 } else {
291 $item->author = $user;
292 }
293 if($userInfo && !$opt['guardmail']){
294 $item->authorEmail = $userInfo['mail'];
295 }else{
296 //cannot obfuscate because some RSS readers may check validity
297 $item->authorEmail = $user.'@'.$ditem['ip'];
298 }
299 }elseif($user){
300 // this happens when no ACL but some Apache auth is used
301 $item->author = $user;
302 $item->authorEmail = $user.'@'.$ditem['ip'];
303 }else{
304 $item->authorEmail = 'anonymous@'.$ditem['ip'];
305 }
306
307 // add category
308 if(isset($meta['subject'])) {
309 $item->category = $meta['subject'];
310 }else{
311 $cat = getNS($id);
312 if($cat) $item->category = $cat;
313 }
314
315 // finally add the item to the feed object, after handing it to registered plugins
316 $evdata = array('item' => &$item,
317 'opt' => &$opt,
318 'ditem' => &$ditem,
319 'rss' => &$rss);
320 $evt = new Doku_Event('FEED_ITEM_ADD', $evdata);
321 if ($evt->advise_before()){
322 $rss->addItem($item);
323 }
324 $evt->advise_after(); // for completeness
325 }
326 }
327 $event->advise_after();
328}
329
330
331/**
332 * Add recent changed pages to the feed object
333 *
334 * @author Andreas Gohr <[email protected]>
335 */
336function rssRecentChanges($opt){
337 $flags = RECENTS_SKIP_DELETED;
338 if(!$opt['show_minor']) $flags += RECENTS_SKIP_MINORS;
339
340 $recents = getRecents(0,$opt['items'],$opt['namespace'],$flags);
341 return $recents;
342}
343
344/**
345 * Add all pages of a namespace to the feed object
346 *
347 * @author Andreas Gohr <[email protected]>
348 */
349function rssListNamespace($opt){
350 require_once(DOKU_INC.'inc/search.php');
351 global $conf;
352
353 $ns=':'.cleanID($opt['namespace']);
354 $ns=str_replace(':','/',$ns);
355
356 $data = array();
357 sort($data);
358 search($data,$conf['datadir'],'search_list','',$ns);
359
360 return $data;
361}
362
363/**
364 * Add the result of a full text search to the feed object
365 *
366 * @author Andreas Gohr <[email protected]>
367 */
368function rssSearch($opt){
369 if(!$opt['search_query']) return;
370
371 require_once(DOKU_INC.'inc/fulltext.php');
372 $data = ft_pageSearch($opt['search_query'],$poswords);
373 $data = array_keys($data);
374
375 return $data;
376}
377
378//Setup VIM: ex: et ts=4 :
Note: See TracBrowser for help on using the repository browser.