1549a0837SAndreas Gohr<?php 2549a0837SAndreas Gohr/** 3549a0837SAndreas Gohr * DokuWiki Plugin struct (Helper Component) 4549a0837SAndreas Gohr * 5549a0837SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 6549a0837SAndreas Gohr * @author Andreas Gohr, Michael Große <dokuwiki@cosmocode.de> 7549a0837SAndreas Gohr */ 8549a0837SAndreas Gohr 9549a0837SAndreas Gohr// must be run within Dokuwiki 107cbcfbdbSAndreas Gohruse dokuwiki\plugin\struct\meta\StructException; 117cbcfbdbSAndreas Gohr 12549a0837SAndreas Gohrif(!defined('DOKU_INC')) die(); 13549a0837SAndreas Gohr 14549a0837SAndreas Gohrclass helper_plugin_struct_db extends DokuWiki_Plugin { 15083afc55SAndreas Gohr /** @var helper_plugin_sqlite */ 16083afc55SAndreas Gohr protected $sqlite; 17083afc55SAndreas Gohr 180fe33e72SAndreas Gohr /** 190fe33e72SAndreas Gohr * helper_plugin_struct_db constructor. 200fe33e72SAndreas Gohr */ 21083afc55SAndreas Gohr public function __construct() { 220fe33e72SAndreas Gohr $this->init(); 230fe33e72SAndreas Gohr } 240fe33e72SAndreas Gohr 250fe33e72SAndreas Gohr /** 260fe33e72SAndreas Gohr * Initialize the database 270fe33e72SAndreas Gohr * 280fe33e72SAndreas Gohr * @throws Exception 290fe33e72SAndreas Gohr */ 300fe33e72SAndreas Gohr protected function init() { 31083afc55SAndreas Gohr /** @var helper_plugin_sqlite $sqlite */ 32083afc55SAndreas Gohr $this->sqlite = plugin_load('helper', 'sqlite'); 33083afc55SAndreas Gohr if(!$this->sqlite) { 3415929be2SAndreas Gohr if(defined('DOKU_UNITTEST')) throw new \Exception('Couldn\'t load sqlite.'); 35083afc55SAndreas Gohr return; 36083afc55SAndreas Gohr } 37083afc55SAndreas Gohr 38*609bd281SMichael Große if ($this->sqlite->getAdapter() === null) { 39*609bd281SMichael Große if(defined('DOKU_UNITTEST')) throw new \Exception('Couldn\'t load PDO sqlite.'); 40*609bd281SMichael Große $this->sqlite = null; 41*609bd281SMichael Große return; 42*609bd281SMichael Große } 43*609bd281SMichael Große 441c502704SAndreas Gohr if($this->sqlite->getAdapter()->getName() != DOKU_EXT_PDO) { 4515929be2SAndreas Gohr if(defined('DOKU_UNITTEST')) throw new \Exception('Couldn\'t load PDO sqlite.'); 461c502704SAndreas Gohr $this->sqlite = null; 471c502704SAndreas Gohr return; 481c502704SAndreas Gohr } 491c502704SAndreas Gohr $this->sqlite->getAdapter()->setUseNativeAlter(true); 501c502704SAndreas Gohr 51083afc55SAndreas Gohr // initialize the database connection 52083afc55SAndreas Gohr if(!$this->sqlite->init('struct', DOKU_PLUGIN . 'struct/db/')) { 5315929be2SAndreas Gohr if(defined('DOKU_UNITTEST')) throw new \Exception('Couldn\'t init sqlite.'); 547cbcfbdbSAndreas Gohr $this->sqlite = null; 55083afc55SAndreas Gohr return; 56083afc55SAndreas Gohr } 578fefbb59SAndreas Gohr 588fefbb59SAndreas Gohr // register our JSON function with variable parameters 598fefbb59SAndreas Gohr // todo this might be useful to be moved into the sqlite plugin 600e72ef50SAndreas Gohr $this->sqlite->create_function('STRUCT_JSON', array($this, 'STRUCT_JSON'), -1); 61083afc55SAndreas Gohr } 62549a0837SAndreas Gohr 63549a0837SAndreas Gohr /** 647cbcfbdbSAndreas Gohr * @param bool $throw throw an Exception when sqlite not available? 65083afc55SAndreas Gohr * @return helper_plugin_sqlite|null 66549a0837SAndreas Gohr */ 677cbcfbdbSAndreas Gohr public function getDB($throw=true) { 68aeca15adSMichael Grosse global $conf; 69aeca15adSMichael Grosse $len = strlen($conf['metadir']); 707cbcfbdbSAndreas Gohr if ($this->sqlite && $conf['metadir'] != substr($this->sqlite->getAdapter()->getDbFile(),0,$len)) { 71aeca15adSMichael Grosse $this->init(); 72aeca15adSMichael Grosse } 737cbcfbdbSAndreas Gohr if(!$this->sqlite && $throw) { 747cbcfbdbSAndreas Gohr throw new StructException('no sqlite'); 757cbcfbdbSAndreas Gohr } 76083afc55SAndreas Gohr return $this->sqlite; 77549a0837SAndreas Gohr } 78549a0837SAndreas Gohr 790fe33e72SAndreas Gohr /** 800fe33e72SAndreas Gohr * Completely remove the database and reinitialize it 810fe33e72SAndreas Gohr * 820fe33e72SAndreas Gohr * You do not want to call this except for testing! 830fe33e72SAndreas Gohr */ 840fe33e72SAndreas Gohr public function resetDB() { 850fe33e72SAndreas Gohr if(!$this->sqlite) return; 860fe33e72SAndreas Gohr $file = $this->sqlite->getAdapter()->getDbFile(); 870fe33e72SAndreas Gohr if(!$file) return; 880fe33e72SAndreas Gohr unlink($file); 890fe33e72SAndreas Gohr clearstatcache(true, $file); 900fe33e72SAndreas Gohr $this->init(); 910fe33e72SAndreas Gohr } 928fefbb59SAndreas Gohr 938fefbb59SAndreas Gohr /** 948fefbb59SAndreas Gohr * Encodes all given arguments into a JSON encoded array 958fefbb59SAndreas Gohr * 968fefbb59SAndreas Gohr * @param string ... 978fefbb59SAndreas Gohr * @return string 988fefbb59SAndreas Gohr */ 990e72ef50SAndreas Gohr public function STRUCT_JSON() { 1008fefbb59SAndreas Gohr $args = func_get_args(); 1018fefbb59SAndreas Gohr return json_encode($args); 1028fefbb59SAndreas Gohr } 103549a0837SAndreas Gohr} 104549a0837SAndreas Gohr 105549a0837SAndreas Gohr// vim:ts=4:sw=4:et: 106