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 10*91c655b4SAndreas Gohruse dokuwiki\ErrorHandler; 11*91c655b4SAndreas 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{ 16*91c655b4SAndreas Gohr /** @var SQLiteDB */ 17083afc55SAndreas Gohr protected $sqlite; 18083afc55SAndreas Gohr 190fe33e72SAndreas Gohr /** 20*91c655b4SAndreas Gohr * @param bool $throw throw an Exception when sqlite not available 21*91c655b4SAndreas Gohr * @return SQLiteDB|null 220fe33e72SAndreas Gohr */ 23*91c655b4SAndreas Gohr public function getDB($throw = true) 24d6d97f60SAnna Dabrowska { 25*91c655b4SAndreas Gohr if ($this->sqlite === null) { 2679b29326SAnna Dabrowska try { 27*91c655b4SAndreas Gohr $this->sqlite = new SQLiteDB('struct', DOKU_PLUGIN . 'struct/db/'); 28*91c655b4SAndreas Gohr } catch (\Exception $exception) { 29*91c655b4SAndreas Gohr if (defined('DOKU_UNITTEST')) throw new \RuntimeException('Could not load SQLite', 0, $exception); 30*91c655b4SAndreas Gohr ErrorHandler::logException($exception); 31*91c655b4SAndreas Gohr if ($throw) throw new StructException('no sqlite'); 32*91c655b4SAndreas Gohr return null; 33083afc55SAndreas Gohr } 34083afc55SAndreas Gohr 358fefbb59SAndreas Gohr // register our JSON function with variable parameters 364183cc11SAndreas Gohr $this->sqlite->getPdo()->sqliteCreateFunction('STRUCT_JSON', [$this, 'STRUCT_JSON'], -1); 37bb8d98c4SAnna Dabrowska 38bb8d98c4SAnna Dabrowska // this function is meant to be overwritten by plugins 394183cc11SAndreas Gohr $this->sqlite->getPdo()->sqliteCreateFunction('IS_PUBLISHER', [$this, 'IS_PUBLISHER'], -1); 40083afc55SAndreas Gohr } 41083afc55SAndreas Gohr return $this->sqlite; 42549a0837SAndreas Gohr } 43549a0837SAndreas Gohr 440fe33e72SAndreas Gohr /** 450fe33e72SAndreas Gohr * Completely remove the database and reinitialize it 460fe33e72SAndreas Gohr * 470fe33e72SAndreas Gohr * You do not want to call this except for testing! 480fe33e72SAndreas Gohr */ 49d6d97f60SAnna Dabrowska public function resetDB() 50d6d97f60SAnna Dabrowska { 510fe33e72SAndreas Gohr if (!$this->sqlite) return; 5279b29326SAnna Dabrowska $file = $this->sqlite->getDbFile(); 530fe33e72SAndreas Gohr if (!$file) return; 540fe33e72SAndreas Gohr unlink($file); 550fe33e72SAndreas Gohr clearstatcache(true, $file); 56*91c655b4SAndreas Gohr $this->sqlite = null; 570fe33e72SAndreas Gohr } 588fefbb59SAndreas Gohr 598fefbb59SAndreas Gohr /** 608fefbb59SAndreas Gohr * Encodes all given arguments into a JSON encoded array 618fefbb59SAndreas Gohr * 628fefbb59SAndreas Gohr * @param string ... 638fefbb59SAndreas Gohr * @return string 648fefbb59SAndreas Gohr */ 65748e747fSAnna Dabrowska public function STRUCT_JSON() // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps 66d6d97f60SAnna Dabrowska { 678fefbb59SAndreas Gohr $args = func_get_args(); 688fefbb59SAndreas Gohr return json_encode($args); 698fefbb59SAndreas Gohr } 70bb8d98c4SAnna Dabrowska 71bb8d98c4SAnna Dabrowska /** 72bb8d98c4SAnna Dabrowska * This dummy implementation can be overwritten by a plugin 73bb8d98c4SAnna Dabrowska * 7400624072SAnna Dabrowska * @return int 75bb8d98c4SAnna Dabrowska */ 76bb8d98c4SAnna Dabrowska public function IS_PUBLISHER() // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps 77bb8d98c4SAnna Dabrowska { 7800624072SAnna Dabrowska return 1; 79bb8d98c4SAnna Dabrowska } 80549a0837SAndreas Gohr} 81549a0837SAndreas Gohr 82549a0837SAndreas Gohr// vim:ts=4:sw=4:et: 83