This repository has been archived by the owner on Aug 18, 2020. It is now read-only.
forked from jonathangreen/islandora_fedora_api
/
FedoraRepository.inc
130 lines (118 loc) · 4.32 KB
/
FedoraRepository.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
<?php
/**
* @file
* This file contains the class FedoraRepository. It wraps the RAW Fedora REST functions to provide an
* interface to repository functions that don't operate on objects, including instantiating an object.
*/
/**
* Wrapper for Fedora Commons API Respository functions (functions that don't operate on existing objects).
*/
class FedoraRepository {
/**
* The FedoraConnection object for this FedoraRespository.
*
* @var FedoraConnection
*/
public $connection;
/**
* Constructs the FedoraRepository object. Creates a new FedoraAPI object, and optionally if
* one isn't supplied it creates a new FedoraConnection object.
*
* @param FedoraConnection $connection
* (optional) If this arguement isn't supplied a new FedoraConnection is created.
*/
function __construct($connection = NULL) {
if (empty($connection)) {
$connection = new FedoraConnection();
}
$this->connection = $connection;
$this->api = new FedoraAPIA($this->connection);
}
/**
* Ingest a new object, with either an FOXML file, an FOXML string or neither for a blank object.
*
* @param string $foxml_file
* (optional) Used if ingesting a FOXML file.
* @param string $foxml_string
* (optional) Used if ingesting FOXML from a string.
* @param array $options
* (optional) An array of options to be passed to the ingest functions. They array keys for this array are:
* pid, new, label, format, encoding, namespace, ownerId, logmessage.
*
* @return FedoraItem the newly-created item.
*
* @throws FedoraAPIRestException
*/
function ingest(
$foxml_file = NULL, $foxml_string = NULL,
$options = array(
'pid' => NULL,
'new' => NULL,
'label' => NULL,
'format' => NULL,
'encoding' => NULL,
'namespace' => NULL,
'ownerId' => NULL,
'logMessage' => NULL,
)
){
$response = $this->api->ingest(
$foxml_file, $foxml_string,
!empty($options['pid']) ? $options['pid'] : NULL,
!empty($options['new']) ? $options['new'] : NULL,
!empty($options['label']) ? $options['label'] : NULL,
!empty($options['format']) ? $options['format'] : NULL,
!empty($options['encoding']) ? $options['encoding'] : NULL,
!empty($options['namespace']) ? $options['namespace'] : NULL,
!empty($options['ownerId']) ? $options['ownerId'] : NULL,
!empty($options['logMessage']) ? $options['logMessage'] : NULL
);
if ($response->code != 201) {
throw new FedoraAPIRestException($response);
}
return new FedoraItem($response->data);
}
/**
* Wrapper for Fedora's findObjects API function.
* @param string $terms Search terms, e.g. "changeme:*"
* @param string $query Detailed search query, e.g. "pid=demo:12"
* @param int $max_results
* @param array $display_fields
* @return array The result set with the specified fields populated
*/
function findObjects($terms = NULL, $query = NULL, $max_results = 50, $display_fields = array('pid', 'label', 'state', 'ownerId', 'cDate', 'mDate', 'dcmDate',
'title', 'creator', 'subject', 'description', 'publisher', 'contributor', 'date', 'type',
'format', 'identifier', 'source', 'language', 'relation', 'coverage', 'rights')) {
// pid needs to be part of the displayFields so we can construct the results array.
if (!in_array('pid', $display_fields)) {
$display_fields[] = 'pid';
}
$resp = $this->api->findObjects($terms, $query, $max_results, 'xml', $display_fields);
if ($resp->code != 200) {
throw new FedoraRestException($resp->error);
}
$results = new SimpleXMLElement($resp->data);
$resultset = array();
foreach ($results->resultList->objectFields as $obj) {
$pid = (string) $obj->pid;
$resultset[$pid] = array();
foreach ($obj->children() as $field) {
$field_name = $field->getName();
if ($field_name != 'pid') {
$resultset[$pid][$field_name] = (string) $field;
}
}
}
return $resultset;
}
/**
* Simple wrapper for findObjects that returns TRUE if an object with the
* specified pid exists in the repository.
* @param string $pid
* @return boolean
*/
public function objectExists($pid) {
$result = $this->findObjects(NULL, "pid=$pid");
return count($result) == 1;
}
}