PEAR is archived and read-only

This mirror preserves historical PEAR package releases and metadata so existing references remain available.

Home » File Formats » File_MARC » Manual

This package creates, reads, and modifies Machine Readable Cataloging (MARC) formatted files.

Reading MARC data

Reading MARC data – Reading MARC data with File_MARC

Overview

File_MARC allows you to read Machine Readable Cataloging (MARC) data in MARC 21 format. File_MARCXML, which is bundled with File_MARC, allows you to read MARCXML formatted data.

Reading MARC data from different sources

Your input source can be a PHP stream (File_MARC::SOURCE_FILE) or a string (File_MARC::SOURCE_STRING). The source location and source types are the first and second arguments to the File_MARC and File_MARCXML constructors.

Reading MARC 21 data from a file

In the following example, MARC21 data has been stored on disk in a file named journals.mrc. To read the MARCX21 data, we call the constructor for File_MARC. We do not need to tell File_MARC that journals.mrc is a filename or stream pointing to the MARC content, because the value of the second parameter of the constructor for File_MARC defaults to File_MARC::SOURCE_FILE.

<?php
require 'File/MARC.php';

// Retrieve a set of MARC records from a file
$journals = new File_MARC('journals.mrc');

// Iterate through the retrieved records
while ($record = $journals->next()) {
    // Pretty print each record
    print $record;
    print "\n";
}
?>

Reading MARCXML data from a string

In the following example, MARCXML data has been returned from a call to a Web service and has therefore been stored in a PHP variable, $xml_data, as a string. To read the MARCXML data, we call the constructor for File_MARCXML. To tell File_MARCXML that $xml_data is a string, we specify File_MARC::SOURCE_STRING as the second parameter of the constructor.

<?php
require 'File/MARCXML.php';

// Retrieve a set of MARCXML records from a string
$journals = new File_MARCXML($xml_data, File_MARC::SOURCE_STRING);

// Iterate through the retrieved records
while ($record = $journals->next()) {
    // Pretty print each record
    print $record;
    print "\n";
}
?>

Reading MARC data from different sources

A File_MARC object consists of a leader and an iterable set of File_MARC_Record objects representing MARC records. Each of these, in turn, consists of an iterable set of File_MARC_Data_Field or File_MARC_Control_Field objects representing MARC fields. A File_MARC_Data_Field consists of a set of iterable File_MARC_Subfield objects representing MARC subfields.

Printing the elements of a record

<?php
require 'File/MARC.php';

// Retrieve a set of MARC records
$bibrecords = new File_MARC('catdump.mrc', File_MARC::SOURCE_FILE);

// Iterate through the retrieved records
while ($record = $bibrecords->next()) {
    // Print the leader
    print $record->getLeader();
    $subjects = $record->getFields('650');
    if ($subjects) {
        // Retrieve just the first 24_ field
        print $record->getField('24.', true);
        print "\n";

        // Now print all of the retrieved subjects
        foreach ($subjects as $field) {
            print $field;
            print "\n";
        } 
        print "\n";
    }
}
?>

All of this means that File_MARC makes it easy to read in a set of MARC records and iterate through the contents to retrieve specific fields and subfields. File_MARC offers convenience methods for retrieving specific fields without forcing you to iterate through the fields. getField returns the first field that matches the field name, while getFields returns an array of all of the fields that match the specified field name. Both of these methods accept an optional boolean parameter that specifies whether your match string should be treated as a Perl Compatible Regular Expression.

Retrieving all 650 fields from a record

<?php
require 'File/MARC.php';

// Retrieve a set of MARC records from a z39 result string
$bibrecords = new File_MARC($z39_result, File_MARC::SOURCE_STRING);

// Iterate through the retrieved records
while ($record = $bibrecords->next()) {
    // Retrieve an array of all of the 650 fields
    $subjects = $record->getFields('650');
    if ($subjects) {
        // Retrieve just the first 24_ field
        print $record->getField('24.', true);
        print "\n";

        // Now print all of the retrieved subjects
        foreach ($subjects as $field) {
            print $field;
            print "\n";
        } 
        print "\n";
    }
}
?>

Iterating through fields and subfields

When you iterate over a File_MARC_Data_Field object using foreach(), the MARC tag for the given field is returned as the key for the element and the set of subfields is returned as the value of the element.

Similarly, when you iterate over a File_MARC_Subfield object using foreach(), the code for the given subfield is returned as the key of the element and the value of the given subfield is returned as the value of the element.

Iterating over fields and subfields in a MARC record

In the following example, we iterate through a set of 650 fields to print out the subject headings contained in the subfields for each field.

<?php
require 'File/MARC.php';

// Retrieve a set of MARC records from a z39 result string
$bibrecords = new File_MARC($z39_result, File_MARC::SOURCE_STRING);

