xref: /plugin/struct/helper/db.php (revision 5e29103a15bd9873f422f66a6a5239b6aec4651e)
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
107234bfb1Ssplitbrainuse dokuwiki\Extension\Plugin;
1191c655b4SAndreas Gohruse dokuwiki\ErrorHandler;
1291c655b4SAndreas Gohruse dokuwiki\plugin\sqlite\SQLiteDB;
137cbcfbdbSAndreas Gohruse dokuwiki\plugin\struct\meta\StructException;
147cbcfbdbSAndreas Gohr
157234bfb1Ssplitbrainclass helper_plugin_struct_db extends Plugin
16d6d97f60SAnna Dabrowska{
1791c655b4SAndreas Gohr    /** @var SQLiteDB */
18083afc55SAndreas Gohr    protected $sqlite;
19083afc55SAndreas Gohr
200fe33e72SAndreas Gohr    /**
217d0142f5SAndreas Gohr     * Initialize the database
227d0142f5SAndreas Gohr     *
237d0142f5SAndreas Gohr     * @throws Exception
247d0142f5SAndreas Gohr     */
257d0142f5SAndreas Gohr    protected function init()
267d0142f5SAndreas Gohr    {
277d0142f5SAndreas Gohr        $this->sqlite = new SQLiteDB('struct', DOKU_PLUGIN . 'struct/db/');
287d0142f5SAndreas Gohr
297d0142f5SAndreas Gohr        // register our JSON function with variable parameters
307d0142f5SAndreas Gohr        $this->sqlite->getPdo()->sqliteCreateFunction('STRUCT_JSON', [$this, 'STRUCT_JSON'], -1);
317d0142f5SAndreas Gohr
327d0142f5SAndreas Gohr        // this function is meant to be overwritten by plugins
337d0142f5SAndreas Gohr        $this->sqlite->getPdo()->sqliteCreateFunction('IS_PUBLISHER', [$this, 'IS_PUBLISHER'], -1);
347d0142f5SAndreas Gohr    }
357d0142f5SAndreas Gohr
367d0142f5SAndreas Gohr    /**
37cfefffa4SAndreas Gohr     * @param bool $throw throw an Exception when sqlite not available or fails to load
3891c655b4SAndreas Gohr     * @return SQLiteDB|null
39cfefffa4SAndreas Gohr     * @throws Exception
400fe33e72SAndreas Gohr     */
4191c655b4SAndreas Gohr    public function getDB($throw = true)
42d6d97f60SAnna Dabrowska    {
437234bfb1Ssplitbrain        if (!$this->sqlite instanceof SQLiteDB) {
44cfefffa4SAndreas Gohr            if (!class_exists(SQLiteDB::class)) {
45cfefffa4SAndreas Gohr                if ($throw || defined('DOKU_UNITTEST')) throw new StructException('no sqlite');
46cfefffa4SAndreas Gohr                return null;
47cfefffa4SAndreas Gohr            }
48cfefffa4SAndreas Gohr
4979b29326SAnna Dabrowska            try {
507d0142f5SAndreas Gohr                $this->init();
5191c655b4SAndreas Gohr            } catch (\Exception $exception) {
5291c655b4SAndreas Gohr                ErrorHandler::logException($exception);
53cfefffa4SAndreas Gohr                if ($throw) throw $exception;
5491c655b4SAndreas Gohr                return null;
55083afc55SAndreas Gohr            }
56083afc55SAndreas Gohr        }
57083afc55SAndreas Gohr        return $this->sqlite;
58549a0837SAndreas Gohr    }
59549a0837SAndreas Gohr
600fe33e72SAndreas Gohr    /**
610fe33e72SAndreas Gohr     * Completely remove the database and reinitialize it
620fe33e72SAndreas Gohr     *
630fe33e72SAndreas Gohr     * You do not want to call this except for testing!
640fe33e72SAndreas Gohr     */
65d6d97f60SAnna Dabrowska    public function resetDB()
66d6d97f60SAnna Dabrowska    {
670fe33e72SAndreas Gohr        if (!$this->sqlite) return;
6879b29326SAnna Dabrowska        $file = $this->sqlite->getDbFile();
690fe33e72SAndreas Gohr        if (!$file) return;
700fe33e72SAndreas Gohr        unlink($file);
710fe33e72SAndreas Gohr        clearstatcache(true, $file);
7291c655b4SAndreas Gohr        $this->sqlite = null;
730fe33e72SAndreas Gohr    }
748fefbb59SAndreas Gohr
758fefbb59SAndreas Gohr    /**
768fefbb59SAndreas Gohr     * Encodes all given arguments into a JSON encoded array
778fefbb59SAndreas Gohr     *
788fefbb59SAndreas Gohr     * @param string ...
798fefbb59SAndreas Gohr     * @return string
808fefbb59SAndreas Gohr     */
817234bfb1Ssplitbrain    public function STRUCT_JSON(...$args) // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
82d6d97f60SAnna Dabrowska    {
83*5e29103aSannda        return json_encode($args, JSON_THROW_ON_ERROR);
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