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