Browse Source

Allow the addition of Redirect and External datastreams.

pull/126/head
Adam Vessey 13 years ago
parent
commit
c3ccdec4b6
  1. 14
      ObjectHelper.inc
  2. 83
      fedora_repository.module
  3. 83
      formClass.inc

14
ObjectHelper.inc

@ -72,7 +72,6 @@ class ObjectHelper {
return ' ';
}
if (variable_get('fedora_object_restrict_datastreams', FALSE) == TRUE) {
if (($cm = ContentModel::loadFromObject($pid)) == FALSE) {
drupal_set_message(t("You do not have access to objects without an Islandora Content Model."), 'error');
@ -91,7 +90,6 @@ class ObjectHelper {
module_load_include('inc', 'fedora_repository', 'api/fedora_item');
$item = new Fedora_Item($pid);
if (isset($item->datastreams[$dsID])) {
$mimeType = $item->datastreams[$dsID]['MIMEType'];
if ($label == NULL) {
@ -122,8 +120,14 @@ class ObjectHelper {
$mimeType = 'image/jpeg';
}
$url = variable_get('fedora_base_url', 'http://localhost:8080/fedora') . '/objects/' . $pid . '/datastreams/' . $dsID . '/content';
$query_options = array();
if ($version) {
$url .= '/' . $version; //drupal_urlencode($version);
$query_options['asOfDateTime'] = $version; //drupal_urlencode($version);
}
if ($query_options) {
$url = url($url, array(
'query' => $query_options,
));
}
$ch = curl_init();
$user_agent = "Mozilla/4.0 pp(compatible; MSIE 5.01; Windows NT 5.0)";
@ -183,7 +187,6 @@ class ObjectHelper {
$curl_out = curl_exec($ch);
if ($curl_out !== FALSE) {
$info = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
//dd($info, 'effective URL');
if ($url !== $info) { //Handle redirect streams (the final URL is not the same as the Fedora URL)
//Add the parameters passed to Drupal, leaving out the 'q'
@ -194,7 +197,7 @@ class ObjectHelper {
}
header('HTTP/1.1 307 Moved Temporarily');
header('Location: ' . $info . '?' . http_build_query($query)); //Fedora seems to discard the query portion.
header('Location: ' . url($info, array('query' => $query)));
}
elseif ((isset($user) && $user->uid != 0) || $forceSoap || isset($_SERVER['HTTPS'])) { //If not anonymous, soap is force or we're using HTTPS
//Have the webserver mediate the transfer (download and restream)
@ -210,6 +213,7 @@ class ObjectHelper {
}
else {
//Curl error...
watchdog('fedora_repository', 'Curl error. Info: @info', array('@info' => print_r(curl_getinfo($ch), TRUE)), WATCHDOG_WARNING);
}
}
curl_close($ch);

83
fedora_repository.module

@ -377,37 +377,25 @@ function add_stream_form_submit($form, &$form_state) {
$form_state['rebuild'] = TRUE;
return;
}
module_load_include('inc', 'fedora_repository', 'MimeClass');
module_load_include('inc', 'fedora_repository', 'ObjectHelper');
module_load_include('inc', 'fedora_repository', 'api/fedora_item');
$pathToModule = drupal_get_path('module', 'fedora_repository');
$file = $form_state['values']['add-stream-file-location'];
$pid = $form_state['values']['pid'];
$dsid = $form_state['values']['stream_id'];
$dsLabel = $form_state['values']['stream_label'] . substr($file, strrpos($file, '.')); // Add the file extention to the end of the label.;
$file_basename = basename($file);
$file_directory = dirname($file);
$streamUrl = $base_url . '/' . $file_directory . '/' . drupal_urlencode($file_basename);
/* -----------------------------------------------------------------
* need a better way to get mimetypes
*/
$mimetype = new MimeClass();
$dformat = $mimetype->getType($file);
$controlGroup = "M";
if ($dformat == 'text/xml') {
$controlGroup = 'X';
}
try {
$item = new Fedora_Item($pid);
$item->add_datastream_from_url($streamUrl, $dsid, $dsLabel, $dformat, $controlGroup);
$item->add_datastream_from_url($form_state['storage']['stream_url'], $dsid, $dsLabel, $form_state['storage']['ds_mimetype'], $form_state['values']['control_group']);
$object_helper = new ObjectHelper();
$object_helper->get_and_do_datastream_rules($pid, $dsid, $file);
if ($file = $form_state['values']['add-stream-file-location']) {
$object_helper = new ObjectHelper();
$object_helper->get_and_do_datastream_rules($pid, $dsid, $file);
file_delete($file);
file_delete($file);
}
} catch (exception $e) {
drupal_set_message(t('@message', array('@message' => check_plain($e->getMessage()))), 'error');
return;
@ -434,6 +422,7 @@ function add_stream_form(&$form_state, $pid) {
* @return type
*/
function add_stream_form_validate($form, &$form_state) {
module_load_include('inc', 'fedora_repository', 'MimeClass');
if ($form_state['clicked_button']['#value'] == 'OK') {
$form_state['rebuild'] = TRUE;
return;
@ -444,15 +433,15 @@ function add_stream_form_validate($form, &$form_state) {
form_set_error('', t('Data stream ID cannot be more than 64 characters.'));
return FALSE;
}
if (!(preg_match("/^[a-zA-Z]/", $dsid))) {
form_set_error('', t("Data stream ID (@dsid) has to start with a letter.", array('@dsid' => check_plain($dsid))));
elseif (!(preg_match("/^[a-zA-Z]/", $dsid))) {
form_set_error('', t("Data stream ID (@dsid) has to start with a letter.", array('@dsid' => $dsid)));
return FALSE;
}
if (strlen($dsLabel) > 64) {
elseif (strlen($dsLabel) > 64) {
form_set_error('', t('Data stream Label cannot be more than 64 characters.'));
return FALSE;
}
if (strpos($dsLabel, '/')) {
elseif (strpos($dsLabel, '/') !== FALSE) {
form_set_error('', t('Data stream Label cannot contain a "/".'));
return FALSE;
}
@ -462,12 +451,30 @@ function add_stream_form_validate($form, &$form_state) {
// 'file_validate_size' => array(30 * 1024),
);
$fileObject = file_save_upload('add-stream-file-location', $validators);
// Move the uploaded file to Drupal's files directory.
file_move($fileObject->filepath, 0, 'FILE_EXISTS_RENAME');
$form_state['values']['add-stream-file-location'] = $fileObject->filepath;
// TODO: Add error checking here.
$mimetype = new MimeClass();
$controlGroup = $form_state['values']['control_group'];
if (in_array($controlGroup, array('X', 'M')) && ($fileObject = file_save_upload('add-stream-file-location', $validators)) !== 0) {
// Move the uploaded file to Drupal's files directory.
file_move($fileObject->filepath, 0, 'FILE_EXISTS_RENAME');
$form_state['values']['add-stream-file-location'] = $fileObject->filepath;
$form_state['storage']['ds_mimetype'] = $mimetype->getType($fileObject->filepath);
$file_basename = basename($fileObject->filepath);
$file_directory = dirname($fileObject->filepath);
$form_state['storage']['stream_url'] = url($file_directory . '/' . drupal_urlencode($file_basename), array(
'absolute' => TRUE,
));
}
elseif (in_array($controlGroup, array('M', 'R', 'E')) && ($ref = $form_state['values']['ds_reference'])) {
$form_state['storage']['ds_mimetype'] = $mimetype->getType($ref);
$form_state['storage']['stream_url'] = $form_state['values']['ds_reference'];
}
else {
form_set_error('', t('No file given when "X" or "M", or no reference given when "M", "R" or "E".'));
}
// TODO: Add error checking here.
$form_state['rebuild'] = FALSE;
}
@ -680,6 +687,11 @@ function fedora_repository_replace_stream_form_validate($form, &$form_state) {
function fedora_repository_replace_stream_form_submit($form, &$form_state) {
global $base_url;
$file = $form_state['values']['file'];
if ($file !== NULL) {
$file = $form_state['values']['reference'];
}
$pid = $form_state['values']['pid'];
$dsid = $form_state['values']['dsId'];
$dsLabel = $form_state['values']['dsLabel'];
@ -702,14 +714,9 @@ function fedora_repository_replace_stream_form_submit($form, &$form_state) {
$mimetype = new MimeClass();
$dformat = $mimetype->getType($file->filepath);
module_load_include('inc', 'fedora_repository', 'api/fedora_item');
$item = new Fedora_Item($pid);
if(in_array($info->datastream->controlGroup, array('M', 'X'))) {
$item->modify_datastream($streamUrl, $dsid, $dsLabel, $dformat);
} else {
drupal_set_message(t('Cannot replace Redirect or Managed Datastream.'), 'error');
}
$item->modify_datastream($streamUrl, $dsid, $dsLabel, $dformat);
$form_state['redirect'] = 'fedora/repository/' . $pid;
}
@ -1052,7 +1059,6 @@ function fedora_repository_urlencode_string($str) {
* @return type
*/
function fedora_object_as_attachment($pid, $dsId, $label=NULL, $version=NULL) {
global $user;
module_load_include('inc', 'fedora_repository', 'ObjectHelper');
if ($pid == NULL || $dsId == NULL) {
@ -1061,7 +1067,7 @@ function fedora_object_as_attachment($pid, $dsId, $label=NULL, $version=NULL) {
}
$objectHelper = new ObjectHelper();
$objectHelper->makeObject($pid, $dsId, 1, $label, FALSE, $version);
$objectHelper->makeObject($pid, $dsId, TRUE, $label, FALSE, $version);
}
/**
@ -2266,9 +2272,6 @@ function theme_fedora_repository_solution_packs_list($solution_packs) {
$header = array();
$rows = array();
drupal_add_css(drupal_get_path('module', 'update') . '/update.css');
return $output;
}

83
formClass.inc

@ -656,17 +656,21 @@ class formClass {
}
}
$form['add_datastream_label'] = array(
'#value' => t('<br /><h3>Add Datastream:</h3>'),
'#weight' => -10,
$form['fieldset'] = array(
'#type' => 'fieldset',
'#title' => t('Add datastream'),
);
//$form['add_datastream_label'] = array(
// '#value' => t('<br /><h3>Add Datastream:</h3>'),
// '#weight' => -10,
//);
$form['pid'] = array(
$form['fieldset']['pid'] = array(
'#type' => 'hidden',
'#value' => "$pid"
);
$form['stream_label'] = array(
$form['fieldset']['stream_label'] = array(
'#title' => 'Datastream Label',
'#required' => 'TRUE',
'#description' => t('A Human readable label'),
@ -674,35 +678,37 @@ class formClass {
);
$form['#attributes']['enctype'] = 'multipart/form-data';
$form['add-stream-file-location'] = array(
$form['fieldset']['add-stream-file-location'] = array(
'#type' => 'file',
'#title' => t('Upload Document'),
'#size' => 48,
// '#required'=>'TRUE',
'#description' => t('The file to upload.')
'#description' => t('The file to upload. (Only for Managed and Inline)')
);
$form['fieldset']['ds_reference'] = array(
'#type' => 'textfield',
'#title' => t('Datastream reference'),
'#size' => 48,
'#description' => t('A URL reference to resolve for the contents of the datastream. (Required for External and Redirect, but will still work for Managed and Inline.)'),
);
$form['#redirect'] = "fedora/repository/$pid/";
$form['submit'] = array(
$form['fieldset']['submit'] = array(
'#type' => 'submit',
'#value' => t('Add Datastream')
);
if (!empty($unused_dsids)) {
$dsidsForForm = array();
foreach ($unused_dsids as $dsid) {
$dsidsForForm[$dsid] = $dsid;
}
$form['stream_id'] = array(
$form['fieldset']['stream_id'] = array(
'#type' => 'select',
'#title' => t('Datastream ID'),
'#default_value' => variable_get('feed_item_length', 'teaser'),
'#weight' => '-1',
'#weight' => -1,
'#description' => t('Datastream IDs defined by the content model.'),
'#options' => array_combine($unused_dsids, $unused_dsids),
);
$form['stream_id']['#options'] = array_combine($unused_dsids, $unused_dsids);
}
else {
$form['stream_id'] = array(
$form['fieldset']['stream_id'] = array(
'#title' => 'Datastream ID',
'#required' => 'TRUE',
'#description' => t('An ID for this stream that is unique to this object. Must start with a letter and contain only alphanumeric characters and dashes and underscores.'),
@ -710,6 +716,18 @@ class formClass {
'#weight' => -1,
);
}
$form['fieldset']['control_group'] = array(
'#type' => 'select',
'#title' => t('Control group'),
'#options' => array(
'X' => t('Inline XML'),
'M' => t('Managed datastream'),
'E' => t('Externally Referenced/managed datastream'),
'R' => t('Redirect datastream'),
),
'#description' => t('The manner in which the datastream will be stored. "Inline XML" must be XML and will be placed directly into the FOXML for the object. "Managed" datastreams are made to live on the filesystem as discrete files in the Fedora data directory. Both "Redirect" and "External" streams are URL references; the difference being the redirect stream instruct clients to perform an HTTP redirect, such that the data does not pass though Fedora (useful for streaming). External streams are mediated (by which I mean loaded and streamed from) the Fedora server.'),
'#weight' => 0,
);
return $form;
}
@ -836,12 +854,33 @@ class formClass {
$form = array();
$form['#attributes']['enctype'] = 'multipart/form-data';
$form['file'] = array(
'#type' => 'file',
'#title' => t('Upload Document'),
'#description' => t('The file to upload.')
);
module_load_include('inc', 'fedora_repository', 'api/fedora_item');
$item = new Fedora_Item($pid);
$info = $item->get_datastream_info($dsId);
$control_group = $info->datastream->controlGroup;
if (in_array($control_group, array('M', 'X'))) {
$form['file'] = array(
'#type' => 'file',
'#title' => t('Upload Document'),
'#description' => t('A file with which to replace the contents of this datastream.'),
);
}
if ($control_group != 'X') {
$form['reference'] = array(
'#type' => 'textfield',
'#title' => t('Reference to object'),
'#description' => t('A URL the datastream will be updated to reference.'),
);
}
if ($control_group == 'M') {
$form['note'] = array(
'#type' => 'item',
'#title' => t('NOTE'),
'#value' => t('If both a file and a reference are given, the file will be given preference.'),
);
}
$form['pid'] = array(
'#type' => 'value',
'#value' => $pid,

Loading…
Cancel
Save