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