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