From 789bebeb998a0c99631685bebc66ef602986b902 Mon Sep 17 00:00:00 2001 From: Nigel Banks Date: Wed, 6 Mar 2013 22:41:18 +0100 Subject: [PATCH] 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; +}