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