/
MARC.php
435 lines (382 loc) · 15.1 KB
/
MARC.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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
<?php
/* vim: set expandtab shiftwidth=4 tabstop=4 softtabstop=4 foldmethod=marker: */
/**
* Parser for MARC records
*
* This package is based on the PHP MARC package, originally called "php-marc",
* that is part of the Emilda Project (http://www.emilda.org). Christoffer
* Landtman generously agreed to make the "php-marc" code available under the
* GNU LGPL so it could be used as the basis of this PEAR package.
*
* PHP version 5
*
* LICENSE: This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* @category File_Formats
* @package File_MARC
* @author Christoffer Landtman <landtman@realnode.com>
* @author Dan Scott <dscott@laurentian.ca>
* @copyright 2003-2008 Oy Realnode Ab, Dan Scott
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @version CVS: $Id$
* @link http://pear.php.net/package/File_MARC
* @example read.php Retrieve specific fields and subfields from a record
* @example subfields.php Create new subfields and add them in specific order
* @example marc_yaz.php Pretty print a MARC record retrieved through the PECL yaz extension
*/
require_once 'PEAR/Exception.php';
require_once 'Structures/LinkedList/Double.php';
require_once 'File/MARC/Record.php';
require_once 'File/MARC/Field.php';
require_once 'File/MARC/Control_Field.php';
require_once 'File/MARC/Data_Field.php';
require_once 'File/MARC/Subfield.php';
require_once 'File/MARC/Exception.php';
require_once 'File/MARC/List.php';
// {{{ class File_MARC
/**
* The main File_MARC class enables you to return File_MARC_Record
* objects from a stream or string.
*
* @category File_Formats
* @package File_MARC
* @author Christoffer Landtman <landtman@realnode.com>
* @author Dan Scott <dscott@laurentian.ca>
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @link http://pear.php.net/package/File_MARC
*/
class File_MARC
{
// {{{ constants
/**
* MARC records retrieved from a file
*/
const SOURCE_FILE = 1;
/**
* MARC records retrieved from a binary string
*/
const SOURCE_STRING = 2;
/**
* Hexadecimal value for Subfield indicator
*/
const SUBFIELD_INDICATOR = "\x1F";
/**
* Hexadecimal value for End of Field
*/
const END_OF_FIELD = "\x1E";
/**
* Hexadecimal value for End of Record
*/
const END_OF_RECORD = "\x1D";
/**
* Length of the Directory
*/
const DIRECTORY_ENTRY_LEN = 12;
/**
* Length of the Leader
*/
const LEADER_LEN = 24;
/**
* Maximum record length
*/
const MAX_RECORD_LENGTH = 99999;
// }}}
// {{{ properties
/**
* Source containing raw records
*
* @var resource
*/
protected $source;
/**
* Source type (SOURCE_FILE or SOURCE_STRING)
*
* @var int
*/
protected $type;
/**
* XMLWriter for writing collections
*
* @var XMLWriter
*/
protected $xmlwriter;
// }}}
// {{{ Constructor: function __construct()
/**
* Read in MARC records
*
* This function reads in MARC record files or strings that
* contain one or more MARC records.
*
* <code>
* <?php
* // Retrieve MARC records from a file
* $journals = new File_MARC('journals.mrc', SOURCE_FILE);
*
* // Retrieve MARC records from a string (e.g. Z39 query results)
* $monographs = new File_MARC($raw_marc, SOURCE_STRING);
* ?>
* </code>
*
* @param string $source Name of the file, or a raw MARC string
* @param int $type Source of the input, either SOURCE_FILE or SOURCE_STRING
*/
function __construct($source, $type = self::SOURCE_FILE)
{
$this->xmlwriter = new XMLWriter();
$this->xmlwriter->openMemory();
switch ($type) {
case self::SOURCE_FILE:
$this->type = self::SOURCE_FILE;
$this->source = fopen($source, 'rb');
if (!$this->source) {
$errorMessage = File_MARC_Exception::formatError(File_MARC_Exception::$messages[File_MARC_Exception::ERROR_INVALID_FILE], array('filename' => $source));
throw new File_MARC_Exception($errorMessage, File_MARC_Exception::ERROR_INVALID_FILE);
}
break;
case self::SOURCE_STRING:
$this->type = self::SOURCE_STRING;
$this->source = explode(File_MARC::END_OF_RECORD, $source);
break;
default:
throw new File_MARC_Exception(File_MARC_Exception::$messages[File_MARC_Exception::ERROR_INVALID_SOURCE], File_MARC_Exception::ERROR_INVALID_SOURCE);
}
}
// }}}
// {{{ nextRaw()
/**
* Return the next raw MARC record
*
* Returns the next raw MARC record, unless all records already have
* been read.
*
* @return string Either a raw record or false
*/
function nextRaw()
{
if ($this->type == self::SOURCE_FILE) {
$record = stream_get_line($this->source, File_MARC::MAX_RECORD_LENGTH, File_MARC::END_OF_RECORD);
// Remove illegal stuff that sometimes occurs between records
$record = preg_replace('/^[\\x0a\\x0d\\x00]+/', "", $record);
} elseif ($this->type == self::SOURCE_STRING) {
$record = array_shift($this->source);
}
// Exit if we are at the end of the file
if (!$record) {
return false;
}
// Append the end of record we lost during stream_get_line() or explode()
$record .= File_MARC::END_OF_RECORD;
return $record;
}
// }}}
// {{{ next()
/**
* Return next {@link File_MARC_Record} object
*
* Decodes the next raw MARC record and returns the {@link File_MARC_Record}
* object.
* <code>
* <?php
* // Retrieve a set of MARC records from a file
* $journals = new File_MARC('journals.mrc', SOURCE_FILE);
*
* // Iterate through the retrieved records
* while ($record = $journals->next()) {
* print $record;
* print "\n";
* }
*
* ?>
* </code>
*
* @return File_MARC_Record next record, or false if there are
* no more records
*/
function next()
{
$raw = $this->nextRaw();
if ($raw) {
return $this->_decode($raw);
} else {
return false;
}
}
// }}}
// {{{ _decode()
/**
* Decode a given raw MARC record
*
* Port of Andy Lesters MARC::File::USMARC->decode() Perl function into PHP.
*
* @param string $text Raw MARC record
*
* @return File_MARC_Record Decoded File_MARC_Record object
*/
private function _decode($text)
{
$marc = new File_MARC_Record($this);
$matches = array();
if (!preg_match("/^(\d{5})/", $text, $matches)) {
$marc->addWarning(File_MARC_Exception::formatError(File_MARC_Exception::$messages[File_MARC_Exception::ERROR_NONNUMERIC_LENGTH], array("record_length" => substr($text, 0, 5))));
}
// Store record length
$record_length = $matches[1];
if ($record_length != strlen($text)) {
$marc->addWarning(File_MARC_Exception::formatError(File_MARC_Exception::$messages[File_MARC_Exception::ERROR_INCORRECT_LENGTH], array("record_length" => $record_length, "actual" => strlen($text))));
// give up and set the record length to the actual byte length
$record_length = strlen($text);
}
if (substr($text, -1, 1) != File_MARC::END_OF_RECORD)
throw new File_MARC_Exception(File_MARC_Exception::$messages[File_MARC_Exception::ERROR_INVALID_TERMINATOR], File_MARC_Exception::ERROR_INVALID_TERMINATOR);
// Store leader
$marc->setLeader(substr($text, 0, File_MARC::LEADER_LEN));
// bytes 12 - 16 of leader give offset to the body of the record
$data_start = 0 + substr($text, 12, 5);
// immediately after the leader comes the directory (no separator)
$dir = substr($text, File_MARC::LEADER_LEN, $data_start - File_MARC::LEADER_LEN - 1); // -1 to allow for \x1e at end of directory
// character after the directory must be \x1e
if (substr($text, $data_start-1, 1) != File_MARC::END_OF_FIELD) {
$marc->addWarning(File_MARC_Exception::$messages[File_MARC_Exception::ERROR_NO_DIRECTORY]);
}
// All directory entries 12 bytes long, so length % 12 must be 0
if (strlen($dir) % File_MARC::DIRECTORY_ENTRY_LEN != 0) {
$marc->addWarning(File_MARC_Exception::$messages[File_MARC_Exception::ERROR_INVALID_DIRECTORY_LENGTH]);
}
// go through all the fields
$nfields = strlen($dir) / File_MARC::DIRECTORY_ENTRY_LEN;
for ($n=0; $n<$nfields; $n++) {
// As pack returns to key 1, leave place 0 in list empty
list(, $tag) = unpack("A3", substr($dir, $n*File_MARC::DIRECTORY_ENTRY_LEN, File_MARC::DIRECTORY_ENTRY_LEN));
list(, $len) = unpack("A3/A4", substr($dir, $n*File_MARC::DIRECTORY_ENTRY_LEN, File_MARC::DIRECTORY_ENTRY_LEN));
list(, $offset) = unpack("A3/A4/A5", substr($dir, $n*File_MARC::DIRECTORY_ENTRY_LEN, File_MARC::DIRECTORY_ENTRY_LEN));
// Check directory validity
if (!preg_match("/^[0-9A-Za-z]{3}$/", $tag)) {
$marc->addWarning(File_MARC_Exception::formatError(File_MARC_Exception::$messages[File_MARC_Exception::ERROR_INVALID_DIRECTORY_TAG], array("tag" => $tag)));
}
if (!preg_match("/^\d{4}$/", $len)) {
$marc->addWarning(File_MARC_Exception::formatError(File_MARC_Exception::$messages[File_MARC_Exception::ERROR_INVALID_DIRECTORY_TAG_LENGTH], array("tag" => $tag, "len" => $len)));
}
if (!preg_match("/^\d{5}$/", $offset)) {
$marc->addWarning(File_MARC_Exception::formatError(File_MARC_Exception::$messages[File_MARC_Exception::ERROR_INVALID_DIRECTORY_OFFSET], array("tag" => $tag, "offset" => $offset)));
}
if ($offset + $len > $record_length) {
$marc->addWarning(File_MARC_Exception::formatError(File_MARC_Exception::$messages[File_MARC_Exception::ERROR_INVALID_DIRECTORY], array("tag" => $tag)));
}
$tag_data = substr($text, $data_start + $offset, $len);
if (substr($tag_data, -1, 1) == File_MARC::END_OF_FIELD) {
/* get rid of the end-of-tag character */
$tag_data = substr($tag_data, 0, -1);
$len--;
} else {
$marc->addWarning(File_MARC_Exception::formatError(File_MARC_Exception::$messages[File_MARC_Exception::ERROR_FIELD_EOF], array("tag" => $tag)));
}
if (preg_match("/^\d+$/", $tag) and ($tag < 10)) {
$marc->appendField(new File_MARC_Control_Field($tag, $tag_data));
} else {
$subfields = explode(File_MARC::SUBFIELD_INDICATOR, $tag_data);
$indicators = array_shift($subfields);
if (strlen($indicators) != 2) {
$errorMessage = File_MARC_Exception::formatError(File_MARC_Exception::$messages[File_MARC_Exception::ERROR_INVALID_INDICATORS], array("tag" => $tag, "indicators" => $indicators));
$marc->addWarning($errorMessage);
// Do the best with the indicators we've got
if (strlen($indicators) == 1) {
$ind1 = $indicators;
$ind2 = " ";
} else {
list($ind1,$ind2) = array(" ", " ");
}
} else {
$ind1 = substr($indicators, 0, 1);
$ind2 = substr($indicators, 1, 1);
}
// Split the subfield data into subfield name and data pairs
$subfield_data = array();
foreach ($subfields as $subfield) {
if (strlen($subfield) > 0) {
$subfield_data[] = new File_MARC_Subfield(substr($subfield, 0, 1), substr($subfield, 1));
} else {
$errorMessage = File_MARC_Exception::formatError(File_MARC_Exception::$messages[File_MARC_Exception::ERROR_EMPTY_SUBFIELD], array("tag" => $tag));
$marc->addWarning($errorMessage);
}
}
if (!isset($subfield_data)) {
$errorMessage = File_MARC_Exception::formatError(File_MARC_Exception::$messages[File_MARC_Exception::ERROR_EMPTY_SUBFIELD], array("tag" => $tag));
$marc->addWarning($errorMessage);
}
// If the data is invalid, let's just ignore the one field
try {
$new_field = new File_MARC_Data_Field($tag, $subfield_data, $ind1, $ind2);
$marc->appendField($new_field);
} catch (Exception $e) {
$marc->addWarning($e->getMessage());
}
}
}
return $marc;
}
// }}}
// {{{ toXMLHeader()
/**
* Initializes the MARCXML output of a record or collection of records
*
* This method produces an XML representation of a MARC record that
* attempts to adhere to the MARCXML standard documented at
* http://www.loc.gov/standards/marcxml/
*
* @return bool true if successful
*/
function toXMLHeader()
{
$this->xmlwriter->startDocument('1.0', 'UTF-8');
$this->xmlwriter->startElement("collection");
$this->xmlwriter->writeAttribute("xmlns", "http://www.loc.gov/MARC21/slim");
return true;
}
// }}}
// {{{ getXMLWriter()
/**
* Returns the XMLWriter object
*
* This method produces an XML representation of a MARC record that
* attempts to adhere to the MARCXML standard documented at
* http://www.loc.gov/standards/marcxml/
*
* @return XMLWriter XMLWriter instance
*/
function getXMLWriter()
{
return $this->xmlwriter;
}
// }}}
// {{{ toXMLFooter()
/**
* Returns the MARCXML collection footer
*
* This method produces an XML representation of a MARC record that
* attempts to adhere to the MARCXML standard documented at
* http://www.loc.gov/standards/marcxml/
*
* @return string representation of MARC record in MARCXML format
*/
function toXMLFooter()
{
$this->xmlwriter->endElement(); // end collection
$this->xmlwriter->endDocument();
return $this->xmlwriter->outputMemory();
}
// }}}
}
// }}}