Skip to content

Commit

Permalink
Add toXMLHeader() and toXMLFooter() methods to wrap collections appro…
Browse files Browse the repository at this point in the history
…priately

Returns the default toXML() mode to outputting a single record wrapped in a
collection element for backwards compatibility. Although it's tempting to
change that before a 1.0 release.

A reasonable example of generating a collection of MARCXML records now looks
like:

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

$xml_data = 'SOCIAL_SCIENCES_UTF_8_20100721.xml';

$books = new File_MARCXML($xml_data);
$public_note = "Available online / Disponible en ligne";

// Add the XML header and opening <collection> element
print $books->toXMLHeader();

// Iterate through the retrieved records
while ($record = $books->next()) {
    // Pretty print each record
    $urls = $record->getFields('856');
    foreach ($urls as $url) {
        // These are the actual resources, ensure indicator 2 is set accordingly
        $url->setIndicator(2, '0');

        // Prepend our proxy URL and fix the existing URL
        $u = $url->getSubfields('u');
        foreach ($u as $uri) {
            $uri->setData('http://librweb.laurentian.ca/login?url=http://' . $uri->getData());
        }

        // Set our desired public note
        $notes = $url->getSubfields('z');
        foreach ($notes as $note) {
            $note->setData($public_note);
        }

        // Some of these don't have public notes; create if necessary
        if (!count($notes)) {
            $url->appendSubfield(new File_MARC_Subfield('z', $public_note));
        }
    }

    // Generate the XML output for this record
    $record->toXML();
}
// Add the </collection> closing element and dump the XMLWriter contents
print $books->toXMLFooter();



git-svn-id: https://svn.php.net/repository/pear/packages/File_MARC/trunk@301725 c90b9560-bf6c-de11-be94-00142212c4b1
  • Loading branch information
Dan Scott committed Jul 30, 2010
1 parent 2c5bf0e commit ba7c1c1
Show file tree
Hide file tree
Showing 3 changed files with 172 additions and 40 deletions.
65 changes: 64 additions & 1 deletion MARC.php
Expand Up @@ -121,6 +121,13 @@ class File_MARC
* @var int * @var int
*/ */
protected $type; protected $type;

/**
* XMLWriter for writing collections
*
* @var XMLWriter
*/
protected $xmlwriter;
// }}} // }}}


