Browse Source

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.
pull/161/head
Adam Vessey 12 years ago
parent
commit
24ffc477ec
  1. 107
      api/fedora_item.inc

107
api/fedora_item.inc

@ -389,34 +389,35 @@ RDF;
* Whether or not this operation has produced any changes in the RELS-EXT * 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) { 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 = new DOMDocument();
$relsextxml->loadXML($relsext);
$modified = FALSE; $modified = FALSE;
$rels = $relsextxml->getElementsByTagNameNS($namespaceURI, $relationship);
if (!empty($rels)) { if ($relsext && $relsextxml->loadXML($relsext)) {
// iterate backwards so if we delete something our pointer doesn't get out of sync $rels = $relsextxml->getElementsByTagNameNS($namespaceURI, $relationship);
for ($i = $rels->length; $i > 0; $i--) { if (!empty($rels)) {
$rel = $rels->item($i - 1); // iterate backwards so if we delete something our pointer doesn't get out of sync
// foreach ($rels as $rel) { // moving forward like this caused iteration errors when something was deleted for ($i = $rels->length; $i > 0; $i--) {
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. $rel = $rels->item($i - 1);
empty($object) || // foreach ($rels as $rel) { // moving forward like this caused iteration errors when something was deleted
(($literal_value == RELS_TYPE_URI) && $rel->getAttributeNS('http://www.w3.org/1999/02/22-rdf-syntax-ns#', 'resource') == $object) || 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.
(($literal_value == RELS_TYPE_PLAIN_LITERAL) && $rel->textContent == $object) || empty($object) ||
(($literal_value == RELS_TYPE_STRING) && $rel->getAttribute('rdf:datatype') == 'http://www.w3.org/2001/XMLSchema#string' && $rel->textContent == $object) || (($literal_value == RELS_TYPE_URI) && $rel->getAttributeNS('http://www.w3.org/1999/02/22-rdf-syntax-ns#', 'resource') == $object) ||
(($literal_value == RELS_TYPE_INT) && $rel->getAttribute('rdf:datatype') == 'http://www.w3.org/2001/XMLSchema#int' && intval($rel->textContent) == $object)) { (($literal_value == RELS_TYPE_PLAIN_LITERAL) && $rel->textContent == $object) ||
$rel->parentNode->removeChild($rel); (($literal_value == RELS_TYPE_STRING) && $rel->getAttribute('rdf:datatype') == 'http://www.w3.org/2001/XMLSchema#string' && $rel->textContent == $object) ||
$modified = TRUE; (($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. //Save changes.
if ($modified) { if ($modified) {
$this->modify_datastream($relsextxml->saveXML(), 'RELS-EXT', "Fedora Object-to-Object Relationship Metadata", 'text/xml'); $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 whether or not we've introduced any changes.
return $modified; return $modified;
} }
@ -433,47 +434,47 @@ RDF;
$dsid = $f_prefix . $this->pid . '/' . $dsid; $dsid = $f_prefix . $this->pid . '/' . $dsid;
} }
$relsint = $this->get_datastream_dissemination('RELS-INT');
$relsxml = new DOMDocument(); $relsxml = new DOMDocument();
$relsxml->loadXML($relsint); $relsint = trim($this->get_datastream_dissemination('RELS-INT'));
$modified = FALSE; $modified = FALSE;
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) { $descs = $relsxml->getElementsByTagNameNS('http://www.w3.org/1999/02/22-rdf-syntax-ns#', 'Description');
if ($dsid === NULL || $desc->getAttributeNS('http://www.w3.org/1999/02/22-rdf-syntax-ns#', 'about') == $dsid) { $description = NULL;
$rels = $desc->getElementsByTagNameNS($namespaceURI, $relationship); foreach ($descs as $desc) {
if (!empty($rels)) { if ($dsid === NULL || $desc->getAttributeNS('http://www.w3.org/1999/02/22-rdf-syntax-ns#', 'about') == $dsid) {
// iterate backwards so if we delete something our pointer doesn't get out of sync $rels = $desc->getElementsByTagNameNS($namespaceURI, $relationship);
for ($i = $rels->length; $i>0; $i--) { if (!empty($rels)) {
$rel = $rels->item($i-1); // iterate backwards so if we delete something our pointer doesn't get out of sync
// foreach ($rels as $rel) { // moving forward like this caused iteration errors when something was deleted for ($i = $rels->length; $i>0; $i--) {
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. $rel = $rels->item($i-1);
empty($object) || // foreach ($rels as $rel) { // moving forward like this caused iteration errors when something was deleted
(($literal_value == RELS_TYPE_URI) && $rel->getAttributeNS('http://www.w3.org/1999/02/22-rdf-syntax-ns#', 'resource') == $object) || 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.
(($literal_value == RELS_TYPE_PLAIN_LITERAL) && $rel->textContent == $object) || empty($object) ||
(($literal_value == RELS_TYPE_STRING) && $rel->getAttribute('rdf:datatype') == 'http://www.w3.org/2001/XMLSchema#string' && $rel->textContent == $object) || (($literal_value == RELS_TYPE_URI) && $rel->getAttributeNS('http://www.w3.org/1999/02/22-rdf-syntax-ns#', 'resource') == $object) ||
(($literal_value == RELS_TYPE_INT) && $rel->getAttribute('rdf:datatype') == 'http://www.w3.org/2001/XMLSchema#int' && intval($rel->textContent) == $object)) { (($literal_value == RELS_TYPE_PLAIN_LITERAL) && $rel->textContent == $object) ||
$rel->parentNode->removeChild($rel); (($literal_value == RELS_TYPE_STRING) && $rel->getAttribute('rdf:datatype') == 'http://www.w3.org/2001/XMLSchema#string' && $rel->textContent == $object) ||
$modified = TRUE; (($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) { if ($desc->getAttributeNS('http://www.w3.org/1999/02/22-rdf-syntax-ns#', 'about') == $dsid) {
break; break;
}
} }
} }
}
//Save changes.
//Save changes. if ($modified) {
if ($modified) { $this->modify_datastream($relsxml->saveXML(), 'RELS-INT', "Fedora Datastream Relationship Metadata", 'text/xml');
$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; return $modified;
} }

Loading…
Cancel
Save