From 1b2bb022e3bb0e911f7b7f1151cc548e38ee93b2 Mon Sep 17 00:00:00 2001 From: int2001 Date: Fri, 5 Dec 2025 06:02:17 +0000 Subject: [PATCH] Use Chunking for Backup as well --- application/controllers/Backup.php | 44 ++++++++++------ application/models/Adif_data.php | 82 +++++++++++++++--------------- 2 files changed, 70 insertions(+), 56 deletions(-) diff --git a/application/controllers/Backup.php b/application/controllers/Backup.php index 5c65e2028..0711c8639 100644 --- a/application/controllers/Backup.php +++ b/application/controllers/Backup.php @@ -29,31 +29,43 @@ class Backup extends CI_Controller { $clean_key = $this->security->xss_clean($key); $this->load->helper('file'); + $this->load->library('AdifHelper'); // Set memory limit to unlimited to allow heavy usage ini_set('memory_limit', '-1'); $this->load->model('adif_data'); + $filename = 'backup/logbook'. date('_Y_m_d_H_i_s') .'.adi'; - $data['qsos'] = $this->adif_data->export_all($clean_key); + header('Content-Type: text/plain; charset=utf-8'); + header('Content-Disposition: attachment; filename="'.$filename.'"'); - $data['filename'] = 'backup/logbook'. date('_Y_m_d_H_i_s') .'.adi'; - - if ( ! write_file($data['filename'], $this->load->view('backup/exportall', $data, true))) - { - $data['status'] = false; - } - else - { - $data['status'] = true; - } + // Output ADIF header // No chance to use exportall-view any longer, because of chunking logic + echo "Wavelog ADIF export\n"; + echo "3.1.6\n"; + echo "config->item('app_name')).">".$this->config->item('app_name')."\r\n"; + echo "optionslib->get_option('version')).">".$this->optionslib->get_option('version')."\r\n"; + echo "\n\n"; - $data['page_title'] = __("ADIF - Backup"); - + // Stream QSOs in 5K chunks + $offset = 0; + $chunk_size = 5000; - $this->load->view('interface_assets/header', $data); - $this->load->view('backup/adif_view'); - $this->load->view('interface_assets/footer'); + do { + $qsos = $this->adif_data->export_all_chunked($clean_key, null, null, false, null, $offset, $chunk_size); + if ($qsos->num_rows() > 0) { + foreach ($qsos->result() as $qso) { + echo $this->adifhelper->getAdifLine($qso); + } + + // Free memory + $qsos->free_result(); + } + + $offset += $chunk_size; + } while ($qsos->num_rows() > 0); + + exit; } /* Export the notes to XML */ diff --git a/application/models/Adif_data.php b/application/models/Adif_data.php index 55e3a569c..ba0ad0de6 100644 --- a/application/models/Adif_data.php +++ b/application/models/Adif_data.php @@ -2,6 +2,48 @@ class adif_data extends CI_Model { + function export_all_chunked($api_key = null, $from = null, $to = null, $exportLotw = false, $onlyop = null, $offset = 0, $limit = 5000) { + $this->load->model('logbooks_model'); + if ($api_key != null) { + $this->load->model('api_model'); + if (strpos($this->api_model->access($api_key), 'r') !== false) { + $this->api_model->update_last_used($api_key); + $user_id = $this->api_model->key_userid($api_key); + $logbooks_locations_array = $this->list_station_locations($user_id); + } + } else { + $this->load->model('stations'); + $logbooks_locations_array = $this->list_station_locations($this->session->userdata('user_id')); + } + + $this->db->select($this->config->item('table_name').'.*, station_profile.*, dxcc_entities.name as station_country'); + $this->db->order_by("COL_TIME_ON", "ASC"); + $this->db->join('station_profile', 'station_profile.station_id = '.$this->config->item('table_name').'.station_id'); + if ($from) { + $this->db->where("date(".$this->config->item('table_name').".COL_TIME_ON) >= ", $from); + } + if ($to) { + $this->db->where("date(".$this->config->item('table_name').".COL_TIME_ON) <= ",$to); + } + if ($onlyop) { + $this->db->where("upper(".$this->config->item('table_name').".col_operator)",$onlyop); + } + if ($exportLotw) { + $this->db->group_start(); + $this->db->where($this->config->item('table_name').".COL_LOTW_QSL_SENT != 'Y'"); + $this->db->or_where($this->config->item('table_name').".COL_LOTW_QSL_SENT", NULL); + $this->db->group_end(); + } + $this->db->where_in('station_profile.station_id', $logbooks_locations_array); + $this->db->join('dxcc_entities', 'station_profile.station_dxcc = dxcc_entities.adif'); + + // Add chunking + $this->db->limit($limit, $offset); + + $query = $this->db->get($this->config->item('table_name')); + return $query; + } + function export_all($api_key = null,$from = null, $to = null, $exportLotw = false, $onlyop = null) { $this->load->model('logbooks_model'); if ($api_key != null) { @@ -239,47 +281,7 @@ class adif_data extends CI_Model { return $this->db->get(); } - function export_all_chunked($api_key = null, $from = null, $to = null, $exportLotw = false, $onlyop = null, $offset = 0, $limit = 5000) { - $this->load->model('logbooks_model'); - if ($api_key != null) { - $this->load->model('api_model'); - if (strpos($this->api_model->access($api_key), 'r') !== false) { - $this->api_model->update_last_used($api_key); - $user_id = $this->api_model->key_userid($api_key); - $logbooks_locations_array = $this->list_station_locations($user_id); - } - } else { - $this->load->model('stations'); - $logbooks_locations_array = $this->list_station_locations($this->session->userdata('user_id')); - } - $this->db->select($this->config->item('table_name').'.*, station_profile.*, dxcc_entities.name as station_country'); - $this->db->order_by("COL_TIME_ON", "ASC"); - $this->db->join('station_profile', 'station_profile.station_id = '.$this->config->item('table_name').'.station_id'); - if ($from) { - $this->db->where("date(".$this->config->item('table_name').".COL_TIME_ON) >= ", $from); - } - if ($to) { - $this->db->where("date(".$this->config->item('table_name').".COL_TIME_ON) <= ",$to); - } - if ($onlyop) { - $this->db->where("upper(".$this->config->item('table_name').".col_operator)",$onlyop); - } - if ($exportLotw) { - $this->db->group_start(); - $this->db->where($this->config->item('table_name').".COL_LOTW_QSL_SENT != 'Y'"); - $this->db->or_where($this->config->item('table_name').".COL_LOTW_QSL_SENT", NULL); - $this->db->group_end(); - } - $this->db->where_in('station_profile.station_id', $logbooks_locations_array); - $this->db->join('dxcc_entities', 'station_profile.station_dxcc = dxcc_entities.adif'); - - // Add chunking - $this->db->limit($limit, $offset); - - $query = $this->db->get($this->config->item('table_name')); - return $query; - } } ?>