1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* Implements hook_menu().
|
5
|
*/
|
6
|
function dwca_export_menu() {
|
7
|
|
8
|
$items = array();
|
9
|
|
10
|
$items['admin/config/system/dwca_export/settings'] = array(
|
11
|
'title' => 'DarwinCore-Archive export',
|
12
|
'page callback' => 'drupal_get_form',
|
13
|
'page arguments' => array('dwca_export_config_form'),
|
14
|
'access arguments' => array('access administration pages'),
|
15
|
'type' => MENU_LOCAL_TASK,
|
16
|
//'file' => 'dwca_export.admin.inc'
|
17
|
);
|
18
|
|
19
|
$items['dwca_export'] = array(
|
20
|
'page callback' => 'dwca_export_deliver_archive',
|
21
|
'access arguments' => array('access content'),
|
22
|
'type' => MENU_CALLBACK
|
23
|
);
|
24
|
|
25
|
|
26
|
return $items;
|
27
|
}
|
28
|
|
29
|
/**
|
30
|
* Implementation of hook_views_api()
|
31
|
*
|
32
|
* drupal will load dwca_export.views_default.inc when this hook is implemented
|
33
|
*/
|
34
|
function dwca_export_views_api() {
|
35
|
return array(
|
36
|
'api' => 3.0
|
37
|
);
|
38
|
}
|
39
|
|
40
|
/**
|
41
|
* Form function, called by drupal_get_form()
|
42
|
* in dwca_export_menu().
|
43
|
*/
|
44
|
function dwca_export_config_form($form, &$form_state) {
|
45
|
|
46
|
global $base_url;
|
47
|
|
48
|
$form['dwca_export_todo'] = array(
|
49
|
'#markup' => '<p><em>No settings implemented yet...</em></p><p>The DarwinCore Archive export is available at '.
|
50
|
l('dwca_export', 'dwca_export')
|
51
|
.'</p>'
|
52
|
);
|
53
|
|
54
|
return system_settings_form($form);
|
55
|
}
|
56
|
|
57
|
|
58
|
/**
|
59
|
* menu callback
|
60
|
*/
|
61
|
function dwca_export_deliver_archive() {
|
62
|
|
63
|
$tmp_archive_file_name = dwca_export_create_archive( _dwca_export_archive_descriptor_file_map() );
|
64
|
|
65
|
if($tmp_archive_file_name && file_valid_uri($tmp_archive_file_name)){
|
66
|
file_transfer($tmp_archive_file_name, array('Content-Type' => 'application/zip'));
|
67
|
} else {
|
68
|
throw new Exception(t('Error creating the archive'));
|
69
|
}
|
70
|
}
|
71
|
|
72
|
|
73
|
/**
|
74
|
* Provides the archive_descriptor_file_map which maps dwca file name to a set of view information.
|
75
|
* The view information contains the fields 'view_name', 'display_id', 'out_file_url'.
|
76
|
* The 'out_file_url' is initailly empty and will be set when this function is called
|
77
|
* with both parameters.
|
78
|
*
|
79
|
* @param unknown_type $file_name
|
80
|
* @param unknown_type $out_file_url
|
81
|
*
|
82
|
* @return the archive_descriptor_file_map
|
83
|
*/
|
84
|
function _dwca_export_archive_descriptor_file_map($file_name = NULL, $out_file_url = null){
|
85
|
static $file_map;
|
86
|
|
87
|
if(!isset($file_map)){
|
88
|
//TODO load from variables, consider using strongarm
|
89
|
$file_map = array(
|
90
|
'classification.txt' => array(
|
91
|
'view_name'=> 'dwca_export_classification',
|
92
|
'display_id' => 'views_data_export_1',
|
93
|
'out_file_url' => NULL
|
94
|
)
|
95
|
);
|
96
|
}
|
97
|
|
98
|
if($file_name && $out_file_url){
|
99
|
$file_map[$file_name]['out_file_url'] = $out_file_url;
|
100
|
}
|
101
|
|
102
|
return $file_map;
|
103
|
}
|
104
|
|
105
|
/**
|
106
|
* Walks all view export paths defined in the $views_map.
|
107
|
* Each file is downloaded to the tmp folder and a zip file
|
108
|
* is bundeled of the resulting csv files plus the meta file.
|
109
|
*
|
110
|
* @param $views_map - maps a view paths to dwca filenames
|
111
|
*
|
112
|
* @return the path in the filesystem to the final archive,
|
113
|
* or FALSE in case of an error.
|
114
|
*/
|
115
|
function dwca_export_create_archive($views_map) {
|
116
|
|
117
|
global $base_url;
|
118
|
|
119
|
// execute all views to export the data into
|
120
|
// temporary csv files (temporary://dwca_export_*). the resulting filenames
|
121
|
// will be stored in _dwca_export_archive_descriptor_file_map()
|
122
|
foreach($views_map as $filename=>$view_data){
|
123
|
|
124
|
$view = views_get_view($view_data['view_name']);
|
125
|
$options = array (
|
126
|
'output_file' => $filename
|
127
|
);
|
128
|
_dwca_export_views_data_export_override_batch($view, $view_data['display_id'], $options);
|
129
|
$view->execute_display($view_data['display_id']);
|
130
|
|
131
|
}
|
132
|
|
133
|
// all data is exported to temporary://dwca_export_*
|
134
|
// now we can start bundeling the actual archive
|
135
|
$tmp_archive_file_name = drupal_tempnam("temporary://", "dwca_export_archive_");
|
136
|
|
137
|
// Unfortunately we cannot use drupals ArchiverZip because there ís
|
138
|
// no way to pass in ZipArchive::CREATE to the constructor to create the archive
|
139
|
// TODO test if zip functionality is available (i.e. if(function_exists('zip_open'))
|
140
|
// but I don't know where the proper location for such a check would be
|
141
|
$zip = new ZipArchive();
|
142
|
// it is safe to use drupal_realpath as the tmp file will be certainly local
|
143
|
// and php's ZipArchive does not handle stream URIs
|
144
|
$result = $zip->open(drupal_realpath($tmp_archive_file_name), ZipArchive::CREATE);
|
145
|
|
146
|
// there might be a better way to get at this information
|
147
|
$module_static_dir_absolute = realpath(drupal_get_path('module', 'dwca_export')) . "/static/";
|
148
|
|
149
|
// at the moment we are using a static meta.xml file
|
150
|
$metadata = "meta.xml";
|
151
|
|
152
|
$zip_root_dir = "dwca_export/";
|
153
|
|
154
|
if ($result !== TRUE) {
|
155
|
throw new Exception(t('Could not create zip_archive %tmp_archive_file_name', array('%tmp_archive_file_name' => $tmp_archive_file_name)));
|
156
|
}else{
|
157
|
// add metadata
|
158
|
$zip->addFile($module_static_dir_absolute.$metadata, $zip_root_dir.$metadata);
|
159
|
// add the csv data files
|
160
|
}
|
161
|
|
162
|
foreach(_dwca_export_archive_descriptor_file_map() as $dwca_filename=>$view_data){
|
163
|
|
164
|
$view_temp_file = $view_data['out_file_url'];
|
165
|
if($view_temp_file){
|
166
|
$zip->addFile(drupal_realpath($view_temp_file), $zip_root_dir.$dwca_filename);
|
167
|
}else{
|
168
|
throw new Exception(t('Cannot create %file', array('%file' => $dwca_filename)));
|
169
|
}
|
170
|
}
|
171
|
|
172
|
$zip->close();
|
173
|
|
174
|
return $tmp_archive_file_name;
|
175
|
|
176
|
|
177
|
}
|
178
|
|
179
|
/**
|
180
|
* Helper function that indicates that we want to
|
181
|
* override the batch that the views_data_export view creates
|
182
|
* on it's initial time through.
|
183
|
*
|
184
|
* Also provides a place to stash options that need to stay around
|
185
|
* until the end of the batch
|
186
|
*
|
187
|
* adapted fom views_data_export.drush.inc
|
188
|
*/
|
189
|
function _dwca_export_views_data_export_override_batch($view = NULL, $display = NULL, $options = TRUE) {
|
190
|
static $_views;
|
191
|
if (isset($view)) {
|
192
|
$_views[$view->name][$display] = $options;
|
193
|
}
|
194
|
return $_views;
|
195
|
}
|
196
|
|
197
|
|
198
|
/**
|
199
|
* Implementation of hook_views_data_export_batch_alter()
|
200
|
*
|
201
|
* adapted fom views_data_export.drush.inc
|
202
|
*
|
203
|
* @see batch_process() in form.inc
|
204
|
*/
|
205
|
function dwca_export_views_data_export_batch_alter(&$batch, &$final_destination, &$querystring) {
|
206
|
|
207
|
$view_name = $batch['view_name'];
|
208
|
$display_id = $batch['display_id'];
|
209
|
|
210
|
$ok_to_override = _dwca_export_views_data_export_override_batch();
|
211
|
|
212
|
// Make sure we do nothing if we are called not following the execution of
|
213
|
// our drush command. This could happen if the file with this function in it
|
214
|
// is included during the normal execution of the view
|
215
|
if (!$ok_to_override[$view_name][$display_id]) {
|
216
|
return;
|
217
|
}
|
218
|
|
219
|
$options = $ok_to_override[$view_name][$display_id];
|
220
|
|
221
|
// We actually never return from the drupal_alter, but
|
222
|
// use drush's batch system to run the same batch
|
223
|
|
224
|
// Add a final callback
|
225
|
$batch['operations'][] = array(
|
226
|
'_dwca_export_views_data_export_batch_finished', array($batch['eid'], $options['output_file']),
|
227
|
);
|
228
|
|
229
|
$batch['progressive'] = FALSE;
|
230
|
}
|
231
|
|
232
|
/**
|
233
|
* Implementation of hook_views_data_export_batch_alter()
|
234
|
*
|
235
|
* @see batch_process() in form.inc
|
236
|
*/
|
237
|
function dwca_export_batch_alter(&$batch, &$final_destination, &$querystring) {
|
238
|
if($batch['source_url'] == 'dwca_export'){
|
239
|
$batch['progressive'] = FALSE;
|
240
|
}
|
241
|
}
|
242
|
|
243
|
|
244
|
|
245
|
/**
|
246
|
* Get's called at the end of the drush batch process that generated our export
|
247
|
*
|
248
|
* adapted fom views_data_export.drush.inc
|
249
|
*/
|
250
|
function _dwca_export_views_data_export_batch_finished($eid, $output_file, &$context) {
|
251
|
// Fetch export info
|
252
|
$export = views_data_export_get($eid);
|
253
|
|
254
|
// Perform cleanup
|
255
|
$view = views_data_export_view_retrieve($eid);
|
256
|
$view->set_display($export->view_display_id);
|
257
|
$view->display_handler->batched_execution_state = $export;
|
258
|
$view->display_handler->remove_index();
|
259
|
|
260
|
// Get path to temp file
|
261
|
$temp_file = $view->display_handler->outputfile_path();
|
262
|
|
263
|
_dwca_export_archive_descriptor_file_map($output_file, $temp_file);
|
264
|
|
265
|
}
|