7
7
* @author Andreas Gohr, Michael Große <dokuwiki@cosmocode.de>
8
8
*/
9
9
10
- use dokuwiki \Extension \Event ;
10
+ use dokuwiki \ErrorHandler ;
11
+ use dokuwiki \plugin \sqlite \SQLiteDB ;
11
12
use dokuwiki \plugin \struct \meta \StructException ;
12
13
13
14
class helper_plugin_struct_db extends DokuWiki_Plugin
14
15
{
15
- /** @var \dokuwiki\plugin\sqlite\ SQLiteDB */
16
+ /** @var SQLiteDB */
16
17
protected $ sqlite ;
17
18
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
-
48
19
/**
49
20
* @param bool $throw throw an Exception when sqlite not available
50
- * @return \dokuwiki\plugin\sqlite\ SQLiteDB|null
21
+ * @return SQLiteDB|null
51
22
*/
52
23
public function getDB ($ throw = true )
53
24
{
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 );
61
40
}
62
41
return $ this ->sqlite ;
63
42
}
@@ -74,7 +53,7 @@ public function resetDB()
74
53
if (!$ file ) return ;
75
54
unlink ($ file );
76
55
clearstatcache (true , $ file );
77
- $ this ->init () ;
56
+ $ this ->sqlite = null ;
78
57
}
79
58
80
59
/**
0 commit comments