-
Notifications
You must be signed in to change notification settings - Fork 0
/
Exporter.php
79 lines (67 loc) · 2.98 KB
/
Exporter.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
<?php
class Exporter
{
protected $collectionCount;
public function Exporter() {
$this->collectionCount = 0;
}
public function processCollection($DAO, $collection, $destDir) {
$collectionDir = $destDir . DIRECTORY_SEPARATOR . StringUtils::windowsFilenameSafeString($collection['NAME']);
if (@mkdir($collectionDir)) {
echo "Created $collectionDir\n";
}
$this->extractFilesForCollection($DAO, $collection, $collectionDir);
if (TESTING) {
if ($this->collectionCount > 20) {
echo "test mode - exiting.\n";
exit(0);
}
}
$this->collectionCount++;
foreach($DAO->getChildCollections($collection) as $childCollection) {
$this->processCollection($DAO, $childCollection, $collectionDir);
}
$DAO->updateCollectionExportDate($collection);
}
public function extractFilesForCollection($DAO, $collection, $collectionDir) {
$this->writeCollectionMetadata($DAO, $collection, $collectionDir);
$files = $DAO->getFilesForCollection($collection);
foreach ($files as $file) {
$sourcePath = $this->getFullPathForFile($file);
$destDir = $collectionDir . DIRECTORY_SEPARATOR . $file['CATALOG_DATE'];
if (!is_dir($destDir)) {
mkdir($destDir);
}
$destPath = $destDir . DIRECTORY_SEPARATOR . $file['NAME'];
echo "copying $sourcePath to $destPath...";
copy($sourcePath, $destPath);
echo "\ndone!\n";
$this->extractMetadata($DAO, $file, $destDir);
$DAO->updateAssetExportDate($file);
}
}
public function extractMetadata($DAO, $file, $destDir) {
$data = "KEY\tVALUE_TYPE\tVALUE\n";
$metadata = $DAO->getMetadataForFile($file);
foreach($metadata as $row) {
$data .= $row['name'] . "\t" . $row['type'] . "\t" . $row['value'] . "\n";
}
file_put_contents($destDir . DIRECTORY_SEPARATOR . '_' . $file['NAME'] . '.tsv', $data);
}
public function writeCollectionMetadata($DAO, $collection, $collectionDir) {
$header = "NAME\tCREATION_DATE\tCREATED_BY\tNOTES\n";
$creator = $DAO->getUser($collection['WM_USER_ID']);
$creator = $creator[0];
$creatorString = $creator['FIRST_NAME'] . ' ' . $creator['LAST_NAME'] .
' (' . $creator['EMAIL'] . ')';
$data = $header . $collection['NAME'] . "\t" . $collection['CREATION_DATE']->format('Y-m-d') . "\t" .
$creatorString . "\t" . $collection['NOTES'] . "\n";
file_put_contents($collectionDir . DIRECTORY_SEPARATOR . '_collection_data.tsv', $data);
}
public function getFullPathForFile($file) {
return ASSET_BASE_PATH . DIRECTORY_SEPARATOR .
trim($file['CATEGORY_PATH'], '/') . DIRECTORY_SEPARATOR .
$file['CATALOG_DATE'] . DIRECTORY_SEPARATOR .
$file['FILE_PATH'];
}
}