// {{{ Constructor: function __construct() // {{{ Constructor: function __construct()
Expand All @@ -145,6 +152,9 @@ class File_MARC
*/ */
function __construct($source, $type = self::SOURCE_FILE) function __construct($source, $type = self::SOURCE_FILE)
{ {

$this->xmlwriter = new XMLWriter();
$this->xmlwriter->openMemory();
switch ($type) { switch ($type) {


case self::SOURCE_FILE: case self::SOURCE_FILE:
Expand Down Expand Up @@ -245,7 +255,7 @@ function next()
*/ */
private function _decode($text) private function _decode($text)
{ {
$marc = new File_MARC_Record(); $marc = new File_MARC_Record($this);


$matches = array(); $matches = array();
if (!preg_match("/^(\d{5})/", $text, $matches)) { if (!preg_match("/^(\d{5})/", $text, $matches)) {
Expand Down Expand Up @@ -367,6 +377,59 @@ private function _decode($text)
} }
// }}} // }}}


// {{{ 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();
}
// }}}

} }
// }}} // }}}


82 changes: 44 additions & 38 deletions MARC/Record.php
Expand Up @@ -73,6 +73,22 @@ class File_MARC_Record
* @var array * @var array
*/ */
protected $warnings; protected $warnings;

/**
* XMLWriter for writing collections
*
* @var XMLWriter
*/
protected $marcxml;

/**
* MARC instance for access to the XML header/footer methods
* We need this so that we can properly wrap a collection of MARC records.
*
* @var File_MARC
*/
protected $marc;

// }}} // }}}


// {{{ Constructor: function __construct() // {{{ Constructor: function __construct()
Expand All @@ -83,10 +99,12 @@ class File_MARC_Record
* *
* @return true * @return true
*/ */
function __construct() function __construct($marc)
{ {
$this->fields = new File_MARC_List(); $this->fields = new File_MARC_List();
$this->leader = str_repeat(' ', 24); $this->leader = str_repeat(' ', 24);
$this->marc = $marc;
$this->marcxml = $marc->getXMLWriter();
} }
// }}} // }}}


Expand Down Expand Up @@ -479,7 +497,6 @@ function __toString()
return $formatted; return $formatted;
} }
// }}} // }}}

// {{{ toXML() // {{{ toXML()
/** /**
* Return the MARC record in MARCXML format * Return the MARC record in MARCXML format
Expand All @@ -488,28 +505,21 @@ function __toString()
* attempts to adhere to the MARCXML standard documented at * attempts to adhere to the MARCXML standard documented at
* http://www.loc.gov/standards/marcxml/ * http://www.loc.gov/standards/marcxml/
* *
* @param string $encoding output encoding for the MARCXML record * @param string $encoding output encoding for the MARCXML record
* @param bool $indent pretty-print the MARCXML record * @param bool $indent pretty-print the MARCXML record
* @param bool $collection wrap the <record> element in a <collection> element * @param bool $single wrap the <record> element in a <collection> element
* *
* @return string representation of MARC record in MARCXML format * @return string representation of MARC record in MARCXML format
* *
* @todo Fix encoding input / output issues (PHP 6.0 required?) * @todo Fix encoding input / output issues (PHP 6.0 required?)
*/ */
function toXML($encoding = "UTF-8", $indent = true, $collection = true) function toXML($encoding = "UTF-8", $indent = true, $single = true)
{ {
$marcxml = new XMLWriter(); $this->marcxml->setIndent($indent);
$marcxml->openMemory(); if ($single) {
$marcxml->setIndent($indent); $this->marc->toXMLHeader();
if ($collection) {
$marcxml->startDocument("1.0", $encoding);
$marcxml->startElement("collection");
$marcxml->writeAttribute("xmlns", "http://www.loc.gov/MARC21/slim");
}
$marcxml->startElement("record");
if (!$collection) {
$marcxml->writeAttribute("xmlns", "http://www.loc.gov/MARC21/slim");
} }
$this->marcxml->startElement("record");


// MARCXML schema has some strict requirements // MARCXML schema has some strict requirements
// We'll set reasonable defaults to avoid invalid MARCXML // We'll set reasonable defaults to avoid invalid MARCXML
Expand All @@ -527,43 +537,39 @@ function toXML($encoding = "UTF-8", $indent = true, $collection = true)
$xmlLeader[6] = "a"; $xmlLeader[6] = "a";
} }


$marcxml->writeElement("leader", $xmlLeader); $this->marcxml->writeElement("leader", $xmlLeader);


foreach ($this->fields as $field) { foreach ($this->fields as $field) {
if (!$field->isEmpty()) { if (!$field->isEmpty()) {
switch(get_class($field)) { switch(get_class($field)) {
case "File_MARC_Control_Field": case "File_MARC_Control_Field":
$marcxml->startElement("controlfield"); $this->marcxml->startElement("controlfield");
$marcxml->writeAttribute("tag", $field->getTag()); $this->marcxml->writeAttribute("tag", $field->getTag());
$marcxml->text($field->getData()); $this->marcxml->text($field->getData());
$marcxml->endElement(); // end control field $this->marcxml->endElement(); // end control field
break; break;


case "File_MARC_Data_Field": case "File_MARC_Data_Field":
$marcxml->startElement("datafield"); $this->marcxml->startElement("datafield");
$marcxml->writeAttribute("tag", $field->getTag()); $this->marcxml->writeAttribute("tag", $field->getTag());
$marcxml->writeAttribute("ind1", $field->getIndicator(1)); $this->marcxml->writeAttribute("ind1", $field->getIndicator(1));
$marcxml->writeAttribute("ind2", $field->getIndicator(2)); $this->marcxml->writeAttribute("ind2", $field->getIndicator(2));
foreach ($field->getSubfields() as $subfield) { foreach ($field->getSubfields() as $subfield) {
$marcxml->startElement("subfield"); $this->marcxml->startElement("subfield");
$marcxml->writeAttribute("code", $subfield->getCode()); $this->marcxml->writeAttribute("code", $subfield->getCode());
$marcxml->text($subfield->getData()); $this->marcxml->text($subfield->getData());
$marcxml->endElement(); // end subfield $this->marcxml->endElement(); // end subfield
} }
$marcxml->endElement(); // end data field $this->marcxml->endElement(); // end data field
break; break;
} }
} }
} }


$marcxml->endElement(); // end record $this->marcxml->endElement(); // end record
if ($collection) { if ($single) {
$marcxml->endElement(); // end collection return $this->marc->toXMLFooter();
$marcxml->endDocument();
} }

return $marcxml->outputMemory();

} }


// }}} // }}}
Expand Down
65 changes: 64 additions & 1 deletion MARCXML.php
Expand Up @@ -97,6 +97,13 @@ class File_MARCXML
* @var int * @var int
*/ */
protected $counter; protected $counter;

/**
* XMLWriter for writing collections
*
* @var XMLWriter
*/
protected $xmlwriter;
// }}} // }}}


// {{{ Constructor: function __construct() // {{{ Constructor: function __construct()
Expand All @@ -123,6 +130,9 @@ function __construct($source, $type = self::SOURCE_FILE)
{ {
$this->counter = 0; $this->counter = 0;


$this->xmlwriter = new XMLWriter();
$this->xmlwriter->openMemory();

switch ($type) { switch ($type) {


case self::SOURCE_FILE: case self::SOURCE_FILE:
Expand Down Expand Up @@ -198,7 +208,7 @@ function next()
*/ */
private function _decode($text) private function _decode($text)
{ {
$marc = new File_MARC_Record(); $marc = new File_MARC_Record($this);


// Store leader // Store leader
$marc->setLeader($text->leader); $marc->setLeader($text->leader);
Expand Down Expand Up @@ -228,6 +238,59 @@ private function _decode($text)
} }
// }}} // }}}


// {{{ 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();
$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();
}
// }}}

} }
// }}} // }}}


0 comments on commit ba7c1c1

Please sign in to comment.