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