forked from rwincewicz/php_listeners
-
Notifications
You must be signed in to change notification settings - Fork 8
/
FedoraConnect.php
159 lines (134 loc) · 4.11 KB
/
FedoraConnect.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
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
<?php
/**
* Class to make an initial connection to Fedora
*
* @author Richard Wincewicz
*/
require_once 'tuque/Datastream.php';
require_once 'tuque/FedoraApi.php';
require_once 'tuque/FedoraApiSerializer.php';
require_once 'tuque/Object.php';
require_once 'tuque/RepositoryConnection.php';
require_once 'tuque/Cache.php';
require_once 'tuque/RepositoryException.php';
require_once 'tuque/Repository.php';
require_once 'tuque/FedoraRelationships.php';
require_once 'FedoraConnectStaticMethods.php';
class FedoraConnection {
/**
* Connection to the repository
*
* @var RepositoryConnection
*/
public $connection = NULL;
/**
* The Fedora API we are using
*
* @var FedoraAPI
*/
public $api = NULL;
/**
* The cache we use to connect.
*
* @var SimpleCache
*/
public $cache = NULL;
/**
* The repository object.
*
* @var FedoraRepository
*/
public $repository = NULL;
function __construct($user = NULL, $url = NULL) {
$user_string = $user->name;
$pass_string = $user->pass;
if (!isset($url)) {
$url = 'http://localhost:8080/fedora';
}
//if (self::exists()) {
try {
$this->connection = new RepositoryConnection($url, $user_string, $pass_string);
$this->connection->reuseConnection = TRUE;
$this->api = new FedoraApi($this->connection);
$this->cache = new SimpleCache();
$this->repository = new FedoraRepository($this->api, $this->cache);
} catch (Exception $e) {
echo "Could not connect: $e";
file_put_contents('php://stderr', "Could not connect to Fedora - $e");
}
//}
}
function getDatastream($pid = NULL, $dsid = NULL) {
if (!isset($pid) || !isset($dsid))
return;
$fedora_object = new FedoraObject($pid, $this->repository);
$datastream_array = array();
foreach ($fedora_object as $datastream)
$datastream_array[] = $datastream->id;
if (!in_array($dsid, $datastream_array))
print "Could not find the $dsid datastream!";
try {
$datastream = $fedora_object->getDatastream($dsid);
} catch (Exception $e) {
print "Could not retrieve datastream - $e";
}
return $datastream;
}
function saveDatastream($datastream, $extension = NULL) {
if (!$datastream)
return;
try {
$mime_type = $datastream->mimetype;
if (!$extension) {
$extension = system_mime_type_extension($mime_type);
}
$tempfile = temp_filename($extension);
$file_handle = fopen($tempfile, 'w');
fwrite($file_handle, $datastream->content);
fclose($file_handle);
} catch (Exception $e) {
print "Could not save datastream - $e";
}
return $tempfile;
}
function addDerivative($pid, $dsid, $label, $content, $mimetype, $log_message = NULL, $delete = TRUE, $from_file = TRUE, $stream_type = "M") {
$return = "success";
//we are don't seem to be sending custom log message for updates only ingests
$fedora_object = $this->repository->getObject($pid);
if (isset($fedora_object[$dsid])) {
if ($from_file) {
$fedora_object[$dsid]->content = file_get_contents($content);
}
else {
$fedora_object[$dsid]->content = $content;
}
}
else {
$datastream = new NewFedoraDatastream($dsid, $stream_type, $fedora_object, $fedora_object->repository);
if ($from_file) {
$datastream->setContentFromFile($content);
}
else {
$datastream->setContentFromString($content);
}
$datastream->label = $label;
$datastream->mimetype = $mimetype;
$datastream->state = 'A';
$datastream->checksum = TRUE;
$datastream->checksumType = 'MD5';
if ($log_message) {
$datastream->logMessage = $log_message;
}
$return = $fedora_object->ingestDatastream($datastream);
}
if ($delete && $from_file) {
unlink($content);
}
//$this->log->lwrite('Finished processing', 'COMPLETE_DATASTREAM', $this->pid, $dsid);
return $return;
}
static function exists() {
return class_exists('RepositoryConnection');
}
}
?>