diff --git a/application/controllers/Adif.php b/application/controllers/Adif.php index b1bb25194..e3dafd313 100644 --- a/application/controllers/Adif.php +++ b/application/controllers/Adif.php @@ -85,17 +85,50 @@ class adif extends CI_Controller { } - // Export all QSO Data in ASC Order of Date. + // Export all QSO Data in ASC Order of Date - use chunks to avoid memory exhaustion public function exportall() { - // Set memory limit to unlimited to allow heavy usage $this->require_tab_access('export'); + ini_set('memory_limit', '-1'); + set_time_limit(300); $this->load->model('adif_data'); + $this->load->library('AdifHelper'); - $data['qsos'] = $this->adif_data->export_all(null, $this->input->post('from', true), $this->input->post('to', true)); + $from = $this->input->post('from', true); + $to = $this->input->post('to', true); - $this->load->view('adif/data/exportall', $data); + $filename = $this->session->userdata('user_callsign').'-'.date('Ymd-Hi').'.adi'; + header('Content-Type: text/plain; charset=utf-8'); + header('Content-Disposition: attachment; filename="'.$filename.'"'); + + // 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"; + + // Stream QSOs in 5K chunks + $offset = 0; + $chunk_size = 5000; + + do { + $qsos = $this->adif_data->export_all_chunked(null, $from, $to, 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; } diff --git a/application/models/Adif_data.php b/application/models/Adif_data.php index e34845546..55e3a569c 100644 --- a/application/models/Adif_data.php +++ b/application/models/Adif_data.php @@ -238,6 +238,48 @@ 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; + } } ?>