digitalResource.php 25.6 KB
Newer Older
Prosper De Laure's avatar
Prosper De Laure committed
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
<?php

/*
 * Copyright (C) 2015 Maarch
 *
 * This file is part of bundle digitalResource.
 *
 * Bundle digitalResource is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Bundle digitalResource is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with bundle digitalResource.  If not, see <http://www.gnu.org/licenses/>.
 */

namespace bundle\digitalResource\Controller;

/**
 * Main digital resource controller
 */
class digitalResource
{

    protected $finfo;
    protected $sdoFactory;
    public $hashAlgorithm;

    /**
     * Cluster controller
     * @var digitalResource/Controller/format
     */
    protected $formatController;

    /**
     * Cluster controller
     * @var digitalResource/cluster
     */
    protected $clusterController;

    /**
     * Previously loaded digital resource clusters
     * @var array
     */
    protected $clusters;

    /**
     * Currently used ddigital resource cluster
     * @var digitalResource/cluster
     */
    protected $currentCluster;

    /**
     * Constructor
     * @param \dependency\sdo\Factory $sdoFactory    The dependency sdo factory
     * @param string                  $hashAlgorithm The hash algorithm as in php::hash_algos()
     */
    public function __construct(\dependency\sdo\Factory $sdoFactory, $hashAlgorithm = "sha256")
    {
        $this->hashAlgorithm = $hashAlgorithm;

        $this->finfo = new \finfo(FILEINFO_MIME_TYPE);

        $this->sdoFactory = $sdoFactory;

        $this->clusterController = \laabs::newController('digitalResource/cluster');

        $this->formatController = \laabs::newController('digitalResource/format');
74
75

        $this->clusters = [];
Prosper De Laure's avatar
Prosper De Laure committed
76
77
78
79
80
81
    }

    /**
     * Get file information about one file
     * @param string $filename The file name
     *
Dylan's avatar
Dylan committed
82
     * @return digitalResource/digitalResource The digitalResource
Prosper De Laure's avatar
Prosper De Laure committed
83
     */
Alexis Ragot's avatar
Alexis Ragot committed
84
    public function createFromFile($filename, $withDateTime = true)
Prosper De Laure's avatar
Prosper De Laure committed
85
86
87
88
89
90
91
92
93
    {
        if (!file_exists($filename)) {
            return false;
        }

        $contents = file_get_contents($filename);

        $resource = $this->newResource();

94
95
96
97
        $UTF8filename = $filename;
        if (!preg_match('//u', $filename)) {
            $UTF8filename = utf8_encode($filename);
        }
Alexandre Morin's avatar
Alexandre Morin committed
98

Prosper De Laure's avatar
Prosper De Laure committed
99
        // Basic path information
100
        $pathinfo = pathinfo($UTF8filename);
Alexis Ragot's avatar
Alexis Ragot committed
101
102
103
104

        $resource->fileName = $pathinfo['filename'];

        if ($withDateTime) {
105
            $resource->fileName .= "_".(string) \laabs::newDate(\laabs::newDatetime(null, "UTC"), "Y-m-d_H-i-s");
Alexis Ragot's avatar
Alexis Ragot committed
106
107
        }

Prosper De Laure's avatar
Prosper De Laure committed
108
        if (isset($pathinfo['extension'])) {
109
            $resource->fileName .= "." . $pathinfo['extension'];
Prosper De Laure's avatar
Prosper De Laure committed
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
            $resource->fileExtension = $pathinfo['extension'];
        }

        $resource->size = filesize($filename);
        $resource->mimetype = $this->finfo->file($filename, \FILEINFO_MIME_TYPE);

        $resource->setContents($contents);

        return $resource;
    }

    /**
     * Get information about resource contents
     * @param string $contents The contents to store
     * @param string $filename The original filename
     *
Dylan's avatar
Dylan committed
126
     * @return digitalResource/digitalResource The digitalResource
Prosper De Laure's avatar
Prosper De Laure committed
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
     */
    public function createFromContents($contents, $filename = false)
    {
        $resource = $this->newResource();

        // Basic path information
        $resource->size = strlen($contents);
        $resource->mimetype = $this->finfo->buffer($contents, \FILEINFO_MIME_TYPE);

        if ($filename) {
            $pathinfo = pathinfo($filename);
            $resource->fileName = $pathinfo['basename'];
            if (isset($pathinfo['extension'])) {
                $resource->fileExtension = $pathinfo['extension'];
            }
        }

        $resource->setContents($contents);

        return $resource;
    }

