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{ 15083afc55SAndreas Gohr /** @var helper_plugin_sqlite */ 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 { 33083afc55SAndreas Gohr /** @var helper_plugin_sqlite $sqlite */ 34083afc55SAndreas Gohr $this->sqlite = plugin_load('helper', 'sqlite'); 35083afc55SAndreas Gohr if (!$this->sqlite) { 3615929be2SAndreas Gohr if (defined('DOKU_UNITTEST')) throw new \Exception('Couldn\'t load sqlite.'); 37083afc55SAndreas Gohr return; 38083afc55SAndreas Gohr } 39083afc55SAndreas Gohr 40609bd281SMichael Große if ($this->sqlite->getAdapter() === null) { 41609bd281SMichael Große if (defined('DOKU_UNITTEST')) throw new \Exception('Couldn\'t load PDO sqlite.'); 42609bd281SMichael Große $this->sqlite = null; 43609bd281SMichael Große return; 44609bd281SMichael Große } 45609bd281SMichael Große 461c502704SAndreas Gohr if ($this->sqlite->getAdapter()->getName() != DOKU_EXT_PDO) { 4715929be2SAndreas Gohr if (defined('DOKU_UNITTEST')) throw new \Exception('Couldn\'t load PDO sqlite.'); 481c502704SAndreas Gohr $this->sqlite = null; 491c502704SAndreas Gohr return; 501c502704SAndreas Gohr } 511c502704SAndreas Gohr $this->sqlite->getAdapter()->setUseNativeAlter(true); 521c502704SAndreas Gohr 53083afc55SAndreas Gohr // initialize the database connection 54083afc55SAndreas Gohr if (!$this->sqlite->init('struct', DOKU_PLUGIN . 'struct/db/')) { 5515929be2SAndreas Gohr if (defined('DOKU_UNITTEST')) throw new \Exception('Couldn\'t init sqlite.'); 567cbcfbdbSAndreas Gohr $this->sqlite = null; 57083afc55SAndreas Gohr return; 58083afc55SAndreas Gohr } 598fefbb59SAndreas Gohr 608fefbb59SAndreas Gohr // register our JSON function with variable parameters 618fefbb59SAndreas Gohr // todo this might be useful to be moved into the sqlite plugin 620e72ef50SAndreas Gohr $this->sqlite->create_function('STRUCT_JSON', array($this, 'STRUCT_JSON'), -1); 63bb8d98c4SAnna Dabrowska 64bb8d98c4SAnna Dabrowska // this function is meant to be overwritten by plugins 65bb8d98c4SAnna Dabrowska $this->sqlite->create_function('IS_PUBLISHER', array($this, 'IS_PUBLISHER'), -1); 66322ec97bSAnna Dabrowska 67322ec97bSAnna Dabrowska // collect and register custom functions from other plugins 68322ec97bSAnna Dabrowska $functions = []; 69322ec97bSAnna Dabrowska Event::createAndTrigger('STRUCT_PLUGIN_SQLITE_FUNCTION', $functions); 70322ec97bSAnna Dabrowska foreach ($functions as $fn) { 71322ec97bSAnna Dabrowska if (isset($fn['obj']) && isset($fn['name']) && is_callable([$fn['obj'], $fn['name']])) { 72322ec97bSAnna Dabrowska $this->sqlite->create_function($fn['name'], [$fn['obj'], $fn['name']], -1); 73322ec97bSAnna Dabrowska } 74322ec97bSAnna Dabrowska } 75083afc55SAndreas Gohr } 76549a0837SAndreas Gohr 77549a0837SAndreas Gohr /** 787cbcfbdbSAndreas Gohr * @param bool $throw throw an Exception when sqlite not available? 79083afc55SAndreas Gohr * @return helper_plugin_sqlite|null 80549a0837SAndreas Gohr */ 81d6d97f60SAnna Dabrowska public function getDB($throw = true) 82d6d97f60SAnna Dabrowska { 83aeca15adSMichael Grosse global $conf; 84aeca15adSMichael Grosse $len = strlen($conf['metadir']); 857cbcfbdbSAndreas Gohr if ($this->sqlite && $conf['metadir'] != substr($this->sqlite->getAdapter()->getDbFile(), 0, $len)) { 86aeca15adSMichael Grosse $this->init(); 87aeca15adSMichael Grosse } 887cbcfbdbSAndreas Gohr if (!$this->sqlite && $throw) { 897cbcfbdbSAndreas Gohr throw new StructException('no sqlite'); 907cbcfbdbSAndreas Gohr } 91083afc55SAndreas Gohr return $this->sqlite; 92549a0837SAndreas Gohr } 93549a0837SAndreas Gohr 940fe33e72SAndreas Gohr /** 950fe33e72SAndreas Gohr * Completely remove the database and reinitialize it 960fe33e72SAndreas Gohr * 970fe33e72SAndreas Gohr * You do not want to call this except for testing! 980fe33e72SAndreas Gohr */ 99d6d97f60SAnna Dabrowska public function resetDB() 100d6d97f60SAnna Dabrowska { 1010fe33e72SAndreas Gohr if (!$this->sqlite) return; 1020fe33e72SAndreas Gohr $file = $this->sqlite->getAdapter()->getDbFile(); 1030fe33e72SAndreas Gohr if (!$file) return; 1040fe33e72SAndreas Gohr unlink($file); 1050fe33e72SAndreas Gohr clearstatcache(true, $file); 1060fe33e72SAndreas Gohr $this->init(); 1070fe33e72SAndreas Gohr } 1088fefbb59SAndreas Gohr 1098fefbb59SAndreas Gohr /** 1108fefbb59SAndreas Gohr * Encodes all given arguments into a JSON encoded array 1118fefbb59SAndreas Gohr * 1128fefbb59SAndreas Gohr * @param string ... 1138fefbb59SAndreas Gohr * @return string 1148fefbb59SAndreas Gohr */ 115748e747fSAnna Dabrowska public function STRUCT_JSON() // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps 116d6d97f60SAnna Dabrowska { 1178fefbb59SAndreas Gohr $args = func_get_args(); 1188fefbb59SAndreas Gohr return json_encode($args); 1198fefbb59SAndreas Gohr } 120bb8d98c4SAnna Dabrowska 121bb8d98c4SAnna Dabrowska /** 122bb8d98c4SAnna Dabrowska * This dummy implementation can be overwritten by a plugin 123bb8d98c4SAnna Dabrowska * 124*00624072SAnna Dabrowska * @return int 125bb8d98c4SAnna Dabrowska */ 126bb8d98c4SAnna Dabrowska public function IS_PUBLISHER() // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps 127bb8d98c4SAnna Dabrowska { 128*00624072SAnna Dabrowska return 1; 129bb8d98c4SAnna Dabrowska } 130549a0837SAndreas Gohr} 131549a0837SAndreas Gohr 132549a0837SAndreas Gohr// vim:ts=4:sw=4:et: 133