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