xref: /plugin/structnotification/helper/db.php (revision 22ceb3ec42ad0c0a0966c8a1084e9b54fe097c53)
1<?php
2
3use dokuwiki\Extension\Plugin;
4
5/**
6 * DokuWiki Plugin structnotification (Helper Component)
7 *
8 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
9 * @author  Szymon Olewniczak <dokuwiki@cosmocode.de>
10 */
11class helper_plugin_structnotification_db extends Plugin
12{
13    /** @var helper_plugin_sqlite */
14    protected $sqlite;
15
16    /**
17     * helper_plugin_struct_db constructor.
18     */
19    public function __construct()
20    {
21        $this->init();
22    }
23
24    /**
25     * Initialize the database
26     *
27     * @throws Exception
28     */
29    protected function init()
30    {
31        $this->sqlite = plugin_load('helper', 'sqlite');
32        if (!$this->sqlite) {
33            if (defined('DOKU_UNITTEST')) {
34                throw new \Exception('Couldn\'t load sqlite.');
35            }
36            return;
37        }
38
39        if ($this->sqlite->getAdapter()->getName() != DOKU_EXT_PDO) {
40            if (defined('DOKU_UNITTEST')) {
41                throw new \Exception('Couldn\'t load PDO sqlite.');
42            }
43            $this->sqlite = null;
44            return;
45        }
46        $this->sqlite->getAdapter()->setUseNativeAlter(true);
47
48        // initialize the database connection
49        if (!$this->sqlite->init('structnotification', DOKU_PLUGIN . 'structnotification/db/')) {
50            if (defined('DOKU_UNITTEST')) {
51                throw new \Exception('Couldn\'t init sqlite.');
52            }
53            $this->sqlite = null;
54        }
55    }
56
57    /**
58     * @return helper_plugin_sqlite|null|bool
59     */
60    public function getDB()
61    {
62        global $conf;
63        $len = strlen($conf['metadir']);
64        if ($this->sqlite && $conf['metadir'] != substr($this->sqlite->getAdapter()->getDbFile(), 0, $len)) {
65            $this->init();
66        }
67        if (!$this->sqlite) {
68            msg($this->getLang('error sqlite missing'), -1);
69            return false;
70        }
71        return $this->sqlite;
72    }
73}
74