xref: /plugin/struct/helper/db.php (revision cfefffa4d2c67e8227ac2e612157363595988ec5)
1549a0837SAndreas Gohr<?php
2d6d97f60SAnna Dabrowska
3549a0837SAndreas Gohr/**
4549a0837SAndreas Gohr * DokuWiki Plugin struct (Helper Component)
5549a0837SAndreas Gohr *
6549a0837SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
7549a0837SAndreas Gohr * @author  Andreas Gohr, Michael Große <dokuwiki@cosmocode.de>
8549a0837SAndreas Gohr */
9549a0837SAndreas Gohr
1091c655b4SAndreas Gohruse dokuwiki\ErrorHandler;
1191c655b4SAndreas Gohruse dokuwiki\plugin\sqlite\SQLiteDB;
127cbcfbdbSAndreas Gohruse dokuwiki\plugin\struct\meta\StructException;
137cbcfbdbSAndreas Gohr
14d6d97f60SAnna Dabrowskaclass helper_plugin_struct_db extends DokuWiki_Plugin
15d6d97f60SAnna Dabrowska{
1691c655b4SAndreas Gohr    /** @var SQLiteDB */
17083afc55SAndreas Gohr    protected $sqlite;
18083afc55SAndreas Gohr
190fe33e72SAndreas Gohr    /**
207d0142f5SAndreas Gohr     * Initialize the database
217d0142f5SAndreas Gohr     *
227d0142f5SAndreas Gohr     * @throws Exception
237d0142f5SAndreas Gohr     */
247d0142f5SAndreas Gohr    protected function init()
257d0142f5SAndreas Gohr    {
267d0142f5SAndreas Gohr        $this->sqlite = new SQLiteDB('struct', DOKU_PLUGIN . 'struct/db/');
277d0142f5SAndreas Gohr
287d0142f5SAndreas Gohr        // register our JSON function with variable parameters
297d0142f5SAndreas Gohr        $this->sqlite->getPdo()->sqliteCreateFunction('STRUCT_JSON', [$this, 'STRUCT_JSON'], -1);
307d0142f5SAndreas Gohr
317d0142f5SAndreas Gohr        // this function is meant to be overwritten by plugins
327d0142f5SAndreas Gohr        $this->sqlite->getPdo()->sqliteCreateFunction('IS_PUBLISHER', [$this, 'IS_PUBLISHER'], -1);
337d0142f5SAndreas Gohr    }
347d0142f5SAndreas Gohr
357d0142f5SAndreas Gohr    /**
36*cfefffa4SAndreas Gohr     * @param bool $throw throw an Exception when sqlite not available or fails to load
3791c655b4SAndreas Gohr     * @return SQLiteDB|null
38*cfefffa4SAndreas Gohr     * @throws Exception
390fe33e72SAndreas Gohr     */
4091c655b4SAndreas Gohr    public function getDB($throw = true)
41d6d97f60SAnna Dabrowska    {
4291c655b4SAndreas Gohr        if ($this->sqlite === null) {
43*cfefffa4SAndreas Gohr            if (!class_exists(SQLiteDB::class)) {
44*cfefffa4SAndreas Gohr                if ($throw || defined('DOKU_UNITTEST')) throw new StructException('no sqlite');
45*cfefffa4SAndreas Gohr                return null;
46*cfefffa4SAndreas Gohr            }
47*cfefffa4SAndreas Gohr
4879b29326SAnna Dabrowska            try {
497d0142f5SAndreas Gohr                $this->init();
5091c655b4SAndreas Gohr            } catch (\Exception $exception) {
5191c655b4SAndreas Gohr                ErrorHandler::logException($exception);
52*cfefffa4SAndreas Gohr                if ($throw) throw $exception;
5391c655b4SAndreas Gohr                return null;
54083afc55SAndreas Gohr            }
55083afc55SAndreas Gohr        }
56083afc55SAndreas Gohr        return $this->sqlite;
57549a0837SAndreas Gohr    }
58549a0837SAndreas Gohr
590fe33e72SAndreas Gohr    /**
600fe33e72SAndreas Gohr     * Completely remove the database and reinitialize it
610fe33e72SAndreas Gohr     *
620fe33e72SAndreas Gohr     * You do not want to call this except for testing!
630fe33e72SAndreas Gohr     */
64d6d97f60SAnna Dabrowska    public function resetDB()
65d6d97f60SAnna Dabrowska    {
660fe33e72SAndreas Gohr        if (!$this->sqlite) return;
6779b29326SAnna Dabrowska        $file = $this->sqlite->getDbFile();
680fe33e72SAndreas Gohr        if (!$file) return;
690fe33e72SAndreas Gohr        unlink($file);
700fe33e72SAndreas Gohr        clearstatcache(true, $file);
7191c655b4SAndreas Gohr        $this->sqlite = null;
720fe33e72SAndreas Gohr    }
738fefbb59SAndreas Gohr
748fefbb59SAndreas Gohr    /**
758fefbb59SAndreas Gohr     * Encodes all given arguments into a JSON encoded array
768fefbb59SAndreas Gohr     *
778fefbb59SAndreas Gohr     * @param string ...
788fefbb59SAndreas Gohr     * @return string
798fefbb59SAndreas Gohr     */
80748e747fSAnna Dabrowska    public function STRUCT_JSON() // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
81d6d97f60SAnna Dabrowska    {
828fefbb59SAndreas Gohr        $args = func_get_args();
838fefbb59SAndreas Gohr        return json_encode($args);
848fefbb59SAndreas Gohr    }
85bb8d98c4SAnna Dabrowska
86bb8d98c4SAnna Dabrowska    /**
87bb8d98c4SAnna Dabrowska     * This dummy implementation can be overwritten by a plugin
88bb8d98c4SAnna Dabrowska     *
8900624072SAnna Dabrowska     * @return int
90bb8d98c4SAnna Dabrowska     */
91bb8d98c4SAnna Dabrowska    public function IS_PUBLISHER() // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
92bb8d98c4SAnna Dabrowska    {
9300624072SAnna Dabrowska        return 1;
94bb8d98c4SAnna Dabrowska    }
95549a0837SAndreas Gohr}
96549a0837SAndreas Gohr
97549a0837SAndreas Gohr// vim:ts=4:sw=4:et:
98