-
Notifications
You must be signed in to change notification settings - Fork 6
/
fedora_api.item.inc
206 lines (177 loc) · 6.16 KB
/
fedora_api.item.inc
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
<?php
/**
* An wrapper for Fedora's raw API calls to present Fedora items as manageable objects.
*/
require_once('PropertyObject.inc');
class FedoraItem extends PropertyObject {
public $pid = NULL; // The $pid of the fedora object represented by an instance of this class.
private $objectProfile = NULL;
private $_datastreams = NULL;
public $client = NULL;
private static $instantiated_pids = array();
/**
* Create an object to represent an item in the Fedora repository.
* Throws a SOAPException if the PID is not in the repository.
*
* @param string $pid
* @param FedoraConnection $connection
* @return FedoraItem
*/
function __construct($pid, $client = NULL) {
if ($client) {
$this->client = $client;
}
else {
// Make a default client using Drupal's variables.
$this->client = new FedoraClient();
}
$this->pid = $pid;
if (isset(FedoraItem::$instantiated_pids[$pid])) {
$this->client =& FedoraItem::$instantiated_pids[$pid]->client;
$this->objectProfile =& FedoraItem::$instantiated_pids[$pid]->objectProfile;
$this->_datastreams =& FedoraItem::$instantiated_pids[$pid]->_datastreams;
$datastreams_doc =& FedoraItem::$instantiated_pids[$pid]->datastreams_list;
}
else {
$this->_refreshObjectProfile();
FedoraItem::$instantiated_pids[$pid]=&$this;
}
}
public function delete() {
$this->state = 'D';
}
public function purge() {
$this->client->api->purgeObject($this->pid);
}
/**
* Add a new datastream. Wrapper for the addDatastream Fedora API-M method.
* @param type $dsID
* @param string $file_path
* @param string $ds_string
* @param type $params
*/
public function addDatastream($dsID, $file_path = NULL, $ds_string = NULL, $params = array('altIDs' => NULL, 'dsLabel' => NULL, 'dsState' => NULL, 'formatURI' => NULL,
'checksumType' => NULL, 'checksum' => NULL, 'mimeType' => NULL,
'logMessage' => NULL)) {
$response = $this->client->api->addDatastream($this->pid, $dsID, $file_path, $ds_string, $params);
if ($response->code != 201) {
throw new FedoraRestException($response->error);
}
$this->_refreshDatastreams();
return $this->datastreams[$dsID];
}
/**
* Returns an associative array of this object's datastreams. Results look like this:
*
* 'DC' =>
* array
* 'label' => string 'Dublin Core Record for this object' (length=34)
* 'MIMEType' => string 'text/xml' (length=8)
* 'RELS-EXT' =>
* array
* 'label' => string 'RDF Statements about this object' (length=32)
* 'MIMEType' => string 'application/rdf+xml' (length=19)
*
* @return array
*/
function get_datastreams() {
if ($this->_datastreams != NULL) {
return $this->_datastreams;
}
$result = $this->client->api->listDatastreams($this->pid);
if ($result->code != 200) {
throw new FedoraRestException($result->error);
}
$datastreams_doc = new SimpleXMLElement($result->data);
$ds_list = array();
if ($datastreams_doc != FALSE) {
foreach ($datastreams_doc->datastream as $ds) {
foreach($ds->attributes() as $field => $value) {
switch ($field) {
case 'dsid':
$dsid = (string) $value;
break;
case 'label':
$dslabel = (string) $value;
break;
case 'mimeType':
$dsmime = (string) $value;
break;
}
}
if (!empty($dsid) && !empty($dsmime) && $dslabel !== NULL) {
$ds_list[$dsid] = new FedoraDatastream($this, $dsid, $dslabel, $dsmime);
}
}
}
$this->_datastreams = $ds_list;
return $ds_list;
}
protected function get_label() {
return (string) $this->objectProfile->objLabel;
}
protected function set_label($label) {
$this->modifyObject($label);
}
protected function get_ownerId() {
return (string) $this->objectProfile->objOwnerId;
}
protected function set_ownerId($ownerId) {
$this->modifyObject($this->label, $ownerId);
}
protected function get_state() {
return (string) $this->objectProfile->objState;
}
protected function set_state($state) {
$this->modifyObject(NULL, NULL, $state);
}
protected function get_pid() {
return $this->pid;
}
protected function get_createDate() {
return new DateTime((string) $this->objectProfile->objCreateDate);
}
protected function get_lastModDate() {
return new DateTime((string) $this->objectProfile->objLastModDate);
}
protected function get_models() {
$models = array();
foreach ($this->objectProfile->objModels->model as $model) {
$models[] = (string) $model;
}
return $models;
}
private function _refreshDatastreams() {
$this->_datastreams = NULL;
}
private function _refreshObjectProfile() {
$response = $this->client->api->getObjectProfile($this->pid);
if ($response->code == 200) {
$this->objectProfile = new SimpleXMLElement($response->data);
$this->_datastreams = $this->get_datastreams();
} else {
$this->objectProfile = '';
$this->_datastreams = array();
}
}
/**
* Wrapper for Fedora's modifyObject REST call. Updates the local objectProfile after each call.
* @param type $label
* @param type $ownerId
* @param type $state One of 'A' for active, 'D' for deleted, or 'I' for inactive
* @param type $logMessage
*/
public function modifyObject($label = NULL, $ownerId = NULL, $state = NULL, $logMessage = NULL) {
$response = $this->client->api->modifyObject($this->pid, $label, $ownerId, $state, $logMessage);
if ($response->code == 200) {
$this->_refreshObjectProfile();
return TRUE;
}
else {
return FALSE;
}
}
static function createItem( ) {
$this->client->api->ingest();
}
}