*/ // must be run within Dokuwiki if(!defined('DOKU_INC')) die(); class admin_plugin_sqlite extends DokuWiki_Admin_Plugin { function getMenuSort() { return 500; } function forAdminOnly() { return true; } function handle() { global $conf; global $INPUT; if($INPUT->bool('sqlite_rename')) { $path = $conf['metadir'].'/'.$INPUT->str('db'); if(io_rename($path.'.sqlite', $path.'.sqlite3')) { msg('Renamed database file succesfull!', 1); //set to new situation $INPUT->set('version', 'sqlite3'); } else { msg('Renaming database file fails!', -1); } } elseif($INPUT->bool('sqlite_export') && checkSecurityToken()) { /** @var $DBI helper_plugin_sqlite */ $DBI = plugin_load('helper', 'sqlite'); $dbname = $INPUT->str('db'); $dumpfile = $DBI->dumpDatabase($dbname, DOKU_EXT_PDO, true); if ($dumpfile) { header('Content-Type: text/sql'); header('Content-Disposition: attachment; filename="'.$dbname.'.sql";'); readfile($dumpfile); exit(0); } } elseif($INPUT->bool('sqlite_import') && checkSecurityToken()) { global $conf; /** @var $DBI helper_plugin_sqlite */ $DBI = plugin_load('helper', 'sqlite'); $dbname = $INPUT->str('db'); $dumpfile = $_FILES['dumpfile']['tmp_name']; if (empty($dumpfile)) { msg($this->getLang('import_no_file'), -1); return; } if ($DBI->fillDatabaseFromDump($dbname, $dumpfile, true)) { msg($this->getLang('import_success'), 1); } } } function html() { global $ID; global $conf; global $INPUT; echo $this->locale_xhtml('intro'); if($INPUT->has('db') && checkSecurityToken()) { echo '

'.$this->getLang('db').' "'.hsc($INPUT->str('db')).'"

'; echo '
'; $sqlcommandform = true; /** @var $DBI helper_plugin_sqlite */ $DBI = plugin_load('helper', 'sqlite'); if($INPUT->str('version') == 'sqlite2') { if(helper_plugin_sqlite_adapter::isSqlite3db($conf['metadir'].'/'.$INPUT->str('db').'.sqlite')) { msg('This is a database in sqlite3 format.', 2); msg( 'This plugin needs your database file has the extension ".sqlite3" instead of ".sqlite" before it will be recognized as sqlite3 database.', 2 ); $form = new Doku_Form(array('method'=> 'post')); $form->addHidden('page', 'sqlite'); $form->addHidden('sqlite_rename', 'go'); $form->addHidden('db', $INPUT->str('db')); $form->addElement(form_makeButton('submit', 'admin', sprintf($this->getLang('rename2to3'), hsc($INPUT->str('db'))))); $form->printForm(); if($DBI->existsPDOSqlite()) $sqlcommandform = false; } else { msg( 'Before PDO sqlite can handle this format, it needs a conversion to the sqlite3 format. Because PHP sqlite extension is no longer supported, you should manually convert "'.hsc($INPUT->str('db')).'.sqlite" in the meta directory to "'.hsc($INPUT->str('db')).'.sqlite3".
See for info about the conversion '.$this->external_link('http://www.sqlite.org/version3.html').'.', -1 ); } } else { if(!$DBI->existsPDOSqlite()) { $sqlcommandform = false; msg('A database in sqlite3 format needs the PHP PDO sqlite plugin.', -1); } } if($sqlcommandform) { echo ''; /** @var $helper helper_plugin_sqlite */ $sqlite_db = plugin_load('helper', 'sqlite'); $sqlite_db->init('sqlite', DOKU_PLUGIN . 'sqlite/db/'); if($INPUT->str('action') == 'save') { $ok = true; if(empty($INPUT->str('sql'))) { msg($this->getLang('validation query_required'), -1); $ok = false; } if(empty($INPUT->str('name'))) { msg($this->getLang('validation query_name_required'), -1); $ok = false; } if($ok) { $sqlite_db->storeEntry('queries', array( 'db' => $INPUT->str('db'), 'name' => $INPUT->str('name'), 'sql' => $INPUT->str('sql') )); msg($this->getLang('success query_saved'), 1); } } elseif($INPUT->str('action') == 'delete') { $sqlite_db->query("DELETE FROM queries WHERE id=?;", $INPUT->int('query_id')); msg($this->getLang('success query_deleted'), 1); } $form = new Doku_Form(array('class'=> 'sqliteplugin', 'action' => wl($ID, '', true, '&'))); $form->startFieldset('SQL Command'); $form->addHidden('id', $ID); $form->addHidden('do', 'admin'); $form->addHidden('page', 'sqlite'); $form->addHidden('db', $INPUT->str('db')); $form->addHidden('version', $INPUT->str('version')); $form->addElement(''); $form->addElement(' '); $form->addElement(' '); $form->addElement(''); $form->endFieldset(); $form->printForm(); // List saved queries $res = $sqlite_db->query("SELECT id, name, sql FROM queries WHERE db=?", $INPUT->str('db')); $result = $sqlite_db->res2arr($res); if(count($result) > 0) { echo '

