1549a0837SAndreas Gohr<?php 2*d6d97f60SAnna 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 10549a0837SAndreas Gohr// must be run within Dokuwiki 117cbcfbdbSAndreas Gohruse dokuwiki\plugin\struct\meta\StructException; 127cbcfbdbSAndreas Gohr 13549a0837SAndreas Gohrif (!defined('DOKU_INC')) die(); 14549a0837SAndreas Gohr 15*d6d97f60SAnna Dabrowskaclass helper_plugin_struct_db extends DokuWiki_Plugin 16*d6d97f60SAnna Dabrowska{ 17083afc55SAndreas Gohr /** @var helper_plugin_sqlite */ 18083afc55SAndreas Gohr protected $sqlite; 19083afc55SAndreas Gohr 200fe33e72SAndreas Gohr /** 210fe33e72SAndreas Gohr * helper_plugin_struct_db constructor. 220fe33e72SAndreas Gohr */ 23*d6d97f60SAnna Dabrowska public function __construct() 24*d6d97f60SAnna Dabrowska { 250fe33e72SAndreas Gohr $this->init(); 260fe33e72SAndreas Gohr } 270fe33e72SAndreas Gohr 280fe33e72SAndreas Gohr /** 290fe33e72SAndreas Gohr * Initialize the database 300fe33e72SAndreas Gohr * 310fe33e72SAndreas Gohr * @throws Exception 320fe33e72SAndreas Gohr */ 33*d6d97f60SAnna Dabrowska protected function init() 34*d6d97f60SAnna Dabrowska { 35083afc55SAndreas Gohr /** @var helper_plugin_sqlite $sqlite */ 36083afc55SAndreas Gohr $this->sqlite = plugin_load('helper', 'sqlite'); 37083afc55SAndreas Gohr if (!$this->sqlite) { 3815929be2SAndreas Gohr if (defined('DOKU_UNITTEST')) throw new \Exception('Couldn\'t load sqlite.'); 39083afc55SAndreas Gohr return; 40083afc55SAndreas Gohr } 41083afc55SAndreas Gohr 42609bd281SMichael Große if ($this->sqlite->getAdapter() === null) { 43609bd281SMichael Große if (defined('DOKU_UNITTEST')) throw new \Exception('Couldn\'t load PDO sqlite.'); 44609bd281SMichael Große $this->sqlite = null; 45609bd281SMichael Große return; 46609bd281SMichael Große } 47609bd281SMichael Große 481c502704SAndreas Gohr if ($this->sqlite->getAdapter()->getName() != DOKU_EXT_PDO) { 4915929be2SAndreas Gohr if (defined('DOKU_UNITTEST')) throw new \Exception('Couldn\'t load PDO sqlite.'); 501c502704SAndreas Gohr $this->sqlite = null; 511c502704SAndreas Gohr return; 521c502704SAndreas Gohr } 531c502704SAndreas Gohr $this->sqlite->getAdapter()->setUseNativeAlter(true); 541c502704SAndreas Gohr 55083afc55SAndreas Gohr // initialize the database connection 56083afc55SAndreas Gohr if (!$this->sqlite->init('struct', DOKU_PLUGIN . 'struct/db/')) { 5715929be2SAndreas Gohr if (defined('DOKU_UNITTEST')) throw new \Exception('Couldn\'t init sqlite.'); 587cbcfbdbSAndreas Gohr $this->sqlite = null; 59083afc55SAndreas Gohr return; 60083afc55SAndreas Gohr } 618fefbb59SAndreas Gohr 628fefbb59SAndreas Gohr // register our JSON function with variable parameters 638fefbb59SAndreas Gohr // todo this might be useful to be moved into the sqlite plugin 640e72ef50SAndreas Gohr $this->sqlite->create_function('STRUCT_JSON', array($this, 'STRUCT_JSON'), -1); 65083afc55SAndreas Gohr } 66549a0837SAndreas Gohr 67549a0837SAndreas Gohr /** 687cbcfbdbSAndreas Gohr * @param bool $throw throw an Exception when sqlite not available? 69083afc55SAndreas Gohr * @return helper_plugin_sqlite|null 70549a0837SAndreas Gohr */ 71*d6d97f60SAnna Dabrowska public function getDB($throw = true) 72*d6d97f60SAnna Dabrowska { 73aeca15adSMichael Grosse global $conf; 74aeca15adSMichael Grosse $len = strlen($conf['metadir']); 757cbcfbdbSAndreas Gohr if ($this->sqlite && $conf['metadir'] != substr($this->sqlite->getAdapter()->getDbFile(), 0, $len)) { 76aeca15adSMichael Grosse $this->init(); 77aeca15adSMichael Grosse } 787cbcfbdbSAndreas Gohr if (!$this->sqlite && $throw) { 797cbcfbdbSAndreas Gohr throw new StructException('no sqlite'); 807cbcfbdbSAndreas Gohr } 81083afc55SAndreas Gohr return $this->sqlite; 82549a0837SAndreas Gohr } 83549a0837SAndreas Gohr 840fe33e72SAndreas Gohr /** 850fe33e72SAndreas Gohr * Completely remove the database and reinitialize it 860fe33e72SAndreas Gohr * 870fe33e72SAndreas Gohr * You do not want to call this except for testing! 880fe33e72SAndreas Gohr */ 89*d6d97f60SAnna Dabrowska public function resetDB() 90*d6d97f60SAnna Dabrowska { 910fe33e72SAndreas Gohr if (!$this->sqlite) return; 920fe33e72SAndreas Gohr $file = $this->sqlite->getAdapter()->getDbFile(); 930fe33e72SAndreas Gohr if (!$file) return; 940fe33e72SAndreas Gohr unlink($file); 950fe33e72SAndreas Gohr clearstatcache(true, $file); 960fe33e72SAndreas Gohr $this->init(); 970fe33e72SAndreas Gohr } 988fefbb59SAndreas Gohr 998fefbb59SAndreas Gohr /** 1008fefbb59SAndreas Gohr * Encodes all given arguments into a JSON encoded array 1018fefbb59SAndreas Gohr * 1028fefbb59SAndreas Gohr * @param string ... 1038fefbb59SAndreas Gohr * @return string 1048fefbb59SAndreas Gohr */ 105*d6d97f60SAnna Dabrowska public function STRUCT_JSON() 106*d6d97f60SAnna Dabrowska { 1078fefbb59SAndreas Gohr $args = func_get_args(); 1088fefbb59SAndreas Gohr return json_encode($args); 1098fefbb59SAndreas Gohr } 110549a0837SAndreas Gohr} 111549a0837SAndreas Gohr 112549a0837SAndreas Gohr// vim:ts=4:sw=4:et: 113