pid = $pid;
$this->dsid = $dsid;
if ($xmlStr !== NULL) {
if (is_object($xmlStr) && get_class($xmlStr) == DOMDocument) {
$this->xml = $xmlStr;
}
else {
$this->xml = new DOMDocument();
$this->xml->loadXML($xmlStr);
}
}
}
/**
* Gets the identifier for this XMLDatastream
* Returns FALSE on failure.
*
* NOTE: not available if constructed directly from file.
*
* @return string identifier
*/
public function getIdentifier() {
return ($this->pid != NULL && $this->dsid != NULL) ? $this->pid . '/' . $this->dsid : FALSE;
}
/**
* Dumps the XMLDatastream as an XML String
*
*
* @return string xml
*/
public function dumpXml() {
if ($this->xml == NULL) {
$this->fetchXml();
}
return $this->xml->saveXml();
}
/**
* Validates the XMLDatastream against the schema location
* defined by the xmlns:schemaLocation attribute of the root
* element. If the xmlns:schemaLocation attribute does not exist,
* then it is assumed to be the old schema and it attempts to convert
* using the convertFromOldSchema method.
*
* TODO: Maybe change it so that it always validates against a known
* schema. This makes more sense because this class assumes the structure
* to be known after it has been validated.
*
* @return boolean $valid
*/
public function validate() {
global $user;
if ($this->valid === NULL) {
$ret = TRUE;
if ($this->xml == NULL) {
$this->fetchXml();
}
// figure out if we're dealing with a new or old schema
$rootEl = $this->xml->firstChild;
if (!$rootEl->hasAttributes() || $rootEl->attributes->getNamedItem('schemaLocation') === NULL) {
//$tmpname = substr($this->pid, strpos($this->pid, ':') + 1);
$tmpname = user_password(10);
$this->convertFromOldSchema();
drupal_add_js("islandora_repository_print_new_schema_$tmpname = function(tagID) {
var target = document.getElementById(tagID);
var content = target.innerHTML;
var text = '
Title' +
'' + content +'';
printerWindow = window.open('', '', 'toolbar=no,location=no,' + 'status=no,menu=no,scrollbars=yes,width=650,height=400');
printerWindow.document.open();
printerWindow.document.write(text);
}", 'inline');
if (user_access('administer site configuration')) {
drupal_set_message('' . htmlentities($this->xml->saveXML()) . 'Warning: XMLDatastream performed conversion of \'' . $this->getIdentifier() . '\' from old schema. Please update the datastream. The new datastream contents are here. ');
}
$rootEl = $this->xml->firstChild;
}
$schemaLocation = NULL;
if ($this->forceSchema) {
// hack because you cant easily get the static property value from
// a subclass.
$vars = get_class_vars(get_class($this));
$schemaLocation = $vars['SCHEMA_URI'];
}
elseif ($rootEl->attributes->getNamedItem('schemaLocation') !== NULL) {
//figure out where the schema is located and validate.
list(, $schemaLocation) = preg_split('/\s+/', $rootEl->attributes->getNamedItem('schemaLocation')->nodeValue);
}
$schemaLocation = NULL;
return TRUE;
if ($schemaLocation !== NULL) {
if (!$this->xml->schemaValidate($schemaLocation)) {
$ret = FALSE;
$errors = libxml_get_errors();
foreach ($errors as $err) {
self::$errors[] = 'XML Error: Line ' . $err->line . ': ' . $err->message;
}
}
else {
$this->name = $rootEl->attributes->getNamedItem('name')->nodeValue;
}
}
else {
$ret = FALSE;
self::$errors[] = 'Unable to load schema.';
}
$this->valid = $ret;
}
return $this->valid;
}
/**
* Saves the current XML datastream back to fedora. The XML must validate.
*
* @return boolean $success
*/
public function saveToFedora() {
module_load_include('inc', 'Fedora_Repository', 'api/fedora_item');
if ($this->validate()) {
$item = new Fedora_Item($this->pid);
$item->modify_datastream_by_value($this->dumpXml(), $this->dsid, $this->name, 'application/xml');
return TRUE;
}
return FALSE;
}
/**
* Purges veersions of the datastream newer than and including the start_date. If
* End date is specified, it can be used to purge a range of versions instead. Date should be in
* DATE_RFC822 format
*
* @param string $start_date
* @param string $end_date
* @return boolean $valid
*/
public function purgeVersions($start_date, $end_date = NULL) {
module_load_include('inc', 'islandora_repository', 'api/fedora_item');
$fedora_item = new Fedora_Item($this->pid);
return $fedora_item->purge_datastream($this->dsid, $start_date, $end_date);
}
/**
* Gets the history of the datastream from fedora.
* Returns false on failure.
*
* @return string[] $ret
*/
public function getHistory() {
module_load_include('inc', 'islandora_repository', 'api/fedora_item');
$fedora_item = new Fedora_Item($this->pid);
$history = $fedora_item->get_datastream_history($this->dsid);
$ret = FALSE;
if ($history !== FALSE) {
$ret = array();
foreach ($history as $version) {
if ($version->versionID !== NULL)
$ret[] = array('versionID' => $version->versionID, 'createDate' => $version->createDate);
}
}
return $ret;
}
/**
* Attempts to convert from the old XML schema to the new by
* traversing the XML DOM and building a new DOM. When done
* $this->xml is replaced by the newly created DOM..
*
* @return void
*/
abstract protected function convertFromOldSchema();
}