1<?php
2
3/**
4 * DokuWiki Plugin feedaggregator (Syntax Component)
5 *
6 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
7 * @author  Sam Wilson <sam@samwilson.id.au>
8 */
9// must be run within Dokuwiki
10if (!defined('DOKU_INC')) {
11    die();
12}
13
14class syntax_plugin_feedaggregator extends DokuWiki_Syntax_Plugin {
15
16    /**
17     * @return string Syntax mode type
18     */
19    public function getType() {
20        return 'container';
21    }
22
23    /**
24     * @return string Paragraph type
25     */
26    public function getPType() {
27        return 'block';
28    }
29
30    /**
31     * @return int Sort order - Low numbers go before high numbers
32     */
33    public function getSort() {
34        return 100;
35    }
36
37    /**
38     * Connect lookup pattern to lexer.
39     *
40     * @param string $mode Parser mode
41     */
42    public function connectTo($mode) {
43        //$this->Lexer->addSpecialPattern('<FIXME>', $mode, 'plugin_feedaggregator');
44        $this->Lexer->addEntryPattern('<feedaggregator>', $mode, 'plugin_feedaggregator');
45    }
46
47    public function postConnect() {
48        $this->Lexer->addExitPattern('</feedaggregator>', 'plugin_feedaggregator');
49    }
50
51    /**
52     * Handle matches of the <feedaggregator> tag, storing the list of feeds in
53     * a file in the ~/data/tmp/feedaggregator directory.
54     *
55     * @param string $match The match of the syntax
56     * @param int    $state The state of the handler
57     * @param int    $pos The position in the document
58     * @param Doku_Handler    $handler The handler
59     * @return array Data for the renderer
60     */
61    public function handle($match, $state, $pos, Doku_Handler $handler) {
62        global $conf;
63        $data = array();
64        // Are we to handle this match? If not, don't.
65        if ($state !== DOKU_LEXER_UNMATCHED) {
66            return $data;
67        }
68
69        // Get the feed URLs.
70        $matchedFeeds = preg_split('/[\n\r]+/', $match, -1, PREG_SPLIT_NO_EMPTY);
71        $feeds = array();
72        foreach ($matchedFeeds as $feed) {
73            if (filter_var($feed, FILTER_VALIDATE_URL) === false) {
74                msg("Feed URL not valid: <code>$feed</code>", 2);
75                continue;
76            }
77            $feeds[] = $feed;
78        }
79        $feedList = array_unique($feeds);
80
81        // Save the feeds to a temporary CSV. It'll be ready by the action script.
82        $file = fullpath($conf['tmpdir'].'/feedaggregator.csv');
83        file_put_contents($file, join("\n", $feedList));
84
85        // Get the most-recently generated HTML feed aggregation. This won't be
86        // up to date with the above feeds yet, but that's okay.
87        $data['html'] = io_readFile(fullpath($conf['cachedir'].'/feedaggregator/output.html'));
88
89        return $data;
90    }
91
92    /**
93     * Render xhtml output or metadata
94     *
95     * @param string         $mode      Renderer mode (supported modes: xhtml)
96     * @param Doku_Renderer  $renderer  The renderer
97     * @param array          $data      The data from the handler() function
98     * @return bool If rendering was successful.
99     */
100    public function render($mode, Doku_Renderer $renderer, $data) {
101        if ($mode != 'xhtml') {
102            return false;
103        }
104        $renderer->doc .= $data['html'];
105        return true;
106    }
107
108}
109