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 10if(!defined('DOKU_INC')) die(); 11 12class helper_plugin_bez_db extends DokuWiki_Plugin { 13 /** @var helper_plugin_sqlite */ 14 protected $sqlite; 15 16 /** 17 * helper_plugin_struct_db constructor. 18 */ 19 public function __construct() { 20 $this->init(); 21 } 22 23 /** 24 * Initialize the database 25 * 26 * @throws Exception 27 */ 28 protected function init() { 29 /** @var helper_plugin_sqlite $sqlite */ 30 $this->sqlite = plugin_load('helper', 'sqlite'); 31 if(!$this->sqlite) { 32 if(defined('DOKU_UNITTEST')) throw new \Exception('Couldn\'t load sqlite.'); 33 return; 34 } 35 36 if($this->sqlite->getAdapter()->getName() != DOKU_EXT_PDO) { 37 if(defined('DOKU_UNITTEST')) throw new \Exception('Couldn\'t load PDO sqlite.'); 38 $this->sqlite = null; 39 return; 40 } 41 $this->sqlite->getAdapter()->setUseNativeAlter(true); 42 43 // initialize the database connection 44 if(!$this->sqlite->init('b3p', DOKU_PLUGIN . 'bez/db/')) { 45 if(defined('DOKU_UNITTEST')) throw new \Exception('Couldn\'t init sqlite.'); 46 $this->sqlite = null; 47 return; 48 } 49 50 51 } 52 53 /** 54 * @param bool $throw throw an Exception when sqlite not available? 55 * @return helper_plugin_sqlite|null 56 */ 57 public function getDB($throw=true) { 58 global $conf; 59 $len = strlen($conf['metadir']); 60 if ($this->sqlite && $conf['metadir'] != substr($this->sqlite->getAdapter()->getDbFile(),0,$len)) { 61 $this->init(); 62 } 63 if(!$this->sqlite && $throw) { 64 throw new \Exception('no sqlite'); 65 } 66 return $this->sqlite; 67 } 68 69 /** 70 * Completely remove the database and reinitialize it 71 * 72 * You do not want to call this except for testing! 73 */ 74 public function resetDB() { 75 if(!$this->sqlite) return; 76 $file = $this->sqlite->getAdapter()->getDbFile(); 77 if(!$file) return; 78 unlink($file); 79 clearstatcache(true, $file); 80 $this->init(); 81 } 82} 83 84// vim:ts=4:sw=4:et: 85