xref: /plugin/struct/helper/db.php (revision 0fe33e720e6122782050fa864daf1ac30d713126)
1549a0837SAndreas Gohr<?php
2549a0837SAndreas Gohr/**
3549a0837SAndreas Gohr * DokuWiki Plugin struct (Helper Component)
4549a0837SAndreas Gohr *
5549a0837SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6549a0837SAndreas Gohr * @author  Andreas Gohr, Michael Große <dokuwiki@cosmocode.de>
7549a0837SAndreas Gohr */
8549a0837SAndreas Gohr
9549a0837SAndreas Gohr// must be run within Dokuwiki
10549a0837SAndreas Gohrif(!defined('DOKU_INC')) die();
11549a0837SAndreas Gohr
12549a0837SAndreas Gohrclass helper_plugin_struct_db extends DokuWiki_Plugin {
13083afc55SAndreas Gohr    /** @var helper_plugin_sqlite */
14083afc55SAndreas Gohr    protected $sqlite;
15083afc55SAndreas Gohr
16*0fe33e72SAndreas Gohr    /**
17*0fe33e72SAndreas Gohr     * helper_plugin_struct_db constructor.
18*0fe33e72SAndreas Gohr     */
19083afc55SAndreas Gohr    public function __construct() {
20*0fe33e72SAndreas Gohr        $this->init();
21*0fe33e72SAndreas Gohr    }
22*0fe33e72SAndreas Gohr
23*0fe33e72SAndreas Gohr    /**
24*0fe33e72SAndreas Gohr     * Initialize the database
25*0fe33e72SAndreas Gohr     *
26*0fe33e72SAndreas Gohr     * @throws Exception
27*0fe33e72SAndreas Gohr     */
28*0fe33e72SAndreas Gohr    protected function init() {
29083afc55SAndreas Gohr        /** @var helper_plugin_sqlite $sqlite */
30083afc55SAndreas Gohr        $this->sqlite = plugin_load('helper', 'sqlite');
31083afc55SAndreas Gohr        if(!$this->sqlite) {
3215929be2SAndreas Gohr            if(defined('DOKU_UNITTEST')) throw new \Exception('Couldn\'t load sqlite.');
3315929be2SAndreas Gohr
341c502704SAndreas Gohr            msg('The struct plugin requires the sqlite plugin. Please install it', -1);
35083afc55SAndreas Gohr            return;
36083afc55SAndreas Gohr        }
37083afc55SAndreas Gohr
381c502704SAndreas Gohr        if($this->sqlite->getAdapter()->getName() != DOKU_EXT_PDO) {
3915929be2SAndreas Gohr            if(defined('DOKU_UNITTEST')) throw new \Exception('Couldn\'t load PDO sqlite.');
4015929be2SAndreas Gohr
4115929be2SAndreas Gohr            msg('The struct plugin requires sqlite3 you\'re still using sqlite2',-1);
421c502704SAndreas Gohr            $this->sqlite = null;
431c502704SAndreas Gohr            return;
441c502704SAndreas Gohr        }
451c502704SAndreas Gohr        $this->sqlite->getAdapter()->setUseNativeAlter(true);
461c502704SAndreas Gohr
47083afc55SAndreas Gohr        // initialize the database connection
48083afc55SAndreas Gohr        if(!$this->sqlite->init('struct', DOKU_PLUGIN . 'struct/db/')) {
4915929be2SAndreas Gohr            if(defined('DOKU_UNITTEST')) throw new \Exception('Couldn\'t init sqlite.');
5015929be2SAndreas Gohr
51083afc55SAndreas Gohr            return;
52083afc55SAndreas Gohr        }
53083afc55SAndreas Gohr    }
54549a0837SAndreas Gohr
55549a0837SAndreas Gohr    /**
56083afc55SAndreas Gohr     * @return helper_plugin_sqlite|null
57549a0837SAndreas Gohr     */
58083afc55SAndreas Gohr    public function getDB() {
59083afc55SAndreas Gohr        return $this->sqlite;
60549a0837SAndreas Gohr    }
61549a0837SAndreas Gohr
62*0fe33e72SAndreas Gohr    /**
63*0fe33e72SAndreas Gohr     * Completely remove the database and reinitialize it
64*0fe33e72SAndreas Gohr     *
65*0fe33e72SAndreas Gohr     * You do not want to call this except for testing!
66*0fe33e72SAndreas Gohr     */
67*0fe33e72SAndreas Gohr    public function resetDB() {
68*0fe33e72SAndreas Gohr        if(!$this->sqlite) return;
69*0fe33e72SAndreas Gohr        $file = $this->sqlite->getAdapter()->getDbFile();
70*0fe33e72SAndreas Gohr        if(!$file) return;
71*0fe33e72SAndreas Gohr        unlink($file);
72*0fe33e72SAndreas Gohr        clearstatcache(true, $file);
73*0fe33e72SAndreas Gohr        $this->init();
74*0fe33e72SAndreas Gohr    }
75549a0837SAndreas Gohr}
76549a0837SAndreas Gohr
77549a0837SAndreas Gohr// vim:ts=4:sw=4:et:
78