    /**
     * Get information about resource stream contents
     * @param string $stream The data stream to store
     *
Dylan's avatar
Dylan committed
153
     * @return digitalResource/digitalResource The digitalResource
Prosper De Laure's avatar
Prosper De Laure committed
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
     */
    public function createFromStream($stream)
    {
        $contents = stream_get_contents($stream);

        return $this->createFromContents($contents);
    }

    protected function newResource()
    {
        $resource = \laabs::newInstance("digitalResource/digitalResource");

        $resource->resId = \laabs::newId();

        $resource->created = \laabs::newTimestamp();

        return $resource;
    }


    /**
     * Get the hash for a given resource
     * @param digitalResource/digitalResource $resource      The resource
     * @param string                          $hashAlgorithm The hash algorithm to use
     */
    public function getHash($resource, $hashAlgorithm = false)
    {
        if (!$hashAlgorithm) {
            $hashAlgorithm = $this->hashAlgorithm;
        }

        $resource->hashAlgorithm = $hashAlgorithm;
        $resource->hash = hash($hashAlgorithm, $resource->getContents());
    }

    /**
     * Use a digital resource cluster for storage
     * @param string $clusterId
     * @param string $mode
     * @param bool   $limit
     *
Dylan's avatar
Dylan committed
195
     * @return digitalResource/cluster The cluster
Prosper De Laure's avatar
Prosper De Laure committed
196
197
198
     */
    public function useCluster($clusterId, $mode, $limit)
    {
199
        if (!isset($this->clusters[$mode][(string) $clusterId])) {
Prosper De Laure's avatar
Prosper De Laure committed
200
            $this->currentCluster = $this->clusterController->openCluster($clusterId, $mode, $limit);
201
            $this->clusters[$mode][(string) $clusterId] = $this->currentCluster;
Prosper De Laure's avatar
Prosper De Laure committed
202
        } else {
203
            $this->currentCluster = $this->clusters[$mode][(string) $clusterId];
Prosper De Laure's avatar
Prosper De Laure committed
204
205
206
207
208
        }

        return $this->currentCluster;
    }

209
210
211
212
213
    /**
     * Create a ressource container on the cluster (after opening it)
     * @param string $clusterId
     * @param string $path
     * @param mixed  $metadata
Alexandre Morin's avatar
Alexandre Morin committed
214
     *
Dylan's avatar
Dylan committed
215
     * @return string Ressource container on the cluster
216
217
218
     */
    public function openContainers($clusterId, $path, $metadata=null)
    {
219
        $cluster = $this->useCluster($clusterId, Cluster::MODE_WRITE, true);
220

221
        return $this->clusterController->openContainers($cluster, $path, $metadata);
222
223
224
    }


Prosper De Laure's avatar
Prosper De Laure committed
225
226
    /**
     * Store a given resource
227
     * @param digitalResource/digitalResource $resource The ressource object
Prosper De Laure's avatar
Prosper De Laure committed
228
     *
Dylan's avatar
Dylan committed
229
     * @return digitalResource/digitalResource digitalResource object
Prosper De Laure's avatar
Prosper De Laure committed
230
     */
231
    public function store($resource)
Prosper De Laure's avatar
Prosper De Laure committed
232
233
    {
        // Store resource + metadata
234
        $this->storeDigitalResource($resource);
Prosper De Laure's avatar
Prosper De Laure committed
235
236
237
238
239
240

        return $resource;
    }

    /**
     * Store a collection of resources sharing the same cluster and repositories from a list of resources
241
242
     * @param array  $resources An array of resources to store
     * @param string $clusterId The cluster to apply for store procedure
Prosper De Laure's avatar
Prosper De Laure committed
243
     *
Dylan's avatar
Dylan committed
244
     * @return mixed Collection of resources
Prosper De Laure's avatar
Prosper De Laure committed
245
     */
246
    public function storeCollection($resources, $clusterId)
Prosper De Laure's avatar
Prosper De Laure committed
247
248
    {
        // Get the storage objects
249
        $this->useCluster($clusterId, Cluster::MODE_WRITE, true);
Prosper De Laure's avatar
Prosper De Laure committed
250
251

        foreach ($resources as $resource) {
252
            $this->storeDigitalResource($resource);
Prosper De Laure's avatar
Prosper De Laure committed
253
254
255
256
257
258
259
260
            $resources[] = $resource;
        }

        return $resources;
    }