' . $this->getLang('saved_queries') . '

'; echo '
'; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; foreach($result as $row) { echo ''; echo ''; $link = wl($ID, array( 'do'=> 'admin', 'page'=> 'sqlite', 'db'=> $INPUT->str('db'), 'version'=> $INPUT->str('version'), 'sql' => $row['sql'], 'sectok'=> getSecurityToken())); echo ''; $link = wl($ID, array( 'do'=> 'admin', 'page'=> 'sqlite', 'db'=> $INPUT->str('db'), 'version'=> $INPUT->str('version'), 'action' => 'delete', 'query_id' => $row['id'], 'sectok'=> getSecurityToken())); echo ''; echo ''; } echo '
namesql
'.hsc($row['name']).''.hsc($row['sql']).'delete
'; echo '
'; } if($INPUT->has('sql')) { if(!$DBI->init($INPUT->str('db'), '')) return; print '

Query results

'; $sql = $DBI->SQLstring2array($INPUT->str('sql')); foreach($sql as $s) { $s = preg_replace('!^\s*--.*$!m', '', $s); $s = trim($s); if(!$s) continue; $time_start = microtime(true); $res = $DBI->query("$s;"); if($res === false) continue; $result = $DBI->res2arr($res); $time_end = microtime(true); $time = $time_end - $time_start; $cnt = $DBI->res2count($res); msg($cnt.' affected rows in '.($time < 0.0001 ? substr($time, 0, 5).substr($time, -3) : substr($time, 0, 7)).' seconds', 1); if(!$cnt) continue; echo '
'; $ths = array_keys($result[0]); echo ''; echo ''; foreach($ths as $th) { echo ''; } echo ''; foreach($result as $row) { echo ''; $tds = array_values($row); foreach($tds as $td) { if($td === null) $td='␀'; echo ''; } echo ''; } echo '
'.hsc($th).'
'.hsc($td).'
'; echo '
'; } } } echo '
'; } } function getTOC() { global $conf; global $ID; $toc = array(); $fileextensions = array('sqlite2'=> '.sqlite', 'sqlite3'=> '.sqlite3'); foreach($fileextensions as $dbformat => $fileextension) { $toc[] = array( 'link' => wl($ID, array('do'=> 'admin', 'page'=> 'sqlite')), 'title' => $dbformat.':', 'level' => 1, 'type' => 'ul', ); $dbfiles = glob($conf['metadir'].'/*'.$fileextension); if(is_array($dbfiles)) foreach($dbfiles as $file) { $db = basename($file, $fileextension); $toc[] = array( 'link' => wl($ID, array('do'=> 'admin', 'page'=> 'sqlite', 'db'=> $db, 'version'=> $dbformat, 'sectok'=> getSecurityToken())), 'title' => $this->getLang('db').' '.$db, 'level' => 2, 'type' => 'ul', ); } } return $toc; } } // vim:ts=4:sw=4:et: