xref: /plugin/sqlite/admin.php (revision 355597ac6cc200084b5e2a147720a4645963407e)
1<?php
2/**
3 * DokuWiki Plugin sqlite (Admin Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Andreas Gohr <andi@splitbrain.org>
7 */
8
9// must be run within Dokuwiki
10if(!defined('DOKU_INC')) die();
11
12class admin_plugin_sqlite extends DokuWiki_Admin_Plugin {
13
14    function getMenuSort() {
15        return 500;
16    }
17
18    function forAdminOnly() {
19        return true;
20    }
21
22    function handle() {
23        global $conf;
24        if(isset($_POST['sqlite_rename'])) {
25
26            $path = $conf['metadir'].'/'.$_REQUEST['db'];
27            if(io_rename($path.'.sqlite', $path.'.sqlite3')) {
28                msg('Renamed database file succesfull!', 1);
29                //set to new situation
30                $_REQUEST['version'] = 'sqlite3';
31
32            } else {
33                msg('Renaming database file fails!', -1);
34            }
35
36        } elseif(isset($_POST['sqlite_convert'])) {
37
38            /** @var $DBI helper_plugin_sqlite */
39            $DBI        =& plugin_load('helper', 'sqlite');
40            $time_start = microtime(true);
41
42            if($dumpfile = $DBI->dumpDatabase($_REQUEST['db'], DOKU_EXT_SQLITE)) {
43                msg('Database temporary dumped to file: '.hsc($dumpfile).'. Now loading in new database...', 1);
44
45                if(!$DBI->fillDatabaseFromDump($_REQUEST['db'], $dumpfile)) {
46                    msg('Conversion failed!', -1);
47                    return false;
48                }
49
50                //TODO delete dumpfile
51                //return @unlink($dumpfile);
52                //TODO delete old sqlite2-db
53                // return @unlink($conf['metadir'].'/'.$_REQUEST['db'].'.sqlite');
54
55                msg('Conversion succeed!', 1);
56                //set to new situation
57                $_REQUEST['version'] = 'sqlite3';
58            }
59            $time_end = microtime(true);
60            $time     = $time_end - $time_start;
61            msg('Database "'.hsc($_REQUEST['db']).'" converted from sqlite 2 to 3 in '.$time.' seconds.', 0);
62
63        }
64    }
65
66    function html() {
67        global $ID;
68        global $conf;
69
70        echo $this->locale_xhtml('intro');
71
72        if(isset($_REQUEST['db']) && checkSecurityToken()) {
73
74            echo '<h2>'.$this->getLang('db').' "'.hsc($_REQUEST['db']).'"</h2>';
75            echo '<div class="level2">';
76
77            $sqlcommandform = true;
78            /** @var $DBI helper_plugin_sqlite */
79            $DBI =& plugin_load('helper', 'sqlite');
80            if($_REQUEST['version'] == 'sqlite2') {
81                if(helper_plugin_sqlite_adapter::isSqlite3db($conf['metadir'].'/'.$_REQUEST['db'].'.sqlite')) {
82
83                    msg('This is a database in sqlite3 format.', 2);
84                    msg(
85                        'This plugin needs your database file has the extension ".sqlite3"
86                        instead of ".sqlite" before it will be recognized as sqlite3 database.', 2
87                    );
88                    $form = new Doku_Form(array('method'=> 'post'));
89                    $form->addHidden('page', 'sqlite');
90                    $form->addHidden('sqlite_rename', 'go');
91                    $form->addHidden('db', $_REQUEST['db']);
92                    $form->addElement(form_makeButton('submit', 'admin', sprintf($this->getLang('rename2to3'), hsc($_REQUEST['db']))));
93                    $form->printForm();
94
95                    if($DBI->existsPDOSqlite()) $sqlcommandform = false;
96
97                } else {
98                    if($DBI->existsPDOSqlite()) {
99                        $sqlcommandform = false;
100                        msg('This is a database in sqlite2 format.', 2);
101
102                        if($DBI->existsSqlite2()) {
103                            $form = new Doku_Form(array('method'=> 'post'));
104                            $form->addHidden('page', 'sqlite');
105                            $form->addHidden('sqlite_convert', 'go');
106                            $form->addHidden('db', $_REQUEST['db']);
107                            $form->addElement(form_makeButton('submit', 'admin', sprintf($this->getLang('convert2to3'), hsc($_REQUEST['db']))));
108                            $form->printForm();
109                        } else {
110                            msg(
111                                'Before PDO sqlite can handle this format, it needs a conversion to the sqlite3 format.
112                                Because PHP sqlite extension is not available,
113                                you should manually convert "'.hsc($_REQUEST['db']).'.sqlite" in the meta directory to "'.hsc($_REQUEST['db']).'.sqlite3".<br />
114                                See for info about the conversion '.$this->external_link('http://www.sqlite.org/version3.html').'.', -1
115                            );
116                        }
117                    }
118                }
119            } else {
120                if(!$DBI->existsPDOSqlite()) {
121                    $sqlcommandform = false;
122                    msg('A database in sqlite3 format needs the PHP PDO sqlite plugin.', -1);
123                }
124            }
125
126            if($sqlcommandform) {
127                echo '<ul>';
128                echo '<li><div class="li"><a href="'.
129                    wl(
130                        $ID, array(
131                                  'do'     => 'admin',
132                                  'page'   => 'sqlite',
133                                  'db'     => $_REQUEST['db'],
134                                  'version'=> $_REQUEST['version'],
135                                  'sql'    => 'SELECT name,sql FROM sqlite_master WHERE type=\'table\' ORDER BY name',
136                                  'sectok' => getSecurityToken()
137                             )
138                    ).
139                    '">'.$this->getLang('table').'</a></div></li>';
140                echo '<li><div class="li"><a href="'.
141                    wl(
142                        $ID, array(
143                                  'do'     => 'admin',
144                                  'page'   => 'sqlite',
145                                  'db'     => $_REQUEST['db'],
146                                  'version'=> $_REQUEST['version'],
147                                  'sql'    => 'SELECT name,sql FROM sqlite_master WHERE type=\'index\' ORDER BY name',
148                                  'sectok' => getSecurityToken()
149                             )
150                    ).
151                    '">'.$this->getLang('index').'</a></div></li>';
152                echo '</ul>';
153
154                $form = new Doku_Form(array('class'=> 'sqliteplugin'));
155                $form->startFieldset('SQL Command');
156                $form->addHidden('id', $ID);
157                $form->addHidden('do', 'admin');
158                $form->addHidden('page', 'sqlite');
159                $form->addHidden('db', $_REQUEST['db']);
160                $form->addHidden('version', $_REQUEST['version']);
161                $form->addElement('<textarea name="sql" class="edit">'.hsc($_REQUEST['sql']).'</textarea>');
162                $form->addElement('<input type="submit" class="button" />');
163                $form->endFieldset();
164                $form->printForm();
165
166                if($_REQUEST['sql']) {
167
168                    if(!$DBI->init($_REQUEST['db'], '')) return;
169
170                    $sql = $DBI->SQLstring2array($_REQUEST['sql']);
171                    foreach($sql as $s) {
172                        $s = preg_replace('!^\s*--.*$!m', '', $s);
173                        $s = trim($s);
174                        if(!$s) continue;
175
176                        $time_start = microtime(true);
177
178                        $res = $DBI->query("$s;");
179                        if($res === false) continue;
180
181                        $result = $DBI->res2arr($res);
182
183                        $time_end = microtime(true);
184                        $time     = $time_end - $time_start;
185
186                        $cnt = $DBI->res2count($res);
187                        msg($cnt.' affected rows in '.($time < 0.0001 ? substr($time, 0, 5).substr($time, -3) : substr($time, 0, 7)).' seconds', 1);
188                        if(!$cnt) continue;
189
190                        echo '<p>';
191                        $ths = array_keys($result[0]);
192                        echo '<table class="inline">';
193                        echo '<tr>';
194                        foreach($ths as $th) {
195                            echo '<th>'.hsc($th).'</th>';
196                        }
197                        echo '</tr>';
198                        foreach($result as $row) {
199                            echo '<tr>';
200                            $tds = array_values($row);
201                            foreach($tds as $td) {
202                                echo '<td>'.hsc($td).'</td>';
203                            }
204                            echo '</tr>';
205                        }
206                        echo '</table>';
207                        echo '</p>';
208                    }
209                }
210
211            }
212            echo '</div>';
213        }
214    }
215
216    function getTOC() {
217        global $conf;
218        global $ID;
219
220        $toc            = array();
221        $fileextensions = array('sqlite2'=> '.sqlite', 'sqlite3'=> '.sqlite3');
222
223        foreach($fileextensions as $dbformat => $fileextension) {
224            $toc[] = array(
225                'link'  => wl($ID, array('do'=> 'admin', 'page'=> 'sqlite')),
226                'title' => $dbformat.':',
227                'level' => 1,
228                'type'  => 'ul',
229            );
230
231            $dbfiles = glob($conf['metadir'].'/*'.$fileextension);
232
233            if(is_array($dbfiles)) foreach($dbfiles as $file) {
234                $db    = basename($file, $fileextension);
235                $toc[] = array(
236                    'link'  => wl($ID, array('do'=> 'admin', 'page'=> 'sqlite', 'db'=> $db, 'version'=> $dbformat, 'sectok'=> getSecurityToken())),
237                    'title' => $this->getLang('db').' '.$db,
238                    'level' => 2,
239                    'type'  => 'ul',
240                );
241            }
242        }
243
244        return $toc;
245    }
246}
247
248// vim:ts=4:sw=4:et:
249