1<?php
2
3use dokuwiki\Extension\Plugin;
4
5/**
6 * DokuWiki Plugin notification (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_notification_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('notification', DOKU_PLUGIN . 'notification/db/')) {
50            if (defined('DOKU_UNITTEST')) {
51                throw new \Exception('Couldn\'t init sqlite.');
52            }
53            $this->sqlite = null;
54            return;
55        }
56    }
57
58    /**
59     * @return helper_plugin_sqlite|null
60     */
61    public function getDB()
62    {
63        global $conf;
64        $len = strlen($conf['metadir']);
65        if ($this->sqlite && $conf['metadir'] != substr($this->sqlite->getAdapter()->getDbFile(), 0, $len)) {
66            $this->init();
67        }
68        if (!$this->sqlite) {
69            msg($this->getLang('error sqlite missing'), -1);
70            return false;
71        }
72        return $this->sqlite;
73    }
74}
75