    /**
     * Store a new resource in current cluster
261
     * @param object $resource The resource
Prosper De Laure's avatar
Prosper De Laure committed
262
     */
263
    public function storeDigitalResource($resource)
Prosper De Laure's avatar
Prosper De Laure committed
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
    {
        $contents = $resource->getContents();
        if (empty($resource->size)) {
            $resource->size = strlen($contents);
        } else {
            if ($resource->size != strlen($contents)) {
            }
        }

        if (empty($resource->mimetype)) {
            $resource->mimetype = $this->finfo->buffer($contents, \FILEINFO_MIME_TYPE);
        }

        $transactionControl = !$this->sdoFactory->inTransaction();

        if ($transactionControl) {
            $this->sdoFactory->beginTransaction();
        }

        try {
            $resource->clusterId = $this->currentCluster->clusterId;
            $resource->created = \laabs::newTimestamp();
Alexandre Morin's avatar
Alexandre Morin committed
286
287
288
289

            if (!isset($resource->hash)) {
                $this->getHash($resource);
            }
Prosper De Laure's avatar
Prosper De Laure committed
290

291
292
            $this->sdoFactory->create($resource, 'digitalResource/digitalResource');

293
            $this->clusterController->storeResource($this->currentCluster, $resource);
Prosper De Laure's avatar
Prosper De Laure committed
294
295
296
297

            if ($resource->relatedResource) {
                foreach ($resource->relatedResource as $relatedResource) {
                    $relatedResource->relatedResId = $resource->resId;
298
                    $relatedResource->archiveId = $resource->archiveId;
299
                    $this->storeDigitalResource($relatedResource);
Prosper De Laure's avatar
Prosper De Laure committed
300
301
302
303
304
305
306
307
308
                }
            }

        } catch (\Exception $exception) {
            $this->clusterController->rollbackStorage($resource);

            if ($transactionControl) {
                $this->sdoFactory->rollback();
            }
309

Prosper De Laure's avatar
Prosper De Laure committed
310
            throw \laabs::newException("digitalResource/clusterException", 'Resource %1$s not created: %2$s', 404, $exception, [$resource->resId, $exception->getMessage()]);
Prosper De Laure's avatar
Prosper De Laure committed
311
312
313
314
315
316
317
318
319
        }

        // All repositories returned an uri, save all
        if ($transactionControl) {
            $this->sdoFactory->commit();
        }
    }

    /**
320
321
     * Get resources of archive
     * @param id $archiveId The archive identifier
Prosper De Laure's avatar
Prosper De Laure committed
322
     *
323
     * @return array Array of digitalResource object
Prosper De Laure's avatar
Prosper De Laure committed
324
     */
325
    public function getResourcesByArchiveId($archiveId)
Prosper De Laure's avatar
Prosper De Laure committed
326
    {
327
        $resources = $this->sdoFactory->find("digitalResource/digitalResource", "archiveId='$archiveId' AND relatedResId=null");
Prosper De Laure's avatar
Prosper De Laure committed
328

329
330
331
332
        foreach ($resources as $key => $resource) {
            $resources[$key] = $this->info($resource->resId);
        }

333
        return $resources;
Prosper De Laure's avatar
Prosper De Laure committed
334
335
336
337
338
339
340
    }

    /**
     * Get related resources
     * @param string $resId            The identifier of the converted or the original resource
     * @param string $relationshipType The relationship type with the resource identified by the second parameter
     *
Dylan's avatar
Dylan committed
341
     * @return digitalResource/digialResource[] Array of digitalResource/digialResource object
Prosper De Laure's avatar
Prosper De Laure committed
342
     */
343
    public function getRelatedResources($resId, $relationshipType = null)
Prosper De Laure's avatar
Prosper De Laure committed
344
345
    {
        // $relationshipType = isConversionOf OR isSignatureOf
346
        $whereClause = "relatedResId='$resId'";
Prosper De Laure's avatar
Prosper De Laure committed
347
348
349
350
351
352
353
354
355
356
357
358
359
360

        if ($relationshipType) {
            $whereClause .= " AND relationshipType='$relationshipType'";
        }

        $resources = $this->sdoFactory->find("digitalResource/digitalResource", $whereClause);

        return $resources;
    }

