Revision 7fcb8ba6
Added by Andreas Kohlbecker over 11 years ago
7.x/modules/dwca_export/dwca_export.module | ||
---|---|---|
8 | 8 |
$items = array(); |
9 | 9 |
|
10 | 10 |
$items['admin/config/system/dwca_export/settings'] = array( |
11 |
|
|
12 |
'title' => t('DarwinCore-Archive export'), |
|
11 |
'title' => 'DarwinCore-Archive export', |
|
13 | 12 |
'page callback' => 'drupal_get_form', |
14 | 13 |
'page arguments' => array('dwca_export_config_form'), |
15 | 14 |
'access arguments' => array('access administration pages'), |
... | ... | |
57 | 56 |
*/ |
58 | 57 |
function dwca_export_deliver_archive() { |
59 | 58 |
|
60 |
$tmp_archive_file_name = dwca_export_create_archive( _get_views_map() );
|
|
59 |
$tmp_archive_file_name = dwca_export_create_archive( _dwca_export_archive_descriptor_file_map() );
|
|
61 | 60 |
|
62 | 61 |
if($tmp_archive_file_name && file_valid_uri($tmp_archive_file_name)){ |
63 | 62 |
file_transfer($tmp_archive_file_name, array('Content-Type' => 'application/zip')); |
... | ... | |
66 | 65 |
} |
67 | 66 |
} |
68 | 67 |
|
68 |
|
|
69 |
/** |
|
70 |
* Provides the archive_descriptor_file_map which maps dwca file name to a set of view information. |
|
71 |
* The view information contains the fields 'view_name', 'display_id', 'out_file_url'. |
|
72 |
* The 'out_file_url' is initailly empty and will be set when this function is called |
|
73 |
* with both parameters. |
|
74 |
* |
|
75 |
* @param unknown_type $file_name |
|
76 |
* @param unknown_type $out_file_url |
|
77 |
* |
|
78 |
* @return the archive_descriptor_file_map |
|
79 |
*/ |
|
80 |
function _dwca_export_archive_descriptor_file_map($file_name = NULL, $out_file_url = null){ |
|
81 |
static $file_map; |
|
82 |
|
|
83 |
if(!isset($file_map)){ |
|
84 |
//TODO load from variables, consider using strongarm |
|
85 |
$file_map = array( |
|
86 |
'classification.txt' => array( |
|
87 |
'view_name'=> 'dwca_export_classification', |
|
88 |
'display_id' => 'views_data_export_1', |
|
89 |
'out_file_url' => NULL |
|
90 |
) |
|
91 |
); |
|
92 |
} |
|
93 |
|
|
94 |
if($file_name && $out_file_url){ |
|
95 |
$file_map[$file_name]['out_file_url'] = $out_file_url; |
|
96 |
} |
|
97 |
|
|
98 |
return $file_map; |
|
99 |
} |
|
100 |
|
|
69 | 101 |
/** |
70 | 102 |
* Walks all view export paths defined in the $views_map. |
71 | 103 |
* Each file is downloaded to the tmp folder and a zip file |
72 | 104 |
* is bundeled of the resulting csv files plus the meta file. |
73 | 105 |
* |
74 |
* @param - $views_map maps a view paths to dwca filenames
|
|
106 |
* @param $views_map - maps a view paths to dwca filenames
|
|
75 | 107 |
* |
76 | 108 |
* @return the path in the filesystem to the final archive, |
77 | 109 |
* or FALSE in case of an error. |
78 | 110 |
*/ |
79 | 111 |
function dwca_export_create_archive($views_map) { |
80 | 112 |
|
81 |
$tmp_archive_file_name = drupal_tempnam("temporary://", "dwca_export_"); |
|
113 |
global $base_url; |
|
114 |
|
|
115 |
// execute all views to export the data into |
|
116 |
// temporary csv files (temporary://dwca_export_*). the resulting filenames |
|
117 |
// will be stored in _dwca_export_archive_descriptor_file_map() |
|
118 |
foreach($views_map as $filename=>$view_data){ |
|
119 |
|
|
120 |
$view = views_get_view($view_data['view_name']); |
|
121 |
$options = array ( |
|
122 |
'output_file' => $filename |
|
123 |
); |
|
124 |
_dwca_export_views_data_export_override_batch($view, $view_data['display_id'], $options); |
|
125 |
$view->execute_display($view_data['display_id']); |
|
126 |
|
|
127 |
} |
|
128 |
|
|
129 |
// all data is exported to temporary://dwca_export_* |
|
130 |
// now we can start bundeling the actual archive |
|
131 |
$tmp_archive_file_name = drupal_tempnam("temporary://", "dwca_export_archive_"); |
|
82 | 132 |
|
83 | 133 |
// Unfortunately we cannot use drupals ArchiverZip because there ís |
84 | 134 |
// no way to pass in ZipArchive::CREATE to the constructor to create the archive |
... | ... | |
103 | 153 |
// add metadata |
104 | 154 |
$zip->addFile($module_static_dir_absolute.$metadata, $zip_root_dir.$metadata); |
105 | 155 |
// add the csv data files |
106 |
foreach($views_map as $view => $file){ |
|
107 |
$view_temp_file = _create_views_file($view); |
|
108 |
if($view_temp_file){ |
|
109 |
$zip->addFile(drupal_realpath($view_temp_file), $zip_root_dir.$file); |
|
110 |
}else{ |
|
111 |
throw new Exception(t('Cannot create %file', array('%file' => $file))); |
|
112 |
} |
|
156 |
} |
|
157 |
|
|
158 |
foreach(_dwca_export_archive_descriptor_file_map() as $dwca_filename=>$view_data){ |
|
159 |
|
|
160 |
$view_temp_file = $view_data['out_file_url']; |
|
161 |
if($view_temp_file){ |
|
162 |
$zip->addFile(drupal_realpath($view_temp_file), $zip_root_dir.$dwca_filename); |
|
163 |
}else{ |
|
164 |
throw new Exception(t('Cannot create %file', array('%file' => $dwca_filename))); |
|
113 | 165 |
} |
114 |
$zip->close(); |
|
166 |
} |
|
167 |
|
|
168 |
$zip->close(); |
|
169 |
|
|
170 |
return $tmp_archive_file_name; |
|
171 |
|
|
115 | 172 |
|
116 |
return $tmp_archive_file_name; |
|
117 |
} |
|
118 | 173 |
} |
119 | 174 |
|
120 | 175 |
/** |
121 |
* TODO Downloads the view and returns the filepath |
|
176 |
* Helper function that indicates that we want to |
|
177 |
* override the batch that the views_data_export view creates |
|
178 |
* on it's initial time through. |
|
122 | 179 |
* |
123 |
* @param string $view
|
|
124 |
* @param string $file
|
|
125 |
* @return a filepath to the downloaded view
|
|
126 |
* @author Niels Hoffmann
|
|
180 |
* Also provides a place to stash options that need to stay around
|
|
181 |
* until the end of the batch
|
|
182 |
* |
|
183 |
* adapted fom views_data_export.drush.inc
|
|
127 | 184 |
*/ |
128 |
function _create_views_file($view, $file) { |
|
129 |
// FIXME this is a mock |
|
130 |
$view_temp_file = drupal_tempnam("temporary://", "dwca_export_view_"); |
|
131 |
return $view_temp_file; |
|
185 |
function _dwca_export_views_data_export_override_batch($view = NULL, $display = NULL, $options = TRUE) { |
|
186 |
static $_views; |
|
187 |
if (isset($view)) { |
|
188 |
$_views[$view->name][$display] = $options; |
|
189 |
} |
|
190 |
return $_views; |
|
132 | 191 |
} |
133 | 192 |
|
193 |
|
|
134 | 194 |
/** |
135 |
* provides the settings which map a view path to |
|
136 |
* a dwca filename. |
|
195 |
* Implementation of hook_views_data_export_batch_alter() |
|
196 |
* |
|
197 |
* adapted fom views_data_export.drush.inc |
|
137 | 198 |
* |
199 |
* @see batch_process() in form.inc |
|
138 | 200 |
*/ |
139 |
function _get_views_map() {
|
|
201 |
function dwca_export_views_data_export_batch_alter(&$batch, &$final_destination, &$querystring) {
|
|
140 | 202 |
|
141 |
return array( |
|
142 |
'dwca_export/classification/csv' => 'classification.txt', |
|
143 |
'dwca_export/extension/test/csv' => 'test.txt', |
|
203 |
$view_name = $batch['view_name']; |
|
204 |
$display_id = $batch['display_id']; |
|
205 |
|
|
206 |
$ok_to_override = _dwca_export_views_data_export_override_batch(); |
|
207 |
|
|
208 |
// Make sure we do nothing if we are called not following the execution of |
|
209 |
// our drush command. This could happen if the file with this function in it |
|
210 |
// is included during the normal execution of the view |
|
211 |
if (!$ok_to_override[$view_name][$display_id]) { |
|
212 |
return; |
|
213 |
} |
|
214 |
|
|
215 |
$options = $ok_to_override[$view_name][$display_id]; |
|
216 |
|
|
217 |
// We actually never return from the drupal_alter, but |
|
218 |
// use drush's batch system to run the same batch |
|
219 |
|
|
220 |
// Add a final callback |
|
221 |
$batch['operations'][] = array( |
|
222 |
'_dwca_export_views_data_export_batch_finished', array($batch['eid'], $options['output_file']), |
|
144 | 223 |
); |
224 |
|
|
225 |
$batch['progressive'] = FALSE; |
|
226 |
} |
|
227 |
|
|
228 |
/** |
|
229 |
* Implementation of hook_views_data_export_batch_alter() |
|
230 |
* |
|
231 |
* @see batch_process() in form.inc |
|
232 |
*/ |
|
233 |
function dwca_export_batch_alter(&$batch, &$final_destination, &$querystring) { |
|
234 |
if($batch['source_url'] == 'dwca_export'){ |
|
235 |
$batch['progressive'] = FALSE; |
|
236 |
} |
|
237 |
} |
|
238 |
|
|
239 |
|
|
240 |
|
|
241 |
/** |
|
242 |
* Get's called at the end of the drush batch process that generated our export |
|
243 |
* |
|
244 |
* adapted fom views_data_export.drush.inc |
|
245 |
*/ |
|
246 |
function _dwca_export_views_data_export_batch_finished($eid, $output_file, &$context) { |
|
247 |
// Fetch export info |
|
248 |
$export = views_data_export_get($eid); |
|
249 |
|
|
250 |
// Perform cleanup |
|
251 |
$view = views_data_export_view_retrieve($eid); |
|
252 |
$view->set_display($export->view_display_id); |
|
253 |
$view->display_handler->batched_execution_state = $export; |
|
254 |
$view->display_handler->remove_index(); |
|
255 |
|
|
256 |
// Get path to temp file |
|
257 |
$temp_file = $view->display_handler->outputfile_path(); |
|
258 |
|
|
259 |
_dwca_export_archive_descriptor_file_map($output_file, $temp_file); |
|
260 |
|
|
145 | 261 |
} |
Also available in: Unified diff
exporting views to temp files, megerd with creating zip