xref: /plugin/struct/helper/db.php (revision 8fefbb597ab9fe91ecc1184f1e5d336795e11c70)
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
160fe33e72SAndreas Gohr    /**
170fe33e72SAndreas Gohr     * helper_plugin_struct_db constructor.
180fe33e72SAndreas Gohr     */
19083afc55SAndreas Gohr    public function __construct() {
200fe33e72SAndreas Gohr        $this->init();
210fe33e72SAndreas Gohr    }
220fe33e72SAndreas Gohr
230fe33e72SAndreas Gohr    /**
240fe33e72SAndreas Gohr     * Initialize the database
250fe33e72SAndreas Gohr     *
260fe33e72SAndreas Gohr     * @throws Exception
270fe33e72SAndreas Gohr     */
280fe33e72SAndreas 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.');
50083afc55SAndreas Gohr            return;
51083afc55SAndreas Gohr        }
52*8fefbb59SAndreas Gohr
53*8fefbb59SAndreas Gohr        // register our JSON function with variable parameters
54*8fefbb59SAndreas Gohr        // todo this might be useful to be moved into the sqlite plugin
55*8fefbb59SAndreas Gohr        $this->sqlite->create_function('JSON', array($this, 'SQL_JSON'), -1);
56083afc55SAndreas Gohr    }
57549a0837SAndreas Gohr
58549a0837SAndreas Gohr    /**
59083afc55SAndreas Gohr     * @return helper_plugin_sqlite|null
60549a0837SAndreas Gohr     */
61083afc55SAndreas Gohr    public function getDB() {
62083afc55SAndreas Gohr        return $this->sqlite;
63549a0837SAndreas Gohr    }
64549a0837SAndreas Gohr
650fe33e72SAndreas Gohr    /**
660fe33e72SAndreas Gohr     * Completely remove the database and reinitialize it
670fe33e72SAndreas Gohr     *
680fe33e72SAndreas Gohr     * You do not want to call this except for testing!
690fe33e72SAndreas Gohr     */
700fe33e72SAndreas Gohr    public function resetDB() {
710fe33e72SAndreas Gohr        if(!$this->sqlite) return;
720fe33e72SAndreas Gohr        $file = $this->sqlite->getAdapter()->getDbFile();
730fe33e72SAndreas Gohr        if(!$file) return;
740fe33e72SAndreas Gohr        unlink($file);
750fe33e72SAndreas Gohr        clearstatcache(true, $file);
760fe33e72SAndreas Gohr        $this->init();
770fe33e72SAndreas Gohr    }
78*8fefbb59SAndreas Gohr
79*8fefbb59SAndreas Gohr    /**
80*8fefbb59SAndreas Gohr     * Encodes all given arguments into a JSON encoded array
81*8fefbb59SAndreas Gohr     *
82*8fefbb59SAndreas Gohr     * @param string ...
83*8fefbb59SAndreas Gohr     * @return string
84*8fefbb59SAndreas Gohr     */
85*8fefbb59SAndreas Gohr    public function SQL_JSON() {
86*8fefbb59SAndreas Gohr        $args = func_get_args();
87*8fefbb59SAndreas Gohr        return json_encode($args);
88*8fefbb59SAndreas Gohr    }
89549a0837SAndreas Gohr}
90549a0837SAndreas Gohr
91549a0837SAndreas Gohr// vim:ts=4:sw=4:et:
92