    /**
     * Retrieve a resource
     * @param string $resId The resource identifier
     *
Dylan's avatar
Dylan committed
361
     * @return digitalResource/digitalResource digitalResource object
Prosper De Laure's avatar
Prosper De Laure committed
362
363
364
365
366
367
368
369
370
371
372
373
374
     */
    public function retrieve($resId)
    {
        $resource = $this->sdoFactory->read("digitalResource/digitalResource", $resId);

        if (!$resource) {
            throw \laabs::newException("digitalResource/resourceNotFoundException");
        }

        if (isset($resource->puid)) {
            $resource->format = $this->formatController->get($resource->puid);
        }

375
        $cluster = $this->useCluster($resource->clusterId, Cluster::MODE_READ, false);
Prosper De Laure's avatar
Prosper De Laure committed
376
377
378
379
380
381
382
383
384
385
386
387
388
        $resource->cluster = $cluster;
        $contents = $this->clusterController->retrieveResource($cluster, $resource);

        if (!$contents) {
            $packedResources = $this->sdoFactory->find('digitalResource/packedResource', "resId='".$resource->resId."'");
            if (count($packedResources) > 0) {
                $packageController = \laabs::newController('digitalResource/package');
                foreach ($packedResources as $packedResource) {
                    try {
                        $package = $this->sdoFactory->read('digitalResource/package', $packedResource->packageId);
                        $package->resource = $this->retrieve($packedResource->packageId);
                        $contents = $packageController->getPackedContents($package, $packedResource->name);
                        // Check hash
Alexandre Morin's avatar
Alexandre Morin committed
389
                        $this->checkHash($contents, $resource->hash, $resource->hashAlgorithm);
Prosper De Laure's avatar
Prosper De Laure committed
390
391
392

                        $resource->setContents($contents);
                    } catch (\Exception $exception) {
Prosper De Laure's avatar
Prosper De Laure committed
393
                        throw \laabs::newException("digitalResource/clusterException", "Resource %s could not be retrieved.", 404, null, [$resource->resId]);
Prosper De Laure's avatar
Prosper De Laure committed
394
395
396
397
398
399
                    }
                }
            }
        }

        if (!$contents) {
Prosper De Laure's avatar
Prosper De Laure committed
400
            throw \laabs::newException("digitalResource/clusterException", "Resource %s could not be retrieved.", 404, null, [$resource->resId]);
Prosper De Laure's avatar
Prosper De Laure committed
401
402
        }

403
        $relatedResources = $this->getRelatedResources($resource->resId);
Prosper De Laure's avatar
Prosper De Laure committed
404
405
406
407
408
409
410
        foreach ($relatedResources as $relatedResource) {
            $resource->relatedResource[] = $this->retrieve($relatedResource->resId);
        }

        return $resource;
    }

Alexandre Morin's avatar
Alexandre Morin committed
411
412
413
414
415
416
417
418
419
    public function checkHash($contents, $hash, $hashAlgorithm)
    {
        $hash_calculated = strtolower(hash($hashAlgorithm, $contents));

        if ($hash_calculated !== strtolower($hash)) {
            throw \laabs::newException("digitalResource/invalidHashException", "Invalid hash.");
        }
    }

Prosper De Laure's avatar
Prosper De Laure committed
420
421
422
423
    /**
     * Get the contents of a given resource
     * @param string $resId
     *
Dylan's avatar
Dylan committed
424
     * @return string The contents of a given resource
Prosper De Laure's avatar
Prosper De Laure committed
425
426
427
428
429
430
431
432
     */
    public function contents($resId)
    {
        $resource = $this->sdoFactory->read("digitalResource/digitalResource", $resId);
        if (!$resource) {
            throw \laabs::newException("digitalResource/resourceNotFoundException");
        }

433
        $cluster = $this->useCluster($resource->clusterId, Cluster::MODE_READ, false);
Prosper De Laure's avatar
Prosper De Laure committed
434
435
436
437
438
439
440
441
442
443

        foreach ($cluster->clusterRepository as $clusterRepository) {
            $repositoryService = $clusterRepository->repository->getService();

            $address = $this->sdoFactory->read("digitalResource/address", array('resId' => $resId, 'repositoryId' => $clusterRepository->repositoryId));

            if ($address) {
                $contents = null;
                if (!$contents) {
                    try {
444
                        $contents = $repositoryService->readObject($address->path);
Alexandre Morin's avatar
Alexandre Morin committed
445
446

                        $this->checkHash($contents, $resource->hash, $resource->hashAlgorithm);
Prosper De Laure's avatar
Prosper De Laure committed
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
                    } catch (\Exception $e) {
                        // TODO : throw exception if resource not available on repo, based on options ?
                    }
                }
            } else {
                // TODO : throw exception if resource not available on repo, based on options ?
            }
        }

        if (!$contents) {
            throw \laabs::newException("digitalResource/clusterException", "Resource unavailable");
        }

        return $contents;
    }

