Skip to content
Browse files

Add toXMLHeader() and toXMLFooter() methods to wrap collections appro…

…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...
1 parent 2c5bf0e commit ba7c1c149863a963f2d10429cc38b993397fc06e Dan Scott committed
Showing with 172 additions and 40 deletions.
  1. +64 −1 MARC.php
  2. +44 −38 MARC/Record.php
  3. +64 −1 MARCXML.php
View
65 MARC.php
@@ -121,6 +121,13 @@ class File_MARC
* @var int
*/
protected $type;
+
+ /**
+ * XMLWriter for writing collections
+ *
+ * @var XMLWriter
+ */
+ protected $xmlwriter;
// }}}
// {{{ Constructor: function __construct()
@@ -145,6 +152,9 @@ class File_MARC
*/
function __construct($source, $type = self::SOURCE_FILE)
{
+
+ $this->xmlwriter = new XMLWriter();
+ $this->xmlwriter->openMemory();
switch ($type) {
case self::SOURCE_FILE:
@@ -245,7 +255,7 @@ function next()
*/
private function _decode($text)
{
- $marc = new File_MARC_Record();
+ $marc = new File_MARC_Record($this);
$matches = array();
if (!preg_match("/^(\d{5})/", $text, $matches)) {
@@ -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();
+ }
+ // }}}
+
}
// }}}
View
82 MARC/Record.php
@@ -73,6 +73,22 @@ class File_MARC_Record
* @var array
*/
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()
@@ -83,10 +99,12 @@ class File_MARC_Record
*
* @return true
*/
- function __construct()
+ function __construct($marc)
{
$this->fields = new File_MARC_List();
$this->leader = str_repeat(' ', 24);
+ $this->marc = $marc;
+ $this->marcxml = $marc->getXMLWriter();
}
// }}}
@@ -479,7 +497,6 @@ function __toString()
return $formatted;
}
// }}}
-
// {{{ toXML()
/**
* Return the MARC record in MARCXML format
@@ -488,28 +505,21 @@ function __toString()
* attempts to adhere to the MARCXML standard documented at
* http://www.loc.gov/standards/marcxml/
*
- * @param string $encoding output encoding for the MARCXML record
- * @param bool $indent pretty-print the MARCXML record
- * @param bool $collection wrap the <record> element in a <collection> element
+ * @param string $encoding output encoding for the MARCXML record
+ * @param bool $indent pretty-print the MARCXML record
+ * @param bool $single wrap the <record> element in a <collection> element
*
* @return string representation of MARC record in MARCXML format
*
* @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();
- $marcxml->openMemory();
- $marcxml->setIndent($indent);
- 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->setIndent($indent);
+ if ($single) {
+ $this->marc->toXMLHeader();
}
+ $this->marcxml->startElement("record");
// MARCXML schema has some strict requirements
// We'll set reasonable defaults to avoid invalid MARCXML
@@ -527,43 +537,39 @@ function toXML($encoding = "UTF-8", $indent = true, $collection = true)
$xmlLeader[6] = "a";
}
- $marcxml->writeElement("leader", $xmlLeader);
+ $this->marcxml->writeElement("leader", $xmlLeader);
foreach ($this->fields as $field) {
if (!$field->isEmpty()) {
switch(get_class($field)) {
case "File_MARC_Control_Field":
- $marcxml->startElement("controlfield");
- $marcxml->writeAttribute("tag", $field->getTag());
- $marcxml->text($field->getData());
- $marcxml->endElement(); // end control field
+ $this->marcxml->startElement("controlfield");
+ $this->marcxml->writeAttribute("tag", $field->getTag());
+ $this->marcxml->text($field->getData());
+ $this->marcxml->endElement(); // end control field
break;
case "File_MARC_Data_Field":
- $marcxml->startElement("datafield");
- $marcxml->writeAttribute("tag", $field->getTag());
- $marcxml->writeAttribute("ind1", $field->getIndicator(1));
- $marcxml->writeAttribute("ind2", $field->getIndicator(2));
+ $this->marcxml->startElement("datafield");
+ $this->marcxml->writeAttribute("tag", $field->getTag());
+ $this->marcxml->writeAttribute("ind1", $field->getIndicator(1));
+ $this->marcxml->writeAttribute("ind2", $field->getIndicator(2));
foreach ($field->getSubfields() as $subfield) {
- $marcxml->startElement("subfield");
- $marcxml->writeAttribute("code", $subfield->getCode());
- $marcxml->text($subfield->getData());
- $marcxml->endElement(); // end subfield
+ $this->marcxml->startElement("subfield");
+ $this->marcxml->writeAttribute("code", $subfield->getCode());
+ $this->marcxml->text($subfield->getData());
+ $this->marcxml->endElement(); // end subfield
}
- $marcxml->endElement(); // end data field
+ $this->marcxml->endElement(); // end data field
break;
}
}
}
- $marcxml->endElement(); // end record
- if ($collection) {
- $marcxml->endElement(); // end collection
- $marcxml->endDocument();
+ $this->marcxml->endElement(); // end record
+ if ($single) {
+ return $this->marc->toXMLFooter();
}
-
- return $marcxml->outputMemory();
-
}
// }}}
View
65 MARCXML.php
@@ -97,6 +97,13 @@ class File_MARCXML
* @var int
*/
protected $counter;
+
+ /**
+ * XMLWriter for writing collections
+ *
+ * @var XMLWriter
+ */
+ protected $xmlwriter;
// }}}
// {{{ Constructor: function __construct()
@@ -123,6 +130,9 @@ function __construct($source, $type = self::SOURCE_FILE)
{
$this->counter = 0;
+ $this->xmlwriter = new XMLWriter();
+ $this->xmlwriter->openMemory();
+
switch ($type) {
case self::SOURCE_FILE:
@@ -198,7 +208,7 @@ function next()
*/
private function _decode($text)
{
- $marc = new File_MARC_Record();
+ $marc = new File_MARC_Record($this);
// Store leader
$marc->setLeader($text->leader);
@@ -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.
Something went wrong with that request. Please try again.