xref: /plugin/sqlite/admin.php (revision a1a04581b845a8536f1c68453b7c9d6d2931fa20)
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                    msg(sqlite_num_rows($res).' affected rows',1);
83                    $result = $DBI->res2arr($res);
84                    if(!count($result)) continue;
85
86                    echo '<p>';
87                    $ths = array_keys($result[0]);
88                    echo '<table class="inline">';
89                    echo '<tr>';
90                    foreach($ths as $th){
91                        echo '<th>'.hsc($th).'</th>';
92                    }
93                    echo '</tr>';
94                    foreach($result as $row){
95                        echo '<tr>';
96                        $tds = array_values($row);
97                        foreach($tds as $td){
98                            echo '<td>'.hsc($td).'</td>';
99                        }
100                        echo '</tr>';
101                    }
102                    echo '</table>';
103                    echo '</p>';
104                }
105
106            }
107
108            echo '</div>';
109        }
110    }
111
112    function getTOC(){
113        global $conf;
114        global $ID;
115
116        $toc = array();
117        $dbfiles = glob($conf['metadir'].'/*.sqlite');
118
119
120        if(is_array($dbfiles)) foreach($dbfiles as $file){
121            $db = basename($file,'.sqlite');
122            $toc[] = array(
123                        'link'  => wl($ID,array('do'=>'admin','page'=>'sqlite','db'=>$db,'sectok'=>getSecurityToken())),
124                        'title' => $this->getLang('db').' '.$db,
125                        'level' => 1,
126                        'type'  => 'ul',
127                     );
128        }
129
130        return $toc;
131    }
132}
133
134// vim:ts=4:sw=4:et:enc=utf-8:
135