Browse Source

Merge branch '7.x' of github.com:Islandora/islandora into 7.x

pull/114/head
Paul Pound 13 years ago
parent
commit
3fd9819e64
  1. 1
      islandora_basic_collection/includes/ChildCollection.inc
  2. 130
      islandora_basic_collection/includes/CollectionManagement.inc
  3. 119
      islandora_basic_collection/includes/CollectionPolicy.inc
  4. 144
      islandora_basic_collection/includes/ManagePolicies.inc
  5. 4
      islandora_basic_collection/islandora_basic_collection.module

1
islandora_basic_collection/includes/ChildCollection.inc

@ -24,6 +24,7 @@ function islandora_create_child_collection_form($form, &$form_state, $this_colle
$form['child_creation']['collection_name'] = array(
'#title' => "Collection Name",
'#type' => 'textfield',
'#size' => 25,
'#description' => t("Human readable name for this collection"),
);

130
islandora_basic_collection/includes/CollectionManagement.inc

@ -435,17 +435,18 @@ function get_represented_content_models($pid) {
$restConnection = new RestConnection();
require_once 'sites/all/libraries/tuque/RepositoryQuery.php';
$query = "select \$model from <#ri>
$query = "select \$model \$title from <#ri>
where (\$object <info:fedora/fedora-system:def/relations-external#isMemberOf> <info:fedora/$pid>
or \$object <info:fedora/fedora-system:def/relations-external#isMemberOfCollection> <info:fedora/$pid>)
and \$object <info:fedora/fedora-system:def/model#hasModel> \$model";
and \$object <info:fedora/fedora-system:def/model#hasModel> \$model
and \$object <dc:title> \$title";
$model_pids = $restConnection->repository->ri->itqlQuery($query, 'unlimited', '0');
$represented_models = array();
foreach ($model_pids as $model_pid) {
if ($model_pid && $model_pid['object']['value'] != 'fedora-system:FedoraObject-3.0') {
if ($model_pid && $model_pid['model']['value'] != 'fedora-system:FedoraObject-3.0') {
$represented_models[$model_pid['object']['value']] = $model_pid['object']['value'] . ' ~ ' . $model_pid['title']['value'];
$represented_models[$model_pid['model']['value']] = $model_pid['model']['value'] . ' ~ ' . $model_pid['title']['value'];
}
}
return $represented_models;
@ -492,7 +493,6 @@ function Islandora_collections_get_collection_from_pid($pid) {
* @param <type> $query_format R
*/
function get_related_items_as_array($collection_pid, $relationship = array('isMemberOfCollection'), $limit = 10000, $offset = 0, $active_objects_only = TRUE, $cmodel = NULL, $orderby = '$title') {
// module_load_include('inc', 'fedora_repository', 'ObjectHelper');
module_load_include('inc', 'islandora', 'RestConnection');
$restConnection = new RestConnection();
require_once 'sites/all/libraries/tuque/RepositoryQuery.php';
@ -533,14 +533,6 @@ function get_related_items_as_array($collection_pid, $relationship = array('isMe
$query_string .= ')
minus $content <mulgara:is> <info:fedora/fedora-system:FedoraObject-3.0>
order by ' . $orderby;
// $query_string = htmlentities(urlencode($query_string));
//
//
// $content = '';
// $url = variable_get('fedora_repository_url', 'http://localhost:8080/fedora/risearch');
// $url .= "?type=tuples&flush=TRUE&format=Sparql&limit=$limit&offset=$offset&lang=itql&stream=on&query=" . $query_string;
// $content .= do_curl($url);
$results = $restConnection->repository->ri->itqlQuery($query_string, $limit, $offset);
@ -561,4 +553,114 @@ function get_related_items_as_array($collection_pid, $relationship = array('isMe
// $resultsarray[] = substr($result->object->attributes()->uri, 12); // Remove 'info:fedora/'.
// }
// return $resultsarray;
//}
//}
/**
* gets the name of the content models for the specified object
* this now returns an array of pids as in Fedora 3 we can have more then one Cmodel for an object
* @param type $pid
* @param type $include_fedora_system_content_models
* @return array
*/
function get_content_models_list($pid, $include_fedora_system_content_models = FALSE) {
// module_load_include('inc', 'fedora_repository', 'CollectionClass');
// module_load_include('inc', 'fedora_repository', 'ContentModel');
// $collectionHelper = new CollectionClass();
module_load_include('inc', 'islandora', 'RestConnection');
require_once 'sites/all/libraries/tuque/RepositoryQuery.php';
$restConnection = new RestConnection();
$pids = array();
$query = 'select $object from <#ri>
where <info:fedora/' . $pid . '> <fedora-model:hasModel> $object
and $object <fedora-model:state> <info:fedora/fedora-system:def/model#Active>';
// $content_models = $collectionHelper->getRelatedItems($pid, $query);
$content_models = $restConnection->repository->ri->itqlQuery($query, 'unlimited', '0');
if (empty($content_models)) {
return $pids;
}
$cmodels = array();
foreach ($content_models as $content_model) {
if (strpos($content_model['object']['uri'], 'fedora-system:FedoraObject-3.0') != FALSE && $include_fedora_system_content_models == FALSE) {
continue;
}
$cmodels[] = substr(strstr($content_model['object']['uri'], '/'), 1);
// $cm = ContentModel::loadFromModel($cmodel_pid);
// if ($cm) {
// $cmodels[] = $cm;
// }
}
return $cmodels;
}
/**
* Function: get_content_models_as_option_array
*
* Description: Returns an associative array of all available content models in Fedora instance
*
* @return array
*/
function get_content_models_as_option_array() {
module_load_include('inc', 'islandora', 'RestConnection');
$restConnection = new RestConnection();
require_once 'sites/all/libraries/tuque/RepositoryQuery.php';
$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';
$list = $restConnection->repository->ri->itqlQuery($query, 'unlimited', '0');
$options = array();
foreach ($list as $item) { //removes blanks
if ($item) {
$item_namespace = explode(':', $item['object']['value']);
if (!$restricted || in_array($item_namespace[0], $allowed)) {
if (!preg_match('/fedora-system/', $item['object']['value'])) {
$options[$item['object']['value']] = $item['title']['value'] . ' ~ ' . $item['object']['value'];
}
}
}
}
return $options;
}
function getContentModels($collection_pid, $showError = TRUE) {
module_load_include('inc', 'islandora', 'RestConnection');
$restConnection = new RestConnection();
// module_load_include('inc', 'Fedora_Repository', 'ContentModel');
$collection_stream = $this->getCollectionPolicyStream($collection_pid);
$collection_object = new FedoraObject($collection_pid, $restConnection->repository);
$collection_stream = $collection_object->getDatastream('COLLECTION_POLICY');
try {
$xml = new SimpleXMLElement($collection_stream);
} catch (Exception $e) {
if ($showError) {
drupal_set_message(t('@e', array('@e' => check_plain($e->getMessage()))), 'error');
}
return NULL;
}
foreach ($xml->contentmodels->contentmodel as $content_model) {
$contentModel = new ContentModel();
$contentModel->contentModelDsid = $content_model->dsid;
$contentModel->contentModelPid = $content_model->pid;
$contentModel->pidNamespace = $content_model->pidNamespace;
$contentModel->contentModelName = $content_model['name'];
$models[] = $contentModel;
}
return $models;
}

119
islandora_basic_collection/includes/CollectionPolicy.inc

@ -1,4 +1,5 @@
<?php
/**
* @file
* This file contains the classes for parsing the collection policy infomration.
@ -112,26 +113,29 @@ class CollectionPolicy {
*/
function addModel($cm, $namespace) {
$ret = FALSE;
if (self::valid_pid($namespace) && $this->validate() && $cm->validate()) {
// var_dump(self::valid_pid($namespace));
// var_dump($this->validate());
// var_dump($cm->validate());
// if (self::valid_pid($namespace) && $this->validate() && $cm->validate()) {
$contentmodelsEl = $this->xml->getElementsByTagName('content_models');
$models = $contentmodelsEl->item(0)->getElementsByTagName('content_model');
$found = FALSE;
for ($i = 0; !$found && $i < $models->length; $i++) {
if ($models->item($i)->getAttribute('pid') == $cm->pid)
$found = TRUE;
}
// for ($i = 0; !$found && $i < $models->length; $i++) {
// if ($models->item($i)->getAttribute('pid') == $cm->pid)
// $found = TRUE;
// }
if (!$found) {
// if (!$found) {
$cmEl = $this->xml->createElement('content_model');
$cmEl->setAttribute('name', $cm->getName());
$cmEl->setAttribute('name', $cm->name);
$cmEl->setAttribute('dsid', $cm->dsid);
$cmEl->setAttribute('namespace', $namespace);
$cmEl->setAttribute('pid', $cm->pid);
$contentmodelsEl->item(0)->appendChild($cmEl);
}
// }
$ret = !$found;
}
// }
return $ret;
}
@ -147,4 +151,101 @@ class CollectionPolicy {
return $ret;
}
/**
* valid_pid
* Validates a fedora PID based on the regexp provided in the fedora
* 3.3 documentation.
* http://www.fedora-commons.org/confluence/display/FCR30/Fedora+Identifiers
*
* @param String $pid
* @return boolean $valid
*/
public static function valid_pid($pid) {
$valid = FALSE;
if (strlen(trim($pid)) <= 64 && preg_match('/^([A-Za-z0-9]|-|\.)+:(([A-Za-z0-9])|-|\.|~|_|(%[0-9A-F]{2}))+$/', trim($pid))) {
$valid = TRUE;
}
return $valid;
}
/**
* Validates the XMLDatastream against the schema location
* defined by the xmlns:schemaLocation attribute of the root
* element. If the xmlns:schemaLocation attribute does not exist,
* then it is assumed to be the old schema and it attempts to convert
* using the convertFromOldSchema method.
*
* TODO: Maybe change it so that it always validates against a known
* schema. This makes more sense because this class assumes the structure
* to be known after it has been validated.
*
* @return boolean $valid
*/
public function validate() {
global $user;
if ($this->valid === NULL) {
$ret = TRUE;
if ($this->xml == NULL) {
$this->fetchXml();
}
// figure out if we're dealing with a new or old schema
$rootEl = $this->xml->firstChild;
if (!$rootEl->hasAttributes() || $rootEl->attributes->getNamedItem('schemaLocation') === NULL) {
//$tmpname = substr($this->pid, strpos($this->pid, ':') + 1);
$tmpname = user_password(10);
$this->convertFromOldSchema();
drupal_add_js("fedora_repository_print_new_schema_$tmpname = function(tagID) {
var target = document.getElementById(tagID);
var content = target.innerHTML;
var text = '<html><head><title>Title' +
'</title><body>' + content +'</body></html>';
printerWindow = window.open('', '', 'toolbar=no,location=no,' + 'status=no,menu=no,scrollbars=yes,width=650,height=400');
printerWindow.document.open();
printerWindow.document.write(text);
}", 'inline');
if (user_access('administer site configuration')) {
drupal_set_message('<span id="new_schema_' . $tmpname . '" style="display: none;">' . htmlentities($this->xml->saveXML()) . '</span>Warning: XMLDatastream performed conversion of \'' . $this->getIdentifier() . '\' from old schema. Please update the datastream. The new datastream contents are <a href="javascript:fedora_repository_print_new_schema_' . $tmpname . '(\'new_schema_' . $tmpname . '\')">here.</a> ');
}
$rootEl = $this->xml->firstChild;
}
$schemaLocation = NULL;
if ($this->forceSchema) {
// hack because you cant easily get the static property value from
// a subclass.
$vars = get_class_vars(get_class($this));
$schemaLocation = $vars['SCHEMA_URI'];
}
elseif ($rootEl->attributes->getNamedItem('schemaLocation') !== NULL) {
//figure out where the schema is located and validate.
list(, $schemaLocation) = preg_split('/\s+/', $rootEl->attributes->getNamedItem('schemaLocation')->nodeValue);
}
$schemaLocation = NULL;
return TRUE;
if ($schemaLocation !== NULL) {
if (!$this->xml->schemaValidate($schemaLocation)) {
$ret = FALSE;
$errors = libxml_get_errors();
foreach ($errors as $err) {
self::$errors[] = 'XML Error: Line ' . $err->line . ': ' . $err->message;
}
}
else {
$this->name = $rootEl->attributes->getNamedItem('name')->nodeValue;
}
}
else {
$ret = FALSE;
self::$errors[] = 'Unable to load schema.';
}
$this->valid = $ret;
}
return $this->valid;
}
}

144
islandora_basic_collection/includes/ManagePolicies.inc

@ -1,12 +1,55 @@
<?php
function islandora_manage_policies_form($form, &$form_state, $collection_pid) {
module_load_include('inc', 'islandora_basic_collection', 'includes/CollectionPolicy');
module_load_include('inc', 'islandora', 'RestConnection');
$restConnection = new RestConnection();
$content_models = get_content_models_list($collection_pid);
$cm_options = array();
$name_mappings = array();
foreach ($content_models as $content_model) {
if ($content_model != "islandora:collectionCModel") {
$item = new FedoraObject($content_model, $restConnection->repository);
$cm_name = $item->Label;
$cm_options[$content_model] = $cm_name;
}
}
$new_options = array();
if (is_array($content_models) && is_array($cm_options)) {
$new_options = array_diff_key($content_models, $cm_options);
}
$namespace = substr($collection_pid, 0, strpos($collection_pid, ":"));
$collection_policy_dsid = variable_get('Islandora_Collection_Policy_DSID', 'COLLECTION_POLICY');
$collection_object = new FedoraObject($collection_pid, $restConnection->repository);
$collection_policy_string = $collection_object->getDatastream($collection_policy_dsid);
$supported_collection_models = array();
if ($collection_policy_string) {
$collection_policy = new CollectionPolicy($collection_policy_string->content);
$supported_collection_models = $collection_policy->getContentModels();
}
$collection_namespace = substr($collection_pid, 0, strpos($collection_pid, ":"));
$represented_content_models = get_represented_content_models($collection_pid);
$collection_name = $collection_object->label;
$new_content_models = get_content_models_as_option_array();
$current_models_in_policy = array();
if ($supported_collection_models) {
foreach ($supported_collection_models as $supported_model) {
$current_models_in_policy[$supported_model['pid']] = $supported_model['pid'];
}
}
$new_options = array();
if (is_array($new_content_models) && is_array($current_models_in_policy)) {
$new_options = array_diff_key($new_content_models, $current_models_in_policy);
}
$form ['manage_collection_policy']['add']['content_model_to_add'] = array(
'#title' => "Choose Content Model",
'#type' => 'select',
@ -18,9 +61,20 @@ function islandora_manage_policies_form($form, &$form_state, $collection_pid) {
'#title' => "Choose Namespace",
'#type' => 'textfield',
'#size' => 15,
'#default_value' => $collection_namespace,
'#default_value' => $namespace,
'#description' => t("Choose namespace for objects in this collection associated with this content model"),
);
$form['parent_collection'] = array(
'#type' => 'hidden',
'#value' => $collection_pid,
);
$form['collection_pid'] = array(
'#type' => 'hidden',
'#value' => $collection_pid,
);
$form['manage_collection_policy']['add']['submit'] = array(
'#type' => 'submit',
'#value' => t('Add Content Model to Collection Policy'),
@ -57,5 +111,91 @@ function islandora_manage_policies_form_validate($form, &$form_state) {
}
function islandora_manage_policies_form_submit($form, &$form_state) {
module_load_include('inc', 'islandora_basic_collection', 'CollectionPolicy');
module_load_include('inc', 'islandora', 'RestConnection');
$restConnection = new RestConnection();
$collection_pid = $form_state['values']['parent_collection'];
$collection_object = new FedoraObject($collection_pid, $restConnection->repository);
$policy = $collection_object->getDatastream(variable_get('Islandora_Collection_Policy_DSID', 'COLLECTION_POLICY'));
$collection_policy = '<?xml version="1.0" encoding="UTF-8"?>
<collection_policy xmlns="http://www.islandora.ca" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="" xsi:schemaLocation="http://www.islandora.ca http://syn.lib.umanitoba.ca/collection_policy.xsd">
<content_models>
<content_model dsid="ISLANDORACM" name="Islandora Collection Model ~ islandora:collectionCModel" namespace="islandora:1" pid="islandora:collectionCModel"/>
</content_models>
<search_terms>
</search_terms>
<staging_area></staging_area>
<relationship>isMemberOfCollection</relationship>
</collection_policy>';
$collection_policy_xml = new DOMDocument();
$collection_policy_xml->loadXML($policy->content);
if ($form_state['clicked_button']['#id'] == 'add_cm') {
if (empty($policy->content)) {
$collection_policy_datastream = $collection_object->constructDatastream('COLLECTION_POLICY', 'X');
$collection_policy_datastream->setContentFromString($collection_policy);
$collection_policy_datastream->label = 'COLLECTION_POLICY';
$collection_object->ingestDatastream($collection_policy_datastream);
$policy = $collection_object->getDatastream(variable_get('Islandora_Collection_Policy_DSID', 'COLLECTION_POLICY'));
}
$cp_namespace = $form_state['values']['new_cp_namespace'];
$cp_content_model = $form_state['values']['content_model_to_add'];
$content_model_object = new FedoraObject($cp_content_model, $restConnection->repository);
$content_model_datastream = $content_model_object->getDatastream(variable_get('Islandora_Content_Model_DSID', 'ISLANDORACM'));
$content_models_element = $collection_policy_xml->getElementsByTagName('content_models');
$content_model_element = $content_models_element->item(0)->getElementsByTagName('content_model');
$content_model_element = $collection_policy_xml->createElement('content_model');
$content_model_element->setAttribute('name', $content_model_object->label);
$content_model_element->setAttribute('dsid', variable_get('Islandora_Content_Model_DSID', 'ISLANDORACM'));
$content_model_element->setAttribute('namespace', $cp_namespace);
$content_model_element->setAttribute('pid', $content_model_object->id);
$content_models_element->item(0)->appendChild($content_model_element);
$new_collection_policy_datastream = $collection_object->getDatastream('COLLECTION_POLICY');
$new_collection_policy_datastream->setContentFromString($collection_policy_xml->saveXML());
$new_collection_policy_datastream->label = 'COLLECTION_POLICY';
$collection_object->ingestDatastream($new_collection_policy_datastream);
drupal_set_message("Collection model successfully added");
}
//remove content model from collection policy
if ($form_state['clicked_button']['#id'] == 'remove_cm') {
$candidates = $form_state['values']['content_models_to_remove'];
$count = 0;
$s = '';
foreach ($candidates as $candidate) {
if (is_string($candidate)) {
$content_models_element = $collection_policy_xml->getElementsByTagName('content_models');
$models = $content_models_element->item(0)->getElementsByTagName('content_model');
$found = FALSE;
for ($i = 0; $found === FALSE && $i < $models->length; $i++) {
if ($models->item($i)->getAttribute('pid') == $candidate) {
$found = $models->item($i);
}
}
if ($found !== FALSE && $models->length > 1) {
$content_models_element->item(0)->removeChild($found);
$ret = TRUE;
}
$count++;
}
}
if ($count > 0) {
$new_collection_policy_datastream = $collection_object->getDatastream('COLLECTION_POLICY');
$new_collection_policy_datastream->setContentFromString($collection_policy_xml->saveXML());
$new_collection_policy_datastream->label = 'COLLECTION_POLICY';
$collection_object->ingestDatastream($new_collection_policy_datastream);
if ($count > 1) {
$s = 's';
}
drupal_set_message("$count collection model$s removed");
}
}
}

4
islandora_basic_collection/islandora_basic_collection.module

@ -30,8 +30,8 @@ function islandora_basic_collection_menu() {
$items = array();
$items['islandora/object/%/manage/collection'] = array(
'title' => 'Collection Related',
'page callback' => 'drupal_get_form',
'page arguments' => array('islandora_basic_collection_manage_object', 2),
'page callback' => 'islandora_basic_collection_manage_object',
'page arguments' => array(2),
'type' => MENU_LOCAL_TASK,
'access callback' => 'islandora_basic_collection_access',
'access arguments' => array(2),

Loading…
Cancel
Save