    /**
     * Retrieve a resource metadata
     * @param string $resId
     *
Dylan's avatar
Dylan committed
467
     * @return mixed Resource metadata
Prosper De Laure's avatar
Prosper De Laure committed
468
469
470
471
472
473
474
475
     */
    public function metadata($resId)
    {
        $resource = $this->sdoFactory->read("digitalResource/digitalResource", $resId);
        if (!$resource) {
            throw \laabs::newException("digitalResource/clusterException", "Resource not found");
        }

476
        $cluster = $this->useCluster($resource->clusterId, Cluster::MODE_READ, false);
Prosper De Laure's avatar
Prosper De Laure committed
477
478
479
480
481
482
483
484
485
486
487

        if (!$cluster->storeMetadata) {
            return;
        }

        foreach ($cluster->clusterRepository as $clusterRepository) {
            $repositoryService = $clusterRepository->repository->getService();

            $address = $this->sdoFactory->read("digitalResource/address", array('resId' => $resId, 'repositoryId' => $clusterRepository->repositoryId));

            if ($address) {
488
                if ($contents = $repositoryService->readObject($address->path, 2)) {
Prosper De Laure's avatar
Prosper De Laure committed
489
490
491
492
493
494
495
496
497
498
                    return $contents;
                }
            }
        }
    }

    /**
     * Get all information about one digital resource
     * @param string $resId The identifier of digital resource
     *
Dylan's avatar
Dylan committed
499
     * @return digitalResource/digitalResource digitalResource object whith all information
Prosper De Laure's avatar
Prosper De Laure committed
500
501
502
503
504
505
506
507
508
509
510
511
512
     */
    public function info($resId)
    {
        $resource = $this->sdoFactory->read("digitalResource/digitalResource", $resId);

        if (!$resource) {
            throw \laabs::newException("digitalResource/clusterException", "Resource not found");
        }

        if (isset($resource->puid)) {
            $resource->format = $this->formatController->get($resource->puid);
        }

513
        $cluster = $this->useCluster($resource->clusterId, Cluster::MODE_READ, false);
Prosper De Laure's avatar
Prosper De Laure committed
514
515
516
517
518
519
520
521
522
523

        foreach ($cluster->clusterRepository as $clusterRepository) {
            $repositoryId = $clusterRepository->repositoryId;
            $repository = $this->sdoFactory->read("digitalResource/repository", $repositoryId);
            $address = $this->sdoFactory->read("digitalResource/address", array('resId' => $resId, 'repositoryId' => $repositoryId));
            $address->path = str_replace('\\', '/', $address->path);
            $address->repository = $repository;
            $resource->address[] = $address;
        }

524
        $relatedResources = $this->getRelatedResources($resource->resId);
Prosper De Laure's avatar
Prosper De Laure committed
525
526
527
528
529
530
531
532
533
534
535
        foreach ($relatedResources as $relatedResource) {
            $resource->relatedResource[] = $this->info($relatedResource->resId);
        }

        return $resource;
    }

