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() { 25 return 500; 26 } 27 28 function forAdminOnly() { 29 return true; 30 } 31 32 function handle() { 33 global $conf; 34 if(isset($_POST['sqlite_rename'])) { 35 36 $path = $conf['metadir'].'/'.$_REQUEST['db']; 37 if(io_rename($path.'.sqlite', $path.'.sqlite3')) { 38 msg('Renamed database file succesfull!', 1); 39 //set to new situation 40 $_REQUEST['version'] = 'sqlite3'; 41 42 } else { 43 msg('Renaming database file fails!', -1); 44 } 45 46 } elseif(isset($_POST['sqlite_convert'])) { 47 48 /** @var $DBI helper_plugin_sqlite */ 49 $DBI =& plugin_load('helper', 'sqlite'); 50 $time_start = microtime(true); 51 52 if($dumpfile = $DBI->dumpDatabase($_REQUEST['db'], DOKU_EXT_SQLITE)) { 53 msg('Database temporary dumped to file: '.hsc($dumpfile).'. Now loading in new database...', 1); 54 55 if(!$DBI->fillDatabaseFromDump($_REQUEST['db'], $dumpfile)) { 56 msg('Conversion failed!', -1); 57 return false; 58 } 59 60 //TODO delete dumpfile 61 //return @unlink($dumpfile); 62 //TODO delete old sqlite2-db 63 // return @unlink($conf['metadir'].'/'.$_REQUEST['db'].'.sqlite'); 64 65 msg('Conversion succeed!', 1); 66 //set to new situation 67 $_REQUEST['version'] = 'sqlite3'; 68 } 69 $time_end = microtime(true); 70 $time = $time_end - $time_start; 71 msg('Database "'.hsc($_REQUEST['db']).'" converted from sqlite 2 to 3 in '.$time.' seconds.', 0); 72 73 } 74 } 75 76 function html() { 77 global $ID; 78 global $conf; 79 80 echo $this->locale_xhtml('intro'); 81 82 if(isset($_REQUEST['db']) && checkSecurityToken()) { 83 84 echo '<h2>'.$this->getLang('db').' "'.hsc($_REQUEST['db']).'"</h2>'; 85 echo '<div class="level2">'; 86 87 $sqlcommandform = true; 88 /** @var $DBI helper_plugin_sqlite */ 89 $DBI =& plugin_load('helper', 'sqlite'); 90 if($_REQUEST['version'] == 'sqlite2') { 91 if(helper_plugin_sqlite_adapter::isSqlite3db($conf['metadir'].'/'.$_REQUEST['db'].'.sqlite')) { 92 93 msg('This is a database in sqlite3 format.', 2); 94 msg( 95 'This plugin needs your database file has the extension ".sqlite3" 96 instead of ".sqlite" before it will be recognized as sqlite3 database.', 2 97 ); 98 $form = new Doku_Form(array('method'=> 'post')); 99 $form->addHidden('page', 'sqlite'); 100 $form->addHidden('sqlite_rename', 'go'); 101 $form->addHidden('db', $_REQUEST['db']); 102 $form->addElement(form_makeButton('submit', 'admin', sprintf($this->getLang('rename2to3'), hsc($_REQUEST['db'])))); 103 $form->printForm(); 104 105 if($DBI->existsPDOSqlite()) $sqlcommandform = false; 106 107 } else { 108 if($DBI->existsPDOSqlite()) { 109 $sqlcommandform = false; 110 msg('This is a database in sqlite2 format.', 2); 111 112 if($DBI->existsSqlite2()) { 113 $form = new Doku_Form(array('method'=> 'post')); 114 $form->addHidden('page', 'sqlite'); 115 $form->addHidden('sqlite_convert', 'go'); 116 $form->addHidden('db', $_REQUEST['db']); 117 $form->addElement(form_makeButton('submit', 'admin', sprintf($this->getLang('convert2to3'), hsc($_REQUEST['db'])))); 118 $form->printForm(); 119 } else { 120 msg( 121 'Before PDO sqlite can handle this format, it needs a conversion to the sqlite3 format. 122 Because PHP sqlite extension is not available, 123 you should manually convert "'.hsc($_REQUEST['db']).'.sqlite" in the meta directory to "'.hsc($_REQUEST['db']).'.sqlite3".<br /> 124 See for info about the conversion '.$this->external_link('http://www.sqlite.org/version3.html').'.', -1 125 ); 126 } 127 } 128 } 129 } else { 130 if(!$DBI->existsPDOSqlite()) { 131 $sqlcommandform = false; 132 msg('A database in sqlite3 format needs the PHP PDO sqlite plugin.', -1); 133 } 134 } 135 136 if($sqlcommandform) { 137 echo '<ul>'; 138 echo '<li><div class="li"><a href="'. 139 wl( 140 $ID, array( 141 'do' => 'admin', 142 'page' => 'sqlite', 143 'db' => $_REQUEST['db'], 144 'version'=> $_REQUEST['version'], 145 'sql' => 'SELECT name,sql FROM sqlite_master WHERE type=\'table\' ORDER BY name', 146 'sectok' => getSecurityToken() 147 ) 148 ). 149 '">'.$this->getLang('table').'</a></div></li>'; 150 echo '<li><div class="li"><a href="'. 151 wl( 152 $ID, array( 153 'do' => 'admin', 154 'page' => 'sqlite', 155 'db' => $_REQUEST['db'], 156 'version'=> $_REQUEST['version'], 157 'sql' => 'SELECT name,sql FROM sqlite_master WHERE type=\'index\' ORDER BY name', 158 'sectok' => getSecurityToken() 159 ) 160 ). 161 '">'.$this->getLang('index').'</a></div></li>'; 162 echo '</ul>'; 163 164 $form = new Doku_Form(array('class'=> 'sqliteplugin')); 165 $form->startFieldset('SQL Command'); 166 $form->addHidden('id', $ID); 167 $form->addHidden('do', 'admin'); 168 $form->addHidden('page', 'sqlite'); 169 $form->addHidden('db', $_REQUEST['db']); 170 $form->addHidden('version', $_REQUEST['version']); 171 $form->addElement('<textarea name="sql" class="edit">'.hsc($_REQUEST['sql']).'</textarea>'); 172 $form->addElement('<input type="submit" class="button" />'); 173 $form->endFieldset(); 174 $form->printForm(); 175 176 if($_REQUEST['sql']) { 177 178 if(!$DBI->init($_REQUEST['db'], '')) return; 179 180 $sql = $DBI->SQLstring2array($_REQUEST['sql']); 181 foreach($sql as $s) { 182 $s = preg_replace('!^\s*--.*$!m', '', $s); 183 $s = trim($s); 184 if(!$s) continue; 185 186 $time_start = microtime(true); 187 188 $res = $DBI->query("$s;"); 189 if($res === false) continue; 190 191 $result = $DBI->res2arr($res); 192 193 $time_end = microtime(true); 194 $time = $time_end - $time_start; 195 196 $cnt = $DBI->res2count($res); 197 msg($cnt.' affected rows in '.($time < 0.0001 ? substr($time, 0, 5).substr($time, -3) : substr($time, 0, 7)).' seconds', 1); 198 if(!$cnt) continue; 199 200 echo '<p>'; 201 $ths = array_keys($result[0]); 202 echo '<table class="inline">'; 203 echo '<tr>'; 204 foreach($ths as $th) { 205 echo '<th>'.hsc($th).'</th>'; 206 } 207 echo '</tr>'; 208 foreach($result as $row) { 209 echo '<tr>'; 210 $tds = array_values($row); 211 foreach($tds as $td) { 212 echo '<td>'.hsc($td).'</td>'; 213 } 214 echo '</tr>'; 215 } 216 echo '</table>'; 217 echo '</p>'; 218 } 219 } 220 221 } 222 echo '</div>'; 223 } 224 } 225 226 function getTOC() { 227 global $conf; 228 global $ID; 229 230 $toc = array(); 231 $fileextensions = array('sqlite2'=> '.sqlite', 'sqlite3'=> '.sqlite3'); 232 233 foreach($fileextensions as $dbformat => $fileextension) { 234 $toc[] = array( 235 'link' => wl($ID, array('do'=> 'admin', 'page'=> 'sqlite')), 236 'title' => $dbformat.':', 237 'level' => 1, 238 'type' => 'ul', 239 ); 240 241 $dbfiles = glob($conf['metadir'].'/*'.$fileextension); 242 243 if(is_array($dbfiles)) foreach($dbfiles as $file) { 244 $db = basename($file, $fileextension); 245 $toc[] = array( 246 'link' => wl($ID, array('do'=> 'admin', 'page'=> 'sqlite', 'db'=> $db, 'version'=> $dbformat, 'sectok'=> getSecurityToken())), 247 'title' => $this->getLang('db').' '.$db, 248 'level' => 2, 249 'type' => 'ul', 250 ); 251 } 252 } 253 254 return $toc; 255 } 256} 257 258// vim:ts=4:sw=4:et: 259