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 107cbcfbdbSAndreas Gohruse dokuwiki\plugin\struct\meta\StructException; 117cbcfbdbSAndreas Gohr 12d6d97f60SAnna Dabrowskaclass helper_plugin_struct_db extends DokuWiki_Plugin 13d6d97f60SAnna Dabrowska{ 14083afc55SAndreas Gohr /** @var helper_plugin_sqlite */ 15083afc55SAndreas Gohr protected $sqlite; 16083afc55SAndreas Gohr 170fe33e72SAndreas Gohr /** 180fe33e72SAndreas Gohr * helper_plugin_struct_db constructor. 190fe33e72SAndreas Gohr */ 20d6d97f60SAnna Dabrowska public function __construct() 21d6d97f60SAnna Dabrowska { 220fe33e72SAndreas Gohr $this->init(); 230fe33e72SAndreas Gohr } 240fe33e72SAndreas Gohr 250fe33e72SAndreas Gohr /** 260fe33e72SAndreas Gohr * Initialize the database 270fe33e72SAndreas Gohr * 280fe33e72SAndreas Gohr * @throws Exception 290fe33e72SAndreas Gohr */ 30d6d97f60SAnna Dabrowska protected function init() 31d6d97f60SAnna Dabrowska { 32083afc55SAndreas Gohr /** @var helper_plugin_sqlite $sqlite */ 33083afc55SAndreas Gohr $this->sqlite = plugin_load('helper', 'sqlite'); 34083afc55SAndreas Gohr if (!$this->sqlite) { 3515929be2SAndreas Gohr if (defined('DOKU_UNITTEST')) throw new \Exception('Couldn\'t load sqlite.'); 36083afc55SAndreas Gohr return; 37083afc55SAndreas Gohr } 38083afc55SAndreas Gohr 39609bd281SMichael Große if ($this->sqlite->getAdapter() === null) { 40609bd281SMichael Große if (defined('DOKU_UNITTEST')) throw new \Exception('Couldn\'t load PDO sqlite.'); 41609bd281SMichael Große $this->sqlite = null; 42609bd281SMichael Große return; 43609bd281SMichael Große } 44609bd281SMichael Große 451c502704SAndreas Gohr if ($this->sqlite->getAdapter()->getName() != DOKU_EXT_PDO) { 4615929be2SAndreas Gohr if (defined('DOKU_UNITTEST')) throw new \Exception('Couldn\'t load PDO sqlite.'); 471c502704SAndreas Gohr $this->sqlite = null; 481c502704SAndreas Gohr return; 491c502704SAndreas Gohr } 501c502704SAndreas Gohr $this->sqlite->getAdapter()->setUseNativeAlter(true); 511c502704SAndreas Gohr 52083afc55SAndreas Gohr // initialize the database connection 53083afc55SAndreas Gohr if (!$this->sqlite->init('struct', DOKU_PLUGIN . 'struct/db/')) { 5415929be2SAndreas Gohr if (defined('DOKU_UNITTEST')) throw new \Exception('Couldn\'t init sqlite.'); 557cbcfbdbSAndreas Gohr $this->sqlite = null; 56083afc55SAndreas Gohr return; 57083afc55SAndreas Gohr } 588fefbb59SAndreas Gohr 598fefbb59SAndreas Gohr // register our JSON function with variable parameters 608fefbb59SAndreas Gohr // todo this might be useful to be moved into the sqlite plugin 610e72ef50SAndreas Gohr $this->sqlite->create_function('STRUCT_JSON', array($this, 'STRUCT_JSON'), -1); 62083afc55SAndreas Gohr } 63549a0837SAndreas Gohr 64549a0837SAndreas Gohr /** 657cbcfbdbSAndreas Gohr * @param bool $throw throw an Exception when sqlite not available? 66083afc55SAndreas Gohr * @return helper_plugin_sqlite|null 67549a0837SAndreas Gohr */ 68d6d97f60SAnna Dabrowska public function getDB($throw = true) 69d6d97f60SAnna Dabrowska { 70aeca15adSMichael Grosse global $conf; 71aeca15adSMichael Grosse $len = strlen($conf['metadir']); 727cbcfbdbSAndreas Gohr if ($this->sqlite && $conf['metadir'] != substr($this->sqlite->getAdapter()->getDbFile(), 0, $len)) { 73aeca15adSMichael Grosse $this->init(); 74aeca15adSMichael Grosse } 757cbcfbdbSAndreas Gohr if (!$this->sqlite && $throw) { 767cbcfbdbSAndreas Gohr throw new StructException('no sqlite'); 777cbcfbdbSAndreas Gohr } 78083afc55SAndreas Gohr return $this->sqlite; 79549a0837SAndreas Gohr } 80549a0837SAndreas Gohr 810fe33e72SAndreas Gohr /** 820fe33e72SAndreas Gohr * Completely remove the database and reinitialize it 830fe33e72SAndreas Gohr * 840fe33e72SAndreas Gohr * You do not want to call this except for testing! 850fe33e72SAndreas Gohr */ 86d6d97f60SAnna Dabrowska public function resetDB() 87d6d97f60SAnna Dabrowska { 880fe33e72SAndreas Gohr if (!$this->sqlite) return; 890fe33e72SAndreas Gohr $file = $this->sqlite->getAdapter()->getDbFile(); 900fe33e72SAndreas Gohr if (!$file) return; 910fe33e72SAndreas Gohr unlink($file); 920fe33e72SAndreas Gohr clearstatcache(true, $file); 930fe33e72SAndreas Gohr $this->init(); 940fe33e72SAndreas Gohr } 958fefbb59SAndreas Gohr 968fefbb59SAndreas Gohr /** 978fefbb59SAndreas Gohr * Encodes all given arguments into a JSON encoded array 988fefbb59SAndreas Gohr * 998fefbb59SAndreas Gohr * @param string ... 1008fefbb59SAndreas Gohr * @return string 1018fefbb59SAndreas Gohr */ 102*748e747fSAnna Dabrowska public function STRUCT_JSON() // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps 103d6d97f60SAnna Dabrowska { 1048fefbb59SAndreas Gohr $args = func_get_args(); 1058fefbb59SAndreas Gohr return json_encode($args); 1068fefbb59SAndreas Gohr } 107549a0837SAndreas Gohr} 108549a0837SAndreas Gohr 109549a0837SAndreas Gohr// vim:ts=4:sw=4:et: 110