Browse Source

Message passing between drupal and camel. (#201)

* Message passing between drupal and camel updated for sprint-002
7.x-2.x
Nick Ruest 9 years ago committed by Diego Pino Navarro
parent
commit
103e8994bf
  1. 61
      islandora/include/admin.form.inc
  2. 21
      islandora/include/rdf.inc
  3. 70
      islandora/include/triplestore.inc
  4. 24
      islandora/include/utils.inc
  5. 79
      islandora/islandora.module
  6. 9
      islandora_basic_image/islandora_basic_image.module
  7. 9
      islandora_collection/islandora_collection.module

61
islandora/include/admin.form.inc

@ -0,0 +1,61 @@
<?php
/**
* @file
* Admin form.
*/
/**
* Form function for module config.
*
* @param array $form
* Drupal renderable array.
* @param array $form_state
* Drupal form state.
*
* @return array
* Final form to render.
*/
function islandora_config_form(array $form, array &$form_state) {
$form = array(
'islandora_stomp_url' => array(
'#type' => 'textfield',
'#title' => t('Stomp URL'),
'#default_value' => variable_get('islandora_stomp_url', 'tcp://localhost:61613'),
'#description' => t('URL to the Stomp broker for async messaging'),
'#element_validate' => array('islandora_config_form_validate_stomp_url'),
'#required' => TRUE,
),
'islandora_triplestore_index_queue' => array(
'#type' => 'textfield',
'#title' => t('Triplestore Index Queue'),
'#default_value' => variable_get('islandora_triplestore_index_queue', 'islandora/triplestore/index'),
'#description' => t('Queue to send triplestore indexing messages'),
'#required' => TRUE,
),
);
return system_settings_form($form);
}
/**
* Stomp broker url validator.
*
* @param array $element
* Form element for stomp broker url.
* @param array $form_state
* Drupal form state.
* @param array $form
* Drupal renderable array.
*/
function islandora_config_form_validate_stomp_url(array $element, array &$form_state, array $form) {
// Try to connect to the server.
try {
$url = $element['#value'];
$stomp = new Stomp($url);
unset($stomp);
}
catch (StompException $e) {
form_error($element, t('Connection failed for @url: @message', array('@url' => $url, '@message' => $e->getMessage())));
}
}

21
islandora/include/rdf.inc

@ -0,0 +1,21 @@
<?php
/**
* @file
* Utility functions for working with RDF.
*/
/**
* Serializes a Drupal node to N-Triples.
*
* @param object $node
* Node to serialize.
*
* @return string
* RDF serialized as N-Triples.
*/
function islandora_serialize_node_to_triples($node) {
$rdf_model = rdfx_get_rdf_model('node', $node);
$serializer = ARC2::getSer('NTriples', array('ns' => $rdf_model->ns));
return $serializer->getSerializedIndex($rdf_model->index);
}

70
islandora/include/triplestore.inc

@ -0,0 +1,70 @@
<?php
/**
* @file
* Triplestore indexing functionality.
*/
/**
* Enqueues a triplestore upsert action.
*
* @param object $node
* Node to upsert in index.
*
* @return bool
* True if publishing the message was successful.
*/
function islandora_triplestore_upsert($node) {
return islandora_triplestore_enqueue_action($node, 'upsert');
}
/**
* Enqueues a triplestore delete action.
*
* @param object $node
* Node to delete from index.
*
* @return bool
* True if publishing the message was successful.
*/
function islandora_triplestore_delete($node) {
return islandora_triplestore_enqueue_action($node, 'delete');
}
/**
* Enqueues a triplestore index action.
*
* @param object $node
* Node to index.
* @param string $action
* Action to take. Either 'upsert' or 'delete' at the moment.
*
* @return bool
* True if publishing the message was successful.
*/
function islandora_triplestore_enqueue_action($node, $action) {
module_load_include('inc', 'islandora', 'include/rdf');
$broker_url = variable_get("islandora_stomp_url", "tcp://localhost:61613");
$queue = variable_get("islandora_triplestore_index_queue", "islandora/triplestore/index");
$msg = islandora_serialize_node_to_triples($node);
// Add enough headers to fool the FcrepoCamel code we're re-using to do this.
global $base_url;
$headers = array(
'action' => $action,
'Content-Type' => 'application/n-triples',
'CamelFcrepoBaseUrl' => $base_url,
'CamelFcrepoIdentifier' => "/node/{$node->nid}",
);
try {
$stomp = new Stomp($broker_url);
$stomp->send($queue, $msg, $headers);
unset($stomp);
return TRUE;
}
catch (StompException $e) {
watchdog('islandora', 'Error publishing triplestore index action to Stomp broker: @err', array('@err' => $e->getMessage()), WATCHDOG_ERROR);
return FALSE;
}
}

24
islandora/include/utils.inc

@ -0,0 +1,24 @@
<?php
/**
* @file
* Miscellaneous utility functions for the islandora module.
*/
/**
* Predicate to determine if we should ignore hooks that talk to Fedora.
*
* @return bool
* True if we shouldn't talk to Fedora (breaks the cycle).
*/
function islandora_ignore_hooks() {
$header_is_set = isset($_SERVER['HTTP_IGNORE_HOOKS']);
if (!$header_is_set) {
return FALSE;
}
$header_is_true = strcmp(strtolower($_SERVER['HTTP_IGNORE_HOOKS']), "true") == 0;
return $header_is_true;
}

79
islandora/islandora.module

@ -28,6 +28,24 @@ define('ISLANDORA_FEDORA_PATH_FIELD', 'field_fedora_path');
define('ISLANDORA_FEDORA_HAS_PARENT_FIELD', 'field_fedora_has_parent'); define('ISLANDORA_FEDORA_HAS_PARENT_FIELD', 'field_fedora_has_parent');
define('ISLANDORA_PCDM_HAS_MEMBER_FIELD', 'field_pcdm_has_member'); define('ISLANDORA_PCDM_HAS_MEMBER_FIELD', 'field_pcdm_has_member');
/**
* Implements hook_menu().
*/
function islandora_menu() {
$items = array(
'admin/islandora/config' => array(
'title' => 'Islandora',
'description' => 'Configure settings for Islandora',
'page callback' => 'drupal_get_form',
'page arguments' => array('islandora_config_form'),
'file' => '/include/admin.form.inc',
'access arguments' => array('administer site configuration'),
'type' => MENU_NORMAL_ITEM,
),
);
return $items;
}
/** /**
* Implements hook_ctools_plugin_api(). * Implements hook_ctools_plugin_api().
*/ */
@ -67,6 +85,67 @@ function islandora_entity_load($entities, $type) {
} }
} }
/**
* Implements hook_node_postinsert().
*/
function islandora_node_postinsert($node) {
module_load_include('inc', 'islandora', 'include/utils');
if (islandora_ignore_hooks()) {
return;
}
$nodes = entity_uuid_load('node', array($node->uuid));
// Exit early if the node never was successfully inserted in the database.
if (empty($nodes)) {
return;
}
$node = array_pop($nodes);
module_load_include('inc', 'islandora', 'include/triplestore');
islandora_triplestore_upsert($node);
}
/**
* Implements hook_node_postupdate().
*/
function islandora_node_postupdate($node) {
module_load_include('inc', 'islandora', 'include/utils');
if (islandora_ignore_hooks()) {
return;
}
// Unfortunately, there's no way to tell if the db transaction got rolled
// back in this hook, so we'll default to doing it anyway so as not to miss
// any updates. Open to suggestions on this one. But we gotta do post_action
// hooks otherwise things can get crazy if the db transaction hasn't closed.
$nodes = entity_uuid_load('node', array($node->uuid));
// Exit early if something has gone wrong and there's no node info.
if (empty($nodes)) {
return;
}
$node = array_pop($nodes);
module_load_include('inc', 'islandora', 'include/triplestore');
islandora_triplestore_upsert($node);
}
/**
* Implements hook_node_postdelete().
*/
function islandora_node_postdelete($node) {
module_load_include('inc', 'islandora', 'include/utils');
if (islandora_ignore_hooks()) {
return;
}
module_load_include('inc', 'islandora', 'include/triplestore');
islandora_triplestore_delete($node);
}
/** /**
* Implements hook_default_services_endpoint(). * Implements hook_default_services_endpoint().
*/ */

