xref: /dokuwiki/feed.php (revision 9329b002986cc3f43c18c207dd9d0fdfd0f8a5e8)
1<?php
2/**
3 * XML feed export
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Andreas Gohr <andi@splitbrain.org>
7 *
8 * @global array $conf
9 * @global Input $INPUT
10 */
11
12use dokuwiki\Cache\Cache;
13use dokuwiki\ChangeLog\MediaChangeLog;
14use dokuwiki\ChangeLog\PageChangeLog;
15use dokuwiki\Extension\AuthPlugin;
16use dokuwiki\Extension\Event;
17use dokuwiki\Search\FulltextSearch;
18
19if(!defined('DOKU_INC')) define('DOKU_INC', dirname(__FILE__).'/');
20require_once(DOKU_INC.'inc/init.php');
21
22//close session
23session_write_close();
24
25//feed disabled?
26if(!actionOK('rss')) {
27    http_status(404);
28    echo '<error>RSS feed is disabled.</error>';
29    exit;
30}
31
32// get params
33$opt = rss_parseOptions();
34
35// the feed is dynamic - we need a cache for each combo
36// (but most people just use the default feed so it's still effective)
37$key   = join('', array_values($opt)).'$'.$_SERVER['REMOTE_USER'].'$'.$_SERVER['HTTP_HOST'].$_SERVER['SERVER_PORT'];
38$cache = new Cache($key, '.feed');
39
40// prepare cache depends
41$depends['files'] = getConfigFiles('main');
42$depends['age']   = $conf['rss_update'];
43$depends['purge'] = $INPUT->bool('purge');
44
45// check cacheage and deliver if nothing has changed since last
46// time or the update interval has not passed, also handles conditional requests
47header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
48header('Pragma: public');
49header('Content-Type: application/xml; charset=utf-8');
50header('X-Robots-Tag: noindex');
51if($cache->useCache($depends)) {
52    http_conditionalRequest($cache->getTime());
53    if($conf['allowdebug']) header("X-CacheUsed: $cache->cache");
54    print $cache->retrieveCache();
55    exit;
56} else {
57    http_conditionalRequest(time());
58}
59
60// create new feed
61$rss                 = new UniversalFeedCreator();
62$rss->title          = $conf['title'].(($opt['namespace']) ? ' '.$opt['namespace'] : '');
63$rss->link           = DOKU_URL;
64$rss->syndicationURL = DOKU_URL.'feed.php';
65$rss->cssStyleSheet  = DOKU_URL.'lib/exe/css.php?s=feed';
66
67$image        = new FeedImage();
68$image->title = $conf['title'];
69$image->url   = tpl_getMediaFile(array(':wiki:favicon.ico', ':favicon.ico', 'images/favicon.ico'), true);
70$image->link  = DOKU_URL;
71$rss->image   = $image;
72
73$data  = null;
74$modes = array(
75    'list'   => 'rssListNamespace',
76    'search' => 'rssSearch',
77    'recent' => 'rssRecentChanges'
78);
79if(isset($modes[$opt['feed_mode']])) {
80    $data = $modes[$opt['feed_mode']]($opt);
81} else {
82    $eventData = array(
83        'opt'  => &$opt,
84        'data' => &$data,
85    );
86    $event     = new Event('FEED_MODE_UNKNOWN', $eventData);
87    if($event->advise_before(true)) {
88        echo sprintf('<error>Unknown feed mode %s</error>', hsc($opt['feed_mode']));
89        exit;
90    }
91    $event->advise_after();
92}
93
94rss_buildItems($rss, $data, $opt);
95$feed = $rss->createFeed($opt['feed_type']);
96
97// save cachefile
98$cache->storeCache($feed);
99
100// finally deliver
101print $feed;
102
103// ---------------------------------------------------------------- //
104
105/**
106 * Get URL parameters and config options and return an initialized option array
107 *
108 * @author Andreas Gohr <andi@splitbrain.org>
109 */
110function rss_parseOptions() {
111    global $conf;
112    global $INPUT;
113
114    $opt = array();
115
116    foreach(array(
117                // Basic feed properties
118                // Plugins may probably want to add new values to these
119                // properties for implementing own feeds
120
121                // One of: list, search, recent
122                'feed_mode'    => array('str', 'mode', 'recent'),
123                // One of: diff, page, rev, current
124                'link_to'      => array('str', 'linkto', $conf['rss_linkto']),
125                // One of: abstract, diff, htmldiff, html
126                'item_content' => array('str', 'content', $conf['rss_content']),
127
128                // Special feed properties
129                // These are only used by certain feed_modes
130
131                // String, used for feed title, in list and rc mode
132                'namespace'    => array('str', 'ns', null),
133                // Positive integer, only used in rc mode
134                'items'        => array('int', 'num', $conf['recent']),
135                // Boolean, only used in rc mode
136                'show_minor'   => array('bool', 'minor', false),
137                // String, only used in list mode
138                'sort'         => array('str', 'sort', 'natural'),
139                // String, only used in search mode
140                'search_query' => array('str', 'q', null),
141                // One of: pages, media, both
142                'content_type' => array('str', 'view', $conf['rss_media'])
143
144            ) as $name => $val) {
145        $opt[$name] = $INPUT->{$val[0]}($val[1], $val[2], true);
146    }
147
148    $opt['items']      = max(0, (int) $opt['items']);
149    $opt['show_minor'] = (bool) $opt['show_minor'];
150    $opt['sort'] = valid_input_set('sort', array('default' => 'natural', 'date'), $opt);
151
152    $opt['guardmail'] = ($conf['mailguard'] != '' && $conf['mailguard'] != 'none');
153
154    $type = $INPUT->valid(
155        'type',
156        array( 'rss', 'rss2', 'atom', 'atom1', 'rss1'),
157        $conf['rss_type']
158    );
159    switch($type) {
160        case 'rss':
161            $opt['feed_type'] = 'RSS0.91';
162            $opt['mime_type'] = 'text/xml';
163            break;
164        case 'rss2':
165            $opt['feed_type'] = 'RSS2.0';
166            $opt['mime_type'] = 'text/xml';
167            break;
168        case 'atom':
169            $opt['feed_type'] = 'ATOM0.3';
170            $opt['mime_type'] = 'application/xml';
171            break;
172        case 'atom1':
173            $opt['feed_type'] = 'ATOM1.0';
174            $opt['mime_type'] = 'application/atom+xml';
175            break;
176        default:
177            $opt['feed_type'] = 'RSS1.0';
178            $opt['mime_type'] = 'application/xml';
179    }
180
181    $eventData = array(
182        'opt' => &$opt,
183    );
184    Event::createAndTrigger('FEED_OPTS_POSTPROCESS', $eventData);
185    return $opt;
186}
187
188/**
189 * Add recent changed pages to a feed object
190 *
191 * @author Andreas Gohr <andi@splitbrain.org>
192 * @param  FeedCreator $rss the FeedCreator Object
193 * @param  array       $data the items to add
194 * @param  array       $opt  the feed options
195 */
196function rss_buildItems(&$rss, &$data, $opt) {
197    global $conf;
198    global $lang;
199    /* @var AuthPlugin $auth */
200    global $auth;
201
202    $eventData = array(
203        'rss'  => &$rss,
204        'data' => &$data,
205        'opt'  => &$opt,
206    );
207    $event     = new Event('FEED_DATA_PROCESS', $eventData);
208    if($event->advise_before(false)) {
209        foreach($data as $ditem) {
210            if(!is_array($ditem)) {
211                // not an array? then only a list of IDs was given
212                $ditem = array('id' => $ditem);
213            }
214
215            $item = new FeedItem();
216            $id   = $ditem['id'];
217            if(!$ditem['media']) {
218                $meta = p_get_metadata($id);
219            } else {
220                $meta = array();
221            }
222
223            // add date
224            if($ditem['date']) {
225                $date = $ditem['date'];
226            } elseif ($ditem['media']) {
227                $date = @filemtime(mediaFN($id));
228            } elseif (file_exists(wikiFN($id))) {
229                $date = @filemtime(wikiFN($id));
230            } elseif($meta['date']['modified']) {
231                $date = $meta['date']['modified'];
232            } else {
233                $date = 0;
234            }
235            if($date) $item->date = date('r', $date);
236
237            // add title
238            if($conf['useheading'] && $meta['title']) {
239                $item->title = $meta['title'];
240            } else {
241                $item->title = $ditem['id'];
242            }
243            if($conf['rss_show_summary'] && !empty($ditem['sum'])) {
244                $item->title .= ' - '.strip_tags($ditem['sum']);
245            }
246
247            // add item link
248            switch($opt['link_to']) {
249                case 'page':
250                    if($ditem['media']) {
251                        $item->link = media_managerURL(
252                            array(
253                                 'image' => $id,
254                                 'ns'    => getNS($id),
255                                 'rev'   => $date
256                            ), '&', true
257                        );
258                    } else {
259                        $item->link = wl($id, 'rev='.$date, true, '&');
260                    }
261                    break;
262                case 'rev':
263                    if($ditem['media']) {
264                        $item->link = media_managerURL(
265                            array(
266                                 'image'       => $id,
267                                 'ns'          => getNS($id),
268                                 'rev'         => $date,
269                                 'tab_details' => 'history'
270                            ), '&', true
271                        );
272                    } else {
273                        $item->link = wl($id, 'do=revisions&rev='.$date, true, '&');
274                    }
275                    break;
276                case 'current':
277                    if($ditem['media']) {
278                        $item->link = media_managerURL(
279                            array(
280                                 'image' => $id,
281                                 'ns'    => getNS($id)
282                            ), '&', true
283                        );
284                    } else {
285                        $item->link = wl($id, '', true, '&');
286                    }
287                    break;
288                case 'diff':
289                default:
290                    if($ditem['media']) {
291                        $item->link = media_managerURL(
292                            array(
293                                 'image'       => $id,
294                                 'ns'          => getNS($id),
295                                 'rev'         => $date,
296                                 'tab_details' => 'history',
297                                 'mediado'     => 'diff'
298                            ), '&', true
299                        );
300                    } else {
301                        $item->link = wl($id, 'rev='.$date.'&do=diff', true, '&');
302                    }
303            }
304
305            // add item content
306            switch($opt['item_content']) {
307                case 'diff':
308                case 'htmldiff':
309                    if($ditem['media']) {
310                        $medialog = new MediaChangeLog($id);
311                        $revs  = $medialog->getRevisions(0, 1);
312                        $rev   = $revs[0];
313                        $src_r = '';
314                        $src_l = '';
315
316                        if($size = media_image_preview_size($id, '', new JpegMeta(mediaFN($id)), 300)) {
317                            $more  = 'w='.$size[0].'&h='.$size[1].'&t='.@filemtime(mediaFN($id));
318                            $src_r = ml($id, $more, true, '&amp;', true);
319                        }
320                        if($rev && $size = media_image_preview_size($id, $rev, new JpegMeta(mediaFN($id, $rev)), 300)) {
321                            $more  = 'rev='.$rev.'&w='.$size[0].'&h='.$size[1];
322                            $src_l = ml($id, $more, true, '&amp;', true);
323                        }
324                        $content = '';
325                        if($src_r) {
326                            $content = '<table>';
327                            $content .= '<tr><th width="50%">'.$rev.'</th>';
328                            $content .= '<th width="50%">'.$lang['current'].'</th></tr>';
329                            $content .= '<tr align="center"><td><img src="'.$src_l.'" alt="" /></td><td>';
330                            $content .= '<img src="'.$src_r.'" alt="'.$id.'" /></td></tr>';
331                            $content .= '</table>';
332                        }
333
334                    } else {
335                        require_once(DOKU_INC.'inc/DifferenceEngine.php');
336                        $pagelog = new PageChangeLog($id);
337                        $revs = $pagelog->getRevisions(0, 1);
338                        $rev  = $revs[0];
339
340                        if($rev) {
341                            $df = new Diff(explode("\n", rawWiki($id, $rev)),
342                                           explode("\n", rawWiki($id, '')));
343                        } else {
344                            $df = new Diff(array(''),
345                                           explode("\n", rawWiki($id, '')));
346                        }
347
348                        if($opt['item_content'] == 'htmldiff') {
349                            // note: no need to escape diff output, TableDiffFormatter provides 'safe' html
350                            $tdf     = new TableDiffFormatter();
351                            $content = '<table>';
352                            $content .= '<tr><th colspan="2" width="50%">'.$rev.'</th>';
353                            $content .= '<th colspan="2" width="50%">'.$lang['current'].'</th></tr>';
354                            $content .= $tdf->format($df);
355                            $content .= '</table>';
356                        } else {
357                            // note: diff output must be escaped, UnifiedDiffFormatter provides plain text
358                            $udf     = new UnifiedDiffFormatter();
359                            $content = "<pre>\n".hsc($udf->format($df))."\n</pre>";
360                        }
361                    }
362                    break;
363                case 'html':
364                    if($ditem['media']) {
365                        if($size = media_image_preview_size($id, '', new JpegMeta(mediaFN($id)))) {
366                            $more    = 'w='.$size[0].'&h='.$size[1].'&t='.@filemtime(mediaFN($id));
367                            $src     = ml($id, $more, true, '&amp;', true);
368                            $content = '<img src="'.$src.'" alt="'.$id.'" />';
369                        } else {
370                            $content = '';
371                        }
372                    } else {
373                        if (@filemtime(wikiFN($id)) === $date) {
374                            $content = p_wiki_xhtml($id, '', false);
375                        } else {
376                            $content = p_wiki_xhtml($id, $date, false);
377                        }
378                        // no TOC in feeds
379                        $content = preg_replace('/(<!-- TOC START -->).*(<!-- TOC END -->)/s', '', $content);
380
381                        // add alignment for images
382                        $content = preg_replace('/(<img .*?class="medialeft")/s', '\\1 align="left"', $content);
383                        $content = preg_replace('/(<img .*?class="mediaright")/s', '\\1 align="right"', $content);
384
385                        // make URLs work when canonical is not set, regexp instead of rerendering!
386                        if(!$conf['canonical']) {
387                            $base    = preg_quote(DOKU_REL, '/');
388                            $content = preg_replace('/(<a href|<img src)="('.$base.')/s', '$1="'.DOKU_URL, $content);
389                        }
390                    }
391
392                    break;
393                case 'abstract':
394                default:
395                    if($ditem['media']) {
396                        if($size = media_image_preview_size($id, '', new JpegMeta(mediaFN($id)))) {
397                            $more    = 'w='.$size[0].'&h='.$size[1].'&t='.@filemtime(mediaFN($id));
398                            $src     = ml($id, $more, true, '&amp;', true);
399                            $content = '<img src="'.$src.'" alt="'.$id.'" />';
400                        } else {
401                            $content = '';
402                        }
403                    } else {
404                        $content = $meta['description']['abstract'];
405                    }
406            }
407            $item->description = $content; //FIXME a plugin hook here could be senseful
408
409            // add user
410            # FIXME should the user be pulled from metadata as well?
411            $user         = @$ditem['user']; // the @ spares time repeating lookup
412            if(blank($user)) {
413                $item->author = 'Anonymous';
414                $item->authorEmail = 'anonymous@undisclosed.example.com';
415            } else {
416                $item->author = $user;
417                $item->authorEmail = $user . '@undisclosed.example.com';
418
419                // get real user name if configured
420                if($conf['useacl'] && $auth) {
421                    $userInfo = $auth->getUserData($user);
422                    if($userInfo) {
423                        switch($conf['showuseras']) {
424                            case 'username':
425                            case 'username_link':
426                                $item->author = $userInfo['name'];
427                                break;
428                            default:
429                                $item->author = $user;
430                                break;
431                        }
432                    } else {
433                        $item->author = $user;
434                    }
435                }
436            }
437
438            // add category
439            if(isset($meta['subject'])) {
440                $item->category = $meta['subject'];
441            } else {
442                $cat = getNS($id);
443                if($cat) $item->category = $cat;
444            }
445
446            // finally add the item to the feed object, after handing it to registered plugins
447            $evdata = array(
448                'item'  => &$item,
449                'opt'   => &$opt,
450                'ditem' => &$ditem,
451                'rss'   => &$rss
452            );
453            $evt    = new Event('FEED_ITEM_ADD', $evdata);
454            if($evt->advise_before()) {
455                $rss->addItem($item);
456            }
457            $evt->advise_after(); // for completeness
458        }
459    }
460    $event->advise_after();
461}
462
463/**
464 * Add recent changed pages to the feed object
465 *
466 * @author Andreas Gohr <andi@splitbrain.org>
467 */
468function rssRecentChanges($opt) {
469    global $conf;
470    $flags = RECENTS_SKIP_DELETED;
471    if(!$opt['show_minor']) $flags += RECENTS_SKIP_MINORS;
472    if($opt['content_type'] == 'media' && $conf['mediarevisions']) $flags += RECENTS_MEDIA_CHANGES;
473    if($opt['content_type'] == 'both' && $conf['mediarevisions']) $flags += RECENTS_MEDIA_PAGES_MIXED;
474
475    $recents = getRecents(0, $opt['items'], $opt['namespace'], $flags);
476    return $recents;
477}
478
479/**
480 * Add all pages of a namespace to the feed object
481 *
482 * @author Andreas Gohr <andi@splitbrain.org>
483 */
484function rssListNamespace($opt) {
485    require_once(DOKU_INC.'inc/search.php');
486    global $conf;
487
488    $ns = ':'.cleanID($opt['namespace']);
489    $ns = utf8_encodeFN(str_replace(':', '/', $ns));
490
491    $data = array();
492    $search_opts = array(
493        'depth' => 1,
494        'pagesonly' => true,
495        'listfiles' => true
496    );
497    search($data, $conf['datadir'], 'search_universal', $search_opts, $ns, $lvl = 1, $opt['sort']);
498
499    return $data;
500}
501
502/**
503 * Add the result of a full text search to the feed object
504 *
505 * @author Andreas Gohr <andi@splitbrain.org>
506 */
507function rssSearch($opt) {
508    if (!$opt['search_query']) return array();
509
510    $FulltextSearch = FulltextSearch::getInstance();
511    $data = $FulltextSearch->pageSearch($opt['search_query'], $poswords);
512    $data = array_keys($data);
513
514    return $data;
515}
516
517//Setup VIM: ex: et ts=4 :
518