-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathplugin.php
More file actions
443 lines (375 loc) · 13.5 KB
/
Copy pathplugin.php
File metadata and controls
443 lines (375 loc) · 13.5 KB
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
<?php
define('BEAMMEUP_PLUGIN_VERSION', '0.1');
/** Plugin hooks */
add_plugin_hook('install', 'beam_install');
add_plugin_hook('uninstall', 'beam_uninstall');
add_plugin_hook('config_form', 'beam_config_form');
add_plugin_hook('config', 'beam_config');
add_plugin_hook('admin_append_to_items_form_files', 'beam_admin_append_to_items_form_files');
add_plugin_hook('after_save_item', 'beam_after_save_item');//the big one
add_plugin_hook('admin_append_to_items_show_secondary', 'beam_admin_append_to_items_show_secondary');
add_plugin_hook('admin_theme_header', 'beam_admin_theme_header');
/** Plugin filters */
add_filter('admin_items_form_tabs', 'beam_admin_item_form_tabs');
function beam_admin_theme_header() {}
/**
* Displays Internet Archive links in admin/show section
* @return void
**/
function beam_admin_append_to_items_show_secondary() {
echo '<div class="info-panel">';
echo '<h2>BeamMeUp</h2>';
echo listInternetArchiveLinks();
echo '</div>';
}
/**
* Gives user the option to post to the Internet Archive
* @return void
**/
function beam_admin_append_to_items_form_files() {
?>
<span><b>Upload to Internet Archive</b></span>
<input type="hidden" name="PostToInternetArchiveBool" value="0">
<input type="checkbox" name="PostToInternetArchiveBool" value="1" <?php if(get_option('post_to_internet_archive_default_bool') == '1') {echo 'checked';} ?>/>
<div><em>Note that if this box is checked, saving the item may take a while.</em></div>
<div><em>Files must be uniquely named to post to the archive.</em></div>
</br>
<span><b>Index at Internet Archive</b></span>
<input type="hidden" name="IndexAtInternetArchiveBool" value="0">
<input type="checkbox" name="IndexAtInternetArchiveBool" value="1" <?php if(get_option('index_at_internet_archive_default_bool') == '1') {echo 'checked';} ?>/>
<div><em>If you index your item, it will appear on the results of search engines such as Google's.</em></div>
</br>
</br>
<?php
}
/**
* Sets configuration options to default
* @return void
**/
function beam_install()
{
set_option('post_to_internet_archive_default_bool', '1');
set_option('index_at_internet_archive_default_bool', '1');
set_option('access_key', 'Enter S3 access key here.');
set_option('secret_key', 'Enter S3 secret key here.');
set_option('collection_name', 'Please contact the Internet Archive.');
set_option('media_type', 'Please contact the Internet Archive.');
$bucketPrefix = str_replace('.','_',preg_replace('/www/', '', $_SERVER["SERVER_NAME"],1));
$bucketPrefix = 'omeka'.((strpos($bucketPrefix,'_') == 0) ? '' : '_').$bucketPrefix;
set_option('bucket_prefix', $bucketPrefix);
$db = get_db();
$sql = "
CREATE TABLE IF NOT EXISTS `$db->InternetArchiveFile` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`item_id` BIGINT UNSIGNED NOT NULL,
`up_to_date` BOOLEAN NOT NULL,
`bucket_url` TEXT,
`bucket_history_url` TEXT,
INDEX (`item_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci";
$db->query($sql);
}
/**
* Deletes persistent variables
* @return void
**/
function beam_uninstall()
{
delete_option('post_to_internet_archive_default_bool');
delete_option('index_at_internet_archive_default_bool');
delete_option('collection_name');
delete_option('media_type');
delete_option('access_key');
delete_option('secret_key');
delete_option('bucket_prefix');
}
/**
* Displays configuration form
* @return void
**/
function beam_config_form()
{
include 'config_form.php';
}
/**
* Configures based on inputs in config_form.php
* @return void
**/
function beam_config()
{
set_option('post_to_internet_archive_default_bool', $_POST['PostToInternetArchiveDefaultBool']);
set_option('index_at_internet_archive_default_bool', $_POST['IndexAtInternetArchiveDefaultBool']);
set_option('collection_name', $_POST['CollectionName']);
set_option('media_type', $_POST['MediaType']);
set_option('access_key', $_POST['AWSAccessKeyId']);
set_option('secret_key', $_POST['SecretKey']);
}
/**
* Add BeamMeUp tab to the edit item page
* @return array
**/
function beam_admin_item_form_tabs($tabs)
{
// insert the map tab before the Miscellaneous tab
$item = get_current_item();
$ttabs = array();
foreach($tabs as $key => $html) {
if ($key == 'Miscellaneous') {
$ht = '';
$ht .= beam_form($item);
$ttabs['BeamMeUp'] = $ht;
}
$ttabs[$key] = $html;
}
$tabs = $ttabs;
return $tabs;
}
/**
* Post Files and metadata of an Omeka Item to the Internet Archive
* @return void
**/
function beam_after_save_item($item)
{
//runs single-thread and throws uncaught exception so echo and print_r statements are seen
$DEBUG = FALSE;
if($_POST["PostToInternetArchiveBool"] == '1') {
function getInitializedCurlObject($first,$title)
{
$cURL = curl_init();
curl_setopt($cURL, CURLOPT_HEADER, 1);
curl_setopt($cURL, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($cURL, CURLOPT_LOW_SPEED_LIMIT, 1);
curl_setopt($cURL, CURLOPT_LOW_SPEED_TIME, 180);
curl_setopt($cURL, CURLOPT_NOSIGNAL, 1);
curl_setopt($cURL, CURLOPT_PUT, 1);
curl_setopt($cURL, CURLOPT_RETURNTRANSFER, TRUE);
//note that curl_setopt does not seem to work with predefined arrays, which is a real deterent to good code
if ($first) {
curl_setopt($cURL, CURLOPT_HTTPHEADER,
array('x-amz-auto-make-bucket:1',
//TODO: which works?
'x-archive-metadata-collection:'.get_option('collection_name'),
'x-archive-meta-collection:'.get_option('collection_name'),
'x-archive-meta-mediatype:'.get_option('media_type'),
'x-archive-meta-title:'.$title,
'x-archive-meta-noindex:'.(($_POST["IndexAtInternetArchiveBool"] == '1') ? '0' : '1'),
'x-archive-meta-creator:'.preg_replace('/www/', '', $_SERVER["SERVER_NAME"],1),
'authorization: LOW '.get_option('access_key').':'.get_option('secret_key')));
} else {
curl_setopt($cURL, CURLOPT_HTTPHEADER, array(
//TODO: which works?
'x-archive-metadata-collection:'.get_option('collection_name'),
'x-archive-meta-collection:'.get_option('collection_name'),
'x-archive-meta-mediatype:'.get_option('media_type'),
'x-archive-meta-title:'.$title,
'x-archive-meta-noindex:'.(($_POST["IndexAtInternetArchiveBool"] == '1') ? '0' : '1'),
'x-archive-meta-creator:'.preg_replace('/www/', '', $_SERVER["SERVER_NAME"],1),
'authorization: LOW '.get_option('access_key').':'.get_option('secret_key')));
}
return $cURL;
}
/**
* @param $first true if this is the first PUT to the bucket, false otherwise
* @return A cURL object with parameters set to upload metadata
*/
function getMetadataCurlObject($first)
{
$cURL = getInitializedCurlObject($first,'Item Metadata');
$body = show_item_metadata($options = array('show_empty_elements' => TRUE), $item = $item);
echo $body;
/** use a max of 256KB of RAM before going to disk */
$fp = fopen('php://temp/maxmemory:256000', 'w');
if (!$fp) {
die('could not open temp memory data');
}
fwrite($fp, $body);
fseek($fp, 0);
curl_setopt($cURL, CURLOPT_URL, 'http://s3.us.archive.org/'.getBucketName().'/metadata.html');
curl_setopt($cURL, CURLOPT_BINARYTRANSFER, TRUE);
curl_setopt($cURL, CURLOPT_INFILE, $fp); // file pointer
curl_setopt($cURL, CURLOPT_INFILESIZE, strlen($body));
return $cURL;
}
/**
* @param $first true if this is the first PUT to the bucket, false otherwise
* @param $fileToBePut the Omeka file to by uploaded to the Internet Archive
* @return A cURL object with parameters set to upload an Omeka File
*/
function getFileCurlObject($first, File $fileToBePut)
{
$cURL = getInitializedCurlObject($first,item_file('original filename'));
// open this directory
set_current_file($fileToBePut);
echo "Julia's Lullaby";
echo preg_replace('/&#\d+;/','_',htmlspecialchars(preg_replace('/\s/','_',"Julia's Lullaby"),ENT_QUOTES));
echo item_file('original filename');
echo preg_replace('/&#\d+;/','_',htmlspecialchars(preg_replace('/\s/','_',item_file('original filename')),ENT_QUOTES));
//TODO Test with hyphen, apostraphe
curl_setopt($cURL, CURLOPT_URL, 'http://s3.us.archive.org/'.getBucketName().'/'.preg_replace('/&#\d+;/','_',htmlspecialchars(preg_replace('/\s/','_',item_file('original filename')),ENT_QUOTES)));
curl_setopt($cURL, CURLOPT_INFILE, fopen(FILES_DIR.'/'.item_file('archive filename'),'r'));
curl_setopt($cURL, CURLOPT_INFILESIZE, item_file('Size'));
return $cURL;
}
/**
* Adds handle for to cURL multi object
* @param $curlMultiHandle pointer to multi cURL multi handle that will be added to
* @param $cURL single cURL handle to add
* @return $curl the object for curl_multi_remove_handle
**/
function addHandle(&$curlMultiHandle,$cURL)
{
curl_multi_add_handle($curlMultiHandle,$cURL);
return $cURL;
}
/**
* Executes PUT method to upload Omeka metadata
* @param $successful pointer to success flag. Will be set to FALSE if HTTP code is not 200
* @param $cURL single cURL handle to execute
* @return void
**/
function execSingleHandle(&$successful,$cURL)
{
curl_exec($cURL);
if (curl_getinfo($cURL,CURLINFO_HTTP_CODE) != 200)
{
$successful = FALSE;
}
print_r(curl_getinfo($cURL));
}
/**
* Executes the cURL multi handle until there are no outstanding jobs
* @return void
**/
function execMultiHandle(&$curlMultiHandle)
{
$flag=null;
do {
//fetch pages in parallel
curl_multi_exec($curlMultiHandle,$flag);
} while ($flag > 0);
}
//set item
set_current_item($item);
if ($DEBUG)
{
$successful = TRUE;//innocent until proven guilty
execSingleHandle($successful, getMetadataCurlObject(TRUE));
//bucket must exist before subsequent requests are made
while(preg_replace('/\s/','', file_get_contents('http://archive.org/metadata/'.getBucketName())) == '{}')
{
usleep ( 1000 );
}
while(loop_files_for_item())
{
execSingleHandle($successful, getFileCurlObject(FALSE,get_current_file()));
}
echo 'Very important: ';
echo (($successful) ? 'success' : 'failure');
//throws uncaught error for debugging
file_download_uri($whatever);
}
else
{
// curl_exec(getMetadataCurlObject(TRUE));
//
// while(preg_replace('/\s/','', file_get_contents('http://archive.org/metadata/'.getBucketName())) == '{}')
// {
// usleep ( 1000 );
// }
//
// //now that bucket has been created, run multi-threaded cURL
// $curlMultiHandle = curl_multi_init();
//
// $i = 0;
// while(loop_files_for_item())
// {
// $curl[$i] = addHandle($curlMultiHandle,getFileCurlObject(TRUE,get_current_file()));
// $i++;
// }
//
// execMultiHandle($curlMultiHandle);
//
// for ($i = 0;$i < count($curl); $i++)//remove the handles
// {
// curl_multi_remove_handle($curlMultiHandle,$curl[$i]);
// }
//
// curl_multi_close($curlMultiHandle);
$jobDispatcher = Zend_Registry::get('job_dispatcher');
$jobDispatcher->setQueueName('uploads');
$jobDispatcher->send('Upload_Job', array());
}
}
}
/**
* Each time we save an item, post to the Internet Archive
* @return void
**/
function beam_form($item) {
$ht = '';
ob_start();
?>
<div>If the box at the bottom of the files tab is checked, the files in this item, along with their metadata, will upload to the Internet Archive upon save.</div>
</br>
<div>Note that BeamMeUp may make saving an item take a while, and that it may take additional time for the Internet Archive to post the files after you save.</div>
</br>
<div>To change the upload defaultor to alter the the upload's configurations, visit the plugin's configuration settings on this site.</div>
</br>
<?php
if (item('id') == '')
{
echo "
<div>Please revisit this tab after you save the item to view its Internet Archive links.</div>
";
}
else
{
echo listInternetArchiveLinks();
}
$ht .= ob_get_contents();
ob_end_clean();
return $ht;
}
/**
* @return bucket name for Omeka Item
*/
function getBucketName()
{
return get_option('bucket_prefix').'_'.item('id');
}
/**
* @return string containing IA links
*/
function listInternetArchiveLinks()
{
return "
<div>If you uploaded the files and the Internet Archive has fully processed them, you can view them <b><a href=http://archive.org/details/".getBucketName()." target=\"_blank\">here</a></b>.</div>
</br>
<div>You can view the upload's Internet Archive history and progress <b><a href=http://archive.org/catalog.php?history=1&identifier=".getBucketName()." target=\"_blank\">here</a></b>.</div>
</br>
";
}
class Upload_Job extends Omeka_JobAbstract
{
public function perform()
{
curl_exec(getMetadataCurlObject(TRUE));
while(preg_replace('/\s/','', file_get_contents('http://archive.org/metadata/'.getBucketName())) == '{}')
{
usleep ( 1000 );
}
//now that bucket has been created, run multi-threaded cURL
$curlMultiHandle = curl_multi_init();
$i = 0;
while(loop_files_for_item())
{
$curl[$i] = addHandle($curlMultiHandle,getFileCurlObject(TRUE,get_current_file()));
}
execMultiHandle($curlMultiHandle);
for ($i = 0;$i < count($curl); $i++)//remove the handles
{
curl_multi_remove_handle($curlMultiHandle,$curl[$i]);
}
curl_multi_close($curlMultiHandle);
}
}