From 24ffc477ec0cb44f889940ad3fdfe67c8aa090bf Mon Sep 17 00:00:00 2001 From: Adam Vessey Date: Tue, 4 Sep 2012 10:21:14 -0300 Subject: [PATCH] Fix errors being reported during purge relationships If the RELS-{IN,EX}T file didn't exist, we would encounter errors. Now, we check that there's an actual file there before attempting to act on it. --- api/fedora_item.inc | 107 ++++++++++++++++++++++---------------------- 1 file changed, 54 insertions(+), 53 deletions(-) diff --git a/api/fedora_item.inc b/api/fedora_item.inc index 20a48cc0..e78ea88c 100644 --- a/api/fedora_item.inc +++ b/api/fedora_item.inc @@ -389,34 +389,35 @@ RDF; * Whether or not this operation has produced any changes in the RELS-EXT */ function purge_relationships($relationship, $object, $namespaceURI = RELS_EXT_URI, $literal_value = FALSE) { - $relsext = $this->get_datastream_dissemination('RELS-EXT'); + $relsext = trim($this->get_datastream_dissemination('RELS-EXT')); - $relsextxml = new DomDocument(); - $relsextxml->loadXML($relsext); + $relsextxml = new DOMDocument(); $modified = FALSE; - $rels = $relsextxml->getElementsByTagNameNS($namespaceURI, $relationship); - if (!empty($rels)) { - // iterate backwards so if we delete something our pointer doesn't get out of sync - for ($i = $rels->length; $i > 0; $i--) { - $rel = $rels->item($i - 1); -// foreach ($rels as $rel) { // moving forward like this caused iteration errors when something was deleted - if (//If either no object is specified, or the object matches (in either the literal or URI case), remove this node from it's parent, and mark as changed. - empty($object) || - (($literal_value == RELS_TYPE_URI) && $rel->getAttributeNS('http://www.w3.org/1999/02/22-rdf-syntax-ns#', 'resource') == $object) || - (($literal_value == RELS_TYPE_PLAIN_LITERAL) && $rel->textContent == $object) || - (($literal_value == RELS_TYPE_STRING) && $rel->getAttribute('rdf:datatype') == 'http://www.w3.org/2001/XMLSchema#string' && $rel->textContent == $object) || - (($literal_value == RELS_TYPE_INT) && $rel->getAttribute('rdf:datatype') == 'http://www.w3.org/2001/XMLSchema#int' && intval($rel->textContent) == $object)) { - $rel->parentNode->removeChild($rel); - $modified = TRUE; + + if ($relsext && $relsextxml->loadXML($relsext)) { + $rels = $relsextxml->getElementsByTagNameNS($namespaceURI, $relationship); + if (!empty($rels)) { + // iterate backwards so if we delete something our pointer doesn't get out of sync + for ($i = $rels->length; $i > 0; $i--) { + $rel = $rels->item($i - 1); + // foreach ($rels as $rel) { // moving forward like this caused iteration errors when something was deleted + if (//If either no object is specified, or the object matches (in either the literal or URI case), remove this node from it's parent, and mark as changed. + empty($object) || + (($literal_value == RELS_TYPE_URI) && $rel->getAttributeNS('http://www.w3.org/1999/02/22-rdf-syntax-ns#', 'resource') == $object) || + (($literal_value == RELS_TYPE_PLAIN_LITERAL) && $rel->textContent == $object) || + (($literal_value == RELS_TYPE_STRING) && $rel->getAttribute('rdf:datatype') == 'http://www.w3.org/2001/XMLSchema#string' && $rel->textContent == $object) || + (($literal_value == RELS_TYPE_INT) && $rel->getAttribute('rdf:datatype') == 'http://www.w3.org/2001/XMLSchema#int' && intval($rel->textContent) == $object)) { + $rel->parentNode->removeChild($rel); + $modified = TRUE; + } } } - } - //Save changes. - if ($modified) { - $this->modify_datastream($relsextxml->saveXML(), 'RELS-EXT', "Fedora Object-to-Object Relationship Metadata", 'text/xml'); + //Save changes. + if ($modified) { + $this->modify_datastream($relsextxml->saveXML(), 'RELS-EXT', "Fedora Object-to-Object Relationship Metadata", 'text/xml'); + } } - //Return whether or not we've introduced any changes. return $modified; } @@ -433,47 +434,47 @@ RDF; $dsid = $f_prefix . $this->pid . '/' . $dsid; } - $relsint = $this->get_datastream_dissemination('RELS-INT'); - $relsxml = new DOMDocument(); - $relsxml->loadXML($relsint); + $relsint = trim($this->get_datastream_dissemination('RELS-INT')); $modified = FALSE; - - $descs = $relsxml->getElementsByTagNameNS('http://www.w3.org/1999/02/22-rdf-syntax-ns#', 'Description'); - $description = NULL; - foreach ($descs as $desc) { - if ($dsid === NULL || $desc->getAttributeNS('http://www.w3.org/1999/02/22-rdf-syntax-ns#', 'about') == $dsid) { - $rels = $desc->getElementsByTagNameNS($namespaceURI, $relationship); - if (!empty($rels)) { - // iterate backwards so if we delete something our pointer doesn't get out of sync - for ($i = $rels->length; $i>0; $i--) { - $rel = $rels->item($i-1); - // foreach ($rels as $rel) { // moving forward like this caused iteration errors when something was deleted - if ( //If either no object is specified, or the object matches (in either the literal or URI case), remove this node from it's parent, and mark as changed. - empty($object) || - (($literal_value == RELS_TYPE_URI) && $rel->getAttributeNS('http://www.w3.org/1999/02/22-rdf-syntax-ns#', 'resource') == $object) || - (($literal_value == RELS_TYPE_PLAIN_LITERAL) && $rel->textContent == $object) || - (($literal_value == RELS_TYPE_STRING) && $rel->getAttribute('rdf:datatype') == 'http://www.w3.org/2001/XMLSchema#string' && $rel->textContent == $object) || - (($literal_value == RELS_TYPE_INT) && $rel->getAttribute('rdf:datatype') == 'http://www.w3.org/2001/XMLSchema#int' && intval($rel->textContent) == $object)) { - $rel->parentNode->removeChild($rel); - $modified = TRUE; + if ($relsint && $relsxml->loadXML($relsint)) { + + + $descs = $relsxml->getElementsByTagNameNS('http://www.w3.org/1999/02/22-rdf-syntax-ns#', 'Description'); + $description = NULL; + foreach ($descs as $desc) { + if ($dsid === NULL || $desc->getAttributeNS('http://www.w3.org/1999/02/22-rdf-syntax-ns#', 'about') == $dsid) { + $rels = $desc->getElementsByTagNameNS($namespaceURI, $relationship); + if (!empty($rels)) { + // iterate backwards so if we delete something our pointer doesn't get out of sync + for ($i = $rels->length; $i>0; $i--) { + $rel = $rels->item($i-1); + // foreach ($rels as $rel) { // moving forward like this caused iteration errors when something was deleted + if ( //If either no object is specified, or the object matches (in either the literal or URI case), remove this node from it's parent, and mark as changed. + empty($object) || + (($literal_value == RELS_TYPE_URI) && $rel->getAttributeNS('http://www.w3.org/1999/02/22-rdf-syntax-ns#', 'resource') == $object) || + (($literal_value == RELS_TYPE_PLAIN_LITERAL) && $rel->textContent == $object) || + (($literal_value == RELS_TYPE_STRING) && $rel->getAttribute('rdf:datatype') == 'http://www.w3.org/2001/XMLSchema#string' && $rel->textContent == $object) || + (($literal_value == RELS_TYPE_INT) && $rel->getAttribute('rdf:datatype') == 'http://www.w3.org/2001/XMLSchema#int' && intval($rel->textContent) == $object)) { + $rel->parentNode->removeChild($rel); + $modified = TRUE; + } } } - } - if ($desc->getAttributeNS('http://www.w3.org/1999/02/22-rdf-syntax-ns#', 'about') == $dsid) { - break; + if ($desc->getAttributeNS('http://www.w3.org/1999/02/22-rdf-syntax-ns#', 'about') == $dsid) { + break; + } } } - } - - //Save changes. - if ($modified) { - $this->modify_datastream($relsxml->saveXML(), 'RELS-INT', "Fedora Datastream Relationship Metadata", 'text/xml'); + //Save changes. + if ($modified) { + $this->modify_datastream($relsxml->saveXML(), 'RELS-INT', "Fedora Datastream Relationship Metadata", 'text/xml'); + } } - //Return whether or not we've introduced any changes. + //Return whether or not we've introduced any changes. return $modified; }