// Go through each record
while ($record = $bibrecords->next()) {
    // Iterate through the fields
    foreach ($record->getFields() as $tag => $subfields) {
        // Skip everything except for 650 fields
        if ($tag == '650') {
            print "Subject:";
            foreach ($subfields->getSubfields() as $code => $value) {
                print " $value";
            }
            print "\n";
        }
    }
}
?>

Retrieving field indicators

Data fields, represented by the File_MARC_Data_Field class, offer a getIndicator() function to enable you to retrieve the value of an indicator.

Retrieving an indicator

In the following example, we retrieve the title (245) field of a MARC record and check the second indicator for the field to determine whether there are non-filing indicators that we should ignore when sorting the contents of the field.

<?php
require 'File/MARC.php';

$titleField = $record->getField('245');
$nonfiling = $titleField->getIndicator(2);

if ($nonfiling) {
  // Sort using the subset of the $a subfield
  $title = substr($titleField->getSubfield('a'), $nonfiling);
} else {
  // Sort using the entire contents of the $a subfield
  $title = $titleField->getSubfield('a');
}
?>

Formatting MARC data

Formatting MARC data – Formatting MARC data with File_MARC

Overview

The File_MARC_Record class enables you to write Machine Readable Cataloging (MARC) data in MARC 21 format, in a human-readable string format, and (with some restrictions) in MARCXML format.

Formatting MARC 21 data

To return a record in MARC 21 format, call the toRaw() method on the File_MARC_Record object.

Writing MARC 21 data to a file

In the following example, we have created one or more MARC records represented by File_MARC_Record objects stored in the $records array. To write this data to a file in MARC 21 format, we simply open the file in binary mode and write the contents of the records in the array to the file by calling the toRaw() method on each record in turn.

<?php
require 'File/MARC.php';

// convert_metadata_to_marc() is a fictional method
// that returns an array of File_MARC_Record objects
$records = convert_metadata_to_marc(); 

// Open a file for binary write access
$marc21_file = fopen("records.mrc", "wb");

// Iterate through the records
while ($record = $records->next()) {
    // Write each record to the file in MARC 21 format
    fwrite($marc21_file, $record->toRaw());
}

// Close the file
fclose($marc21_file);
?>

Creating human-readable output from MARC data

To return a human-readable version of a MARC 21 or MARCXML record, call the __toString() method on the File_MARC_Record object. Note that you call the __toString() method implicitly when you call the print() function on a File_MARC_Record object.

Returning a human-readable representation of MARC

In the following example, we print the contents of each MARC record in a human-readable format and also explicitly call the __toString() method so that we can write the human-readable contents to a file. Notice that it does not matter whether the source format is MARC or MARCXML, the methods we call to format the data for output are the same.

<?php
require 'File/MARCXML.php';

// Retrieve a set of MARCXML records from a string
$journals = new File_MARCXML($xml_data, File_MARC::SOURCE_STRING);

// Open a file for binary write access
$marc21_file = fopen("records.mrc", "wb");

// Iterate through the retrieved records
while ($record = $journals->next()) {
    // Pretty print each record
    print $record;
    print "\n";

    // Write the pretty-printed record to file
    fwrite($marc21_file, $record->__toString() . "\n");
}

// Close the file
fclose($marc21_file);
?>

Formatting MARCXML data

To return a record in MARCXML format, call the toXML() method on the File_MARC_Record object.

Significant restrictions on the toXML() method

  • Most significantly, PHP offers no means of converting from the MARC8 encoding that most legacy MARC records have been encoded in to a valid XML encoding such as UTF-8. MARC libraries in other languages have worked around this basic lack of infrastructure by creating their own character encoding conversion libraries. At this time, the author of File_MARC does not have the capacity to build the same support as a PEAR package but would welcome any assistance. Better still would be the addition of ANSEL and MARC8 encoding support to the iconv and ICU toolkits that are used to supply encoding conversion by most open-source projects and languages.
  • The toXML() method currently produces a single, complete, valid XML MARCXML document for a single File_MARC_Record object. You cannot simply concatenate the results of calling toXML() on two File_MARC_Record objects, because that will produce invalid an invalid XML document. At this time, it is up to the developer to extract the record node from each MARCXML document and concatenate them inside a collection root element if they want to create a MARCXML document that contains more than a single record.

Writing MARCXML data to a file

In the following example, we have created a MARC record represented by a File_MARC_Record object stored in the $record variable. To write this data to a file in MARCXML format, we simply open the file in binary mode and write the record to the file by calling the toXML() method on the record object.

<?php
require 'File/MARC.php';

// Create a MARC record
$record = create_a_marc_record();

// Open a file for binary write access
$marcxml_file = fopen("records.mrc", "wb");

// Write the record to the file in MARCXML format
fwrite($marcxml_file, $record->toXML());

// Close the file
fclose($marcxml_file);
?>