1<?php 2/** 3 * DokuWiki Plugin struct (Helper Component) 4 * 5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 6 * @author Andreas Gohr, Michael Große <dokuwiki@cosmocode.de> 7 */ 8 9// must be run within Dokuwiki 10use dokuwiki\plugin\struct\meta\StructException; 11 12if(!defined('DOKU_INC')) die(); 13 14class helper_plugin_bez_db extends DokuWiki_Plugin { 15 /** @var helper_plugin_sqlite */ 16 protected $sqlite; 17 18 /** 19 * helper_plugin_struct_db constructor. 20 */ 21 public function __construct() { 22 $this->init(); 23 } 24 25 /** 26 * Initialize the database 27 * 28 * @throws Exception 29 */ 30 protected function init() { 31 /** @var helper_plugin_sqlite $sqlite */ 32 $this->sqlite = plugin_load('helper', 'sqlite'); 33 if(!$this->sqlite) { 34 if(defined('DOKU_UNITTEST')) throw new \Exception('Couldn\'t load sqlite.'); 35 return; 36 } 37 38 if($this->sqlite->getAdapter()->getName() != DOKU_EXT_PDO) { 39 if(defined('DOKU_UNITTEST')) throw new \Exception('Couldn\'t load PDO sqlite.'); 40 $this->sqlite = null; 41 return; 42 } 43 $this->sqlite->getAdapter()->setUseNativeAlter(true); 44 45 // initialize the database connection 46 if(!$this->sqlite->init('b3p', DOKU_PLUGIN . 'bez/db/')) { 47 if(defined('DOKU_UNITTEST')) throw new \Exception('Couldn\'t init sqlite.'); 48 $this->sqlite = null; 49 return; 50 } 51 52 53 } 54 55 /** 56 * @param bool $throw throw an Exception when sqlite not available? 57 * @return helper_plugin_sqlite|null 58 */ 59 public function getDB($throw=true) { 60 global $conf; 61 $len = strlen($conf['metadir']); 62 if ($this->sqlite && $conf['metadir'] != substr($this->sqlite->getAdapter()->getDbFile(),0,$len)) { 63 $this->init(); 64 } 65 if(!$this->sqlite && $throw) { 66 throw new StructException('no sqlite'); 67 } 68 return $this->sqlite; 69 } 70 71 /** 72 * Completely remove the database and reinitialize it 73 * 74 * You do not want to call this except for testing! 75 */ 76 public function resetDB() { 77 if(!$this->sqlite) return; 78 $file = $this->sqlite->getAdapter()->getDbFile(); 79 if(!$file) return; 80 unlink($file); 81 clearstatcache(true, $file); 82 $this->init(); 83 } 84} 85 86// vim:ts=4:sw=4:et: 87