Browse Source

Added wrapper to validation message - Now only seen by users with administer site configuration permission.

pull/105/head
Alan Stanley 14 years ago
parent
commit
c528165dfd
  1. 113
      XMLDatastream.inc

113
XMLDatastream.inc

@ -6,17 +6,13 @@ abstract class XMLDatastream {
static $XMLNS = 'http://www.islandora.ca'; static $XMLNS = 'http://www.islandora.ca';
static $errors = NULL; static $errors = NULL;
protected $xml = NULL; protected $xml = NULL;
private $valid = NULL; private $valid = NULL;
protected $forceSchema = FALSE; // if set, the datastream will be validated against the specified schema in self::$SCHEMA_URI instead of protected $forceSchema = FALSE; // if set, the datastream will be validated against the specified schema in self::$SCHEMA_URI instead of
// reading the schema URI from the datastream. // reading the schema URI from the datastream.
public $pid; public $pid;
public $dsid; public $dsid;
/** /**
* Parses an PID from an identifier. * Parses an PID from an identifier.
* @param string $identifier * @param string $identifier
@ -26,7 +22,6 @@ abstract class XMLDatastream {
return substr($identifier, 0, strpos($identifier, "/")); return substr($identifier, 0, strpos($identifier, "/"));
} }
/** /**
* validPid * validPid
* Validates a fedora PID based on the regexp provided in the fedora * Validates a fedora PID based on the regexp provided in the fedora
@ -45,7 +40,7 @@ abstract class XMLDatastream {
return $valid; return $valid;
} }
/** /**
* validDsid * validDsid
* Validates a fedora Dsid based on the the allowed XML standard NCName. * Validates a fedora Dsid based on the the allowed XML standard NCName.
* The regexp is a "regular" subset of names allowed, it excludes some extended hex characters that are * The regexp is a "regular" subset of names allowed, it excludes some extended hex characters that are
@ -64,8 +59,6 @@ abstract class XMLDatastream {
return $valid; return $valid;
} }
/** /**
* Parses the DSID from an identifier. * Parses the DSID from an identifier.
* TODO: combine this method with getPidFromIdentifier? * TODO: combine this method with getPidFromIdentifier?
@ -77,7 +70,6 @@ abstract class XMLDatastream {
return substr($temp, 1); return substr($temp, 1);
} }
/** /**
* Constructs an XMLDatastream object from the XML file specified. * Constructs an XMLDatastream object from the XML file specified.
* Returns FALSE on failure. * Returns FALSE on failure.
@ -89,7 +81,6 @@ abstract class XMLDatastream {
return new self(file_get_contents($filename)); return new self(file_get_contents($filename));
} }
/** /**
* Constructor * Constructor
* NOTE: Use the static constructor methods whenever possible. * NOTE: Use the static constructor methods whenever possible.
@ -100,16 +91,15 @@ abstract class XMLDatastream {
* @return XMLDatastream $cm * @return XMLDatastream $cm
*/ */
public function __construct($xmlStr, $pid = NULL, $dsid = NULL) { public function __construct($xmlStr, $pid = NULL, $dsid = NULL) {
libxml_use_internal_errors(true); libxml_use_internal_errors(true);
$this->pid=$pid; $this->pid = $pid;
$this->dsid=$dsid; $this->dsid = $dsid;
if ($xmlStr !== NULL) { if ($xmlStr !== NULL) {
$this->xml = (is_object($xmlStr) && get_class($xmlStr) == DOMDocument)?$xmlStr : DOMDocument::loadXML($xmlStr); $this->xml = (is_object($xmlStr) && get_class($xmlStr) == DOMDocument) ? $xmlStr : DOMDocument::loadXML($xmlStr);
} }
} }
/** /**
* Gets the identifier for this XMLDatastream * Gets the identifier for this XMLDatastream
* Returns FALSE on failure. * Returns FALSE on failure.
@ -119,10 +109,9 @@ abstract class XMLDatastream {
* @return string identifier * @return string identifier
*/ */
public function getIdentifier() { public function getIdentifier() {
return ($this->pid != NULL && $this->dsid != NULL) ? $this->pid . '/'. $this->dsid : FALSE; return ($this->pid != NULL && $this->dsid != NULL) ? $this->pid . '/' . $this->dsid : FALSE;
} }
/** /**
* Dumps the XMLDatastream as an XML String * Dumps the XMLDatastream as an XML String
* *
@ -136,7 +125,6 @@ abstract class XMLDatastream {
return $this->xml->saveXml(); return $this->xml->saveXml();
} }
/** /**
* Validates the XMLDatastream against the schema location * Validates the XMLDatastream against the schema location
* defined by the xmlns:schemaLocation attribute of the root * defined by the xmlns:schemaLocation attribute of the root
@ -151,29 +139,33 @@ abstract class XMLDatastream {
* @return boolean $valid * @return boolean $valid
*/ */
public function validate() { public function validate() {
global $user;
if ($this->valid === NULL) { if ($this->valid === NULL) {
$ret = TRUE; $ret = TRUE;
if ($this->xml == NULL) { if ($this->xml == NULL) {
$this->fetchXml(); $this->fetchXml();
} }
// figure out if we're dealing with a new or old schema // figure out if we're dealing with a new or old schema
$rootEl = $this->xml->firstChild; $rootEl = $this->xml->firstChild;
if (!$rootEl->hasAttributes() || $rootEl->attributes->getNamedItem('schemaLocation') === NULL ) { if (!$rootEl->hasAttributes() || $rootEl->attributes->getNamedItem('schemaLocation') === NULL) {
//$tmpname = substr($this->pid, strpos($this->pid, ':') + 1); //$tmpname = substr($this->pid, strpos($this->pid, ':') + 1);
$tmpname = user_password(10); $tmpname = user_password(10);
$this->convertFromOldSchema(); $this->convertFromOldSchema();
drupal_add_js("fedora_repository_print_new_schema_$tmpname = function(tagID) { drupal_add_js("fedora_repository_print_new_schema_$tmpname = function(tagID) {
var target = document.getElementById(tagID); var target = document.getElementById(tagID);
var content = target.innerHTML; var content = target.innerHTML;
var text = '<html><head><title>Title' + var text = '<html><head><title>Title' +
'</title><body>' + content +'</body></html>'; '</title><body>' + content +'</body></html>';
printerWindow = window.open('', '', 'toolbar=no,location=no,' + 'status=no,menu=no,scrollbars=yes,width=650,height=400'); printerWindow = window.open('', '', 'toolbar=no,location=no,' + 'status=no,menu=no,scrollbars=yes,width=650,height=400');
printerWindow.document.open(); printerWindow.document.open();
printerWindow.document.write(text); printerWindow.document.write(text);
} ", 'inline'); }", 'inline');
if (user_access('administer site configuration')) {
drupal_set_message('<span id="new_schema_' . $tmpname . '" style="display: none;">' . htmlentities($this->xml->saveXML()) . '</span>Warning: XMLDatastream performed conversion of \'' . $this->getIdentifier() . '\' from old schema. Please update the datastream. The new datastream contents are <a href="javascript:fedora_repository_print_new_schema_' . $tmpname . '(\'new_schema_' . $tmpname . '\')">here.</a> ');
}
drupal_set_message('<span id="new_schema_'. $tmpname .'" style="display: none;">'. htmlentities($this->xml->saveXML()) .'</span>Warning: XMLDatastream performed conversion of \''. $this->getIdentifier() .'\' from old schema. Please update the datastream. The new datastream contents are <a href="javascript:fedora_repository_print_new_schema_'. $tmpname .'(\'new_schema_'. $tmpname .'\')">here.</a> '); $rootEl = $this->xml->firstChild;
$rootEl = $this->xml->firstChild;
} }
$schemaLocation = NULL; $schemaLocation = NULL;
@ -182,31 +174,23 @@ printerWindow.document.write(text);
// a subclass. // a subclass.
$vars = get_class_vars(get_class($this)); $vars = get_class_vars(get_class($this));
$schemaLocation = $vars['SCHEMA_URI']; $schemaLocation = $vars['SCHEMA_URI'];
} elseif ($rootEl->attributes->getNamedItem('schemaLocation') !== NULL) {
}
elseif ($rootEl->attributes->getNamedItem('schemaLocation') !== NULL) {
//figure out where the schema is located and validate. //figure out where the schema is located and validate.
list(, $schemaLocation) = preg_split('/\s+/', $rootEl->attributes->getNamedItem('schemaLocation')->nodeValue); list(, $schemaLocation) = preg_split('/\s+/', $rootEl->attributes->getNamedItem('schemaLocation')->nodeValue);
$name = $rootEl->getAttribute('name');
if ($name !== FALSE) {
$this->name = $name;
}
} }
$schemaLocation = NULL; $schemaLocation = NULL;
return TRUE; return TRUE;
if ($schemaLocation !== NULL) { if ($schemaLocation !== NULL) {
if (!$this->xml->schemaValidate($schemaLocation)) { if (!$this->xml->schemaValidate($schemaLocation)) {
$ret = FALSE; $ret = FALSE;
$errors=libxml_get_errors(); $errors = libxml_get_errors();
foreach ($errors as $err) { foreach ($errors as $err) {
self::$errors[] = 'XML Error: Line '. $err->line .': '. $err->message; self::$errors[] = 'XML Error: Line ' . $err->line . ': ' . $err->message;
} }
} else {
$this->name = $rootEl->attributes->getNamedItem('name')->nodeValue;
} }
else { } else {
$this->name=$rootEl->attributes->getNamedItem('name')->nodeValue;
}
}
else {
$ret = FALSE; $ret = FALSE;
self::$errors[] = 'Unable to load schema.'; self::$errors[] = 'Unable to load schema.';
} }
@ -217,7 +201,6 @@ printerWindow.document.write(text);
return $this->valid; return $this->valid;
} }
/** /**
* Saves the current XML datastream back to fedora. The XML must validate. * Saves the current XML datastream back to fedora. The XML must validate.
* *
@ -243,9 +226,9 @@ printerWindow.document.write(text);
* @return boolean $valid * @return boolean $valid
*/ */
public function purgeVersions($start_date, $end_date = NULL) { public function purgeVersions($start_date, $end_date = NULL) {
module_load_include('inc', 'fedora_repository', 'api/fedora_item'); module_load_include('inc', 'fedora_repository', 'api/fedora_item');
$fedora_item = new Fedora_Item($this->pid); $fedora_item = new Fedora_Item($this->pid);
return $fedora_item->purge_datastream( $this->dsid, $start_date, $end_date); return $fedora_item->purge_datastream($this->dsid, $start_date, $end_date);
} }
/** /**
@ -255,18 +238,19 @@ printerWindow.document.write(text);
* @return string[] $ret * @return string[] $ret
*/ */
public function getHistory() { public function getHistory() {
module_load_include('inc', 'fedora_repository', 'api/fedora_item'); module_load_include('inc', 'fedora_repository', 'api/fedora_item');
$fedora_item = new Fedora_Item($this->pid); $fedora_item = new Fedora_Item($this->pid);
$history = $fedora_item->get_datastream_history($this->dsid); $history = $fedora_item->get_datastream_history($this->dsid);
$ret = FALSE; $ret = FALSE;
if ($history !== FALSE) { if ($history !== FALSE) {
$ret = array(); $ret = array();
foreach ($history as $version) { foreach ($history as $version) {
if ($version->versionID !== NULL) $ret[] = array('versionID' => $version->versionID, 'createDate' => $version->createDate); if ($version->versionID !== NULL)
} $ret[] = array('versionID' => $version->versionID, 'createDate' => $version->createDate);
} }
return $ret; }
return $ret;
} }
/** /**
@ -277,5 +261,4 @@ printerWindow.document.write(text);
* @return void * @return void
*/ */
abstract protected function convertFromOldSchema(); abstract protected function convertFromOldSchema();
} }

Loading…
Cancel
Save