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 10549a0837SAndreas Gohrif(!defined('DOKU_INC')) die(); 11549a0837SAndreas Gohr 12549a0837SAndreas Gohrclass helper_plugin_struct_db extends DokuWiki_Plugin { 13083afc55SAndreas Gohr /** @var helper_plugin_sqlite */ 14083afc55SAndreas Gohr protected $sqlite; 15083afc55SAndreas Gohr 160fe33e72SAndreas Gohr /** 170fe33e72SAndreas Gohr * helper_plugin_struct_db constructor. 180fe33e72SAndreas Gohr */ 19083afc55SAndreas Gohr public function __construct() { 200fe33e72SAndreas Gohr $this->init(); 210fe33e72SAndreas Gohr } 220fe33e72SAndreas Gohr 230fe33e72SAndreas Gohr /** 240fe33e72SAndreas Gohr * Initialize the database 250fe33e72SAndreas Gohr * 260fe33e72SAndreas Gohr * @throws Exception 270fe33e72SAndreas Gohr */ 280fe33e72SAndreas Gohr protected function init() { 29083afc55SAndreas Gohr /** @var helper_plugin_sqlite $sqlite */ 30083afc55SAndreas Gohr $this->sqlite = plugin_load('helper', 'sqlite'); 31083afc55SAndreas Gohr if(!$this->sqlite) { 3215929be2SAndreas Gohr if(defined('DOKU_UNITTEST')) throw new \Exception('Couldn\'t load sqlite.'); 3315929be2SAndreas Gohr 341c502704SAndreas Gohr msg('The struct plugin requires the sqlite plugin. Please install it', -1); 35083afc55SAndreas Gohr return; 36083afc55SAndreas Gohr } 37083afc55SAndreas Gohr 381c502704SAndreas Gohr if($this->sqlite->getAdapter()->getName() != DOKU_EXT_PDO) { 3915929be2SAndreas Gohr if(defined('DOKU_UNITTEST')) throw new \Exception('Couldn\'t load PDO sqlite.'); 4015929be2SAndreas Gohr 4115929be2SAndreas Gohr msg('The struct plugin requires sqlite3 you\'re still using sqlite2', -1); 421c502704SAndreas Gohr $this->sqlite = null; 431c502704SAndreas Gohr return; 441c502704SAndreas Gohr } 451c502704SAndreas Gohr $this->sqlite->getAdapter()->setUseNativeAlter(true); 461c502704SAndreas Gohr 47083afc55SAndreas Gohr // initialize the database connection 48083afc55SAndreas Gohr if(!$this->sqlite->init('struct', DOKU_PLUGIN . 'struct/db/')) { 4915929be2SAndreas Gohr if(defined('DOKU_UNITTEST')) throw new \Exception('Couldn\'t init sqlite.'); 50083afc55SAndreas Gohr return; 51083afc55SAndreas Gohr } 528fefbb59SAndreas Gohr 538fefbb59SAndreas Gohr // register our JSON function with variable parameters 548fefbb59SAndreas Gohr // todo this might be useful to be moved into the sqlite plugin 558fefbb59SAndreas Gohr $this->sqlite->create_function('JSON', array($this, 'SQL_JSON'), -1); 56083afc55SAndreas Gohr } 57549a0837SAndreas Gohr 58549a0837SAndreas Gohr /** 59083afc55SAndreas Gohr * @return helper_plugin_sqlite|null 60549a0837SAndreas Gohr */ 61083afc55SAndreas Gohr public function getDB() { 62*aeca15adSMichael Grosse global $conf; 63*aeca15adSMichael Grosse $len = strlen($conf['metadir']); 64*aeca15adSMichael Grosse if ($conf['metadir'] != substr($this->sqlite->getAdapter()->getDbFile(),0,$len)) { 65*aeca15adSMichael Grosse $this->init(); 66*aeca15adSMichael Grosse } 67083afc55SAndreas Gohr return $this->sqlite; 68549a0837SAndreas Gohr } 69549a0837SAndreas Gohr 700fe33e72SAndreas Gohr /** 710fe33e72SAndreas Gohr * Completely remove the database and reinitialize it 720fe33e72SAndreas Gohr * 730fe33e72SAndreas Gohr * You do not want to call this except for testing! 740fe33e72SAndreas Gohr */ 750fe33e72SAndreas Gohr public function resetDB() { 760fe33e72SAndreas Gohr if(!$this->sqlite) return; 770fe33e72SAndreas Gohr $file = $this->sqlite->getAdapter()->getDbFile(); 780fe33e72SAndreas Gohr if(!$file) return; 790fe33e72SAndreas Gohr unlink($file); 800fe33e72SAndreas Gohr clearstatcache(true, $file); 810fe33e72SAndreas Gohr $this->init(); 820fe33e72SAndreas Gohr } 838fefbb59SAndreas Gohr 848fefbb59SAndreas Gohr /** 858fefbb59SAndreas Gohr * Encodes all given arguments into a JSON encoded array 868fefbb59SAndreas Gohr * 878fefbb59SAndreas Gohr * @param string ... 888fefbb59SAndreas Gohr * @return string 898fefbb59SAndreas Gohr */ 908fefbb59SAndreas Gohr public function SQL_JSON() { 918fefbb59SAndreas Gohr $args = func_get_args(); 928fefbb59SAndreas Gohr return json_encode($args); 938fefbb59SAndreas Gohr } 94549a0837SAndreas Gohr} 95549a0837SAndreas Gohr 96549a0837SAndreas Gohr// vim:ts=4:sw=4:et: 97