-
Notifications
You must be signed in to change notification settings - Fork 0
/
citation_exporter_extra.module
333 lines (314 loc) · 10.5 KB
/
citation_exporter_extra.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
<?php
/**
* Implements hook_init().
*/
function citation_exporter_extra_init() {
// Load block definition
module_load_include('inc', 'citation_exporter_extra', 'includes/blocks/export.block');
}
/**
* Implements hook_help().
*/
function citation_exporter_extra_help($path, $arg) {
switch ($path) {
case 'admin/help#citation_exporter_extra':
return '<p>' . t('Enables stream downloads from object data streams thought the use of bibutils and easyrdf') . '</p>';
break;
default;
}
}
/**
* Implements hook_mods_download_format_alter
* @param $formats
* @return array
*/
function citation_exporter_extra_mods_download_format_alter(&$formats) {
$formats = array_merge($formats, array(
'BibTex' => array(
'url' => 'islandora/object/[fedora:pid]/download_mods_as_bibtext',
'title' => "Compatible with Zotero"
),
'EndNote' => array(
'url' => 'islandora/object/[fedora:pid]/download_mods_as_endnote',
'title' => "Compatible with EndNote"
),
'MARC' => array(
'url' => 'islandora/object/[fedora:pid]/download_mods_as_marc'
),
'MARCXML' => array(
'url' => 'islandora/object/[fedora:pid]/download_mods_as_marcxml',
'title' => 'Compatible with XML viewers'
),
'DublinCore' => array(
'url' => 'islandora/object/[fedora:pid]/datastream/DC/download',
'title' => 'Compatible with XML viewers'
),
'RIS' => array(
'url' => 'islandora/object/[fedora:pid]/download_mods_as_ris',
'title' => "Compatible with EndNote, Refworks, Zotero, and Reference Manager"
),
//'JSON',
'JSON' => array(
'url' => 'islandora/object/[fedora:pid]/download_mods_as_json',
'title' => 'Compatible with Zotero'
),
));
return $formats;
}
/**
* @param IslandoraFedoraObject|bool $object
*
* @return array|bool
*/
function citation_exporter_extra_get_export_formats($object = FALSE) {
$format_links = array();
$formats = array();
$enabled_options = variable_get('islandora_export_formats_available_options', []);
// Pull download formats from other modules whereas possible
drupal_alter('mods_download_format', $formats);
// If this is an object, a fedora Object
if (is_object($object) && $object instanceof FedoraObject) {
foreach ($formats as $format => $value) {
if (isset($enabled_options[$format]) && !empty($enabled_options[$format])) {
$format_links[] = l($format, url(token_replace($value['url'], array('fedora' => $object))), array(
'attributes' => array (
'title' => (isset($value['title'])) ? $value['title'] : ''
)
));
}
}
return $format_links;
}
return $formats;
}
/**
* Implements hook_menu().
*/
function citation_exporter_extra_menu() {
return array(
'islandora/object/%islandora_object/download_mods_as_marc' => array(
'title' => 'Download MARC',
'type' => MENU_CALLBACK,
'page callback' => 'citation_exporter_extra_xml_download_mods_to_marc',
'page arguments' => array(2),
'delivery callback' => 'citation_exporter_extra_download',
'access callback' => 'citation_exporter_extra_access_callback',
'access arguments' => array(2)
),
'islandora/object/%islandora_object/download_mods_as_endnote' => array(
'title' => 'Download EndNoteXml',
'type' => MENU_CALLBACK,
'page callback' => 'citation_exporter_extra_xml_download_mods_to_format',
'page arguments' => array(2, 'MODS', 'EndNote'),
'delivery callback' => 'citation_exporter_extra_download',
'access callback' => 'citation_exporter_extra_access_callback',
'access arguments' => array(2)
),
'islandora/object/%islandora_object/download_mods_as_bibtext' => array(
'title' => 'Download BibText',
'type' => MENU_CALLBACK,
'page callback' => 'citation_exporter_extra_xml_download_mods_to_format',
'page arguments' => array(2, 'MODS', 'BibTeX'),
'delivery callback' => 'citation_exporter_extra_download',
'access callback' => 'citation_exporter_extra_access_callback',
'access arguments' => array(2)
),
'islandora/object/%islandora_object/download_mods_as_ris' => array(
'title' => 'Download RIS',
'type' => MENU_CALLBACK,
'page callback' => 'citation_exporter_extra_xml_download_mods_to_format',
'page arguments' => array(2, 'MODS', 'RIS'),
'delivery callback' => 'citation_exporter_extra_download',
'access callback' => 'citation_exporter_extra_access_callback',
'access arguments' => array(2)
),
'islandora/object/%islandora_object/download_mods_as_json' => array(
'title' => 'Download JSON',
'type' => MENU_CALLBACK,
'page callback' => 'citation_exporter_extra_xml_download_mods_to_json',
'page arguments' => array(2),
'delivery callback' => 'citation_exporter_extra_download',
'access callback' => 'citation_exporter_extra_access_callback',
'access arguments' => array(2)
),
);
}
/**
* Prepares a fedora object with a MODS stream to JSON
*
* @param \FedoraObject $object
*
* @return string
*/
function citation_exporter_extra_xml_download_mods_to_json($object) {
module_load_include('inc', 'citeproc', 'includes/converter');
$ds = $object->getDatastream('MODS');
$raw = convert_mods_to_citeproc_jsons($ds->content);
if ($raw) {
$raw_json = json_encode($raw);
if ($raw_json) {
return $raw_json;
}
}
drupal_not_found();
}
/**
* Access callback.
*
* Requires that the given object contains a MODS datastream (and to be
* viewable, as per the stock permissions).
*
* @param AbstractObject $object
* The object to test, if NULL the given object doesn't exist or is
* inaccessible.
*
* @return bool
* TRUE if the user is allowed to access the object, FALSE
* otherwise.
*/
function citation_exporter_extra_access_callback($object) {
return is_object($object) && isset($object['MODS']) && islandora_datastream_access(ISLANDORA_VIEW_OBJECTS, $object['MODS']);
}
/**
*
* Converts an object MODS datastream to marcXML for download
*
* @param $object \FedoraObject
*
* @return string
*/
function citation_exporter_extra_xml_download_mods_to_marc($object) {
$base_module = 'islandora_marcxml';
$marc = function() use ($base_module, $object) {
module_load_include('inc', $base_module, 'includes/utilities');
return islandora_marcxml_transform_marc_to_html($object);
};
if (citation_exporter_extra_access_callback($object)) {
if (!module_exists($base_module)) {
// MarcXML is NOT enabled, try to enable it
if (module_enable(array($base_module))) {
$ob = $marc();
module_disable(array($base_module));
return $ob;
} else {
// We can't enable the module, abort
}
} else {
// Module already enabled
return $marc();
}
} else {
drupal_access_denied();
}
}
/**
*
* Downloads a MODS datastream into the $to format, and returns a file in the
* system
*
* @param \FedoraObject $object
*
* @param $from
* @param $to
*
* @return false|string|void
*/
function citation_exporter_extra_xml_download_mods_to_format($object, $from, $to) {
module_load_include('inc', 'citation_exporter_extra', 'includes/utilities');
/**
* @var \IslandoraFedoraDatastream $ds
*/
$ds = $object->getDatastream('MODS');
// Set unique name to avoid errors when downloading several files from the same object
$t_name = time();
$enxml_file = file_save_data($ds->content, "temporary://temp_en_{$t_name}_{$object->id}");
$enxml_file->status &= ~FILE_STATUS_PERMANENT;
file_save($enxml_file);
$mods_file = drupal_tempnam('temporary://', "temp_mods_{$t_name}_{$object->id}");
$res = BibutilsExtra::Convert(
drupal_realpath($enxml_file->uri),
$from,
drupal_realpath($mods_file),
$to
);
if ($res) {
$to_return = file_get_contents($mods_file);
$f_path = $enxml_file->uri;
file_unmanaged_delete($f_path);
file_unmanaged_delete($mods_file);
return $to_return;
}
else {
drupal_not_found();
}
}
/**
* Delivery callback used to download the output as XML.
*
* @param string $output
* The content to download.
*/
function citation_exporter_extra_download($output) {
$key = arg(3);
$object = arg(2);
$name = 'export';
// If this object has a valid label let's use it to generate our download file
if ($object) {
$fedora_object = islandora_object_load($object);
$name = $fedora_object->label;
}
drupal_add_http_header('Content-length', strlen($output));
drupal_add_http_header('Content-type', 'text/plain;charset=utf8');
switch ($key) {
case 'download_mods_as_marc':
drupal_add_http_header('Content-Disposition', "attachment; filename=\"{$name}.marc\"");
break;
case 'download_mods_as_bibtext':
case 'download_mods_as_format':
drupal_add_http_header('Content-Disposition', "attachment; filename=\"{$name}.bib\"");
break;
case 'download_mods_as_endnote':
drupal_add_http_header('Content-Disposition', "attachment; filename=\"{$name}.end\"");
break;
case 'download_mods_as_ris':
drupal_add_http_header('Content-Disposition', "attachment; filename=\"{$name}.ris\"");
break;
case 'download_mods_as_json':
drupal_add_http_header('Content-Disposition', "attachment; filename=\"{$name}.json\"");
break;
default:
drupal_add_http_header('Content-Disposition', "attachment; filename=\"{$name}.export\"");
}
print $output;
drupal_page_footer();
}
/**
* Implements hook_theme().
*/
function citation_exporter_extra_theme($existing, $type, $theme, $path) {
return array(
'islandora_export_available_formats' => array(
'template' => 'object--export_available_formats',
//'function' => 'theme_islandora_export_available_formats',
'path' => drupal_get_path('module', 'citation_exporter_extra') . '/templates',
'variables' => array (
'object' => NULL,
'formats' => citation_exporter_extra_get_export_formats()
)
)
);
}
/**
* Generates the HTML that will be used with the block
* Implements hook preprocess_islandora_export_available_formats
* @param $variables
*/
function citation_exporter_extra_preprocess_islandora_export_available_formats(&$variables) {
//$generate a list of potential new values!
$variables['available_formats'] = theme_item_list([
'items' => citation_exporter_extra_get_export_formats($variables['object']),
'title' => t('Downloadable format'),
'type' => 'ul',
'attributes' => [],
]);
}