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