From 46a40cb8264245eda153a52f6b3f8b2b2ce50635 Mon Sep 17 00:00:00 2001 From: Nigel Banks Date: Wed, 6 Mar 2013 22:38:47 +0100 Subject: [PATCH 1/2] No longer throw exceptions on invalid configuration. --- includes/ingest.form.inc | 39 ++++++++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/includes/ingest.form.inc b/includes/ingest.form.inc index c285c5a1..e2e97d5f 100644 --- a/includes/ingest.form.inc +++ b/includes/ingest.form.inc @@ -31,8 +31,15 @@ * The form definition of the current step. */ function islandora_ingest_form(array $form, array &$form_state, array $configuration) { - islandora_ingest_form_init_form_state_storage($form_state, $configuration); - return islandora_ingest_form_execute_step($form, $form_state); + try { + islandora_ingest_form_init_form_state_storage($form_state, $configuration); + return islandora_ingest_form_execute_step($form, $form_state); + } + catch(Exception $e) { + drupal_set_message($e->getMessage(), 'error'); + return array(array( + '#markup' => l(t('Back'), 'javascript:window.history.back();', array('external' => TRUE)))); + } } /** @@ -42,21 +49,16 @@ function islandora_ingest_form(array $form, array &$form_state, array $configura * * @todo Add hook for manipulating/validating the configuration. * + * @see islandora_ingest_form() + * * @throws InvalidArgumentException * * @param array $configuration * The key value pairs that are used to build the multi-paged ingest process. - * - * @see islandora_ingest_form() - * - * @return bool - * TRUE if the configuration is valid, FALSE otherwise. */ -function islandora_ingest_form_validiate_configuration(array $configuration) { +function islandora_ingest_form_validate_configuration(array $configuration) { if (empty($configuration['models'])) { - $message = t('Ingest configuration not vaild, no models were given'); - drupal_set_message($message, 'error'); - throw new InvalidArgumentException($message); + throw new InvalidArgumentException('Ingest configuration not vaild, no models were given'); } } @@ -71,8 +73,8 @@ function islandora_ingest_form_validiate_configuration(array $configuration) { */ function islandora_ingest_form_init_form_state_storage(array &$form_state, array $configuration) { if (empty($form_state['islandora'])) { - // Validate the configuration before using it. - islandora_ingest_form_validiate_configuration($configuration); + // Validate the configuration before we use it. + islandora_ingest_form_validate_configuration($configuration); $object = islandora_ingest_form_prepare_new_object($configuration); $form_state['islandora'] = array( 'step_id' => NULL, @@ -233,12 +235,19 @@ function islandora_ingest_form_decrement_step(array &$form_state) { * The list of key/value pairs of configuration. */ function islandora_ingest_get_approximate_steps(array $configuration) { - $fake_form_state = array( + try { + islandora_ingest_form_validate_configuration($configuration); + } + catch(InvalidArgumentException $e) { + // Don't log or display exception. + return array(); + } + $stubbed_form_state = array( 'islandora' => array( 'shared_storage' => $configuration, ), ); - $steps = islandora_ingest_form_get_steps($fake_form_state); + $steps = islandora_ingest_form_get_steps($stubbed_form_state); drupal_static_reset('islandora_ingest_form_get_steps'); return $steps; } From 789bebeb998a0c99631685bebc66ef602986b902 Mon Sep 17 00:00:00 2001 From: Nigel Banks Date: Wed, 6 Mar 2013 22:41:18 +0100 Subject: [PATCH 2/2] Some additional helper functions. islandora_is_valid_namespace() islandora_get_allowed_namespaces() islandora_get_content_models() --- includes/utilities.inc | 65 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/includes/utilities.inc b/includes/utilities.inc index 3efb6f28..f061f72c 100644 --- a/includes/utilities.inc +++ b/includes/utilities.inc @@ -79,6 +79,19 @@ function islandora_is_valid_pid($pid) { return drupal_strlen(trim($pid)) <= 64 && preg_match('/^([A-Za-z0-9]|-|\.)+:(([A-Za-z0-9])|-|\.|~|_|(%[0-9A-F]{2}))+$/', trim($pid)); } +/** + * Checks if the given namespace is valid. + * + * @param string $namespace + * The namespace to check without the ":" character. + * + * @return bool + * TRUE if valid, FALSE otherwise. + */ +function islandora_is_valid_namespace($namespace) { + return drupal_strlen(trim($namespace)) <= 64 && preg_match('/^([A-Za-z0-9]|-|\.)+$/', trim($namespace)); +} + /** * Checks if the given datastream id is valid. * @@ -673,3 +686,55 @@ function islandora_user_access_any(array $perms, $account = NULL) { } return FALSE; } + +/** + * Gets the list of allowed namespaces as defined by 'islandora_pids_allowed'. + * + * @return array + * The list of namespaces striped of trailing ":" characters. + */ +function islandora_get_allowed_namespaces() { + $matches = array(); + $allowed_namespaces = variable_get('islandora_pids_allowed', 'default: demo: changeme: islandora:'); + preg_match_all('/([A-Za-z0-9-\.]+):/', $allowed_namespaces, $matches); + return $matches[1]; +} + +/** + * Gets a list of all existing content models. + * + * If 'islandora_namespace_restriction_enforced' is set to true only return + * content models in the allowed namespace. + * + * @param bool $ignore_system_namespace + * Ignore content models in the 'fedora-system' namespace. + * + * @return array + * An associative array of all existing content models. + * - pid: The PID of the content model object. + * - pid: The PID of the content model object. + * - label: The label of the content model object. + */ +function islandora_get_content_models($ignore_system_namespace = TRUE) { + module_load_include('inc', 'islandora', 'includes/utilities'); + $tuque = islandora_get_tuque_connection(); + $query = 'select $object $label from <#ri> + where ($object $label + and ($object + or $object ) + and $object ) + order by $label'; + $content_models = array(); + $results = $tuque->repository->ri->itqlQuery($query, 'unlimited'); + foreach ($results as $result) { + $content_model = $result['object']['value']; + $label = $result['label']['value']; + $namespace = islandora_get_namespace($content_model); + $ignore = $ignore_system_namespace && $namespace == 'fedora-system'; + $ignore |= !islandora_namespace_accessible($namespace); + if (!$ignore) { + $content_models[$content_model] = array('pid' => $content_model, 'label' => $label); + } + } + return $content_models; +}