xref: /plugin/sqlite/admin.php (revision 8404199129b696e4ea180c1edf961ce8283fa642)
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    }
29
30    function html() {
31        global $ID;
32
33        echo $this->locale_xhtml('intro');
34
35        if($_REQUEST['db'] && checkSecurityToken()){
36
37            echo '<h2>'.$this->getLang('db').' '.hsc($_REQUEST['db']).'</h2>';
38            echo '<div class="level2">';
39
40            echo '<ul>';
41            echo '<li><div class="li"><a href="'.
42                    wl($ID,array('do'     => 'admin',
43                                 'page'   => 'sqlite',
44                                 'db'     => $_REQUEST['db'],
45                                 'version'=> $_REQUEST['version'],
46                                 'sql'    => 'SELECT name,sql FROM sqlite_master WHERE type=\'table\' ORDER BY name',
47                                 'sectok' => getSecurityToken())).
48                 '">'.$this->getLang('table').'</a></div></li>';
49            echo '<li><div class="li"><a href="'.
50                    wl($ID,array('do'     => 'admin',
51                                 'page'   => 'sqlite',
52                                 'db'     => $_REQUEST['db'],
53                                 'version'=> $_REQUEST['version'],
54                                 'sql'    => 'SELECT name,sql FROM sqlite_master WHERE type=\'index\' ORDER BY name',
55                                 'sectok' => getSecurityToken())).
56                 '">'.$this->getLang('index').'</a></div></li>';
57            echo '</ul>';
58
59            $form = new Doku_Form(array('class'=>'sqliteplugin'));
60            $form->startFieldset('SQL Command');
61            $form->addHidden('id',$ID);
62            $form->addHidden('do','admin');
63            $form->addHidden('page','sqlite');
64            $form->addHidden('db',$_REQUEST['db']);
65            $form->addHidden('version', $_REQUEST['version']);
66            $form->addElement('<textarea name="sql" class="edit">'.hsc($_REQUEST['sql']).'</textarea>');
67            $form->addElement('<input type="submit" class="button" />');
68            $form->endFieldset();
69            $form->printForm();
70
71
72            if($_REQUEST['sql']){
73
74                /** @var $DBI helper_plugin_sqlite */
75                $DBI =& plugin_load('helper', 'sqlite');
76                if(!$DBI->init($_REQUEST['db'],'')) return;
77
78                $sql = explode(";",$_REQUEST['sql']);
79                foreach($sql as $s){
80                    $s = preg_replace('!^\s*--.*$!m', '', $s);
81                    $s = trim($s);
82                    if(!$s) continue;
83
84                    $time_start = microtime(true);
85
86                    $res = $DBI->query("$s;");
87                    if ($res === false) continue;
88
89                    $result = $DBI->res2arr($res);
90
91                    $time_end = microtime(true);
92                    $time = $time_end - $time_start;
93
94                    $cnt = $DBI->res2count($res);
95                    msg($cnt.' affected rows in '.($time<0.0001 ? substr($time,0,5).substr($time,-3) : substr($time,0,7)).' seconds',1);
96                    if(!$cnt) continue;
97
98                    echo '<p>';
99                    $ths = array_keys($result[0]);
100                    echo '<table class="inline">';
101                    echo '<tr>';
102                    foreach($ths as $th){
103                        echo '<th>'.hsc($th).'</th>';
104                    }
105                    echo '</tr>';
106                    foreach($result as $row){
107                        echo '<tr>';
108                        $tds = array_values($row);
109                        foreach($tds as $td){
110                            echo '<td>'.hsc($td).'</td>';
111                        }
112                        echo '</tr>';
113                    }
114                    echo '</table>';
115                    echo '</p>';
116                }
117
118            }
119
120            echo '</div>';
121        }
122    }
123
124    function getTOC(){
125        global $conf;
126        global $ID;
127
128        $toc = array();
129        $fileextensions = array('sqlite2'=>'.sqlite','sqlite3'=>'.sqlite3');
130
131        foreach($fileextensions as $dbformat => $fileextension){
132            $toc[] = array(
133                            'link'  => '',
134                            'title' => $dbformat.':',
135                            'level' => 1,
136                            'type'  => 'ul',
137                         );
138
139            $dbfiles = glob($conf['metadir'].'/*'.$fileextension);
140
141            if(is_array($dbfiles)) foreach($dbfiles as $file){
142                $db = basename($file,$fileextension);
143                $toc[] = array(
144                            'link'  => wl($ID,array('do'=>'admin','page'=>'sqlite','db'=>$db,'version'=>$dbformat,'sectok'=>getSecurityToken())),
145                            'title' => $this->getLang('db').' '.$db,
146                            'level' => 2,
147                            'type'  => 'ul',
148                         );
149            }
150        }
151
152        return $toc;
153    }
154}
155
156// vim:ts=4:sw=4:et:
157