xref: /plugin/struct/helper/db.php (revision cf8ea5507214f4063e624e01abaed1908c931d2b)
1<?php
2/**
3 * DokuWiki Plugin struct (Helper Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Andreas Gohr, Michael Große <dokuwiki@cosmocode.de>
7 */
8
9// must be run within Dokuwiki
10use dokuwiki\plugin\struct\meta\StructException;
11
12if(!defined('DOKU_INC')) die();
13
14class helper_plugin_struct_db extends DokuWiki_Plugin {
15    /** @var helper_plugin_sqlite */
16    protected $sqlite;
17
18    /**
19     * helper_plugin_struct_db constructor.
20     */
21    public function __construct() {
22        $this->init();
23    }
24
25    /**
26     * Initialize the database
27     *
28     * @throws Exception
29     */
30    protected function init() {
31        /** @var helper_plugin_sqlite $sqlite */
32        $this->sqlite = plugin_load('helper', 'sqlite');
33        if(!$this->sqlite) {
34            if(defined('DOKU_UNITTEST')) throw new \Exception('Couldn\'t load sqlite.');
35            return;
36        }
37
38        if ($this->sqlite->getAdapter() === null) {
39            if(defined('DOKU_UNITTEST')) throw new \Exception('Couldn\'t load PDO sqlite.');
40            $this->sqlite = null;
41            return;
42        }
43
44        if($this->sqlite->getAdapter()->getName() != DOKU_EXT_PDO) {
45            if(defined('DOKU_UNITTEST')) throw new \Exception('Couldn\'t load PDO sqlite.');
46            $this->sqlite = null;
47            return;
48        }
49        $this->sqlite->getAdapter()->setUseNativeAlter(true);
50
51        // initialize the database connection
52        if(!$this->sqlite->init('struct', DOKU_PLUGIN . 'struct/db/')) {
53            if(defined('DOKU_UNITTEST')) throw new \Exception('Couldn\'t init sqlite.');
54            $this->sqlite = null;
55            return;
56        }
57
58        // register our JSON function with variable parameters
59        // todo this might be useful to be moved into the sqlite plugin
60        $this->sqlite->create_function('STRUCT_JSON', array($this, 'STRUCT_JSON'), -1);
61    }
62
63    /**
64     * @param bool $throw throw an Exception when sqlite not available?
65     * @return helper_plugin_sqlite|null
66     */
67    public function getDB($throw=true) {
68        global $conf;
69        $len = strlen($conf['metadir']);
70        if ($this->sqlite && $conf['metadir'] != substr($this->sqlite->getAdapter()->getDbFile(),0,$len)) {
71            $this->init();
72        }
73        if(!$this->sqlite && $throw) {
74            throw new StructException('no sqlite');
75        }
76        return $this->sqlite;
77    }
78
79    /**
80     * Completely remove the database and reinitialize it
81     *
82     * You do not want to call this except for testing!
83     */
84    public function resetDB() {
85        if(!$this->sqlite) return;
86        $file = $this->sqlite->getAdapter()->getDbFile();
87        if(!$file) return;
88        unlink($file);
89        clearstatcache(true, $file);
90        $this->init();
91    }
92
93    /**
94     * Encodes all given arguments into a JSON encoded array
95     *
96     * @param string ...
97     * @return string
98     */
99    public function STRUCT_JSON() {
100        $args = func_get_args();
101        return json_encode($args);
102    }
103}
104
105// vim:ts=4:sw=4:et:
106