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 10322ec97bSAnna Dabrowskause dokuwiki\Extension\Event; 117cbcfbdbSAndreas Gohruse dokuwiki\plugin\struct\meta\StructException; 127cbcfbdbSAndreas Gohr 13d6d97f60SAnna Dabrowskaclass helper_plugin_struct_db extends DokuWiki_Plugin 14d6d97f60SAnna Dabrowska{ 1579b29326SAnna Dabrowska /** @var \dokuwiki\plugin\sqlite\SQLiteDB */ 16083afc55SAndreas Gohr protected $sqlite; 17083afc55SAndreas Gohr 180fe33e72SAndreas Gohr /** 190fe33e72SAndreas Gohr * helper_plugin_struct_db constructor. 200fe33e72SAndreas Gohr */ 21d6d97f60SAnna Dabrowska public function __construct() 22d6d97f60SAnna Dabrowska { 230fe33e72SAndreas Gohr $this->init(); 240fe33e72SAndreas Gohr } 250fe33e72SAndreas Gohr 260fe33e72SAndreas Gohr /** 270fe33e72SAndreas Gohr * Initialize the database 280fe33e72SAndreas Gohr * 290fe33e72SAndreas Gohr * @throws Exception 300fe33e72SAndreas Gohr */ 31d6d97f60SAnna Dabrowska protected function init() 32d6d97f60SAnna Dabrowska { 3379b29326SAnna Dabrowska try { 3479b29326SAnna Dabrowska /** @var \dokuwiki\plugin\sqlite\SQLiteDB $sqlite */ 3579b29326SAnna Dabrowska $this->sqlite = new \dokuwiki\plugin\sqlite\SQLiteDB('struct', DOKU_PLUGIN . 'struct/db/'); 3679b29326SAnna Dabrowska } catch (Exception $exception) { 3715929be2SAndreas Gohr if (defined('DOKU_UNITTEST')) throw new \Exception('Couldn\'t load sqlite.'); 38083afc55SAndreas Gohr return; 39083afc55SAndreas Gohr } 40083afc55SAndreas Gohr 418fefbb59SAndreas Gohr // register our JSON function with variable parameters 42*4183cc11SAndreas Gohr $this->sqlite->getPdo()->sqliteCreateFunction('STRUCT_JSON', [$this, 'STRUCT_JSON'], -1); 43bb8d98c4SAnna Dabrowska 44bb8d98c4SAnna Dabrowska // this function is meant to be overwritten by plugins 45*4183cc11SAndreas Gohr $this->sqlite->getPdo()->sqliteCreateFunction('IS_PUBLISHER', [$this, 'IS_PUBLISHER'], -1); 46083afc55SAndreas Gohr } 47549a0837SAndreas Gohr 48549a0837SAndreas Gohr /** 4979b29326SAnna Dabrowska * @param bool $throw throw an Exception when sqlite not available 5079b29326SAnna Dabrowska * @return \dokuwiki\plugin\sqlite\SQLiteDB|null 51549a0837SAndreas Gohr */ 52d6d97f60SAnna Dabrowska public function getDB($throw = true) 53d6d97f60SAnna Dabrowska { 54aeca15adSMichael Grosse global $conf; 55aeca15adSMichael Grosse $len = strlen($conf['metadir']); 5679b29326SAnna Dabrowska if ($this->sqlite && $conf['metadir'] != substr($this->sqlite->getDbFile(), 0, $len)) { 57aeca15adSMichael Grosse $this->init(); 58aeca15adSMichael Grosse } 597cbcfbdbSAndreas Gohr if (!$this->sqlite && $throw) { 607cbcfbdbSAndreas Gohr throw new StructException('no sqlite'); 617cbcfbdbSAndreas Gohr } 62083afc55SAndreas Gohr return $this->sqlite; 63549a0837SAndreas Gohr } 64549a0837SAndreas Gohr 650fe33e72SAndreas Gohr /** 660fe33e72SAndreas Gohr * Completely remove the database and reinitialize it 670fe33e72SAndreas Gohr * 680fe33e72SAndreas Gohr * You do not want to call this except for testing! 690fe33e72SAndreas Gohr */ 70d6d97f60SAnna Dabrowska public function resetDB() 71d6d97f60SAnna Dabrowska { 720fe33e72SAndreas Gohr if (!$this->sqlite) return; 7379b29326SAnna Dabrowska $file = $this->sqlite->getDbFile(); 740fe33e72SAndreas Gohr if (!$file) return; 750fe33e72SAndreas Gohr unlink($file); 760fe33e72SAndreas Gohr clearstatcache(true, $file); 770fe33e72SAndreas Gohr $this->init(); 780fe33e72SAndreas Gohr } 798fefbb59SAndreas Gohr 808fefbb59SAndreas Gohr /** 818fefbb59SAndreas Gohr * Encodes all given arguments into a JSON encoded array 828fefbb59SAndreas Gohr * 838fefbb59SAndreas Gohr * @param string ... 848fefbb59SAndreas Gohr * @return string 858fefbb59SAndreas Gohr */ 86748e747fSAnna Dabrowska public function STRUCT_JSON() // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps 87d6d97f60SAnna Dabrowska { 888fefbb59SAndreas Gohr $args = func_get_args(); 898fefbb59SAndreas Gohr return json_encode($args); 908fefbb59SAndreas Gohr } 91bb8d98c4SAnna Dabrowska 92bb8d98c4SAnna Dabrowska /** 93bb8d98c4SAnna Dabrowska * This dummy implementation can be overwritten by a plugin 94bb8d98c4SAnna Dabrowska * 9500624072SAnna Dabrowska * @return int 96bb8d98c4SAnna Dabrowska */ 97bb8d98c4SAnna Dabrowska public function IS_PUBLISHER() // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps 98bb8d98c4SAnna Dabrowska { 9900624072SAnna Dabrowska return 1; 100bb8d98c4SAnna Dabrowska } 101549a0837SAndreas Gohr} 102549a0837SAndreas Gohr 103549a0837SAndreas Gohr// vim:ts=4:sw=4:et: 104