Skip to content

Commit 91c655b

Browse files
committed
streamline database loading
This removes the init() method in the helper and handles all database initialization in getDB(), lazyloading it only when accessed. If an exception happens during the initialization it is logged before signaling the no sqlite cause.
1 parent 4702d11 commit 91c655b

File tree

1 file changed

+20
-41
lines changed

1 file changed

+20
-41
lines changed

helper/db.php

Lines changed: 20 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -7,57 +7,36 @@
77
* @author Andreas Gohr, Michael Große <dokuwiki@cosmocode.de>
88
*/
99

10-
use dokuwiki\Extension\Event;
10+
use dokuwiki\ErrorHandler;
11+
use dokuwiki\plugin\sqlite\SQLiteDB;
1112
use dokuwiki\plugin\struct\meta\StructException;
1213

1314
class helper_plugin_struct_db extends DokuWiki_Plugin
1415
{
15-
/** @var \dokuwiki\plugin\sqlite\SQLiteDB */
16+
/** @var SQLiteDB */
1617
protected $sqlite;
1718

18-
/**
19-
* helper_plugin_struct_db constructor.
20-
*/
21-
public function __construct()
22-
{
23-
$this->init();
24-
}
25-
26-
/**
27-
* Initialize the database
28-
*
29-
* @throws Exception
30-
*/
31-
protected function init()
32-
{
33-
try {
34-
/** @var \dokuwiki\plugin\sqlite\SQLiteDB $sqlite */
35-
$this->sqlite = new \dokuwiki\plugin\sqlite\SQLiteDB('struct', DOKU_PLUGIN . 'struct/db/');
36-
} catch (Exception $exception) {
37-
if (defined('DOKU_UNITTEST')) throw new \Exception('Couldn\'t load sqlite.');
38-
return;
39-
}
40-
41-
// register our JSON function with variable parameters
42-
$this->sqlite->getPdo()->sqliteCreateFunction('STRUCT_JSON', [$this, 'STRUCT_JSON'], -1);
43-
44-
// this function is meant to be overwritten by plugins
45-
$this->sqlite->getPdo()->sqliteCreateFunction('IS_PUBLISHER', [$this, 'IS_PUBLISHER'], -1);
46-
}
47-
4819
/**
4920
* @param bool $throw throw an Exception when sqlite not available
50-
* @return \dokuwiki\plugin\sqlite\SQLiteDB|null
21+
* @return SQLiteDB|null
5122
*/
5223
public function getDB($throw = true)
5324
{
54-
global $conf;
55-
$len = strlen($conf['metadir']);
56-
if ($this->sqlite && $conf['metadir'] != substr($this->sqlite->getDbFile(), 0, $len)) {
57-
$this->init();
58-
}
59-
if (!$this->sqlite && $throw) {
60-
throw new StructException('no sqlite');
25+
if ($this->sqlite === null) {
26+
try {
27+
$this->sqlite = new SQLiteDB('struct', DOKU_PLUGIN . 'struct/db/');
28+
} catch (\Exception $exception) {
29+
if (defined('DOKU_UNITTEST')) throw new \RuntimeException('Could not load SQLite', 0, $exception);
30+
ErrorHandler::logException($exception);
31+
if ($throw) throw new StructException('no sqlite');
32+
return null;
33+
}
34+
35+
// register our JSON function with variable parameters
36+
$this->sqlite->getPdo()->sqliteCreateFunction('STRUCT_JSON', [$this, 'STRUCT_JSON'], -1);
37+
38+
// this function is meant to be overwritten by plugins
39+
$this->sqlite->getPdo()->sqliteCreateFunction('IS_PUBLISHER', [$this, 'IS_PUBLISHER'], -1);
6140
}
6241
return $this->sqlite;
6342
}
@@ -74,7 +53,7 @@ public function resetDB()
7453
if (!$file) return;
7554
unlink($file);
7655
clearstatcache(true, $file);
77-
$this->init();
56+
$this->sqlite = null;
7857
}
7958

8059
/**

0 commit comments

Comments
 (0)