    /**
     * Delete entire resource and all addresses
     * @param string $resId The resource identifier
     *
Dylan's avatar
Dylan committed
536
     * @return bool The result of the operation
Prosper De Laure's avatar
Prosper De Laure committed
537
538
539
540
541
542
543
544
     */
    public function delete($resId)
    {
        $resource = $this->sdoFactory->read("digitalResource/digitalResource", $resId);
        if (!$resource) {
            throw \laabs::newException("digitalResource/clusterException", "Resource not found");
        }

545
        $relatedResources = $this->getRelatedResources($resource->resId);
Prosper De Laure's avatar
Prosper De Laure committed
546
547
548
549
        foreach ($relatedResources as $relatedResource) {
            $resource->relatedResource[] = $this->delete($relatedResource->resId);
        }

550
        $cluster = $this->useCluster($resource->clusterId, Cluster::MODE_DELETE, false);
Prosper De Laure's avatar
Prosper De Laure committed
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570

        foreach ($cluster->clusterRepository as $clusterRepository) {
            $repositoryId = $clusterRepository->repositoryId;
            $address = $this->sdoFactory->read("digitalResource/address", array('resId' => $resId, 'repositoryId' => $repositoryId));
            $address->repository = $clusterRepository->repository;
            $resource->address[] = $address;
        }

        $transactionControl = !$this->sdoFactory->inTransaction();

        if ($transactionControl) {
            $this->sdoFactory->beginTransaction();
        }

        try {
            foreach ($resource->address as $address) {
                $this->sdoFactory->read("digitalResource/repository", $address->repositoryId);

                $repositoryService = $address->repository->getService();

571
                $repositoryService->deleteObject($address->path);
Prosper De Laure's avatar
Prosper De Laure committed
572
573
574
575
576
577
578
579
580

                $this->sdoFactory->delete($address);
            }

            $this->sdoFactory->delete($resource);
        } catch (\Exception $exception) {
            if ($transactionControl) {
                $this->sdoFactory->rollback();
            }
Prosper De Laure's avatar
Prosper De Laure committed
581
            throw \laabs::newException("digitalResource/clusterException", 'Resource not deleted at address %1$s : %2$s',  404, $exception, [$address->path, $exception->getMessage()]);
Prosper De Laure's avatar
Prosper De Laure committed
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
        }
        if ($transactionControl) {
            $this->sdoFactory->commit();
        }

        return true;
    }

    /**
     * Delete resource contents only
     * @param object $resource
     */
    public function rollbackStorage($resource)
    {
        $this->clusterController->rollbackStorage($resource);
    }

    /**
     * Verify integrity of resource
     * @param digitalResource/digitalResource $resource The digital resource
602
     * @param string                          $mode     The mode
Prosper De Laure's avatar
Prosper De Laure committed
603
     *
604
     * @return digitalResource/digitalResource The digitalResource object verify
Prosper De Laure's avatar
Prosper De Laure committed
605
606
607
     *
     * @throws digitalResource/resourceNotFoundException
     */
608
    public function verifyResource($resource, $mode = Cluster::MODE_READ)
Prosper De Laure's avatar
Prosper De Laure committed
609
610
611
612
613
    {
        if (!$resource) {
            throw \laabs::newException("digitalResource/resourceNotFoundException");
        }

614
        $cluster = $this->useCluster($resource->clusterId, $mode, false);
Prosper De Laure's avatar
Prosper De Laure committed
615
616
617
618
619
620
621
622

        return $this->clusterController->verifyResource($cluster, $resource);
    }

