diff --git a/includes/add_datastream.form.inc b/includes/add_datastream.form.inc index 1ef1bb33..62490c9a 100644 --- a/includes/add_datastream.form.inc +++ b/includes/add_datastream.form.inc @@ -62,7 +62,7 @@ function islandora_add_datastream_form(array $form, array &$form_state, Abstract '#element_validate' => array('islandora_add_datastream_form_field_does_not_contain_a_forward_slash'), ), 'file' => array( - '#type' => 'file', + '#type' => 'managed_file', '#required' => TRUE, '#title' => t('Upload Document'), '#size' => 48, diff --git a/includes/datastream.inc b/includes/datastream.inc index dc01bdfe..8a02439a 100644 --- a/includes/datastream.inc +++ b/includes/datastream.inc @@ -26,8 +26,19 @@ function islandora_download_datastream(AbstractDatastream $datastream) { * @param bool $download * If TRUE the file is download to the user computer for viewing otherwise it * will attempt to display in the browser natively. + * @param int $version + * The version of the datastream to display */ -function islandora_view_datastream(AbstractDatastream $datastream, $download = FALSE) { +function islandora_view_datastream(AbstractDatastream $datastream, $download = FALSE, $version = NULL) { + if ($version !== NULL) { + if (isset($datastream[$version])) { + $datastream = $datastream[$version]; + } + else { + return drupal_not_found(); + } + } + header_remove('Cache-Control'); header_remove('Expires'); header('Content-type: ' . $datastream->mimetype); @@ -69,12 +80,31 @@ function islandora_datastream_get_human_readable_size(AbstractDatastream $datast * * @param AbstractDatastream $datastream * The datastream to generated the url to. + * @param string $type + * One of: + * - download + * - view + * @param int $version + * (Optional) The version of the datastream to get a URL for. * * @return string * either the 'view' or 'download' url for the given datastream. */ -function islandora_datastream_get_url(AbstractDatastream $datastream, $type = 'download') { - return $datastream->controlGroup == 'R' ? $datastream->url : "islandora/object/{$datastream->parent->id}/datastream/{$datastream->id}/$type"; +function islandora_datastream_get_url(AbstractDatastream $datastream, $type = 'download', $version = NULL) { + if ($version === NULL) { + $link = "islandora/object/{$datastream->parent->id}/datastream/{$datastream->id}/$type"; + } + else { + $link = "islandora/object/{$datastream->parent->id}/datastream/{$datastream->id}/version/$version/$type"; + $datastream = $datastream[$version]; + } + + if ($datastream->controlGroup == 'R') { + return $datastream->url; + } + else { + return $link; + } } /** diff --git a/includes/datastream.version.inc b/includes/datastream.version.inc new file mode 100644 index 00000000..c58b2a8c --- /dev/null +++ b/includes/datastream.version.inc @@ -0,0 +1,125 @@ + $datastream->id))); + + $header = array(); + $header[] = array('data' => 'Created Date'); + $header[] = array('data' => t('Size')); + $header[] = array('data' => t('Label')); + $header[] = array('data' => t('Mime type')); + $header[] = array('data' => t('Operations')); + $rows = array(); + + foreach ($datastream as $version => $datastream_version) { + $row = array(); + $row[] = array( + 'class' => 'datastream-date', + 'data' => theme('islandora_datastream_view_link', array( + 'datastream' => $datastream, + 'label' => $datastream_version->createdDate->format(DATE_RFC850), + 'version' => $version, + )), + ); + $row[] = array( + 'class' => 'datastream-size', + 'data' => islandora_datastream_get_human_readable_size($datastream_version), + ); + $row[] = array( + 'class' => 'datastream-label', + 'data' => $datastream_version->label, + ); + $row[] = array( + 'class' => 'datastream-mime', + 'data' => $datastream_version->mimeType, + ); + $row[] = array( + 'class' => 'datastream-delete', + 'data' => theme('islandora_datastream_delete_link', array( + 'datastream' => $datastream, + 'version' => $version, + )), + ); + $rows[] = $row; + } + + return theme('table', array('header' => $header, 'rows' => $rows)); +} + +/** + * The admin delete datastream form. + * + * @param array $form + * The Drupal form. + * @param array $form_state + * The Drupal form state. + * @param AbstractDatastream $datastream + * The datastream to be deleted. + * + * @return array + * The drupal form definition. + */ +function islandora_delete_datastream_version_form(array $form, array &$form_state, AbstractDatastream $datastream, $version) { + if (!isset($datastream[$version]) || count($datastream) < 2) { + return drupal_not_found(); + } + + $form_state['datastream'] = $datastream; + $form_state['version'] = $version; + return confirm_form($form, + t('Are you sure you want to delete version @version of the @dsid datastream?', array('@dsid' => $datastream->id, '@version' => $version)), + "islandora/object/{$datastream->parent->id}", + t('This action cannot be undone.'), + t('Delete'), + t('Cancel') + ); +} + +/** + * Submit handler for the delete datastream form. + * + * Purges/Delete's the given AbstractDatastream if possible. + * + * The ISLANDORA_PRE_PURGE_DATASTREAM_HOOK will query other modules as to + * whether the given FedoraDatastream + * should be: blocked from purging; state set to 'Deleted'; or purged. + * + * @param array $form + * The Drupal form. + * @param array $form_state + * The Drupal form state. + */ +function islandora_delete_datastream_version_form_submit(array $form, array &$form_state) { + $datastream = $form_state['datastream']; + $version = $form_state['version']; + + $datastream_id = $datastream->id; + $object = $datastream->parent; + + try { + unset($datastream[$version]); + } + catch (Exception $e) { + drupal_set_message(t('Error deleting version %v of %s datastream from object %o %e', array( + '%v' => $version, + '%s' => $datastream_id, + '%o' => $object->label, + '%e' => $e->getMessage())), 'error'); + } + + drupal_set_message(t('%d datastream version sucessfully deleted from Islandora object %o', array( + '%d' => $datastream_id, + '%o' => $object->label))); + + $form_state['redirect'] = "islandora/object/{$object->id}/datastream/{$datastream->id}/version"; +} diff --git a/includes/datastream.versions.inc b/includes/datastream.versions.inc deleted file mode 100644 index 5c946aba..00000000 --- a/includes/datastream.versions.inc +++ /dev/null @@ -1,42 +0,0 @@ -id Previous Versions"); - - $header = array(); - $header[] = array ('data' => 'Created Date'); - $header[] = array('data' => t('Size')); - $header[] = array('data' => t('Label')); - $header[] = array('data' => t('Mime type')); - $rows = array(); - - foreach ($datastream as $version => $datastream_version) { - $row = array(); - $row[] = array( - 'class' => 'datastream-date', - 'data' => theme('islandora_datastream_view_link', array( - 'datastream' => $datastream, - 'label' => $datastream_version->createdDate->format(DATE_RFC850), - 'version' => $version, - )), - ); - $row[] = array( - 'class' => 'datastream-size', - 'data' => islandora_datastream_get_human_readable_size($datastream_version), - ); - $row[] = array( - 'class' => 'datastream-label', - 'data' => $datastream_version->label, - ); - $row[] = array( - 'class' => 'datastream-mime', - 'data' => $datastream_version->mimeType, - ); - $rows[] = $row; - } - - return theme('table', array('header' => $header, 'rows' => $rows)); -} \ No newline at end of file diff --git a/islandora.info b/islandora.info index ce6ba947..fea3ead2 100644 --- a/islandora.info +++ b/islandora.info @@ -17,4 +17,5 @@ files[] = tests/hooks.test files[] = tests/ingest.test files[] = tests/hooked_access.test files[] = tests/islandora_manage_permissions.test +files[] = tests/datastream_versions.test php = 5.3 diff --git a/islandora.module b/islandora.module index ab79ca9e..d1f15542 100644 --- a/islandora.module +++ b/islandora.module @@ -234,16 +234,36 @@ function islandora_menu() { 'access arguments' => array(FEDORA_PURGE, 4), 'load arguments' => array(2), ); - $items['islandora/object/%islandora_object/datastream/%islandora_datastream/versions'] = array( + $items['islandora/object/%islandora_object/datastream/%islandora_datastream/version'] = array( 'title' => 'Datastream Versions', 'page arguments' => array(4), - 'page callback' => 'islandora_datastream_versions_table', - 'file' => 'includes/datastream.versions.inc', + 'page callback' => 'islandora_datastream_version_table', + 'file' => 'includes/datastream.version.inc', 'type' => MENU_CALLBACK, 'access callback' => 'islandora_datastream_access', 'access arguments' => array(ISLANDORA_VIEW_DATASTREAM_HISTORY, 4), 'load arguments' => array(2), ); + $items['islandora/object/%islandora_object/datastream/%islandora_datastream/version/%/delete'] = array( + 'title' => 'Delete datastream version', + 'page arguments' => array('islandora_delete_datastream_version_form', 4, 6), + 'page callback' => 'drupal_get_form', + 'file' => 'includes/datastream.version.inc', + 'type' => MENU_CALLBACK, + 'access callback' => 'islandora_datastream_access', + 'access arguments' => array(FEDORA_PURGE, 4), + 'load arguments' => array(2), + ); + $items['islandora/object/%islandora_object/datastream/%islandora_datastream/version/%/view'] = array( + 'title' => 'View datastream version', + 'page callback' => 'islandora_view_datastream', + 'page arguments' => array(4, FALSE, 6), + 'type' => MENU_CALLBACK, + 'file' => 'includes/datastream.inc', + 'access callback' => 'islandora_datastream_access', + 'access arguments' => array(ISLANDORA_VIEW_DATASTREAM_HISTORY, 4), + 'load arguments' => array(2), + ); $items['islandora/object/%islandora_object/print'] = array( 'title' => 'Print Object', 'page callback' => 'islandora_print_object', @@ -279,7 +299,7 @@ function islandora_menu() { function islandora_admin_paths() { $paths = array(); $paths['islandora/object/*/manage*'] = TRUE; - $paths['islandora/object/*/delete'] = TRUE; + $paths['islandora/object/*/delete*'] = TRUE; $paths['islandora/object/*/datastream/*/edit'] = TRUE; $paths['islandora/object/*/datastream/*/versions'] = TRUE; return $paths; @@ -341,11 +361,15 @@ function islandora_theme() { ), 'islandora_datastream_delete_link' => array( 'file' => 'theme/theme.inc', - 'variables' => array('datastream' => NULL), + 'variables' => array('datastream' => NULL, 'version' => NULL), ), 'islandora_datastream_view_link' => array( 'file' => 'theme/theme.inc', - 'variables' => array('datastream' => NULL, 'version' => NULL, 'label' => NULL), + 'variables' => array( + 'datastream' => NULL, + 'version' => NULL, + 'label' => NULL, + ), ), 'islandora_datastream_download_link' => array( 'file' => 'theme/theme.inc', diff --git a/tests/datastream_versions.test b/tests/datastream_versions.test new file mode 100644 index 00000000..6b22bd95 --- /dev/null +++ b/tests/datastream_versions.test @@ -0,0 +1,198 @@ + 'Islandora Datastream Versions', + 'description' => 'Tests the functionality related to datastream versions in Islandora.', + 'group' => 'Islandora', + ); + } + + /** + * Create an object with many datastram versions. + * + * @see IslandoraWebTestCase::setUp() + */ + public function setUp() { + parent::setUp(); + + $this->pid = $this->randomName() . ":" . $this->randomName(); + $tuque = islandora_get_tuque_connection(); + $object = $tuque->repository->constructObject($this->pid); + $object = $tuque->repository->ingestObject($object); + $this->dsid = $this->randomName(); + $ds = $object->constructDatastream($this->dsid); + $ds->label = 'Test'; + $ds->content = 'test'; + $object->ingestDatastream($ds); + + // Create three versions. + $ds->mimetype = 'application/pdf'; + $ds->label = 'jaaaaa maaaan'; + $ds->content = 'Tests... are the bests.'; + } + + /** + * Free any objects/resources created for this test. + * + * @see IslandoraWebTestCase::tearDown() + */ + public function tearDown() { + $tuque = islandora_get_tuque_connection(); + $tuque->repository->purgeObject($this->pid); + + parent::tearDown(); + } + + /** + * Check if the user can see datastream versions in the datastream table. + */ + public function testSeeDatastreamVersions() { + $user = $this->drupalCreateUser(array( + 'view fedora repository objects', + 'ingest fedora objects', + 'view old datastream versions', + 'add fedora datastreams', + )); + $this->drupalLogin($user); + $this->drupalGet("islandora/object/{$this->pid}/manage/datastreams"); + $this->assertLink($this->dsid); + $this->assertLink("4"); + $encoded_pid = urlencode($this->pid); + $this->assertLinkByHref("islandora/object/$encoded_pid/datastream/{$this->dsid}/version"); + } + + /** + * Check that users without permission cannot see datastream versions. + */ + public function testNotSeeDatastreamVersions() { + $user = $this->drupalCreateUser(array( + 'view fedora repository objects', + 'ingest fedora objects', + 'add fedora datastreams', + )); + $this->drupalLogin($user); + $this->drupalGet("islandora/object/{$this->pid}/manage/datastreams"); + $this->assertLink($this->dsid); + $this->assertNoLink("4"); + $encoded_pid = urlencode($this->pid); + $this->assertNoLinkByHref("islandora/object/$encoded_pid/datastream/{$this->dsid}/version"); + } + + /** + * Check that users without permission cannot see datastream version pages. + */ + public function testDatastreamVersionPermissions() { + $this->drupalGet("islandora/object/{$this->pid}/datastream/{$this->dsid}/version"); + $this->assertResponse(403); + $this->drupalGet("islandora/object/{$this->pid}/datastream/{$this->dsid}/version/0/view"); + $this->assertResponse(403); + } + + /** + * Check that the proper infomration is displayed on the ds version page. + */ + public function testDatastreamVersionPage() { + $user = $this->drupalCreateUser(array( + 'view old datastream versions', + )); + $this->drupalLogin($user); + $this->drupalGet("islandora/object/{$this->pid}/datastream/{$this->dsid}/version"); + $this->assertNoLink("Delete"); + $this->assertText("text/xml"); + $this->assertText("application/pdf"); + $this->assertText("jaaaaa maaaan"); + $this->assertText("Test"); + + $encoded_pid = urlencode($this->pid); + $this->assertLinkByHref("islandora/object/$encoded_pid/datastream/{$this->dsid}/version/0/view"); + $this->assertLinkByHref("islandora/object/$encoded_pid/datastream/{$this->dsid}/version/1/view"); + $this->assertLinkByHref("islandora/object/$encoded_pid/datastream/{$this->dsid}/version/2/view"); + $this->assertLinkByHref("islandora/object/$encoded_pid/datastream/{$this->dsid}/version/3/view"); + } + + /** + * Make sure the correct content is displayed for each datastream version. + */ + public function testDatastreamVersionContent() { + $user = $this->drupalCreateUser(array( + 'view old datastream versions', + )); + $this->drupalLogin($user); + $this->drupalGet("islandora/object/{$this->pid}/datastream/{$this->dsid}/version/3/view"); + $content = $this->drupalGetContent(); + if ($content != 'test') { + $this->fail("Incorrect datastream content"); + } + $this->drupalGet("islandora/object/{$this->pid}/datastream/{$this->dsid}/version/2/view"); + $content = $this->drupalGetContent(); + if ($content != 'test') { + $this->fail("Incorrect datastream content"); + } + $this->drupalGet("islandora/object/{$this->pid}/datastream/{$this->dsid}/version/1/view"); + $content = $this->drupalGetContent(); + if ($content != 'test') { + $this->fail("Incorrect datastream content"); + } + $this->drupalGet("islandora/object/{$this->pid}/datastream/{$this->dsid}/version/0/view"); + $content = $this->drupalGetContent(); + if ($content != 'Tests... are the bests.') { + $this->fail("Incorrect datastream content"); + } + $this->drupalGet("islandora/object/{$this->pid}/datastream/{$this->dsid}/version/5/view"); + $this->assertResponse(404); + } + + /** + * Make sure you can delete datastream versions. + */ + public function testDatastreamVersionDelete() { + $user = $this->drupalCreateUser(array( + 'view old datastream versions', + 'delete fedora objects and datastreams', + )); + $this->drupalLogin($user); + $this->drupalGet("islandora/object/{$this->pid}/datastream/{$this->dsid}/version"); + $this->assertLink("delete"); + $this->assertText('text/xml'); + + $encoded_pid = urlencode($this->pid); + $this->assertLinkByHref("islandora/object/$encoded_pid/datastream/{$this->dsid}/version/0/delete"); + $this->assertLinkByHref("islandora/object/$encoded_pid/datastream/{$this->dsid}/version/1/delete"); + $this->assertLinkByHref("islandora/object/$encoded_pid/datastream/{$this->dsid}/version/2/delete"); + $this->assertLinkByHref("islandora/object/$encoded_pid/datastream/{$this->dsid}/version/3/delete"); + + $this->drupalGet("islandora/object/{$this->pid}/datastream/{$this->dsid}/version/3/delete"); + $this->drupalPost(NULL, array(), t('Delete')); + $this->assertNoText('text/xml'); + } + + /** + * Make sure you can't delete versions that don't exist/have only 1 version. + */ + public function testDatastreamVersionDeleteEdgeCase() { + $user = $this->drupalCreateUser(array( + 'view old datastream versions', + 'delete fedora objects and datastreams', + )); + $this->drupalLogin($user); + + $this->drupalGet("islandora/object/{$this->pid}/datastream/{$this->dsid}/version/6/delete"); + $this->assertResponse(404); + + $this->drupalGet("islandora/object/{$this->pid}/datastream/DC/version/0/delete"); + $this->assertResponse(404); + } +} diff --git a/theme/theme.inc b/theme/theme.inc index 8964960c..85341838 100644 --- a/theme/theme.inc +++ b/theme/theme.inc @@ -54,7 +54,7 @@ function islandora_preprocess_islandora_default_edit(array &$variables) { 'data' => islandora_datastream_get_human_readable_size($ds), ); if (user_access(ISLANDORA_VIEW_DATASTREAM_HISTORY)) { - $row[] = array( + $row[] = array( 'class' => 'datastream-versions', 'data' => theme('islandora_datastream_version_link', array( 'datastream' => $ds, @@ -268,29 +268,29 @@ function theme_islandora_datastream_download_link(array $vars) { * @param array $vars * An array containing: * - datastream: An AbstractDatastream for which to generate a view link. + * - label: (Optional) The label for the link. + * - version: (Optional) The version of the datstream to link to. */ function theme_islandora_datastream_view_link(array $vars) { $datastream = $vars['datastream']; module_load_include('inc', 'islandora', 'includes/utilities'); - if($vars['label'] === NULL) { + if ($vars['label'] === NULL) { $label = check_plain($datastream->id); } else { $label = check_plain($vars['label']); } - if($vars['version'] === NULL) { + if ($vars['version'] === NULL) { $perm = FEDORA_VIEW_OBJECTS; - $query = array(); } else { $perm = ISLANDORA_VIEW_DATASTREAM_HISTORY; - $query = array('version' => $vars['version']); } if (islandora_datastream_access($perm, $datastream)) { - return l($label, islandora_datastream_get_url($datastream, 'view'), array('query' => $query)); + return l($label, islandora_datastream_get_url($datastream, 'view', $vars['version'])); } else { return $label; @@ -303,6 +303,7 @@ function theme_islandora_datastream_view_link(array $vars) { * @param array $vars * An array containing: * - datastream: An AbstractDatastream for which to generate a delete link. + * - version: (optional) the version of the datastream to delete. */ function theme_islandora_datastream_delete_link(array $vars) { $datastream = $vars['datastream']; @@ -311,9 +312,22 @@ function theme_islandora_datastream_delete_link(array $vars) { $can_delete = !in_array($datastream->id, $datastreams) && islandora_datastream_access(FEDORA_PURGE, $datastream); - return $can_delete ? - l(t('delete'), "islandora/object/{$datastream->parent->id}/datastream/{$datastream->id}/delete") : + if ($vars['version'] !== NULL) { + if (count($datastream) == 1) { + $can_delete = FALSE; + } + $link = "islandora/object/{$datastream->parent->id}/datastream/{$datastream->id}/version/{$vars['version']}/delete"; + } + else { + $link = "islandora/object/{$datastream->parent->id}/datastream/{$datastream->id}/delete"; + } + + if ($can_delete) { + return l(t('delete'), $link); + } + else { ''; + } } /** @@ -335,16 +349,31 @@ function theme_islandora_datastream_edit_link(array $vars) { ''; } +/** + * Renders a link to take you to the datastream versions page. + * + * @param array $vars + * An array containing: + * - datastream: An AbstractDatastream to generate the version link from. + * + * @return string + * Markup. + */ function theme_islandora_datastream_version_link(array $vars) { $datastream = $vars['datastream']; module_load_include('inc', 'islandora', 'includes/utilities'); $see_history = islandora_datastream_access(ISLANDORA_VIEW_DATASTREAM_HISTORY, $datastream); - if($see_history) { - return l(count($datastream), "islandora/object/{$datastream->parent->id}/datastream/{$datastream->id}/versions"); + if ($see_history) { + if ($datastream->versionable) { + return l(count($datastream), "islandora/object/{$datastream->parent->id}/datastream/{$datastream->id}/version"); + } + else { + return t('Not Versioned'); + } } else { return ''; } -} \ No newline at end of file +}