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 /** 20*7d0142f5SAndreas Gohr * Initialize the database 21*7d0142f5SAndreas Gohr * 22*7d0142f5SAndreas Gohr * @throws Exception 23*7d0142f5SAndreas Gohr */ 24*7d0142f5SAndreas Gohr protected function init() 25*7d0142f5SAndreas Gohr { 26*7d0142f5SAndreas Gohr $this->sqlite = new SQLiteDB('struct', DOKU_PLUGIN . 'struct/db/'); 27*7d0142f5SAndreas Gohr 28*7d0142f5SAndreas Gohr // register our JSON function with variable parameters 29*7d0142f5SAndreas Gohr $this->sqlite->getPdo()->sqliteCreateFunction('STRUCT_JSON', [$this, 'STRUCT_JSON'], -1); 30*7d0142f5SAndreas Gohr 31*7d0142f5SAndreas Gohr // this function is meant to be overwritten by plugins 32*7d0142f5SAndreas Gohr $this->sqlite->getPdo()->sqliteCreateFunction('IS_PUBLISHER', [$this, 'IS_PUBLISHER'], -1); 33*7d0142f5SAndreas Gohr } 34*7d0142f5SAndreas Gohr 35*7d0142f5SAndreas Gohr /** 3691c655b4SAndreas Gohr * @param bool $throw throw an Exception when sqlite not available 3791c655b4SAndreas Gohr * @return SQLiteDB|null 380fe33e72SAndreas Gohr */ 3991c655b4SAndreas Gohr public function getDB($throw = true) 40d6d97f60SAnna Dabrowska { 4191c655b4SAndreas Gohr if ($this->sqlite === null) { 4279b29326SAnna Dabrowska try { 43*7d0142f5SAndreas Gohr $this->init(); 4491c655b4SAndreas Gohr } catch (\Exception $exception) { 4591c655b4SAndreas Gohr if (defined('DOKU_UNITTEST')) throw new \RuntimeException('Could not load SQLite', 0, $exception); 4691c655b4SAndreas Gohr ErrorHandler::logException($exception); 4791c655b4SAndreas Gohr if ($throw) throw new StructException('no sqlite'); 4891c655b4SAndreas Gohr return null; 49083afc55SAndreas Gohr } 50083afc55SAndreas Gohr } 51083afc55SAndreas Gohr return $this->sqlite; 52549a0837SAndreas Gohr } 53549a0837SAndreas Gohr 540fe33e72SAndreas Gohr /** 550fe33e72SAndreas Gohr * Completely remove the database and reinitialize it 560fe33e72SAndreas Gohr * 570fe33e72SAndreas Gohr * You do not want to call this except for testing! 580fe33e72SAndreas Gohr */ 59d6d97f60SAnna Dabrowska public function resetDB() 60d6d97f60SAnna Dabrowska { 610fe33e72SAndreas Gohr if (!$this->sqlite) return; 6279b29326SAnna Dabrowska $file = $this->sqlite->getDbFile(); 630fe33e72SAndreas Gohr if (!$file) return; 640fe33e72SAndreas Gohr unlink($file); 650fe33e72SAndreas Gohr clearstatcache(true, $file); 6691c655b4SAndreas Gohr $this->sqlite = null; 670fe33e72SAndreas Gohr } 688fefbb59SAndreas Gohr 698fefbb59SAndreas Gohr /** 708fefbb59SAndreas Gohr * Encodes all given arguments into a JSON encoded array 718fefbb59SAndreas Gohr * 728fefbb59SAndreas Gohr * @param string ... 738fefbb59SAndreas Gohr * @return string 748fefbb59SAndreas Gohr */ 75748e747fSAnna Dabrowska public function STRUCT_JSON() // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps 76d6d97f60SAnna Dabrowska { 778fefbb59SAndreas Gohr $args = func_get_args(); 788fefbb59SAndreas Gohr return json_encode($args); 798fefbb59SAndreas Gohr } 80bb8d98c4SAnna Dabrowska 81bb8d98c4SAnna Dabrowska /** 82bb8d98c4SAnna Dabrowska * This dummy implementation can be overwritten by a plugin 83bb8d98c4SAnna Dabrowska * 8400624072SAnna Dabrowska * @return int 85bb8d98c4SAnna Dabrowska */ 86bb8d98c4SAnna Dabrowska public function IS_PUBLISHER() // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps 87bb8d98c4SAnna Dabrowska { 8800624072SAnna Dabrowska return 1; 89bb8d98c4SAnna Dabrowska } 90549a0837SAndreas Gohr} 91549a0837SAndreas Gohr 92549a0837SAndreas Gohr// vim:ts=4:sw=4:et: 93