1<?php
2/**
3 * DokuWiki Plugin database2 (Syntax Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Oliver Geisen <oliver@rehkopf-geisen.de>
7 * @author  Thomas Urban <soletan@nihilum.de>
8 */
9
10// must be run within Dokuwiki
11if (!defined('DOKU_INC')) die();
12
13class syntax_plugin_database2 extends DokuWiki_Syntax_Plugin {
14	protected $dbName;
15	protected $tableName;
16	protected $options = array();
17
18    /**
19     * @return string Syntax mode type
20     */
21    public function getType() {
22        return 'substition';
23    }
24
25    /**
26     * @return string Paragraph type
27     */
28    public function getPType() {
29        return 'block';
30    }
31
32    /**
33     * @return int Sort order - Low numbers go before high numbers
34     */
35    public function getSort() {
36        return 158;
37    }
38
39    /**
40     * Connect lookup pattern to lexer.
41     *
42     * @param string $mode Parser mode
43     */
44    public function connectTo($mode) {
45        $this->Lexer->addEntryPattern("<database2.*?>(?=.*?</database2>)", $mode, 'plugin_database2');
46    }
47
48    public function postConnect() {
49        $this->Lexer->addExitPattern('</database2>','plugin_database2');
50    }
51
52    /**
53     * Handle matches of the database2 syntax
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        switch ( $state )
63        {
64            // extract tag's attributes
65            case DOKU_LEXER_ENTER :
66                $temp = trim(substr($match, strlen('database2')+1, -1)); # isolate options (if any)
67                $nameMap = array(
68                    'db'     => 'database',
69                    'dsn'    => 'database',
70                    'file'   => 'database',
71                    'host'   => 'database',
72                    'server' => 'database',
73                    'slot'   => 'auth',
74                );
75                $pos  = 0;
76                $args = array();
77                $this->load_database2();
78                while($pos < strlen($temp)) {
79                    $arg = Database2::parseAssignment($temp, $pos);
80                    if ($arg === false) {
81                        return false;
82                    }
83                    if (is_array($arg)) {
84                        list($name, $value) = $arg;
85                        $mapped = $nameMap[$name];
86                        if ($mapped) {
87                            $name = $mapped;
88                        }
89                        if (($value === true) && ! isset($args['table'])) {
90                            $args['table'] = $name;
91                            unset($args[$name]);
92                        } else {
93                            $args[$name] = $value;
94                        }
95                    } else {
96                        break;
97                    }
98                }
99                return array($state, $args);
100
101            case DOKU_LEXER_UNMATCHED :
102                return array($state, $match);
103
104            case DOKU_LEXER_EXIT :
105                return array($state, '');
106        }
107        return array();
108    }
109
110    /**
111     * Render xhtml output or metadata
112     *
113     * @param string         $mode      Renderer mode (supported modes: xhtml)
114     * @param Doku_Renderer  $renderer  The renderer
115     * @param array          $data      The data from the handler() function
116     * @return bool If rendering was successful.
117     */
118    public function render($mode, Doku_Renderer $renderer, $data) {
119        if ($mode == 'xhtml') {
120            list($state, $args) = $data;
121            switch ($state) {
122                case DOKU_LEXER_ENTER :
123                    $this->tableName = trim($args['table']);
124                    $this->dbName    = trim($args['database']);
125                    if ($this->dbName == '') {
126                        // missing explicit selection of database
127                        // --> choose file according to current page's namespace
128                        $this->dbName = getID();
129                    }
130                    $this->options = $args;
131                    break;
132
133                case DOKU_LEXER_UNMATCHED :
134                    $this->load_database2();
135                    $db = new Database2($renderer, $this);
136                    if ($db->connect($this->dbName, $this->options['auth'])) {
137                        $db->process( $this->tableName, $args, $this->options );
138                    }
139                    break;
140
141                case DOKU_LEXER_EXIT :
142                    break;
143            }
144            return true;
145        }
146        elseif ($mode == 'metadata') {
147            // metadata renderer tries to detect change of content to
148            // support page caching ... disable by providing random meta data
149            /**
150            * @todo implement better cache control here
151            */
152            $renderer->doc .= uniqid( mt_rand(), true );
153            return true;
154        }
155        return false;
156    }
157
158    /**
159     * Include library
160     */
161    private function load_database2() {
162        global $INFO;
163
164        if ( ! class_exists('Database2')) {
165            if (isset($INFO['user']) && $this->getConf('develusers')) {
166                $ua = explode(',',trim($this->getConf('develusers')));
167                if (in_array($INFO['user'], $ua) && is_file(dirname(__FILE__) . '/database2.dev.php')) {
168                    $libFile = dirname(__FILE__) . '/database2.dev.php';
169                    msg($this->getLang('usingdevel'), 2);
170                }
171            }
172            else {
173                $libFile = dirname(__FILE__) . '/database2.php';
174            }
175            require_once($libFile);
176        }
177    }
178}
179
180// vim:ts=4:sw=4:et:
181