    /**
     * Convert resource to another format
     * @param object $digitalResource
     *
Dylan's avatar
Dylan committed
623
     * @return object The resource converted
Prosper De Laure's avatar
Prosper De Laure committed
624
     */
Alexis Ragot's avatar
Alexis Ragot committed
625
    public function convert($digitalResource)
Prosper De Laure's avatar
Prosper De Laure committed
626
    {
627
        $convert = $this->isConvertible($digitalResource);
Alexis Ragot's avatar
Alexis Ragot committed
628

Alexandre Morin's avatar
Alexandre Morin committed
629
        if ($convert == false) {
Prosper De Laure's avatar
Prosper De Laure committed
630
631
632
            return false;
        }

633
634
        $configuration =  \laabs::configuration('dependency.fileSystem');
        $conversionRule = $this->sdoFactory->read("digitalResource/conversionRule", array('puid' => $digitalResource->puid));
Prosper De Laure's avatar
Prosper De Laure committed
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
        $conversionServices = $configuration['conversionServices'];

        $outputFormats = null;
        $convertService = null;
        foreach ($conversionServices as $service) {
            if ($service["serviceName"] == $conversionRule->conversionService) {
                $outputFormats = $service["outputFormats"];
                $convertService = $service;
            }
        }


        $contents = $digitalResource->getContents();

        $tempdir = str_replace("/", DIRECTORY_SEPARATOR, \laabs\tempdir());

        if (isset($digitalResource->fileName)) {
            $srcfile = $tempdir.DIRECTORY_SEPARATOR.$digitalResource->fileName;
        } else {
            $srcfile = $tempdir.DIRECTORY_SEPARATOR.$digitalResource->resId;
        }

        file_put_contents($srcfile, $contents);

659
        $conversionRule = $this->sdoFactory->read("digitalResource/conversionRule", array('puid' => $digitalResource->puid));
Prosper De Laure's avatar
Prosper De Laure committed
660

661
        $converter = \laabs::newService($conversionRule->conversionService);
Prosper De Laure's avatar
Prosper De Laure committed
662
663
664
665
666
667
668
669
670

        $tgtfile = $converter->convert($srcfile, $outputFormats[$conversionRule->targetPuid]);

        if (!file_exists($tgtfile)) {
            return false;
        }

        $convertedResource = $this->createFromFile($tgtfile);
        $convertedResource->resId = \laabs::newId();
671
        $convertedResource->archiveId = $digitalResource->archiveId;
Prosper De Laure's avatar
Prosper De Laure committed
672
        $convertedResource->puid = $conversionRule->targetPuid;
673
674
        $convertedResource->softwareName = $convertService["softwareName"];
        $convertedResource->softwareVersion = $convertService["softwareVersion"];
Prosper De Laure's avatar
Prosper De Laure committed
675
676
        $this->getHash($convertedResource);

677
678
        $convertedResource->relationshipType = "isConversionOf";

679
        // Get previous
680
681
682
        while ($digitalResource->relatedResId != "" && $digitalResource->relationshipType == "isConversionOf") {
            $digitalResource = $this->sdoFactory->read("digitalResource/digitalResource", $digitalResource->relatedResId);
        }
Alexis Ragot's avatar
Alexis Ragot committed
683
        $convertedResource->relatedResId = $digitalResource->resId;
684

Prosper De Laure's avatar
Prosper De Laure committed
685
686
        return $convertedResource;
    }
687

688
689
690
691
    /**
     * Check if the resource can be convertible
     * @param $digitalResource/digitalResource $digitalResource The resource object
     *
692
     * @return mixed The convertion rule or false if it's no possible
693
     */
Alexandre Morin's avatar
Alexandre Morin committed
694
695
    public function isConvertible($digitalResource)
    {
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
        if (!$this->sdoFactory->exists("digitalResource/conversionRule", array('puid' => $digitalResource->puid))) {
            return false;
        }

        $configuration =  \laabs::configuration('dependency.fileSystem');

        if (!isset($configuration['conversionServices'])) {
            return false;
        }

        $conversionServices = $configuration['conversionServices'];

        if (!is_array($conversionServices)) {
            return false;
        }

        $conversionRule = $this->sdoFactory->read("digitalResource/conversionRule", array('puid' => $digitalResource->puid));

        $outputFormats = null;
        $convertService = null;
        foreach ($conversionServices as $service) {
            if ($service["serviceName"] == $conversionRule->conversionService) {
                $outputFormats = $service["outputFormats"];
                $convertService = $service;
            }
        }

        if (!in_array($digitalResource->puid, $convertService["inputFormats"])) {
            return false;
        }

        if (!$outputFormats) {
            return false;
        }

        $conversionRule = $this->sdoFactory->read("digitalResource/conversionRule", array('puid' => $digitalResource->puid));

        $converter = \laabs::newService($conversionRule->conversionService);

        if (!($converter instanceof \dependency\fileSystem\conversionInterface)) {
            return false;
        }

        return $converter;
    }

742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
    /**
     * Get the text of the resources of an archive
     * @param string $archiveId The resources archive identifier
     *
     * @return string The text of the resources
     */
    public function getFullTextByArchiveId($archiveId)
    {
        $fullText = "";
        $digitalResources = $this->sdoFactory->find("digitalResource/digitalResource", "archiveId='$archiveId' AND relatedResId=null");

        if (count($digitalResources)) {
            $fullTextService = \laabs::newService('dependency/fileSystem/plugins/tika');

            foreach ($digitalResources as $digitalResource) {
                $contents = $this->contents($digitalResource->resId);
                $tempdir = str_replace("/", DIRECTORY_SEPARATOR, \laabs\tempdir());
Alexandre Morin's avatar
Alexandre Morin committed
759

760
761
762
763
764
765
766
767
768
769
                $srcfile = $tempdir.DIRECTORY_SEPARATOR.$digitalResource->resId;

                file_put_contents($srcfile, $contents);

                $fullText .= $fullTextService->getText($srcfile);
            }
        }

        return $fullText;
    }
Prosper De Laure's avatar
Prosper De Laure committed
770
}