xref: /plugin/struct/helper/db.php (revision 7938ca48e9616e2e020a922137123f8442612482)
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
10if(!defined('DOKU_INC')) die();
11
12class helper_plugin_struct_db extends DokuWiki_Plugin {
13    /** @var helper_plugin_sqlite */
14    protected $sqlite;
15
16    /**
17     * helper_plugin_struct_db constructor.
18     */
19    public function __construct() {
20        $this->init();
21    }
22
23    /**
24     * Initialize the database
25     *
26     * @throws Exception
27     */
28    protected function init() {
29        /** @var helper_plugin_sqlite $sqlite */
30        $this->sqlite = plugin_load('helper', 'sqlite');
31        if(!$this->sqlite) {
32            if(defined('DOKU_UNITTEST')) throw new \Exception('Couldn\'t load sqlite.');
33
34            msg('The struct plugin requires the sqlite plugin. Please install it', -1);
35            return;
36        }
37
38        if($this->sqlite->getAdapter()->getName() != DOKU_EXT_PDO) {
39            if(defined('DOKU_UNITTEST')) throw new \Exception('Couldn\'t load PDO sqlite.');
40
41            msg('The struct plugin requires sqlite3 you\'re still using sqlite2', -1);
42            $this->sqlite = null;
43            return;
44        }
45        $this->sqlite->getAdapter()->setUseNativeAlter(true);
46
47        // initialize the database connection
48        if(!$this->sqlite->init('struct', DOKU_PLUGIN . 'struct/db/')) {
49            if(defined('DOKU_UNITTEST')) throw new \Exception('Couldn\'t init sqlite.');
50            return;
51        }
52
53        // register our JSON function with variable parameters
54        // todo this might be useful to be moved into the sqlite plugin
55        $this->sqlite->create_function('STRUCT_JSON', array($this, 'STRUCT_JSON'), -1);
56    }
57
58    /**
59     * @return helper_plugin_sqlite|null
60     */
61    public function getDB() {
62        global $conf;
63        $len = strlen($conf['metadir']);
64        if ($conf['metadir'] != substr($this->sqlite->getAdapter()->getDbFile(),0,$len)) {
65            $this->init();
66        }
67        return $this->sqlite;
68    }
69
70    /**
71     * Completely remove the database and reinitialize it
72     *
73     * You do not want to call this except for testing!
74     */
75    public function resetDB() {
76        if(!$this->sqlite) return;
77        $file = $this->sqlite->getAdapter()->getDbFile();
78        if(!$file) return;
79        unlink($file);
80        clearstatcache(true, $file);
81        $this->init();
82    }
83
84    /**
85     * Encodes all given arguments into a JSON encoded array
86     *
87     * @param string ...
88     * @return string
89     */
90    public function STRUCT_JSON() {
91        $args = func_get_args();
92        return json_encode($args);
93    }
94}
95
96// vim:ts=4:sw=4:et:
97