-
Notifications
You must be signed in to change notification settings - Fork 1
/
deleteOldestDS.php
98 lines (77 loc) · 2.8 KB
/
deleteOldestDS.php
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
#!/usr/bin/env drush
#<?php
/**
* This script is intended to delete the oldest version of the given datastream
* for the given object
*
* Usage: pass the PID of the object and the DS label to the script
*
* Example: drush php-script deleteOldestDS.php PID datastreamlabel
* ie) "drush scr deleteOldestDS.php islandora:3 OBJ"
* will delete the OBJ.0 or oldest DS version from the islandora:3 object
*
* @author Paul Church
* @date July 2014
*/
/**
* Taken from stackoverflow:
* https://stackoverflow.com/questions/2510434/format-bytes-to-kilobytes-megabytes-gigabytes
*
* @param unknown $size
* @param number $precision
* @return string
*/
function formatBytes($size, $precision = 2)
{
$base = log($size) / log(1024);
$suffixes = array('', 'kB', 'MB', 'GB', 'TB');
return round(pow(1024, $base - floor($base)), $precision) . $suffixes[floor($base)];
}
// grab the first user supplied parameter as the PID
$PID = drush_shift();
if (! $PID) {
drush_print("***Error: please provide the PID as the first argument");
drush_print("Example: drush php-script printDSInfo.php RULA:193");
return;
}
// grab the second user supplied parameter as the DS name
$dslabel = drush_shift();
if (! $dslabel) {
drush_print("***Error: please provide the DS name as the second argument");
drush_print("Example: drush php-script printDSInfo.php RULA:193 OBJ.0");
return;
}
# include all Tuque php files
$tuquePath = libraries_get_path('tuque') . '/*.php';
foreach ( glob($tuquePath) as $filename) {
require_once($filename);
}
// repository connection parameters
$url = 'localhost:8080/fedora';
$username = 'fedoraAdmin';
$password = 'fedoraAdmin';
// set up connection and repository variables
$connection = new RepositoryConnection($url, $username, $password);
$api = new FedoraApi($connection);
$repository = new FedoraRepository($api, new SimpleCache());
$api_m = $repository->api->m;
$api_a = $repository->api->a;
$spaceFreed = 0;
$dshistory = $api_m->getDatastreamHistory($PID, $dslabel); //NB: ds's are returned in order from most to least recent
$oldestToNewestDS = array_reverse($dshistory);
drush_print("$dslabel datastream history before deletion");
print_r($oldestToNewestDS);
$oldestDS = $oldestToNewestDS[0];
$spaceFreed += $oldestDS['dsSize'];
// remove the oldest DS version
$api_m->purgeDatastream($PID, $dslabel, array(
'startDT' => $oldestToNewestDS[0]['dsCreateDate'],
'endDT' => $oldestToNewestDS[0]['dsCreateDate'],
'logMessage' => '',
));
$dshistory = $api_m->getDatastreamHistory($PID, $dslabel); //NB: ds's are returned in order from most to least recent
$oldestToNewestDS = array_reverse($dshistory);
drush_print("$dslabel datastream history after deletion");
print_r($oldestToNewestDS);
drush_print("\nAmount of space freed: " . formatBytes($spaceFreed, 3));
return;