forked from Islandora/Crayfish
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TransactionController.php
175 lines (163 loc) · 6.07 KB
/
TransactionController.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
namespace Islandora\Crayfish\TransactionService\Controller;
use Silex\Application;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Islandora\Crayfish\KeyCache\UuidCache;
class TransactionController
{
/**
* @var bool $transformsInstalled
* Are the UUID to F4 path transforms installed.
*/
protected static $transformsInstalled = false;
/**
* @var IUuidCache $keyCache
* The cache to store UUID -> F4 path mappings for transactions.
*/
protected $keyCache;
/**
* @var string $uuidTransformKey
* The key used to install the transform in Fedora.
*/
public static $uuidTransformKey = 'isl_uuid';
public function __construct(Application $app, UuidCache $keyCache)
{
$this->keyCache = $keyCache;
if (TransactionController::$transformsInstalled === false) {
TransactionController::installUuidTransform($app);
}
}
public function create(Application $app, Request $request)
{
try {
$response = $app['api']->createTransaction();
} catch (\Exception $e) {
$app->abort(503, 'Chullo says "Fedora4 Repository Not available"');
}
return $response;
}
public function extend(Application $app, Request $request, $id)
{
try {
$response = $app['api']->extendTransaction($id);
} catch (\Exception $e) {
$app->abort(503, 'Chullo says "Fedora4 Repository Not available"');
}
return $response;
}
public function commit(Application $app, Request $request, $id)
{
try {
$response = $app['api']->commitTransaction($id);
} catch (\Exception $e) {
$app->abort(503, 'Chullo says "Fedora4 Repository Not available"');
}
return $response;
}
public function rollback(Application $app, Request $request, $id)
{
try {
$response = $app['api']->rollbackTransaction($id);
} catch (\Exception $e) {
$app->abort(503, 'Chullo says "Fedora4 Repository Not available"');
}
return $response;
}
/**
* Parse a response to get the transaction ID.
*
* @param $response
* Either a Symfony or Guzzle/Psr7 response.
* @return string
* The transaction ID.
*/
public function getId($response)
{
if (get_class($response) == 'Symfony\Component\HttpFoundation\Response') {
if ($response->headers->has('location')) {
return $this->parseTransactionId($response->headers->get('location'));
}
}
if (get_class($response) == 'GuzzleHttp\Psr7\Response') {
if ($response->hasHeader('location')) {
return $this->parseTransactionId($response->getHeader('location'));
}
}
return null;
}
/**
* Utility function to get the transaction ID from the Header.
*
* @param array|string $header
* array of headers or the single string.
* @return string
* the transaction ID.
*/
private function parseTransactionId($header)
{
if (is_array($header)) {
$header = reset($header);
}
$ids = explode('tx:', $header);
return 'tx:' . $ids[1];
}
/**
* Install the UUID transforms into Fedora
*
* @var Application $app
* The Silex webapp object.
*/
protected static function installUuidTransform(Application $app)
{
$loadTransform = false;
if (TransactionController::$transformsInstalled === false) {
$response = $app['api']->getResourceHeaders(
"fedora:system/fedora:transform/fedora:ldpath/" . TransactionController::$uuidTransformKey
);
if ($response->getStatusCode() == 200) {
// This variable is reset at server restart, no need to re-upload the transform.
TransactionController::$transformsInstalled = true;
return true;
} else {
$response = $app['api']->getResourceHeaders("fedora:system/fedora:transform");
if ($response->getStatusCode() == 404) {
$path = $app['UuidGenerator']->generateV4();
$response = $app['api']->saveResource("{$path}");
if ($response->getStatusCode() == 201) {
$response = $app['api']->getResourceHeaders("/{$path}/fcr:transform/default");
if ($response->getStatusCode() == 200) {
$loadTransform = true;
}
# Delete the temporary resource we created
$response = $app['api']->deleteResource("/{$path}");
if ($response->getStatusCode() == 204 || $response->getStatusCode() == 410) {
$response = $app['api']->deleteResource("/{$path}/fcr:tombstone");
}
}
} else {
$loadTransform = true;
}
if ($loadTransform) {
$ldpath_content = file_get_contents(__DIR__ . '/../resources/islandora_uuid.txt');
if ($ldpath_content !== false) {
$url = '/fedora:system/fedora:transform/fedora:ldpath/' .
TransactionController::$uuidTransformKey . '/fedora:Resource';
$response = $app['api']->saveResource(
$url,
$ldpath_content,
array('Content-type' => 'application/rdf+ldpath')
);
if ($response->getStatusCode() == 201) {
TransactionController::$transformsInstalled = true;
return true;
}
}
}
error_log("Unable to load transaction transforms into Fedora.");
return false;
}
}
return true;
}
}