9
islandora_basic_image/islandora_basic_image.module

@ -102,7 +102,8 @@ function islandora_basic_image_rdf_mapping() {
* Implements hook_node_postinsert(). * Implements hook_node_postinsert().
*/ */
function islandora_basic_image_node_postinsert($node) { function islandora_basic_image_node_postinsert($node) {
if (isset($_SERVER['HTTP_IGNORE_HOOKS']) && strcmp(strtolower($_SERVER['HTTP_IGNORE_HOOKS']), "true") == 0) { module_load_include('inc', 'islandora', 'include/utils');
if (islandora_ignore_hooks()) {
return; return;
} }
@ -151,7 +152,8 @@ function islandora_basic_image_node_postinsert($node) {
* Implements hook_node_postupdate(). * Implements hook_node_postupdate().
*/ */
function islandora_basic_image_node_postupdate($node) { function islandora_basic_image_node_postupdate($node) {
if (isset($_SERVER['HTTP_IGNORE_HOOKS']) && strcmp(strtolower($_SERVER['HTTP_IGNORE_HOOKS']), "true") == 0) { module_load_include('inc', 'islandora', 'include/utils');
if (islandora_ignore_hooks()) {
return; return;
} }
@ -194,7 +196,8 @@ function islandora_basic_image_node_postupdate($node) {
* Implements hook_node_postdelete(). * Implements hook_node_postdelete().
*/ */
function islandora_basic_image_node_postdelete($node) { function islandora_basic_image_node_postdelete($node) {
if (isset($_SERVER['HTTP_IGNORE_HOOKS']) && strcmp(strtolower($_SERVER['HTTP_IGNORE_HOOKS']), "true") == 0) { module_load_include('inc', 'islandora', 'include/utils');
if (islandora_ignore_hooks()) {
return; return;
} }

9
islandora_collection/islandora_collection.module

@ -106,7 +106,8 @@ function islandora_collection_rdf_mapping() {
* Implements hook_node_postinsert(). * Implements hook_node_postinsert().
*/ */
function islandora_collection_node_postinsert($node) { function islandora_collection_node_postinsert($node) {
if (isset($_SERVER['HTTP_IGNORE_HOOKS']) && strcmp(strtolower($_SERVER['HTTP_IGNORE_HOOKS']), "true") == 0) { module_load_include('inc', 'islandora', 'include/utils');
if (islandora_ignore_hooks()) {
return; return;
} }
@ -142,7 +143,8 @@ function islandora_collection_node_postinsert($node) {
* Implements hook_node_postupdate(). * Implements hook_node_postupdate().
*/ */
function islandora_collection_node_postupdate($node) { function islandora_collection_node_postupdate($node) {
if (isset($_SERVER['HTTP_IGNORE_HOOKS']) && strcmp(strtolower($_SERVER['HTTP_IGNORE_HOOKS']), "true") == 0) { module_load_include('inc', 'islandora', 'include/utils');
if (islandora_ignore_hooks()) {
return; return;
} }
@ -179,7 +181,8 @@ function islandora_collection_node_postupdate($node) {
* Implements hook_node_postdelete(). * Implements hook_node_postdelete().
*/ */
function islandora_collection_node_postdelete($node) { function islandora_collection_node_postdelete($node) {
if (isset($_SERVER['HTTP_IGNORE_HOOKS']) && strcmp(strtolower($_SERVER['HTTP_IGNORE_HOOKS']), "true") == 0) { module_load_include('inc', 'islandora', 'include/utils');
if (islandora_ignore_hooks()) {
return; return;
} }

Loading…
Cancel
Save