xref: /plugin/struct/helper/db.php (revision bb8d98c4da8c9e71a9a1d62a6b706af3a628e17a)
1549a0837SAndreas Gohr<?php
2d6d97f60SAnna Dabrowska
3549a0837SAndreas Gohr/**
4549a0837SAndreas Gohr * DokuWiki Plugin struct (Helper Component)
5549a0837SAndreas Gohr *
6549a0837SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
7549a0837SAndreas Gohr * @author  Andreas Gohr, Michael Große <dokuwiki@cosmocode.de>
8549a0837SAndreas Gohr */
9549a0837SAndreas Gohr
107cbcfbdbSAndreas Gohruse dokuwiki\plugin\struct\meta\StructException;
117cbcfbdbSAndreas Gohr
12d6d97f60SAnna Dabrowskaclass helper_plugin_struct_db extends DokuWiki_Plugin
13d6d97f60SAnna Dabrowska{
14083afc55SAndreas Gohr    /** @var helper_plugin_sqlite */
15083afc55SAndreas Gohr    protected $sqlite;
16083afc55SAndreas Gohr
170fe33e72SAndreas Gohr    /**
180fe33e72SAndreas Gohr     * helper_plugin_struct_db constructor.
190fe33e72SAndreas Gohr     */
20d6d97f60SAnna Dabrowska    public function __construct()
21d6d97f60SAnna Dabrowska    {
220fe33e72SAndreas Gohr        $this->init();
230fe33e72SAndreas Gohr    }
240fe33e72SAndreas Gohr
250fe33e72SAndreas Gohr    /**
260fe33e72SAndreas Gohr     * Initialize the database
270fe33e72SAndreas Gohr     *
280fe33e72SAndreas Gohr     * @throws Exception
290fe33e72SAndreas Gohr     */
30d6d97f60SAnna Dabrowska    protected function init()
31d6d97f60SAnna Dabrowska    {
32083afc55SAndreas Gohr        /** @var helper_plugin_sqlite $sqlite */
33083afc55SAndreas Gohr        $this->sqlite = plugin_load('helper', 'sqlite');
34083afc55SAndreas Gohr        if (!$this->sqlite) {
3515929be2SAndreas Gohr            if (defined('DOKU_UNITTEST')) throw new \Exception('Couldn\'t load sqlite.');
36083afc55SAndreas Gohr            return;
37083afc55SAndreas Gohr        }
38083afc55SAndreas Gohr
39609bd281SMichael Große        if ($this->sqlite->getAdapter() === null) {
40609bd281SMichael Große            if (defined('DOKU_UNITTEST')) throw new \Exception('Couldn\'t load PDO sqlite.');
41609bd281SMichael Große            $this->sqlite = null;
42609bd281SMichael Große            return;
43609bd281SMichael Große        }
44609bd281SMichael Große
451c502704SAndreas Gohr        if ($this->sqlite->getAdapter()->getName() != DOKU_EXT_PDO) {
4615929be2SAndreas Gohr            if (defined('DOKU_UNITTEST')) throw new \Exception('Couldn\'t load PDO sqlite.');
471c502704SAndreas Gohr            $this->sqlite = null;
481c502704SAndreas Gohr            return;
491c502704SAndreas Gohr        }
501c502704SAndreas Gohr        $this->sqlite->getAdapter()->setUseNativeAlter(true);
511c502704SAndreas Gohr
52083afc55SAndreas Gohr        // initialize the database connection
53083afc55SAndreas Gohr        if (!$this->sqlite->init('struct', DOKU_PLUGIN . 'struct/db/')) {
5415929be2SAndreas Gohr            if (defined('DOKU_UNITTEST')) throw new \Exception('Couldn\'t init sqlite.');
557cbcfbdbSAndreas Gohr            $this->sqlite = null;
56083afc55SAndreas Gohr            return;
57083afc55SAndreas Gohr        }
588fefbb59SAndreas Gohr
598fefbb59SAndreas Gohr        // register our JSON function with variable parameters
608fefbb59SAndreas Gohr        // todo this might be useful to be moved into the sqlite plugin
610e72ef50SAndreas Gohr        $this->sqlite->create_function('STRUCT_JSON', array($this, 'STRUCT_JSON'), -1);
62*bb8d98c4SAnna Dabrowska
63*bb8d98c4SAnna Dabrowska        // this function is meant to be overwritten by plugins
64*bb8d98c4SAnna Dabrowska        $this->sqlite->create_function('IS_PUBLISHER', array($this, 'IS_PUBLISHER'), -1);
65083afc55SAndreas Gohr    }
66549a0837SAndreas Gohr
67549a0837SAndreas Gohr    /**
687cbcfbdbSAndreas Gohr     * @param bool $throw throw an Exception when sqlite not available?
69083afc55SAndreas Gohr     * @return helper_plugin_sqlite|null
70549a0837SAndreas Gohr     */
71d6d97f60SAnna Dabrowska    public function getDB($throw = true)
72d6d97f60SAnna Dabrowska    {
73aeca15adSMichael Grosse        global $conf;
74aeca15adSMichael Grosse        $len = strlen($conf['metadir']);
757cbcfbdbSAndreas Gohr        if ($this->sqlite && $conf['metadir'] != substr($this->sqlite->getAdapter()->getDbFile(), 0, $len)) {
76aeca15adSMichael Grosse            $this->init();
77aeca15adSMichael Grosse        }
787cbcfbdbSAndreas Gohr        if (!$this->sqlite && $throw) {
797cbcfbdbSAndreas Gohr            throw new StructException('no sqlite');
807cbcfbdbSAndreas Gohr        }
81083afc55SAndreas Gohr        return $this->sqlite;
82549a0837SAndreas Gohr    }
83549a0837SAndreas Gohr
840fe33e72SAndreas Gohr    /**
850fe33e72SAndreas Gohr     * Completely remove the database and reinitialize it
860fe33e72SAndreas Gohr     *
870fe33e72SAndreas Gohr     * You do not want to call this except for testing!
880fe33e72SAndreas Gohr     */
89d6d97f60SAnna Dabrowska    public function resetDB()
90d6d97f60SAnna Dabrowska    {
910fe33e72SAndreas Gohr        if (!$this->sqlite) return;
920fe33e72SAndreas Gohr        $file = $this->sqlite->getAdapter()->getDbFile();
930fe33e72SAndreas Gohr        if (!$file) return;
940fe33e72SAndreas Gohr        unlink($file);
950fe33e72SAndreas Gohr        clearstatcache(true, $file);
960fe33e72SAndreas Gohr        $this->init();
970fe33e72SAndreas Gohr    }
988fefbb59SAndreas Gohr
998fefbb59SAndreas Gohr    /**
1008fefbb59SAndreas Gohr     * Encodes all given arguments into a JSON encoded array
1018fefbb59SAndreas Gohr     *
1028fefbb59SAndreas Gohr     * @param string ...
1038fefbb59SAndreas Gohr     * @return string
1048fefbb59SAndreas Gohr     */
105748e747fSAnna Dabrowska    public function STRUCT_JSON() // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
106d6d97f60SAnna Dabrowska    {
1078fefbb59SAndreas Gohr        $args = func_get_args();
1088fefbb59SAndreas Gohr        return json_encode($args);
1098fefbb59SAndreas Gohr    }
110*bb8d98c4SAnna Dabrowska
111*bb8d98c4SAnna Dabrowska    /**
112*bb8d98c4SAnna Dabrowska     * This dummy implementation can be overwritten by a plugin
113*bb8d98c4SAnna Dabrowska     *
114*bb8d98c4SAnna Dabrowska     * @return bool
115*bb8d98c4SAnna Dabrowska     */
116*bb8d98c4SAnna Dabrowska    public function IS_PUBLISHER() // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
117*bb8d98c4SAnna Dabrowska    {
118*bb8d98c4SAnna Dabrowska        return true;
119*bb8d98c4SAnna Dabrowska    }
120549a0837SAndreas Gohr}
121549a0837SAndreas Gohr
122549a0837SAndreas Gohr// vim:ts=4:sw=4:et:
123