Пример #1
0
 public function run_import()
 {
     global $woocsv_product;
     /**
      * Are we starting for the first time, than create a batch and continue, else just pick uo the batch code and start where you left
      */
     do_action('woocsv_start_import');
     if (empty($_POST['batch_code'])) {
         //create a new batch
         $batch_code = woocsv_batches::create();
         if ($batch_code) {
             //get max time we have and set the block size
             $max_execution_time = @ini_get('max_execution_time');
             if ($max_execution_time == 0) {
                 $max_execution_time = 30;
             }
             $block_size = $this->get_blocksize();
             $data = array('filename' => $_POST['filename'], 'row' => 0, 'block_size' => $block_size, 'header_name' => $_POST['header_name'], 'seperator' => $_POST['seperator'], 'total_rows' => (int) $_POST['total_rows'], 'start_date' => time(), 'max_execution_time' => $max_execution_time);
             woocsv_batches::update($batch_code, $data);
         } else {
             //@todo die nice
         }
         //and get the batch
         $batch = woocsv_batches::get_batch($batch_code);
     } else {
         $batch_code = $_POST['batch_code'];
         $batch = woocsv_batches::get_batch($_POST['batch_code']);
     }
     //lets check if we are done?
     if ($batch['row'] >= $batch['total_rows']) {
         $batch['end_date'] = time();
         $batch['status'] = 'done';
         woocsv_batches::update($batch_code, $batch);
         do_action('woocsv_after_import_finished');
         //@todo DIE NICE
         $this->die_nicer($batch_code, $batch);
     }
     // do we need to skip the first line?
     if ($this->get_skip_first_line() == 1 && $batch['row'] == 0) {
         $batch['row'] = 1;
     }
     //get the from and till
     $from = $batch['row'];
     $till = $batch['row'] + $batch['block_size'] < $batch['total_rows'] ? $batch['row'] + $batch['block_size'] : $batch['total_rows'];
     //get the lines
     $lines = $this->get_lines_from_file($batch['filename'], $from, $till, $batch['seperator']);
     //get the header
     $header = $this->get_header_from_name($batch['header_name']);
     //turn stuff off
     if (function_exists('wp_suspend_cache_invalidation')) {
         wp_suspend_cache_invalidation(true);
     }
     if (function_exists('wp_defer_term_counting ')) {
         wp_defer_term_counting(true);
     }
     $time_started = microtime(true);
     //loop over the lines and fill,pase and save the lines
     foreach ($lines['lines'] as $line) {
         //reset time ever time around
         @set_time_limit(0);
         //new one and fill in the header and the raw data
         $woocsv_product = new woocsv_import_product();
         $woocsv_product->header = $header;
         $woocsv_product->raw_data = $line;
         //fill it, parse it and save it
         $woocsv_product->fill_in_data();
         $this->write_to_log($batch_code, '-----> row: ' . $batch['row']);
         $woocsv_product->parse_data();
         $this->write_to_log($batch_code, $woocsv_product->log);
         $woocsv_product->save();
         $this->write_to_log($batch_code, $woocsv_product->log);
         //write tot log if debug is on
         if ($this->get_debug() == 0) {
             $this->write_to_log($batch_code, '-----> product dump');
             $this->write_to_log($batch_code, $woocsv_product);
         }
         //close log
         $this->write_to_log($batch_code, '-----> end row: ' . $batch['row']);
         $this->write_to_log($batch_code, '');
         //goto the next row
         $batch['row']++;
         //delete transionts
         if (function_exists('wc_delete_product_transients')) {
             wc_delete_product_transients($woocsv_product->body['ID']);
         }
     }
     $time_finished = microtime(true);
     if (!$this->get_blocksize()) {
         $time_factor = ceil(($batch['max_execution_time'] - ($time_finished - $time_started)) / $batch['max_execution_time'] * 100);
         switch ($time_factor) {
             case $time_factor > 90:
                 $block_size = 10;
                 break;
             case $time_factor > 50:
                 $block_size = 5;
                 break;
             case $time_factor > 10:
                 $block_size = 1;
                 break;
             default:
                 $block_size = 0;
         }
         $batch['block_size'] += $block_size;
         woocsv_batches::update($batch_code, $batch);
     }
     $this->die_nicer($batch_code, $batch);
 }
 public function run_import()
 {
     global $woocsv_product, $wpdb;
     //reset time
     set_time_limit(0);
     //no more cache
     wp_suspend_cache_invalidation(true);
     //disable term counting
     wp_defer_term_counting(true);
     $post_data = $_POST;
     /* solve escape problem when running on windows */
     if (isset($post_data['filename'])) {
         //get the filename and save it
         $filename = $post_data['filename'];
         update_option('woocsv_importfile', $filename);
         unset($post_data['filename']);
     } else {
         $filename = get_option('woocsv_importfile');
     }
     $post_data['batch_filename'] = $filename;
     //we are starting - first time around
     if (empty($post_data['batch'])) {
         do_action('woocsv_start_import');
         $post_data['batch'] = $this->unique_number();
         //create a new batch
         $this->update_batch($post_data, 'running');
     }
     $count = 0;
     $csvcontent = '';
     $handle = fopen($filename, 'r');
     //================================
     // only import the rows needed.
     //================================
     while (($line = fgetcsv($handle, 0, $this->get_separator())) !== FALSE) {
         if ($count >= $post_data['currentrow'] && $count < (int) $post_data['currentrow'] + (int) $post_data['blocksize']) {
             //utf-8 support
             if (get_option('woocsv_convert_to_utf8')) {
                 $line = array_map("utf8_encode", $line);
             }
             $csvContent[$count] = $line;
         }
         $count++;
     }
     unset($handle, $line);
     //========================================================
     // Run only the block from currentrow and the blocksize
     //========================================================
     for ($i = 1; $i <= $this->get_blocksize(); $i++) {
         $woocsv_product = new woocsv_import_product();
         $woocsv_product->header = $this->header;
         $realRow = (int) $post_data['currentrow'] + 1;
         //===================
         // We are finished
         //===================
         if ($post_data['currentrow'] >= $post_data['rows']) {
             ob_get_clean();
             update_option('woocsv_lastrun', array('date' => date("Y-m-d H:i:s"), 'filename' => basename($filename), 'rows' => $post_data['rows']));
             delete_option('woocsv_importfile');
             do_action('woocsv_after_import_finished');
             //finish a new batch
             $this->die_nice($post_data, true);
         }
         // count the rows here else we have a row and than die.
         $this->import_log[] = "--> " . __('row', 'woocsv') . " : " . $realRow . " / " . (int) $post_data['rows'];
         //==================================
         // We want to skip the first line
         //==================================
         if ($this->get_skip_first_line() == 1 && $post_data['currentrow'] == 0) {
             $post_data['currentrow']++;
             $this->import_log[] = __('Skipping the first row', 'woocsv');
             $this->die_nice($post_data);
         }
         //=========================================
         // We do not want to skip the first line
         //=========================================
         if ($this->get_skip_first_line() == 0 && $post_data['currentrow'] == 0) {
             $woocsv_product->raw_data = $csvContent[0];
         }
         if ((int) $post_data['currentrow'] > 0) {
             $woocsv_product->raw_data = $csvContent[$post_data['currentrow']];
         }
         $post_data['currentrow']++;
         //=========================
         // Lets fill in the data
         //=========================
         do_action('woocsv_before_fill_in_data');
         $woocsv_product->fill_in_data();
         do_action('woocsv_after_fill_in_data');
         //===================
         //  lets parse data
         //===================
         $woocsv_product->parse_data();
         //=======================
         // let's save the data
         //=======================
         try {
             $id = $woocsv_product->save();
         } catch (Exception $e) {
             $id = '';
         }
         //===============================================
         // lets fill in the memory stuff for debugging
         //===============================================
         $post_data['memory'] = round(memory_get_usage() / 1024 / 1024, 2);
     }
     //and die nice
     $this->die_nice($post_data);
 }