-
Notifications
You must be signed in to change notification settings - Fork 3
/
MarcXml.php
360 lines (336 loc) · 10.8 KB
/
MarcXml.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
<?php
/**
* MARCXML format support class.
*
* PHP version 7
*
* Copyright (C) The National Library of Finland 2020-2022.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2,
* as published by the Free Software Foundation.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* @category VuFind
* @package MARC
* @author Ere Maijala <ere.maijala@helsinki.fi>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link https://vufind.org/wiki/development:plugins:record_drivers Wiki
*/
namespace VuFind\Marc\Serialization;
/**
* MARCXML format support class.
*
* @category VuFind
* @package MARC
* @author Ere Maijala <ere.maijala@helsinki.fi>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link https://vufind.org/wiki/development:plugins:record_drivers Wiki
*/
class MarcXml extends AbstractSerializationFile implements SerializationInterface
{
/**
* Current file
*
* @var string
*/
protected $fileName = '';
/**
* XML Reader for current file
*
* @var \XMLReader
*/
protected $xml = null;
/**
* Current XML element path
*
* @var array
*/
protected $currentXmlPath = [];
/**
* Check if this class can parse the given MARC string
*
* @param string $marc MARC
*
* @return bool
*/
public static function canParse(string $marc): bool
{
// A pretty naïve check, but it's enough to tell the different formats apart
return strncmp(trim($marc), '<', 1) === 0;
}
/**
* Check if the serialization class can parse the given MARC collection string
*
* @param string $marc MARC
*
* @return bool
*/
public static function canParseCollection(string $marc): bool
{
// A pretty naïve check, but it's enough to tell the different formats apart
return strncmp(trim($marc), '<', 1) === 0;
}
/**
* Check if the serialization class can parse the given MARC collection file
*
* @param string $file File name
*
* @return bool
*/
public static function canParseCollectionFile(string $file): bool
{
if (false === ($f = @fopen($file, 'r'))) {
throw new \Exception("Cannot open file '$file' for reading");
}
do {
$s = ltrim(fgets($f, 10));
} while (!$s && !feof($f));
fclose($f);
return self::canParseCollection($s);
}
/**
* Parse MARC collection from a string into an array
*
* @param string $collection MARC record collection in the format supported by
* the serialization class
*
* @throws \Exception
* @return array
*/
public static function collectionFromString(string $collection): array
{
$xml = static::loadXML(trim($collection));
$results = [];
foreach ($xml->record as $record) {
$results[] = $record->asXML();
}
return $results;
}
/**
* Parse MARCXML string
*
* @param string $marc MARCXML
*
* @throws \Exception
* @return array
*/
public static function fromString(string $marc): array
{
$xml = static::loadXML(trim($marc));
// Move to the record element if we were given a collection
if ($xml->record) {
$xml = $xml->record;
}
$leader = isset($xml->leader) ? (string)$xml->leader[0] : '';
$fields = [];
foreach ($xml->controlfield as $field) {
$fields[] = [(string)$field['tag'] => (string)$field];
}
foreach ($xml->datafield as $field) {
$newField = [
'ind1' => str_pad((string)$field['ind1'], 1),
'ind2' => str_pad((string)$field['ind2'], 1),
'subfields' => [],
];
foreach ($field->subfield as $subfield) {
$newField['subfields'][]
= [(string)$subfield['code'] => (string)$subfield];
}
$fields[] = [(string)$field['tag'] => $newField];
}
return compact('leader', 'fields');
}
/**
* Convert record to a MARCXML string
*
* @param array $record Record data
*
* @return string
*/
public static function toString(array $record): string
{
$xml = new \XMLWriter();
$xml->openMemory();
$xml->setIndent(true);
$xml->startDocument('1.0', 'UTF-8');
$xml->startElementNs(null, 'collection', "http://www.loc.gov/MARC21/slim");
$xml->writeAttribute(
'xmlns:xsi',
'http://www.w3.org/2001/XMLSchema-instance'
);
$xml->writeAttribute(
'xsi:schemaLocation',
'http://www.loc.gov/MARC21/slim'
. ' http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd'
);
$xml->startElement('record');
if ($record['leader']) {
$xml->writeElement('leader', $record['leader']);
}
foreach ($record['fields'] as $fieldData) {
$tag = (string)key($fieldData);
$field = current($fieldData);
if (is_string($field)) {
$xml->startElement('controlfield');
$xml->writeAttribute('tag', $tag);
$xml->text($field);
$xml->endElement();
} else {
$xml->startElement('datafield');
$xml->writeAttribute('tag', $tag);
$xml->writeAttribute('ind1', $field['ind1']);
$xml->writeAttribute('ind2', $field['ind2']);
foreach ($field['subfields'] ?? [] as $subfield) {
$subfieldData = current($subfield);
$subfieldCode = (string)key($subfield);
if ($subfieldData == '') {
continue;
}
$xml->startElement('subfield');
$xml->writeAttribute('code', $subfieldCode);
$xml->text($subfieldData);
$xml->endElement();
}
$xml->endElement();
}
}
$xml->endElement();
$xml->endDocument();
// Strip illegal characters from XML:
return preg_replace(
'/[^\x{0009}\x{000a}\x{000d}\x{0020}-\x{D7FF}\x{E000}-\x{FFFD}]+/u',
'',
$xml->outputMemory(true)
);
}
/**
* Load XML into SimpleXMLElement
*
* @param string $xml XML
*
* @throws \Exception
* @return \SimpleXMLElement
*/
protected static function loadXML(string $xml): \SimpleXMLElement
{
// Make sure we have an XML prolog with proper encoding:
$xmlHead = '<?xml version';
if (strcasecmp(substr($xml, 0, strlen($xmlHead)), $xmlHead) === 0) {
$decl = substr($xml, 0, strpos($xml, '?>'));
if (strstr($decl, 'encoding') === false) {
$xml = $decl . ' encoding="utf-8"' . substr($xml, strlen($decl));
}
} else {
$xml = '<?xml version="1.0" encoding="utf-8"?>' . "\n\n$xml";
}
$saveUseErrors = libxml_use_internal_errors(true);
try {
libxml_clear_errors();
$doc = \simplexml_load_string(
$xml,
\SimpleXMLElement::class,
LIBXML_COMPACT
);
if (false === $doc) {
$errors = libxml_get_errors();
$messageParts = [];
foreach ($errors as $error) {
$messageParts[] = '[' . $error->line . ':' . $error->column
. '] Error ' . $error->code . ': ' . $error->message;
}
throw new \Exception(implode("\n", $messageParts));
}
libxml_use_internal_errors($saveUseErrors);
return $doc;
} catch (\Exception $e) {
libxml_use_internal_errors($saveUseErrors);
throw $e;
}
}
/**
* Open a collection file
*
* @param string $file File name
*
* @return void
*
* @throws \Exception
*/
public function openCollectionFile(string $file): void
{
$this->fileName = $file;
$this->xml = new \XMLReader();
$result = $this->xml->open($file);
if (false === $result) {
throw new \Exception("Cannot open file '$file' for reading");
}
$this->currentXmlPath = [];
}
/**
* Rewind the collection file
*
* @return void
*
* @throws \Exception
*/
public function rewind(): void
{
if ('' === $this->fileName) {
throw new \Exception('Collection file not open');
}
$this->openCollectionFile($this->fileName);
}
/**
* Get next record from the file or an empty string on EOF
*
* @return string
*
* @throws \Exception
*/
public function getNextRecord(): string
{
if (null === $this->xml) {
throw new \Exception('Collection file not open');
}
while ($this->xml->read()) {
if ($this->xml->nodeType !== \XMLReader::ELEMENT) {
continue;
}
$this->currentXmlPath
= array_slice($this->currentXmlPath, 0, $this->xml->depth);
$this->currentXmlPath[] = $this->xml->name;
$ns = $this->xml->namespaceURI;
$currentPathString = '/' . implode('/', $this->currentXmlPath);
$pathOk = '/collection/record' === $currentPathString;
$namespaceOk = !$ns || 'http://www.loc.gov/MARC21/slim' === $ns;
if (!$pathOk || !$namespaceOk) {
if (count($this->currentXmlPath) === 2) {
if (!$pathOk) {
$this->message(
"Unknown element \"$currentPathString\"",
E_NOTICE
);
} elseif (!$namespaceOk) {
$this->message(
"Unknown namespace \"$ns\" for element \""
. $currentPathString . '"',
E_NOTICE
);
}
}
continue;
}
return $this->xml->readOuterXML();
}
return '';
}
}