xref: /plugin/struct/helper/db.php (revision 7cbcfbdb68125878b37fede99d5e33997295c2f6)
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
10*7cbcfbdbSAndreas Gohruse dokuwiki\plugin\struct\meta\StructException;
11*7cbcfbdbSAndreas Gohr
12549a0837SAndreas Gohrif(!defined('DOKU_INC')) die();
13549a0837SAndreas Gohr
14549a0837SAndreas Gohrclass helper_plugin_struct_db extends DokuWiki_Plugin {
15083afc55SAndreas Gohr    /** @var helper_plugin_sqlite */
16083afc55SAndreas Gohr    protected $sqlite;
17083afc55SAndreas Gohr
180fe33e72SAndreas Gohr    /**
190fe33e72SAndreas Gohr     * helper_plugin_struct_db constructor.
200fe33e72SAndreas Gohr     */
21083afc55SAndreas Gohr    public function __construct() {
220fe33e72SAndreas Gohr        $this->init();
230fe33e72SAndreas Gohr    }
240fe33e72SAndreas Gohr
250fe33e72SAndreas Gohr    /**
260fe33e72SAndreas Gohr     * Initialize the database
270fe33e72SAndreas Gohr     *
280fe33e72SAndreas Gohr     * @throws Exception
290fe33e72SAndreas Gohr     */
300fe33e72SAndreas Gohr    protected function init() {
31083afc55SAndreas Gohr        /** @var helper_plugin_sqlite $sqlite */
32083afc55SAndreas Gohr        $this->sqlite = plugin_load('helper', 'sqlite');
33083afc55SAndreas Gohr        if(!$this->sqlite) {
3415929be2SAndreas Gohr            if(defined('DOKU_UNITTEST')) throw new \Exception('Couldn\'t load sqlite.');
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.');
401c502704SAndreas Gohr            $this->sqlite = null;
411c502704SAndreas Gohr            return;
421c502704SAndreas Gohr        }
431c502704SAndreas Gohr        $this->sqlite->getAdapter()->setUseNativeAlter(true);
441c502704SAndreas Gohr
45083afc55SAndreas Gohr        // initialize the database connection
46083afc55SAndreas Gohr        if(!$this->sqlite->init('struct', DOKU_PLUGIN . 'struct/db/')) {
4715929be2SAndreas Gohr            if(defined('DOKU_UNITTEST')) throw new \Exception('Couldn\'t init sqlite.');
48*7cbcfbdbSAndreas Gohr            $this->sqlite = null;
49083afc55SAndreas Gohr            return;
50083afc55SAndreas Gohr        }
518fefbb59SAndreas Gohr
528fefbb59SAndreas Gohr        // register our JSON function with variable parameters
538fefbb59SAndreas Gohr        // todo this might be useful to be moved into the sqlite plugin
540e72ef50SAndreas Gohr        $this->sqlite->create_function('STRUCT_JSON', array($this, 'STRUCT_JSON'), -1);
55083afc55SAndreas Gohr    }
56549a0837SAndreas Gohr
57549a0837SAndreas Gohr    /**
58*7cbcfbdbSAndreas Gohr     * @param bool $throw throw an Exception when sqlite not available?
59083afc55SAndreas Gohr     * @return helper_plugin_sqlite|null
60549a0837SAndreas Gohr     */
61*7cbcfbdbSAndreas Gohr    public function getDB($throw=true) {
62aeca15adSMichael Grosse        global $conf;
63aeca15adSMichael Grosse        $len = strlen($conf['metadir']);
64*7cbcfbdbSAndreas Gohr        if ($this->sqlite && $conf['metadir'] != substr($this->sqlite->getAdapter()->getDbFile(),0,$len)) {
65aeca15adSMichael Grosse            $this->init();
66aeca15adSMichael Grosse        }
67*7cbcfbdbSAndreas Gohr        if(!$this->sqlite && $throw) {
68*7cbcfbdbSAndreas Gohr            throw new StructException('no sqlite');
69*7cbcfbdbSAndreas Gohr        }
70083afc55SAndreas Gohr        return $this->sqlite;
71549a0837SAndreas Gohr    }
72549a0837SAndreas Gohr
730fe33e72SAndreas Gohr    /**
740fe33e72SAndreas Gohr     * Completely remove the database and reinitialize it
750fe33e72SAndreas Gohr     *
760fe33e72SAndreas Gohr     * You do not want to call this except for testing!
770fe33e72SAndreas Gohr     */
780fe33e72SAndreas Gohr    public function resetDB() {
790fe33e72SAndreas Gohr        if(!$this->sqlite) return;
800fe33e72SAndreas Gohr        $file = $this->sqlite->getAdapter()->getDbFile();
810fe33e72SAndreas Gohr        if(!$file) return;
820fe33e72SAndreas Gohr        unlink($file);
830fe33e72SAndreas Gohr        clearstatcache(true, $file);
840fe33e72SAndreas Gohr        $this->init();
850fe33e72SAndreas Gohr    }
868fefbb59SAndreas Gohr
878fefbb59SAndreas Gohr    /**
888fefbb59SAndreas Gohr     * Encodes all given arguments into a JSON encoded array
898fefbb59SAndreas Gohr     *
908fefbb59SAndreas Gohr     * @param string ...
918fefbb59SAndreas Gohr     * @return string
928fefbb59SAndreas Gohr     */
930e72ef50SAndreas Gohr    public function STRUCT_JSON() {
948fefbb59SAndreas Gohr        $args = func_get_args();
958fefbb59SAndreas Gohr        return json_encode($args);
968fefbb59SAndreas Gohr    }
97549a0837SAndreas Gohr}
98549a0837SAndreas Gohr
99549a0837SAndreas Gohr// vim:ts=4:sw=4:et:
100