Beispiel #1
0
/**
 * Upgrade database
 *
 * @param string $path directory with database file
 * @param bool $track track executed queries yes/no
 * @return boolean always true
 */
function db_import_sql_file($path)
{
    $executed_queries = array();
    if (file_exists($path)) {
        $f = fopen($path, 'r');
        if ($f) {
            $ret = array();
            $rest = '';
            while (!feof($f)) {
                $str = $rest . fread($f, 1024);
                $rest = fn_parse_queries($ret, $str);
                if (!empty($ret)) {
                    foreach ($ret as $query) {
                        if (!in_array($query, $executed_queries)) {
                            fn_echo(' .');
                            db_query($query);
                        }
                    }
                    $ret = array();
                }
            }
            fclose($f);
        }
    }
    return true;
}
Beispiel #2
0
/**
 * Fuctnions parses SQL file and import data from it
 *
 * @param string $file File for import
 * @param integer $buffer Buffer size for fread function
 * @param bool $show_status Show or do not show process by printing ' .'
 * @param integer $show_create_table 0 - Do not print the name of created table, 1 - Print name and get lang_var('create_table'), 2 - Print name without getting lang_var
 * @param bool $check_prefix Check table prefix and replace it with the installed in config.php
 * @param bool $track Use queries cache. Do not execute queries that already are executed.
 * @param bool $skip_errors Skip errors or not
 * @param bool $move_progress_bar Move COMET progress bar or not on show progress
 * @return bool false, if file is not accessible
 */
function db_import_sql_file($file, $buffer = 16384, $show_status = true, $show_create_table = 1, $check_prefix = false, $track = false, $skip_errors = false, $move_progress_bar = true)
{
    if (file_exists($file)) {
        $path = dirname($file);
        $file_name = fn_basename($file);
        $tmp_file = $path . "/{$file_name}.tmp";
        $executed_queries = array();
        if ($track && file_exists($tmp_file)) {
            $executed_queries = unserialize(fn_get_contents($tmp_file));
        }
        if ($skip_errors) {
            $_skip_errors = Registry::get('runtime.database.skip_errors');
            Registry::set('runtime.database.skip_errors', true);
        }
        $fd = fopen($file, 'r');
        if ($fd) {
            $ret = array();
            $rest = '';
            $fs = filesize($file);
            if ($show_status && $move_progress_bar) {
                fn_set_progress('step_scale', ceil($fs / $buffer));
            }
            while (!feof($fd)) {
                $str = $rest . fread($fd, $buffer);
                $rest = fn_parse_queries($ret, $str);
                if ($show_status) {
                    fn_set_progress('echo', '<br />' . __('importing_data'), $move_progress_bar);
                }
                if (!empty($ret)) {
                    foreach ($ret as $query) {
                        if (!in_array($query, $executed_queries)) {
                            if ($show_create_table && preg_match('/CREATE\\s+TABLE\\s+`?(\\w+)`?/i', $query, $matches)) {
                                if ($show_create_table == 1) {
                                    $_text = __('creating_table');
                                } elseif ($show_create_table == 2) {
                                    $_text = 'Creating table';
                                }
                                $table_name = $check_prefix ? fn_check_db_prefix($matches[1], Registry::get('config.table_prefix')) : $matches[1];
                                if ($show_status) {
                                    fn_set_progress('echo', '<br />' . $_text . ': <b>' . $table_name . '</b>', $move_progress_bar);
                                }
                            }
                            if ($check_prefix) {
                                $query = fn_check_db_prefix($query);
                            }
                            Database::query($query);
                            if ($track) {
                                $executed_queries[] = $query;
                                fn_put_contents($tmp_file, serialize($executed_queries));
                            }
                            if ($show_status) {
                                fn_echo(' .');
                            }
                        }
                    }
                    $ret = array();
                }
            }
            fclose($fd);
            return true;
        }
        if ($skip_errors) {
            Registry::set('runtime.database.skip_errors', $_skip_errors);
        }
    }
    return false;
}
Beispiel #3
0
 /**
  * Parse and import sql file
  *
  * @param  string $filename path to SQL file
  * @param  string $title    Language value that will be showed on import
  * @param  array  $extra    Extra param
  * @return bool   True on success, false otherwise
  */
 private function _parseSql($filename, $title, $extra = array())
 {
     $app = App::instance();
     $title_shown = false;
     $fd = fopen($filename, 'r');
     if ($fd) {
         $_sess_name = md5($filename);
         if (!empty($_SESSION['parse_sql'][$_sess_name])) {
             if ($_SESSION['parse_sql'][$_sess_name] == 'COMPLETED') {
                 fclose($fd);
                 return true;
             }
             fseek($fd, $_SESSION['parse_sql'][$_sess_name]);
         }
         $rest = '';
         $ret = array();
         $counter = 0;
         while (!feof($fd)) {
             $str = $rest . fread($fd, 16384);
             $rest = fn_parse_queries($ret, $str);
             if (!empty($ret)) {
                 if ($title_shown == false) {
                     $app->setNotification('N', '', $app->t($title, $extra), true);
                     $title_shown = true;
                 }
                 foreach ($ret as $query) {
                     $counter++;
                     if (strpos($query, 'CREATE TABLE') !== false) {
                         preg_match("/CREATE\\s+TABLE\\s+`(\\w*)`/i", $query, $matches);
                         $table_name = str_replace(App::DEFAULT_PREFIX, '', $matches[1]);
                         fn_set_progress('echo', $app->t('creating_table', array('table' => $table_name)));
                     } else {
                         if ($counter > 30 && !App::instance()->isConsole()) {
                             fn_set_progress('echo', '');
                             $counter = 0;
                         }
                     }
                     $query = str_replace(App::DEFAULT_PREFIX, $this->_database_settings['table_prefix'], $query);
                     db_query($query);
                 }
                 $ret = array();
             }
             // Break the connection and re-request
             if (time() - TIME > INSTALL_DB_EXECUTION && !App::instance()->isConsole()) {
                 $pos = ftell($fd);
                 $pos = $pos - strlen($rest);
                 fclose($fd);
                 $_SESSION['parse_sql'][$_sess_name] = $pos;
                 $location = $_SERVER['REQUEST_URI'] . '&no_checking=1';
                 fn_echo("<meta http-equiv=\"Refresh\" content=\"0;URL={$location}\" />");
                 die;
             }
         }
         fclose($fd);
         $_SESSION['parse_sql'][$_sess_name] = 'COMPLETED';
         return true;
     }
     return false;
 }