xref: /plugin/struct/helper/db.php (revision 6af24d3eff33249280549e60e18474e2cc0bf9d0)
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
51            return;
52        }
53    }
54
55    /**
56     * @return helper_plugin_sqlite|null
57     */
58    public function getDB() {
59        return $this->sqlite;
60    }
61
62    /**
63     * Completely remove the database and reinitialize it
64     *
65     * You do not want to call this except for testing!
66     */
67    public function resetDB() {
68        if(!$this->sqlite) return;
69        $file = $this->sqlite->getAdapter()->getDbFile();
70        if(!$file) return;
71        unlink($file);
72        clearstatcache(true, $file);
73        $this->init();
74    }
75}
76
77// vim:ts=4:sw=4:et:
78