Browse Source

Merge pull request #168 from willtp87/ISLANDORA-624

Islandora 624
pull/164/merge
Jonathan Green 12 years ago
parent
commit
e70586e27f
  1. 157
      api/fedora_utils.inc
  2. 115
      fedora_repository.module

157
api/fedora_utils.inc

@ -1,23 +1,23 @@
<?php
/**
* @file
* Base utilities used by the Islandora fedora module.
*/
if (!function_exists('str_getcsv')) {
/**
* Functions that emulate php5.3 functionality for backwards compatiablity
*
* @param type $input
* @param type $delimiter
* @param type $enclosure
* @param type $escape
* @param type $eol
*
* @return type
*/
function str_getcsv($input, $delimiter=',', $enclosure='"', $escape=NULL, $eol=NULL) {
function str_getcsv($input, $delimiter = ',', $enclosure = '"', $escape = NULL, $eol = NULL) {
$temp = fopen("php://memory", "rw");
fwrite($temp, $input);
fseek($temp, 0);
@ -50,7 +50,7 @@ if (!function_exists('str_getcsv')) {
* @param $post
* Whether the curl_exec is done as a "get" or a "post"
*
* @return
* @return mixed
* TRUE, FALSE, NULL, or the data returned from accessing the URL
*/
function do_curl($url, $return_to_variable = 1, $number_of_post_vars = 0, $post = NULL) {
@ -59,7 +59,6 @@ function do_curl($url, $return_to_variable = 1, $number_of_post_vars = 0, $post
}
/**
*
* Utility method to get data from a url location using curl_exec
*
* This method takes a URL and three associated parameters and initializes the
@ -70,7 +69,7 @@ function do_curl($url, $return_to_variable = 1, $number_of_post_vars = 0, $post
* Various defaults are used for the parameters required by curl_exec including
* the user agent and timeout. These are hard-coded.
*
* @param $url
* @param string $url
* URL to be accessed by the function
* @param $return_to_variable
* Indicates whether the resource accessed by the curl call (HTML page,
@ -81,7 +80,7 @@ function do_curl($url, $return_to_variable = 1, $number_of_post_vars = 0, $post
* @param $post
* Whether the curl_exec is done as a "get" or a "post"
*
* @return
* @return mixed
* an array that consists of three value or NULL if curl is not suported:
* - element 0:
* The value returned from the curl_exec function call.
@ -112,11 +111,15 @@ function do_curl_ext($url, $return_to_variable = TRUE, $number_of_post_vars = 0,
$user_agent = "Mozilla/4.0 pp(compatible; MSIE 5.01; Windows NT 5.0)";
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_FAILONERROR, TRUE); // Fail on errors
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); // allow redirects
curl_setopt($ch, CURLOPT_TIMEOUT, 90); // times out after 90s
// Fail on errors.
curl_setopt($ch, CURLOPT_FAILONERROR, TRUE);
// Allow redirects.
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
// Times out after 90s.
curl_setopt($ch, CURLOPT_TIMEOUT, 90);
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, $return_to_variable); // return into a variable
// Return into a variable.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, $return_to_variable);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, "$fedora_user:$fedora_pass");
//curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
@ -141,7 +144,8 @@ function do_curl_ext($url, $return_to_variable = TRUE, $number_of_post_vars = 0,
/**
* Fedora available
* @return type
*
* @return mixed
*/
function fedora_available() {
@ -151,7 +155,8 @@ function fedora_available() {
/**
* Resource index search available
* @return type
*
* @return mixed
*/
function risearch_available() {
@ -161,7 +166,9 @@ function risearch_available() {
/**
* Returns a UTF-8-encoded transcripiton of the string given in $in_str.
*
* @param string $in_str
*
* @return string A UTF-8 encoded string.
*/
function fix_encoding($in_str) {
@ -176,7 +183,9 @@ function fix_encoding($in_str) {
/**
* valid pid ??
*
* @param type $pid
*
* @return boolean
*/
function valid_pid($pid) {
@ -190,7 +199,9 @@ function valid_pid($pid) {
/**
* Valid Dsid ??
*
* @param type $dsid
*
* @return boolean
*/
function valid_dsid($dsid) {
@ -204,7 +215,9 @@ function valid_dsid($dsid) {
/**
* fixDsid ??
*
* @param type $dsid
*
* @return string
*/
function fix_dsid($dsid) {
@ -214,14 +227,17 @@ function fix_dsid($dsid) {
$replace = '';
$new_dsid = preg_replace($find, $replace, $new_dsid);
if (strlen($new_dsid) > 63)
if (strlen($new_dsid) > 63) {
$new_dsid = substr($new_dsid, -63);
}
if (preg_match('/^[^a-zA-Z]/', $dsid))
if (preg_match('/^[^a-zA-Z]/', $dsid)) {
$new_dsid = 'x' . $new_dsid;
}
if (strlen($new_dsid) == 0)
if (strlen($new_dsid) == 0) {
$new_dsid = 'item' . rand(1, 100);
}
return $new_dsid;
}
@ -229,9 +245,8 @@ function fix_dsid($dsid) {
/**
* Function: get_collections_as_option_array
*
* Description: Returns an associative array of all collection objects in Fedora instance
*
* @return array
* Returns an associative array of all collection objects in Fedora instance
*/
function get_collections_as_option_array() {
module_load_include('inc', 'fedora_repository', 'api/fedora_utils');
@ -254,7 +269,7 @@ function get_collections_as_option_array() {
'stream' => 'on',
'query' => $query,
);
//The url function will take care of URL encoding...
// The url function will take care of URL encoding.
$content = do_curl(url($url, array('query' => $options)));
$list = explode("\n", $content);
@ -264,7 +279,8 @@ function get_collections_as_option_array() {
$trimmed_names[] = trim($namespace);
}
$options = array();
foreach ($list as $item) { //removes blanks
// Removes blanks.
foreach ($list as $item) {
if ($item) {
$parts = explode(',', $item);
$namespace = explode(':', $parts[0]);
@ -283,46 +299,75 @@ function get_collections_as_option_array() {
/**
* Function: get_content_models_as_option_array
*
* Description: Returns an associative array of all available content models in Fedora instance
*
* @return array
* associative array of all available content models in Fedora instance
*/
function get_content_models_as_option_array() {
module_load_include('inc', 'fedora_repository', 'api/fedora_item');
module_load_include('inc', 'fedora_repository', 'api/fedora_utils');
$restricted = variable_get('fedora_namespace_restriction_enforced', TRUE);
$allowed_string = variable_get('fedora_pids_allowed', 'default: demo: changeme: islandora:');
$namespaces = explode(':', $allowed_string);
foreach ($namespaces as $namespace) {
if ($namespace) {
$allowed[] = trim($namespace);
}
module_load_include('inc', 'fedora_repository', 'api/fedora_item');
module_load_include('inc', 'fedora_repository', 'api/fedora_utils');
$restricted = variable_get('fedora_namespace_restriction_enforced', TRUE);
$allowed_string = variable_get('fedora_pids_allowed', 'default: demo: changeme: islandora:');
$namespaces = explode(':', $allowed_string);
foreach ($namespaces as $namespace) {
if ($namespace) {
$allowed[] = trim($namespace);
}
$query = 'select $object $title from <#ri>
where ($object <fedora-model:label> $title
and ($object <fedora-model:hasModel> <info:fedora/fedora-system:ContentModel-3.0>
or $object <fedora-rels-ext:isMemberOfCollection> <info:fedora/islandora:ContentModelsCollection>)
and $object <fedora-model:state> <info:fedora/fedora-system:def/model#Active>)
order by $title';
$url = variable_get('fedora_repository_url', 'http://localhost:8080/fedora/risearch');
$url .= "?type=tuples&flush=TRUE&format=csv&limit=1000&lang=itql&stream=on&query=";
$content = do_curl($url . htmlentities(urlencode($query)));
$list = explode("\n", $content);
array_shift($list);
$list = preg_replace('/info:fedora\//', '', $list);
foreach ($list as $item) { //removes blanks
if ($item) {
$parts = explode(',', $item);
$nameparts = explode(':', $parts[0]);
if (!$restricted || in_array($nameparts[0], $allowed)) {
if (!preg_match('/fedora-system/', $nameparts[0])) {
$options[$parts[0]] = $parts[1] . ' ~ ' . $parts[0];
}
}
}
$query = 'select $object $title from <#ri>
where ($object <fedora-model:label> $title
and ($object <fedora-model:hasModel> <info:fedora/fedora-system:ContentModel-3.0>
or $object <fedora-rels-ext:isMemberOfCollection> <info:fedora/islandora:ContentModelsCollection>)
and $object <fedora-model:state> <info:fedora/fedora-system:def/model#Active>)
order by $title';
$url = variable_get('fedora_repository_url', 'http://localhost:8080/fedora/risearch');
$url .= "?type=tuples&flush=TRUE&format=csv&limit=1000&lang=itql&stream=on&query=";
$content = do_curl($url . htmlentities(urlencode($query)));
$list = explode("\n", $content);
array_shift($list);
$list = preg_replace('/info:fedora\//', '', $list);
// Removes blanks.
foreach ($list as $item) {
if ($item) {
$parts = explode(',', $item);
$nameparts = explode(':', $parts[0]);
if (!$restricted || in_array($nameparts[0], $allowed)) {
if (!preg_match('/fedora-system/', $nameparts[0])) {
$options[$parts[0]] = $parts[1] . ' ~ ' . $parts[0];
}
}
}
}
return $options;
}
/**
* This function will retrieve the collections that the given PID belongs to.
*
* @param string $PID
* The PID to find the parents of.
*
* @return array
* $object_PIDs the list of PIDs of the collections that the
* indicated object is a member of.
*/
function get_parent_collections_from_pid($PID) {
$query_string = 'select $parent from <#ri>
where ($object <fedora-rels-ext:isMemberOf> $parent
or $object <fedora-rels-ext:isMemberOfCollection> $parent)
and $object <dc:identifier> \'' . $PID . '\'
order by $object';
$query_string = htmlentities(urlencode($query_string));
$url = variable_get('fedora_repository_url', 'http://localhost:8080/fedora/risearch');
$url .= '?type=tuples&flush=true&format=csv&limit=13000&lang=itql&stream=on&query=' . $query_string;
$content = do_curl($url, TRUE);
$results = explode("\n", $content);
$object_PIDs = preg_replace('/^info:fedora\/|"parent"| /', '', $results);
$object_PIDs = array_values(array_filter($object_PIDs));
return $options;
return $object_PIDs;
}

115
fedora_repository.module

@ -40,8 +40,10 @@ function fedora_repository_help($path, $arg) {
/**
* fedora repository purge object
*
* @param type $pid
* @param type $name
*
* @return type
*/
function fedora_repository_purge_object($pid = NULL, $name = NULL) {
@ -64,9 +66,11 @@ function fedora_repository_purge_object($pid = NULL, $name = NULL) {
/**
* fedora repository ingest object
*
* @param type $collection_pid
* @param type $collection_label
* @param type $content_model
*
* @return type
*/
function fedora_repository_ingest_object($collection_pid=NULL, $collection_label = NULL, $content_model = NULL) {
@ -266,11 +270,15 @@ function fedora_repository_ingest_form(&$form_state, $collection_pid, $collectio
/**
* fedora repository purge object form
*
* @global type $base_url
*
* @param type $form_state
* @param type $pid
* @param type $referrer
* @return type
*
* @return mixed
* NULL or the form array.
*/
function fedora_repository_purge_object_form(&$form_state, $pid, $referrer = NULL) {
global $base_url;
@ -282,7 +290,7 @@ function fedora_repository_purge_object_form(&$form_state, $pid, $referrer = NUL
}
$form['pid'] = array(
'#type' => 'hidden',
'#value' => "$pid"
'#value' => "$pid",
);
if (!strstr(drupal_get_destination(), urlencode('fedora/repository'))) {
$form['referrer'] = array(
@ -291,27 +299,26 @@ function fedora_repository_purge_object_form(&$form_state, $pid, $referrer = NUL
);
}
if (!isset($form_state['storage']['confirm'])) {
// do your normal $form definition here
// Do your normal $form definition here.
$form['submit'] = array(
'#type' => 'image_button',
'#src' => drupal_get_path('module', 'fedora_repository') . '/images/purge_big.png',
'#value' => t('Purge'),
'#suffix' => 'Purge this object',
);
if (!empty($collectionPid)) {
$collectionPid = $_SESSION['fedora_collection'];
}
//$form['#rebuild'] = $false;
return $form;
}
else {
// ALSO do $form definition here. Your final submit handler (after user clicks Yes, I Confirm) will only see $form_state info defined here. Form you create here passed as param1 to confirm_form
/* ALSO do $form definition here. Your final submit handler
* (after user clicks Yes, I Confirm)
* will only see $form_state info defined here.
* Form you create here passed as param1 to confirm_form*/
return confirm_form($form, 'Confirm Purge Object', $referrer, 'Are you sure you want to delete this object? This action cannot be undone.', 'Delete', 'Cancel'); //Had better luck leaving off last param 'name'
// Had better luck leaving off last param 'name'.
return confirm_form($form, 'Confirm Purge Object', $referrer, 'Are you sure you want to delete this object? This action cannot be undone.', 'Delete', 'Cancel');
}
return $form;
}
@ -487,18 +494,26 @@ function fedora_repository_purge_stream($pid = NULL, $dsId = NULL, $name = NULL)
return $output;
}
/**
* Validates the purge object form.
*
* @param array $form
* The form to validate.
* @param array $form_state
* The state of the form to validate
*/
function fedora_repository_purge_object_form_validate($form, &$form_state) {
module_load_include('inc', 'fedora_repository', 'api/fedora_collection');
$pid = $form_state['values']['pid'];
$objectHelper = new ObjectHelper();
$contentModels = $objectHelper->get_content_models_list($pid);
foreach ($contentModels as $contentModel) {
if ($contentModel->pid == 'islandora:collectionCModel') {
$object_helper = new ObjectHelper();
$content_models = $object_helper->get_content_models_list($pid);
foreach ($content_models as $content_model) {
if ($content_model->pid == 'islandora:collectionCModel') {
$member_pids = get_related_items_as_array($pid, 'isMemberOfCollection');
if (is_array($member_pids) && ! empty($member_pids)) {
form_set_error('new_collection_pid', t("Please purge all members of this collection before deleting the collection itself."));
return;
form_set_error('new_collection_pid', t("Please purge all members of this collection before deleting the collection itself."));
return;
}
}
}
@ -506,27 +521,37 @@ function fedora_repository_purge_object_form_validate($form, &$form_state) {
/**
* fedora repository purge object form submit
*
* @param type $form
* The submited form.
* @param type $form_state
* @return type
* The state of the submitted form.
*/
function fedora_repository_purge_object_form_submit($form, &$form_state) {
module_load_include('inc', 'fedora_repository', 'ConnectionHelper');
module_load_include('inc', 'fedora_repository', 'api/fedora_utils');
$pid = $form_state['values']['pid'];
$parents = get_parent_collections_from_pid($pid);
if (!isset($form_state['storage']['confirm'])) {
$form_state['storage']['confirm'] = TRUE; // this will cause the form to be rebuilt, entering the confirm part of the form
$form_state['rebuild'] = TRUE; // along with this
/* This will cause the form to be rebuilt,
* entering the confirm part of the form.*/
$form_state['storage']['confirm'] = TRUE;
// Along with this.
$form_state['rebuild'] = TRUE;
}
else {
// this is where you do your processing after they have pressed the confirm button
/* This is where you do your processing after
* they have pressed the confirm button.*/
$params = array(
"pid" => $pid,
"logMessage" => "Purged",
"force" => ""
"force" => "",
);
try {
$soapHelper = new ConnectionHelper();
$client = $soapHelper->getSoapClient(variable_get('fedora_soap_manage_url', 'http://localhost:8080/fedora/wsdl?api=API-M'));
$soap_helper = new ConnectionHelper();
$client = $soap_helper->getSoapClient(variable_get('fedora_soap_manage_url', 'http://localhost:8080/fedora/wsdl?api=API-M'));
$object = $client->__soapCall('purgeObject', array($params));
unset($form_state['storage']['confirm']);
} catch (exception $e) {
@ -538,39 +563,33 @@ function fedora_repository_purge_object_form_submit($form, &$form_state) {
}
return;
}
if (!empty($form_state['values']['referrer'])) {
$form_state['redirect'] = $form_state['values']['referrer'];
}
elseif (empty($collectionPid) && !empty($_SESSION['fedora_collection']) && $_SESSION['fedora_collection'] != $pid) {
$collectionPid = $_SESSION['fedora_collection'];
$form_state['redirect'] = "fedora/repository/$collectionPid/";
}
else {
$form_state['redirect'] = 'fedora/repository/';
}
// Set the form's redirect to its first identifiable parent collection.
$form_state['redirect'] = "fedora/repository/$parents[0]/";
}
}
/**
* fedora repository purge stream form
*
* @param type $form_state
* @param type $pid
* @param type $dsId
*
* @return type
*/
function fedora_repository_purge_stream_form(&$form_state, $pid, $dsId) {
$form['pid'] = array(
'#type' => 'hidden',
'#value' => "$pid"
'#value' => "$pid",
);
$form['dsid'] = array(
'#type' => 'hidden',
'#value' => "$dsId"
'#value' => "$dsId",
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Purge')
'#value' => t('Purge'),
);
return $form;
@ -578,7 +597,9 @@ function fedora_repository_purge_stream_form(&$form_state, $pid, $dsId) {
/**
* fedora repository purge stream form submit
*
* @global type $base_url
*
* @param type $form
* @param array $form_state
*/
@ -596,6 +617,13 @@ function fedora_repository_purge_stream_form_submit($form, &$form_state) {
$form_state['redirect'] = "fedora/repository/$pid";
}
/**
*
* @param unknown_type $form_state
* @param unknown_type $pid
* @param unknown_type $dsid
* @param unknown_type $label
*/
function fedora_repository_download_datastream_form(&$form_state, $pid, $dsid, $label) {
module_load_include('inc', 'fedora_repository', 'ObjectHelper');
$form = array(
@ -621,7 +649,7 @@ function fedora_repository_download_datastream_form(&$form_state, $pid, $dsid, $
if (count($version_array) > 1) {
$form['#attributes'] = array(
'onsubmit' => 'this.action="' . $form['#action'] . '/" + this.version.value;'
'onsubmit' => 'this.action="' . $form['#action'] . '/" + this.version.value;',
);
$form['version'] = array(
'#type' => 'select',
@ -635,11 +663,13 @@ function fedora_repository_download_datastream_form(&$form_state, $pid, $dsid, $
/**
* fedora repository replace stream
*
* @param type $pid
* @param type $dsId
* @param type $dsLabel
* @param type $collectionName
* @return type
*
* @return string
*/
function fedora_repository_replace_stream($pid, $dsId, $dsLabel = '', $collectionName = NULL) {
if ($pid == NULL || $dsId == NULL) {
@ -653,10 +683,12 @@ function fedora_repository_replace_stream($pid, $dsId, $dsLabel = '', $collectio
/**
* fedora repository replace stream form
*
* @param type $form_state
* @param type $pid
* @param type $dsId
* @param type $dsLabel
*
* @return type
*/
function fedora_repository_replace_stream_form(&$form_state, $pid, $dsId, $dsLabel) {
@ -675,7 +707,7 @@ function fedora_repository_replace_stream_form_validate($form, &$form_state) {
// If a file was uploaded, process it.
if (isset($_FILES['files']) && is_uploaded_file($_FILES['files']['tmp_name']['file'])) {
// attempt to save the uploaded file
// Attempt to save the uploaded file.
$file = file_save_upload('file', array(), file_directory_path());
// set error is file was not uploaded
@ -843,7 +875,8 @@ function fedora_repository_edit_qdc_form_submit($form, &$form_state) {
$soap_helper = new ConnectionHelper();
$client = $soap_helper->getSoapClient(variable_get('fedora_soap_manage_url', 'http://localhost:8080/fedora/wsdl?api=API-M'));
// Check the content model for a custom edit metadata form submit function.
/* Check the content model for a custom
* edit metadata form submit function.*/
if (isset($form_state['values']['pid'])) {
module_load_include('inc', 'fedora_repository', 'ContentModel');
if (($cm = ContentModel::loadFromObject($form_state['values']['pid'])) !== FALSE) {

Loading…
Cancel
Save