xref: /plugin/sqlite/admin.php (revision 7f5f9a980e9722f61c7528ae38d17bb6a81e4627)
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                                 'sql'    => 'SELECT name,sql FROM sqlite_master WHERE type=\'table\' ORDER BY name',
46                                 'sectok' => getSecurityToken())).
47                 '">'.$this->getLang('table').'</a></li>';
48            echo '<li><div class="li"><a href="'.
49                    wl($ID,array('do'     => 'admin',
50                                 'page'   => 'sqlite',
51                                 'db'     => $_REQUEST['db'],
52                                 'sql'    => 'SELECT name,sql FROM sqlite_master WHERE type=\'index\' ORDER BY name',
53                                 'sectok' => getSecurityToken())).
54                 '">'.$this->getLang('index').'</a></li>';
55            echo '</ul>';
56
57            $form = new Doku_Form(array());
58            $form->startFieldset('SQL Command');
59            $form->addHidden('id',$ID);
60            $form->addHidden('do','admin');
61            $form->addHidden('page','sqlite');
62            $form->addHidden('db',$_REQUEST['db']);
63            $form->addElement('<textarea name="sql" class="edit">'.hsc($_REQUEST['sql']).'</textarea>');
64            $form->addElement('<input type="submit" class="button" />');
65            $form->endFieldset();
66            $form->printForm();
67
68
69            if($_REQUEST['sql']){
70
71                $DBI =& plugin_load('helper', 'sqlite');
72                if(!$DBI->init($_REQUEST['db'],'')) return;
73
74                $sql = explode(";",$_REQUEST['sql']);
75                foreach($sql as $s){
76                    $s = preg_replace('!^\s*--.*$!m', '', $s);
77                    $s = trim($s);
78                    if(!$s) continue;
79                    $res = $DBI->query("$s;");
80                    if ($res === false) continue;
81
82                    $result = $DBI->res2arr($res);
83                    if(!count($result)) continue;
84
85                    echo '<p>';
86                    $ths = array_keys($result[0]);
87                    echo '<table class="inline">';
88                    echo '<tr>';
89                    foreach($ths as $th){
90                        echo '<th>'.hsc($th).'</th>';
91                    }
92                    echo '</tr>';
93                    foreach($result as $row){
94                        echo '<tr>';
95                        $tds = array_values($row);
96                        foreach($tds as $td){
97                            echo '<td>'.hsc($td).'</td>';
98                        }
99                        echo '</tr>';
100                    }
101                    echo '</table>';
102                    echo '</p>';
103                }
104
105            }
106
107            echo '</div>';
108        }
109    }
110
111    function getTOC(){
112        global $conf;
113        global $ID;
114
115        $toc = array();
116        $dbfiles = glob($conf['metadir'].'/*.sqlite');
117
118
119        foreach($dbfiles as $file){
120            $db = basename($file,'.sqlite');
121            $toc[] = array(
122                        'link'  => wl($ID,array('do'=>'admin','page'=>'sqlite','db'=>$db,'sectok'=>getSecurityToken())),
123                        'title' => $this->getLang('db').' '.$db,
124                        'level' => 1,
125                        'type'  => 'ul',
126                     );
127        }
128
129        return $toc;
130    }
131}
132
133// vim:ts=4:sw=4:et:enc=utf-8:
134