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