xref: /dokuwiki/inc/Parsing/ParserMode/Rss.php (revision 71096e46fcbfaeaa808667aba794e77fe2780169)
1<?php
2
3namespace dokuwiki\Parsing\ParserMode;
4
5use dokuwiki\Parsing\Handler;
6
7class Rss extends AbstractMode
8{
9    /** @inheritdoc */
10    public function getSort()
11    {
12        return 310;
13    }
14
15    /** @inheritdoc */
16    public function connectTo($mode)
17    {
18        $this->Lexer->addSpecialPattern("\{\{rss>[^\}]+\}\}", $mode, 'rss');
19    }
20
21    /** @inheritdoc */
22    public function handle($match, $state, $pos, Handler $handler)
23    {
24        $link = preg_replace(['/^\{\{rss>/', '/\}\}$/'], '', $match);
25
26        // get params
27        [$link, $params] = sexplode(' ', $link, 2, '');
28
29        $p = [];
30        if (preg_match('/\b(\d+)\b/', $params, $m)) {
31            $p['max'] = $m[1];
32        } else {
33            $p['max'] = 8;
34        }
35        $p['reverse'] = (preg_match('/rev/', $params));
36        $p['author'] = (preg_match('/\b(by|author)/', $params));
37        $p['date'] = (preg_match('/\b(date)/', $params));
38        $p['details'] = (preg_match('/\b(desc|detail)/', $params));
39        $p['nosort'] = (preg_match('/\b(nosort)\b/', $params));
40
41        if (preg_match('/\b(\d+)([dhm])\b/', $params, $m)) {
42            $period = ['d' => 86400, 'h' => 3600, 'm' => 60];
43            $p['refresh'] = max(600, $m[1] * $period[$m[2]]);  // n * period in seconds, minimum 10 minutes
44        } else {
45            $p['refresh'] = 14400;   // default to 4 hours
46        }
47
48        $handler->addCall('rss', [$link, $p], $pos);
49        return true;
50    }
51}
52