-
Notifications
You must be signed in to change notification settings - Fork 11
/
CsvBooks.php
253 lines (228 loc) · 9.49 KB
/
CsvBooks.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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
<?php
namespace mik\writers;
use Monolog\Logger;
class CsvBooks extends Writer
{
/**
* @var array $settings - settings from the confugration class.
*/
public $settings;
/**
* @var object $fetcher - fetcher class for item info methods.
*/
private $fetcher;
/**
* @var object - filegetter class for CSV books.
*/
private $fileGetter;
/**
* Create a new book writer instance.
*
* @param array $settings configuration settings.
*/
public function __construct($settings)
{
parent::__construct($settings);
$fileGetterClass = 'mik\\filegetters\\' . $settings['FILE_GETTER']['class'];
$this->fileGetter = new $fileGetterClass($settings);
$this->output_directory = $settings['WRITER']['output_directory'];
$this->metadata_filename = $settings['WRITER']['metadata_filename'];
// Default is to generate page-level MODS.xml files.
if (isset($settings['WRITER']['generate_page_modsxml'])) {
$this->generate_page_modsxml = $settings['WRITER']['generate_page_modsxml'];
} else {
$this->generate_page_modsxml = true;
}
// Default is to use - as the sequence separator in the page filename.
if (isset($settings['WRITER']['page_sequence_separator'])) {
$this->page_sequence_separator = $settings['WRITER']['page_sequence_separator'];
} else {
$this->page_sequence_separator = '-';
}
// Set up logger.
$this->pathToLog = $this->settings['LOGGING']['path_to_log'];
$this->log = new \Monolog\Logger('Writer');
$this->logStreamHandler = new \Monolog\Handler\StreamHandler(
$this->pathToLog,
Logger::INFO
);
$this->log->pushHandler($this->logStreamHandler);
$this->ocr_extension = '.txt';
// Default is to not log the absence of page-level OCR files.
if (isset($settings['WRITER']['log_missing_ocr_files'])) {
$this->log_missing_ocr_files= $settings['WRITER']['log_missing_ocr_files'];
} else {
$this->log_missing_ocr_files = false;
}
}
/**
* Write folders and files.
*
* @param $metadata
* @param $pages
* @param $record_id
*/
public function writePackages($metadata, $pages, $record_id)
{
// If there were no datastreams explicitly set in the configuration,
// set flag so that all datastreams in the writer class are run.
// $this->datastreams is an empty array by default.
$no_datastreams_setting_flag = false;
if (count($this->datastreams) == 0) {
$no_datastreams_setting_flag = true;
}
$book_level_input_dir = $this->fileGetter->getBookSourcePath($record_id);
if ($this->inputValidator->validateInputType == 'realtime' && $this->inputValidator->validateInput) {
if (!$this->inputValidator->validatePackage($record_id, $book_level_input_dir)) {
$this->problemLog->addError($record_id);
return;
}
}
//Create root output folder
$this->createOutputDirectory();
// Create a book-level subdirectory in the output directory.
$book_level_output_dir = $this->output_directory . DIRECTORY_SEPARATOR . $record_id;
if (!file_exists($book_level_output_dir)) {
mkdir($book_level_output_dir, 0777, true);
}
// Report a missing input dir only when the book level input dir is missing
// and all datastreams are expected, or when both MODS and OBJ are explicit.
// But don't report a missing input dir when MODS is the only datastream and
// the input directory setting is empty.
if (!file_exists($book_level_input_dir)) {
if ($this->settings['FILE_GETTER']['input_directory'] !== '' &&
($this->datastreams != array('MODS') xor $no_datastreams_setting_flag)) {
if ($no_datastreams_setting_flag) {
$this->log->addWarning(
"CSV Books warning",
array('Book-level input directory does not exist' => $book_level_input_dir)
);
return;
}
}
}
$MODS_expected = in_array('MODS', $this->datastreams);
if ($MODS_expected xor $no_datastreams_setting_flag) {
$metadata_file_path = $book_level_output_dir . DIRECTORY_SEPARATOR .
$this->metadata_filename;
$this->writeMetadataFile($metadata, $metadata_file_path);
}
// @todo: Add error handling on mkdir and copy.
foreach ($pages as $page_path) {
// Get the sequence number from the last segment of the filename.
$pathinfo = pathinfo($page_path);
$filename_segments = explode($this->page_sequence_separator, $pathinfo['filename']);
$page_number = ltrim(end($filename_segments), '0');
if (strlen($page_number) === 0) {
$page_number = '0';
}
$page_level_output_dir = $book_level_output_dir . DIRECTORY_SEPARATOR . $page_number;
mkdir($page_level_output_dir);
$OBJ_expected = in_array('OBJ', $this->datastreams);
if ($OBJ_expected xor $no_datastreams_setting_flag) {
$extension = $pathinfo['extension'];
$page_output_path = $page_level_output_dir . DIRECTORY_SEPARATOR .
'OBJ.' . $extension;
copy($page_path, $page_output_path);
}
if ($MODS_expected xor $no_datastreams_setting_flag) {
if ($this->generate_page_modsxml) {
$this->writePageMetadataFile($metadata, $page_number, $page_level_output_dir);
}
}
// If the datastreams list is comprised of only 'MODS' we're generating metadata only.
if ($this->datastreams != array('MODS')) {
$OCR_expected = in_array('OCR', $this->datastreams);
if ($OCR_expected xor $no_datastreams_setting_flag) {
$ocr_input_path = $pathinfo['dirname'] . DIRECTORY_SEPARATOR .
$pathinfo['filename'] . $this->ocr_extension;
$ocr_output_path = $page_level_output_dir . DIRECTORY_SEPARATOR .
'OCR' . $this->ocr_extension;
if (file_exists($ocr_input_path)) {
copy($ocr_input_path, $ocr_output_path);
} else {
if ($this->log_missing_ocr_files) {
$this->log->addWarning(
"CSV Books warning",
array('Page-level OCR file does not exist' => $ocr_input_path)
);
}
}
}
}
}
}
/**
* Writes out the book-level MODS.xml file.
*
* @param $metadata
* The MODS XML document produced by the metadta parser.
* @param $path
* The path to write the XML file to.
*/
public function writeMetadataFile($metadata, $path)
{
// Add XML decleration
$doc = new \DomDocument('1.0');
$doc->loadXML($metadata);
$doc->formatOutput = true;
$metadata = $doc->saveXML();
if ($path !='') {
$fileCreationStatus = file_put_contents($path, $metadata);
if ($fileCreationStatus === false) {
$this->log->addWarning(
"There was a problem writing the book-level metadata to a file",
array('file' => $path)
);
}
}
}
/**
* Generates a very simple MODS.xml file for a book page.
*
* @param $parent_metadata
* The MODS document associated with the page's parent book.
* @param $page_number
* The page's page number, taken from its file's filename.
* @param $path
* The path to write the XML file to.
*/
public function writePageMetadataFile($parent_metadata, $page_number, $path)
{
// Get the first title element from the book's MODS.
$dom = new \DOMDocument;
$dom->loadXML($parent_metadata);
$xpath = new \DOMXPath($dom);
$titles = $xpath->query("//mods:titleInfo/mods:title");
$page_title = $titles->item(0)->nodeValue . ', page ' . $page_number;
$page_title = htmlspecialchars($page_title, ENT_NOQUOTES|ENT_XML1);
$namespace = sprintf(
'xmlns="%s" xmlns:mods="%s" xmlns:xsi="%s" xmlns:xlink="%s"',
"http://www.loc.gov/mods/v3",
"http://www.loc.gov/mods/v3",
"http://www.w3.org/2001/XMLSchema-instance",
"http://www.w3.org/1999/xlink"
);
$page_mods = <<<EOQ
<mods {$namespace}>
<titleInfo>
<title>{$page_title}</title>
</titleInfo>
</mods>
EOQ;
$path = $path . DIRECTORY_SEPARATOR . $this->metadata_filename;
$doc = new \DomDocument('1.0');
$doc->loadXML($page_mods);
$doc->formatOutput = true;
$metadata = $doc->saveXML();
if ($path !='') {
$fileCreationStatus = file_put_contents($path, $metadata);
if ($fileCreationStatus === false) {
$this->log->addWarning(
"There was a problem writing the page-level metadata to a file",
array('file' => $path)
);
}
}
}
}