Index: branches/5.2.x/core/kernel/utility/debugger.php =================================================================== diff -u -N -r14706 -r14707 --- branches/5.2.x/core/kernel/utility/debugger.php (.../debugger.php) (revision 14706) +++ branches/5.2.x/core/kernel/utility/debugger.php (.../debugger.php) (revision 14707) @@ -1,6 +1,6 @@ getMoment(); + $timeStamp = microtime(true); } $this->ProfilerData[$key] = Array('begins' => $timeStamp, 'ends' => 5000, 'debuggerRowID' => count($this->Data)); if (isset($description)) { @@ -999,7 +999,7 @@ function profileFinish($key, $description = null, $timeStamp = null) { if (!isset($timeStamp)) { - $timeStamp = $this->getMoment(); + $timeStamp = microtime(true); } $this->ProfilerData[$key]['ends'] = $timeStamp; @@ -1064,16 +1064,10 @@ $this->ProfilerTotalCount[$total_key]++; } - function getMoment() - { - list($usec, $sec) = explode(' ', microtime()); - return ((float)$usec + (float)$sec); - } - function appendTimestamp($message) { global $start; - $time = $this->getMoment(); + $time = microtime(true); $from_last = $time - $this->LastMoment; $from_start = $time - $start; $this->appendHTML(sprintf("%s %.5f from last %.5f from start", $message, $from_last, $from_start)); Index: branches/5.2.x/core/units/admin/admin_tag_processor.php =================================================================== diff -u -N -r14699 -r14707 --- branches/5.2.x/core/units/admin/admin_tag_processor.php (.../admin_tag_processor.php) (revision 14699) +++ branches/5.2.x/core/units/admin/admin_tag_processor.php (.../admin_tag_processor.php) (revision 14707) @@ -1,6 +1,6 @@ getBackupFiles(); + $backup_helper =& $this->Application->recallObject('BackupHelper'); + /* @var $backup_helper BackupHelper */ - foreach ($datearray as $value) { - $params['backuptimestamp'] = $value['filedate']; - $params['backuptime'] = date('F j, Y, g:i a', $value['filedate']); - $params['backupsize'] = round($value['filesize'] / 1024 / 1024, 2); // MBytes + $ret = ''; + $dates = $backup_helper->getBackupFiles(); + + foreach ($dates as $date) { + $params['backuptimestamp'] = $date['filedate']; + $params['backuptime'] = date('F j, Y, g:i a', $date['filedate']); + $params['backupsize'] = round($date['filesize'] / 1024 / 1024, 2); // MBytes + $ret .= $this->Application->ParseBlock($params); } return $ret; } - function getBackupFiles() - { - $file_helper =& $this->Application->recallObject('FileHelper'); - /* @var $file_helper FileHelper */ - - $ret = Array (); - $backup_path = $this->Application->ConfigValue('Backup_Path'); - $file_helper->CheckFolder($backup_path); - $backup_files = glob($backup_path . DIRECTORY_SEPARATOR . 'dump*.txt'); - - if ( !$backup_files ) { - return Array (); - } - - foreach ($backup_files as $backup_file) { - $ret[] = Array ( - 'filedate' => preg_replace('/^dump([\d]+)\.txt$/', '\\1', basename($backup_file)), - 'filesize' => filesize($backup_file) - ); - } - - rsort($ret); - - return $ret; - - } - /** * Returns phpinfo() output * Index: branches/5.2.x/core/units/helpers/backup_helper.php =================================================================== diff -u -N --- branches/5.2.x/core/units/helpers/backup_helper.php (revision 0) +++ branches/5.2.x/core/units/helpers/backup_helper.php (revision 14707) @@ -0,0 +1,525 @@ +path = $this->Application->ConfigValue('Backup_Path'); + } + + /** + * Backup all data + * + * @return bool + */ + function initBackup() + { + $file_helper =& $this->Application->recallObject('FileHelper'); + /* @var $file_helper FileHelper */ + + if (!$file_helper->CheckFolder($this->path) || !is_writable($this->path)) { + $this->Application->SetVar('error_msg', $this->Application->Phrase('la_Text_backup_access')); + + return false; + } + + $tables = $this->_getBackupTables(); + + $backup_progress = Array ( + 'table_num' => 0, + 'table_names' => $tables, + 'table_count' => count($tables), + 'record_count' => 0, + 'file_name' => $this->getBackupFile( adodb_mktime() ), + ); + + $this->Application->RemoveVar('adm.backupcomplete_filename'); + $this->Application->RemoveVar('adm.backupcomplete_filesize'); + + $fp = fopen($backup_progress['file_name'], 'a'); + + // write down module versions, used during backup + foreach ($this->Application->ModuleInfo as $module_name => $module_info) { + fwrite($fp, '# ' . $module_name . ' Version: ' . $module_info['Version'] . ";\n"); + } + + fwrite($fp, "#------------------------------------------\n\n"); + + // write down table structure + $out = Array (); + + foreach ($tables as $table) { + $out[] = $this->_getCreateTableSql($table); + } + + fwrite($fp, implode("\n", $out) . "\n"); + fclose($fp); + + $this->Application->StoreVar('adm.backup_status', serialize($backup_progress)); + + return true; + } + + /** + * Returns tables, that can be backed up + * + * @return Array + * @access protected + */ + protected function _getBackupTables() + { + $ret = Array (); + $tables = $this->Conn->GetCol('SHOW TABLES LIKE "' . TABLE_PREFIX . '%"'); + + foreach ($tables as $table) { + if ( strpos($table, 'ses_') === false ) { + $ret[] = $table; + } + } + + return $ret; + } + + public function performBackup() + { + $backup_progress = unserialize($this->Application->RecallVar('adm.backup_status')); + $current_table = $backup_progress['table_names'][ $backup_progress['table_num'] ]; + + // get records + $a_records = $this->_getInsertIntoSql($current_table, $backup_progress['record_count'], self::BACKUP_PER_STEP); + + if ( $a_records['num'] < self::BACKUP_PER_STEP ) { + $backup_progress['table_num']++; + $backup_progress['record_count'] = 0; + } + else { + $backup_progress['record_count'] += self::BACKUP_PER_STEP; + } + + if ( $a_records['sql'] ) { + $fp = fopen($backup_progress['file_name'], 'a'); + fwrite($fp, $a_records['sql']); + fclose($fp); + } + + $percent = ($backup_progress['table_num'] / $backup_progress['table_count']) * 100; + + if ( $percent >= 100 ) { + $percent = 100; + $this->Application->StoreVar('adm.backupcomplete_filename', $backup_progress['file_name']); + $this->Application->StoreVar('adm.backupcomplete_filesize', round(filesize($backup_progress['file_name']) / 1024 / 1024, 2)); // Mbytes + } + else { + $this->Application->StoreVar('adm.backup_status', serialize($backup_progress)); + } + + return round($percent); + + } + + /** + * Returns sql, that will insert given amount of data into given table + * + * @param string $table + * @param int $start + * @param int $limit + * @return Array + * @access protected + */ + protected function _getInsertIntoSql($table, $start, $limit) + { + $sql = 'SELECT * + FROM ' . $table . ' + ' . $this->Conn->getLimitClause($start, $limit); + $a_data = $this->Conn->Query($sql); + + if ( !$a_data ) { + return Array ('num' => 0, 'sql' => '',); + } + + $ret = ''; + $fields_sql = $this->_getFieldsSql( $a_data[0] ); + + foreach ($a_data AS $a_row) { + $values_sql = ''; + + foreach ($a_row as $field_value) { + $values_sql .= $this->Conn->qstr($field_value) . ','; + } + + $values_sql = substr($values_sql, 0, -1); + + $sql = 'INSERT INTO ' . $table . ' (' . $fields_sql . ') VALUES (' . $values_sql . ');'; + $sql = str_replace("\n", "\\n", $sql); + $sql = str_replace("\r", "\\r", $sql); + $ret .= $sql . "\n"; + } + + if ( strlen(TABLE_PREFIX) ) { + $ret = str_replace('INSERT INTO ' . TABLE_PREFIX, 'INSERT INTO ', $ret); + } + + return Array ('num' => count($a_data), 'sql' => $ret); + } + + /** + * Builds field list part of INSERT INTO statement based on given fields + * + * @param Array $row + * @return string + */ + protected function _getFieldsSql($row) + { + $ret = ''; + $a_fields = array_keys($row); + + foreach ($a_fields AS $field_name) { + $ret .= '`' . $field_name . '`,'; + } + + return substr($ret, 0, -1); + } + + /** + * Returns sql, that will create given table + * + * @param string $table + * @param string $crlf + * @return string + */ + protected function _getCreateTableSql($table, $crlf = "\n") + { + $this->Conn->Query('SET SQL_QUOTE_SHOW_CREATE = 0'); + $tmp_res = $this->Conn->Query('SHOW CREATE TABLE ' . $table); + + if ( !is_array($tmp_res) || !isset($tmp_res[0]) ) { + return ''; + } + + // add drop table & create table + $schema_create = 'DROP TABLE IF EXISTS ' . $table . ';' . $crlf; + $schema_create .= '# --------------------------------------------------------' . $crlf; + $schema_create .= str_replace("\n", $crlf, $tmp_res[0]['Create Table']); + + // remove table prefix from dump + if ( strlen(TABLE_PREFIX) ) { + $schema_create = str_replace('DROP TABLE IF EXISTS ' . TABLE_PREFIX, 'DROP TABLE ', $schema_create); + $schema_create = str_replace('CREATE TABLE ' . TABLE_PREFIX, 'CREATE TABLE ', $schema_create); + } + + // remove any table properties (e.g. auto-increment, collation, etc) + $last_brace_pos = strrpos($schema_create, ')'); + + if ( $last_brace_pos !== false ) { + $schema_create = substr($schema_create, 0, $last_brace_pos + 1); + } + + $schema_create .= "\n# --------------------------------------------------------\n"; + + return $schema_create; + } + + public function initRestore() + { + $file = $this->getBackupFile(); + + $restoreProgress = Array ( + 'file_pos' => 0, + 'file_name' => $file, + 'file_size' => filesize($file), + ); + + $this->Application->RemoveVar('adm.restore_success'); + $this->Application->StoreVar('adm.restore_status', serialize($restoreProgress)); + } + + /** + * Restores part of backup file + * + * @return int + * @access public + */ + public function performRestore() + { + $restore_progress = unserialize($this->Application->RecallVar('adm.restore_status')); + $filename = $restore_progress['file_name']; + $file_offset = $restore_progress['file_pos']; + $size = filesize($filename); + + if ( $file_offset > $size ) { + return self::FAILED_READING_BACKUP_FILE; + } + + $fp = fopen($filename, "r"); + if ( !$fp ) { + return self::FAILED_READING_BACKUP_FILE; + } + + if ( $file_offset > 0 ) { + fseek($fp, $file_offset); + } + else { + $end_of_sql = FALSE; + $sql = ""; + + while ( !feof($fp) && !$end_of_sql ) { + $line = fgets($fp); + + if ( substr($line, 0, 11) == "INSERT INTO" ) { + $end_of_sql = TRUE; + } + else { + $sql .= $line; + $file_offset = ftell($fp) - strlen($line); + } + } + + if ( strlen($sql) ) { + $error = $this->runSchemaText($sql); + + if ( $error != '' ) { + $this->Application->StoreVar('adm.restore_error', $error); + + return self::SQL_ERROR_DURING_RESTORE; + } + } + + fseek($fp, $file_offset); + } + + $lines_read = 0; + $all_sqls = Array (); + + while ( $lines_read < self::RESTORE_PER_STEP && !feof($fp) ) { + $sql = fgets($fp); + + if ( strlen($sql) ) { + $all_sqls[] = $sql; + $lines_read++; + } + } + + $file_offset = !feof($fp) ? ftell($fp) : $size; + + fclose($fp); + + if ( count($all_sqls) > 0 ) { + $error = $this->runSQLText($all_sqls); + + if ( $error != '' ) { + $this->Application->StoreVar('adm.restore_error', $error); + + return self::SQL_ERROR_DURING_RESTORE; + } + } + + $restore_progress['file_pos'] = $file_offset; + $this->Application->StoreVar('adm.restore_status', serialize($restore_progress)); + + return round($file_offset / $size * 100); + } + + /** + * Run given schema sqls and return error, if any + * + * @param $sql + * @return string + * @access protected + */ + protected function runSchemaText($sql) + { + $table_prefix = 'restore' . TABLE_PREFIX; + + if ( strlen($table_prefix) > 0 ) { + $replacements = Array ('INSERT INTO ', 'UPDATE ', 'ALTER TABLE ', 'DELETE FROM ', 'REPLACE INTO '); + + foreach ($replacements as $replacement) { + $sql = str_replace($replacement, $replacement . $table_prefix, $sql); + } + } + + $sql = str_replace('CREATE TABLE ', 'CREATE TABLE IF NOT EXISTS ' . $table_prefix, $sql); + $sql = str_replace('DROP TABLE ', 'DROP TABLE IF EXISTS ' . $table_prefix, $sql); + + $commands = explode("# --------------------------------------------------------", $sql); + + if ( count($commands) > 0 ) { + $commands = array_map('trim', $commands); + + foreach ($commands as $cmd) { + if ( strlen($cmd) > 0 ) { + $this->Conn->Query($cmd); + + if ( $this->Conn->hasError() ) { + return $this->Conn->getErrorMsg() . " COMMAND:
$cmd
"; + } + } + } + } + + return ''; + } + + /** + * Runs given sqls and return error message, if any + * + * @param $all_sqls + * @return string + * @access protected + */ + protected function runSQLText($all_sqls) + { + $line = 0; + + while ( $line < count($all_sqls) ) { + $sql = $all_sqls[$line]; + + if ( strlen(trim($sql)) > 0 && substr($sql, 0, 1) != "#" ) { + $table_prefix = 'restore' . TABLE_PREFIX; + + if ( strlen($table_prefix) > 0 ) { + $replacements = Array ('INSERT INTO ', 'UPDATE ', 'ALTER TABLE ', 'DELETE FROM ', 'REPLACE INTO '); + + foreach ($replacements as $replacement) { + $sql = str_replace($replacement, $replacement . $table_prefix, $sql); + } + } + + $sql = str_replace('CREATE TABLE ', 'CREATE TABLE IF NOT EXISTS ' . $table_prefix, $sql); + $sql = str_replace('DROP TABLE ', 'DROP TABLE IF EXISTS ' . $table_prefix, $sql); + $sql = trim($sql); + + if ( strlen($sql) > 0 ) { + $this->Conn->Query($sql); + + if ( $this->Conn->hasError() ) { + return $this->Conn->getErrorMsg() . " COMMAND:
$sql
"; + } + } + } + + $line++; + } + + return ''; + } + + /** + * Replaces current tables with restored tables + * + * @return void + * @access public + */ + public function replaceRestoredFiles() + { + // gather restored table names + $tables = $this->Conn->GetCol('SHOW TABLES'); + $mask_restore_table = '/^restore' . TABLE_PREFIX . '(.*)$/'; + + foreach ($tables as $table) { + if ( preg_match($mask_restore_table, $table) ) { + $old_table = substr($table, 7); + + $this->Conn->Query('DROP TABLE IF EXISTS ' . $old_table); + $this->Conn->Query('CREATE TABLE ' . $old_table . ' LIKE ' . $table); + $this->Conn->Query('INSERT INTO ' . $old_table . ' SELECT * FROM ' . $table); + $this->Conn->Query('DROP TABLE ' . $table); + } + } + } + + /** + * Deletes current backup file + * + * @return void + * @access public + */ + public function delete() + { + @unlink($this->getBackupFile()); + } + + /** + * Returns path to current backup file + * + * @param int|null $backup_date + * @return string + * @access protected + */ + protected function getBackupFile($backup_date = null) + { + if ( !isset($backup_date) ) { + $backup_date = $this->Application->GetVar('backupdate'); + } + + return $this->path . '/dump' . $backup_date . '.txt'; + } + + /** + * Returns list of backup files, available for restore + * + * @return Array + * @access public + */ + public function getBackupFiles() + { + $file_helper =& $this->Application->recallObject('FileHelper'); + /* @var $file_helper FileHelper */ + + $ret = Array (); + $backup_path = $this->Application->ConfigValue('Backup_Path'); + $file_helper->CheckFolder($backup_path); + $backup_files = glob($backup_path . DIRECTORY_SEPARATOR . 'dump*.txt'); + + if ( !$backup_files ) { + return Array (); + } + + foreach ($backup_files as $backup_file) { + $ret[] = Array ( + 'filedate' => preg_replace('/^dump([\d]+)\.txt$/', '\\1', basename($backup_file)), + 'filesize' => filesize($backup_file)); + } + + rsort($ret); + + return $ret; + } +} \ No newline at end of file Index: branches/5.2.x/core/units/admin/admin_events_handler.php =================================================================== diff -u -N -r14699 -r14707 --- branches/5.2.x/core/units/admin/admin_events_handler.php (.../admin_events_handler.php) (revision 14699) +++ branches/5.2.x/core/units/admin/admin_events_handler.php (.../admin_events_handler.php) (revision 14707) @@ -1,6 +1,6 @@ Application->ConfigValue('Backup_Path'); + $backup_helper =& $this->Application->recallObject('BackupHelper'); + /* @var $backup_helper BackupHelper */ - $file_helper =& $this->Application->recallObject('FileHelper'); - /* @var $file_helper FileHelper */ - - if (!$file_helper->CheckFolder($backup_path) || !is_writable($backup_path)) { + if ( !$backup_helper->initBackup() ) { $event->status = kEvent::erFAIL; - - $this->Application->SetVar('error_msg', $this->Application->Phrase('la_Text_backup_access')); - return ; } - $a_tables = $this->Conn->GetCol('SHOW TABLES'); // array_keys($tables); - $TableNames = Array(); - for($x=0;$x 0, - 'table_names' => $TableNames, - 'table_count' => count($TableNames), - 'record_count' => 0, - 'file_name' => $backup_path."/dump".adodb_mktime().".txt", - ); - $this->Application->RemoveVar('adm.backupcomplete_filename'); - $this->Application->RemoveVar('adm.backupcomplete_filesize'); - - $out = array(); - - for($x=0;$xGetTableCreate($TableNames[$x]); - } - } - - $fp = fopen($backupProgress['file_name'], 'a'); - - $sql = "SELECT Name, Version FROM ".TABLE_PREFIX."Modules"; - $r = $this->Conn->Query($sql); - foreach ($r AS $a_module) { - $version = $a_module['Version']; - fwrite($fp, "# ".$a_module['Name']." Version: $version;\n"); - } - fwrite($fp, "#------------------------------------------\n\n"); - - fwrite($fp,implode("\n",$out)); - fwrite($fp,"\n"); - - fclose($fp); - - $this->Application->StoreVar('adm.backup_status', serialize($backupProgress)); $event->redirect = 'tools/backup2'; } @@ -756,15 +705,18 @@ */ function OnBackupProgress(&$event) { - $done_percent = $this->performBackup(); + $backup_helper =& $this->Application->recallObject('BackupHelper'); + /* @var $backup_helper BackupHelper */ - if ($done_percent == 100) { + $done_percent = $backup_helper->performBackup(); + + if ( $done_percent == 100 ) { $event->redirect = 'tools/backup3'; - return ; + return; } - echo $done_percent; $event->status = kEvent::erSTOP; + echo $done_percent; } /** @@ -778,422 +730,65 @@ } /** - * Stops Restore & redirect to Restore template - * - * @param kEvent $event - */ - function OnRestoreCancel(&$event) - { - $event->redirect = 'tools/restore1'; - } - - function performBackup() - { - $backupProgress = unserialize($this->Application->RecallVar('adm.backup_status')); -// echo "
"; print_r($backupProgress); echo "
"; -// exit; - $CurrentTable = $backupProgress['table_names'][$backupProgress['table_num']]; - - // get records - $a_records = $this->insert_data($CurrentTable,$backupProgress['record_count'],50,""); -// echo "
"; print_r($a_records); echo "
"; -// exit; - - if ($a_records['num'] < 50) { - $backupProgress['table_num']++; -// if ($backupProgress['table_names'][$backupProgress['table_num']] == TABLE_PREFIX.'Cache') { -// $backupProgress['table_num']++; -// } - $backupProgress['record_count'] = 0; - } else { - $backupProgress['record_count']+=50; - } - - if ($a_records['sql']) { - $fp = fopen($backupProgress['file_name'], 'a'); - fwrite($fp, $a_records['sql']); - fclose($fp); - } - $percent = ($backupProgress['table_num'] / $backupProgress['table_count']) * 100; - if ($percent >= 100) { - $percent = 100; - $this->Application->StoreVar('adm.backupcomplete_filename', $backupProgress['file_name']); - $this->Application->StoreVar('adm.backupcomplete_filesize', round(filesize($backupProgress['file_name'])/1024/1024, 2)); // Mbytes - } else { - $this->Application->StoreVar('adm.backup_status', serialize($backupProgress)); - } - - return round($percent); - - } - - //extracts the rows of data from tables using limits - function insert_data($table, $start, $limit, $mywhere) - { -// global $out; - - if ($mywhere !="") - { - $whereclause= " WHERE ".$mywhere." "; - } - else - { - $whereclause = ""; - } - - $a_data = $this->Conn->Query("SELECT * from $table $whereclause LIMIT $start, $limit"); -// echo "SELECT * from $table $whereclause LIMIT $start, $limit"; -// echo "
"; print_r($a_records); echo "
"; -// exit; - if (!$a_data) { - return Array( - 'num' => 0, - 'sql' => '', - ); - } -// $prefix = GetTablePrefix(); - $rowcount = 0; - $a_fields = array_keys($a_data[0]); - $fields_sql = ''; - foreach ($a_fields AS $field_name) { - $fields_sql .= '`'.$field_name.'`,'; - } - $fields_sql = substr($fields_sql, 0, -1); - $temp = ''; - foreach ($a_data AS $a_row) - { - $values_sql = ''; - foreach ($a_row as $field_name => $field_value) { - $values_sql .= $this->Conn->qstr($field_value).','; - } - $values_sql = substr($values_sql, 0, -1); - $sql = 'INSERT INTO '.$table.' ('.$fields_sql.') VALUES ('.$values_sql.');'; - $sql = str_replace("\n", "\\n", $sql); - $sql = str_replace("\r", "\\r", $sql); - $temp .= $sql."\n"; - } - - if(strlen(TABLE_PREFIX)) - { - $temp = str_replace("INSERT INTO ".TABLE_PREFIX, "INSERT INTO ", $temp); - } - - return Array( - 'num' => count($a_data), - 'sql' => $temp, - ); - } - - - - function GetTableCreate($table, $crlf="\n") - { - $schema_create = 'DROP TABLE IF EXISTS ' . $table . ';' . $crlf; - $schema_create .="# --------------------------------------------------------".$crlf; - $this->Conn->Query("SET SQL_QUOTE_SHOW_CREATE = 0"); - $tmpres = $this->Conn->Query("SHOW CREATE TABLE $table"); -// echo "
"; print_r($tmpres); echo "
"; -// exit; - if(is_array($tmpres) && isset($tmpres[0])) - { - $tmpres = $tmpres[0]; - $pos = strpos($tmpres["Create Table"], ' ('); - $pos2 = strpos($tmpres["Create Table"], '('); - if ($pos2 != $pos + 1) - { - $pos = $pos2; - $tmpres["Create Table"] = str_replace(",", ",\n ", $tmpres["Create Table"]); - } - - $tmpres["Create Table"] = substr($tmpres["Create Table"], 0, 13) - . (($use_backquotes) ? $tmpres["Table"] : $tmpres["Table"]) - . substr($tmpres["Create Table"], $pos); - $tmpres["Create Table"] = str_replace("\n", $crlf, $tmpres["Create Table"]); - if (preg_match_all('((,\r?\n[\s]*(CONSTRAINT|FOREIGN[\s]*KEY)[^\r\n,]+)+)', $tmpres["Create Table"], $regs)) { - if (!isset($sql_constraints)) { - if (isset($GLOBALS['no_constraints_comments'])) { - $sql_constraints = ''; - } else { - $sql_constraints = $crlf . '#' . $crlf - . '# ' . $GLOBALS['strConstraintsForDumped'] . $crlf - . '#' . $crlf; - } - } - if (!isset($GLOBALS['no_constraints_comments'])) { - $sql_constraints .= $crlf .'#' . $crlf .'# ' . $GLOBALS['strConstraintsForTable'] . ' ' . $table . $crlf . '#' . $crlf; - } - $sql_constraints .= 'ALTER TABLE $table $crlf ' - . preg_replace('/(,\r?\n|^)([\s]*)(CONSTRAINT|FOREIGN[\s]*KEY)/', '\1\2ADD \3', substr($regs[0][0], 2)) - . ";\n"; - $tmpres["Create Table"] = preg_replace('((,\r?\n[\s]*(CONSTRAINT|FOREIGN[\s]*KEY)[^\r\n,]+)+)', '', $tmpres["Create Table"]); - } - $schema_create .= $tmpres["Create Table"]; - } - if(strlen($schema_create)) - { - $schema_create = str_replace("DROP TABLE IF EXISTS ".TABLE_PREFIX,"DROP TABLE ",$schema_create); - $schema_create = str_replace("CREATE TABLE ".TABLE_PREFIX,"CREATE TABLE ",$schema_create); - while(strlen($schema_create && substr($schema_create,-1)!=")")) - { - $schema_create = substr($schema_create,0,-1); - } - } - $schema_create .= "\n# --------------------------------------------------------\n"; - return $schema_create; - } - - /** - * Deletes one backup file - * - * @param kEvent $event - */ - function OnDeleteBackup(&$event) - { - @unlink($this->get_backup_file()); - } - - function get_backup_file() - { - return $this->Application->ConfigValue('Backup_Path').'/dump'.$this->Application->GetVar('backupdate').'.txt'; - } - - /** * Starts restore process * * @param kEvent $event */ function OnRestore(&$event) { - $file = $this->get_backup_file(); + $backup_helper =& $this->Application->recallObject('BackupHelper'); + /* @var $backup_helper BackupHelper */ - $restoreProgress = Array ( - 'file_pos' => 0, - 'file_name' => $file, - 'file_size' => filesize($file), - ); - $this->Application->RemoveVar('adm.restore_success'); - $this->Application->StoreVar('adm.restore_status', serialize($restoreProgress)); + $backup_helper->initRestore(); $event->redirect = 'tools/restore3'; } function OnRestoreProgress(&$event) { - $done_percent = $this->performRestore(); + $backup_helper =& $this->Application->recallObject('BackupHelper'); + /* @var $backup_helper BackupHelper */ - if ($done_percent == -3) { + $done_percent = $backup_helper->performRestore(); + + if ( $done_percent == BackupHelper::SQL_ERROR_DURING_RESTORE ) { $event->redirect = 'tools/restore4'; - return ; } - - if ($done_percent < 0) { - $this->Application->StoreVar('adm.restore_error', 'File read error'); $event->redirect = 'tools/restore4'; - return ; + elseif ( $done_percent == BackupHelper::FAILED_READING_BACKUP_FILE ) { + $this->Application->StoreVar('adm.restore_error', 'File read error'); + $event->redirect = 'tools/restore4'; } - - if ($done_percent == 100) { - $this->replaceRestoredFiles(); + elseif ( $done_percent == 100 ) { + $backup_helper->replaceRestoredFiles(); $this->Application->StoreVar('adm.restore_success', 1); $event->redirect = 'tools/restore4'; - return ; } - - echo $done_percent; - $event->status = kEvent::erSTOP; - - } - - function replaceRestoredFiles() - { - // gather restored table names - $tables = $this->Conn->GetCol('SHOW TABLES'); - $mask_restore_table = '/^restore'.TABLE_PREFIX.'(.*)$/'; - foreach($tables as $table) - { - if( preg_match($mask_restore_table,$table,$rets) ) - { - $old_table = substr($table, 7); - $this->Conn->Query('DROP TABLE IF EXISTS '.$old_table); - $this->Conn->Query('CREATE TABLE '.$old_table.' LIKE '.$table); - $this->Conn->Query('INSERT INTO '.$old_table.' SELECT * FROM '.$table); - $this->Conn->Query('DROP TABLE '.$table); - } + else { + $event->status = kEvent::erSTOP; + echo $done_percent; } - } - function performRestore() - { - $restoreProgress = unserialize($this->Application->RecallVar('adm.restore_status')); - $filename = $restoreProgress['file_name']; - $FileOffset = $restoreProgress['file_pos']; - $MaxLines = 200; - $size = filesize($filename); - - if($FileOffset > $size) { - return -2; - } - - $fp = fopen($filename,"r"); - if(!$fp) { - return -1; - } - - - if($FileOffset>0) - { - fseek($fp,$FileOffset); - } - else - { - $EndOfSQL = FALSE; - $sql = ""; - while(!feof($fp) && !$EndOfSQL) - { - $l = fgets($fp); - if(substr($l,0,11)=="INSERT INTO") - { - $EndOfSQL = TRUE; - } - else - { - $sql .= $l; - $FileOffset = ftell($fp) - strlen($l); - } - } - if(strlen($sql)) - { - $error = $this->runSchemaText($sql); - if ($error != '') { - - $this->Application->StoreVar('adm.restore_error', $error); - return -3; - } - } - fseek($fp,$FileOffset); - } - - $LinesRead = 0; - $sql = ""; - $AllSql = array(); - while($LinesRead < $MaxLines && !feof($fp)) - { - $sql = fgets($fp); - if(strlen($sql)) - { - $AllSql[] = $sql; - $LinesRead++; - } - } - if(!feof($fp)) - { - $FileOffset = ftell($fp); - } - else - { - $FileOffset = $size; - } - fclose($fp); - - if(count($AllSql)>0) { - $error = $this->runSQLText($AllSql); - if ($error != '') { - - $this->Application->StoreVar('adm.restore_error', $error); - return -3; - } - - } - $restoreProgress['file_pos'] = $FileOffset; - $this->Application->StoreVar('adm.restore_status', serialize($restoreProgress)); - - return round($FileOffset/$size * 100); -// $this->Application->StoreVar('adm.restore_error', 'lalalal'); -// $event->redirect = 'tools/restore4'; - } - - /** - * Run given schema sqls and return error, if any + * Stops Restore & redirect to Restore template * - * @param $sql - * @return string - * @access protected + * @param kEvent $event */ - protected function runSchemaText($sql) + function OnRestoreCancel(&$event) { - $table_prefix = 'restore' . TABLE_PREFIX; - - if ( strlen($table_prefix) > 0 ) { - $replacements = Array ('INSERT INTO ', 'UPDATE ', 'ALTER TABLE ', 'DELETE FROM ', 'REPLACE INTO '); - - foreach ($replacements as $replacement) { - $sql = str_replace($replacement, $replacement . $table_prefix, $sql); - } - } - - $sql = str_replace('CREATE TABLE ', 'CREATE TABLE IF NOT EXISTS ' . $table_prefix, $sql); - $sql = str_replace('DROP TABLE ', 'DROP TABLE IF EXISTS ' . $table_prefix, $sql); - - $commands = explode("# --------------------------------------------------------", $sql); - - if ( count($commands) > 0 ) { - for ($i = 0; $i < count($commands); $i++) { - $cmd = trim( $commands[$i] ); - - if ( strlen($cmd) > 0 ) { - $this->Conn->Query($cmd); - - if ( $this->Conn->hasError() ) { - return $this->Conn->getErrorMsg() . " COMMAND:
$cmd
"; - } - } - } - } - - return ''; + $event->redirect = 'tools/restore1'; } /** - * Runs given sqls and return error message, if any + * Deletes one backup file * - * @param $all_sqls - * @return string - * @access protected + * @param kEvent $event */ - protected function runSQLText($all_sqls) + function OnDeleteBackup(&$event) { - $line = 0; + $backup_helper =& $this->Application->recallObject('BackupHelper'); + /* @var $backup_helper BackupHelper */ - while ( $line < count($all_sqls) ) { - $sql = $all_sqls[$line]; - if ( strlen(trim($sql)) > 0 && substr($sql, 0, 1) != "#" ) { - $table_prefix = 'restore' . TABLE_PREFIX; - - if ( strlen($table_prefix) > 0 ) { - $replacements = Array ('INSERT INTO ', 'UPDATE ', 'ALTER TABLE ', 'DELETE FROM ', 'REPLACE INTO '); - - foreach ($replacements as $replacement) { - $sql = str_replace($replacement, $replacement . $table_prefix, $sql); - } - } - - $sql = str_replace('CREATE TABLE ', 'CREATE TABLE IF NOT EXISTS ' . $table_prefix, $sql); - $sql = str_replace('DROP TABLE ', 'DROP TABLE IF EXISTS ' . $table_prefix, $sql); - $sql = trim($sql); - - if ( strlen($sql) > 0 ) { - $this->Conn->Query($sql); - - if ( $this->Conn->hasError() ) { - return $this->Conn->getErrorMsg() . " COMMAND:
$sql
"; - } - } - } - - $line++; - } - - return ''; + $backup_helper->delete(); } /** @@ -1205,9 +800,9 @@ { $sql = $this->Application->GetVar('sql'); if ($sql) { - $start = $this->getMoment(); + $start = microtime(true); $result = $this->Conn->Query($sql); - $this->Application->SetVar('sql_time', round($this->getMoment() - $start, 7)); + $this->Application->SetVar('sql_time', round(microtime(true) - $start, 7)); if ($result) @@ -1235,12 +830,6 @@ $event->status = kEvent::erFAIL; } - function getMoment() - { - list($usec, $sec) = explode(' ', microtime()); - return ((float)$usec + (float)$sec); - } - /** * Occurs after unit config cache was successfully rebuilt * Index: branches/5.2.x/core/units/helpers/helpers_config.php =================================================================== diff -u -N -r14585 -r14707 --- branches/5.2.x/core/units/helpers/helpers_config.php (.../helpers_config.php) (revision 14585) +++ branches/5.2.x/core/units/helpers/helpers_config.php (.../helpers_config.php) (revision 14707) @@ -1,6 +1,6 @@ 'SiteHelper', 'class' => 'SiteHelper', 'file' => 'site_helper.php', 'build_event' => '', 'require_classes' => 'kHelper'), Array ('pseudo' => 'DeploymentHelper', 'class' => 'DeploymentHelper', 'file' => 'deployment_helper.php', 'build_event' => '', 'require_classes' => 'kHelper'), + Array ('pseudo' => 'BackupHelper', 'class' => 'BackupHelper', 'file' => 'backup_helper.php', 'build_event' => ''), ), ); \ No newline at end of file