123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957 |
- <?php
- function hook_tripal_ws_value(&$items, $field, $instance) {
-
-
- if ($field['type'] == 'image' and $field['module'] == 'image') {
- foreach ($items as $delta => $details) {
- if ($items[$delta] and array_key_exists('uri', $items[$delta])) {
- $items[$delta]['value']['schema:url'] = file_create_url($items[$delta]['uri']);
- }
- }
- }
- }
- function tripal_get_web_services() {
- $services = [];
- $modules = module_list(TRUE);
- foreach ($modules as $module) {
-
- $service_path = drupal_get_path('module', $module) . '/includes/TripalWebService';
- $service_files = file_scan_directory($service_path, '/.inc$/');
-
- foreach ($service_files as $file) {
- $class = $file->name;
- module_load_include('inc', $module, 'includes/TripalWebService/' . $class);
- if (class_exists($class) and is_subclass_of($class, 'TripalWebService')) {
- $services[] = $class;
- }
- }
- }
- return $services;
- }
- function tripal_load_include_web_service_class($class) {
- $modules = module_list(TRUE);
- foreach ($modules as $module) {
- module_load_include('inc', $module, 'includes/TripalWebService/' . $class);
- if (class_exists($class)) {
- return TRUE;
- }
- }
- return FALSE;
- }
- function tripal_add_site($name, $url, $version, $description) {
- $check_url = NULL;
- $check_name = NULL;
- $write_to_db = TRUE;
-
- $check_url =
- db_select('tripal_sites', 'ts')
- ->fields('ts', ['id'])
- ->condition('url', $url)
- ->condition('version', $version)
- ->execute()
- ->fetchField();
- $check_name =
- db_select('tripal_sites', 'ts')
- ->fields('ts', ['id'])
- ->condition('name', $name)
- ->execute()
- ->fetchField();
- if ($check_url) {
- drupal_set_message(t('The URL and version is used by another site.'), 'error');
- $write_to_db = FALSE;
- }
- if ($check_name) {
- drupal_set_message(t('The name is used by another site.'), 'error');
- $write_to_db = FALSE;
- }
- if ($write_to_db === TRUE) {
- db_insert('tripal_sites')
- ->fields([
- 'name' => $name,
- 'url' => $url,
- 'version' => $version,
- 'description' => $description,
- ])
- ->execute();
- drupal_set_message(t('Tripal site \'' . $name . '\' has been added.'));
- return $write_to_db;
- }
- return $write_to_db;
- }
- function tripal_remove_site($record_id) {
- if ($record_id) {
- db_delete('tripal_sites')
- ->condition('id', $record_id)
- ->execute();
- drupal_set_message('The Tripal site \'' . $record_id . '\' has been removed.');
- return TRUE;
- }
- return FALSE;
- }
- function tripal_build_remote_content_url($remote_site, $path = '', $query = '') {
-
- $ws_version = $remote_site->version;
- $ws_url = $remote_site->url;
- $ws_url = trim($ws_url, '/');
- $ws_url .= '/web-services/content/' . $ws_version . '/' . $path;
-
- if ($query) {
- $ws_url = $ws_url . '?' . $query;
- }
- return $ws_url;
- }
- function tripal_get_remote_content($site_id, $path = '', $query = '') {
- if (!$site_id) {
- throw new Exception('Please provide a numeric site ID for the tripal_get_remote_content function.');
- }
-
- $remote_site = db_select('tripal_sites', 'ts')
- ->fields('ts')
- ->condition('ts.id', $site_id)
- ->execute()
- ->fetchObject();
- if (!$remote_site) {
- $data = [
- 'error' => t('Could not find a remote tripal site using the id provided: !id.',
- ['!id' => $site_id]),
- ];
- _tripal_report_ws_error($data);
- return $data;
- }
-
- $ws_url = tripal_build_remote_content_url($remote_site, $path, $query);
- $data = drupal_http_request($ws_url);
- if (!$data) {
- $data = [
- 'error' => t('Could not connect to the remote web service using the url: !url',
- ['!url' => $ws_url]),
- ];
- _tripal_report_ws_error($data);
- return $data;
- }
-
-
- if (property_exists($data, 'error')) {
- $data = [
- 'error' => $data->error,
- ];
- _tripal_report_ws_error($data);
- return $data;
- }
-
- $data = drupal_json_decode($data->data);
-
- if (array_key_exists('error', $data)) {
- _tripal_report_ws_error($data);
- }
- return $data;
- }
- function _tripal_report_ws_error($data) {
- $error = '</pre>' . print_r($data['error'], TRUE) . '</pre>';
- tripal_report_error('tripal_ws', TRIPAL_ERROR,
- 'Tripal remote web services reports the following error: !error.',
- ['!error' => $error]);
- }
- function tripal_get_remote_context($context_url, $cache_id) {
- if (!$context_url) {
- throw new Exception('PLease provide a context_url for the tripal_get_remote_context function.');
- }
- if (!$cache_id) {
- throw new Exception('PLease provide unique $cache_id for the tripal_get_remote_context function.');
- }
- if ($cache = cache_get($cache_id)) {
- return $cache->data;
- }
- $context = drupal_http_request($context_url);
- if (!$context) {
- tripal_report_error('tripal_ws', TRIPAL_ERROR,
- 'There was a poblem retrieving the context from the remote site: !context_url.',
- ['!context_url' => $context_url]);
- return FALSE;
- }
- $context = drupal_json_decode($context->data);
- $context = $context['@context'];
- cache_set($cache_id, $context);
- return $context;
- }
- function tripal_get_remote_content_context($site_id, $context_url, $bundle_accession, $field_accession = '') {
- $cache_id = substr('trp_ws_context_' . $site_id . '-' . $bundle_accession . '-' . $field_accession, 0, 254);
- $context = tripal_get_remote_context($context_url, $cache_id);
- return $context;
- }
- function tripal_clear_remote_cache($site_id) {
- if (!$site_id) {
- throw new Exception('Please provide a numeric site ID for the tripal_clear_remote_cache function.');
- }
- cache_clear_all('trp_ws_context_' . $site_id, 'cache', TRUE);
- cache_clear_all('trp_ws_doc_' . $site_id, 'cache', TRUE);
- }
- function tripal_get_remote_API_doc($site_id) {
- $site_doc = '';
- if (!$site_id) {
- throw new Exception('Please provide a numeric site ID for the tripal_get_remote_API_doc function.');
- }
- $cache_name = 'trp_ws_doc_' . $site_id;
- if ($cache = cache_get($cache_name)) {
- return $cache->data;
- }
-
- $remote_site = db_select('tripal_sites', 'ts')
- ->fields('ts')
- ->condition('ts.id', $site_id)
- ->execute()
- ->fetchObject();
- if (!$remote_site) {
- throw new Exception(t('Cannot find a remote site with id: "!id"', ['!id' => $site_id]));
- }
-
- $ws_version = $remote_site->version;
- $ws_url = $remote_site->url;
- $ws_url = trim($ws_url, '/');
- $ws_url .= '/web-services/doc/' . $ws_version;
-
- $options = [];
- $data = drupal_http_request($ws_url, $options);
- if (!$data) {
- tripal_report_error('tripal_ws', TRIPAL_ERROR,
- t('Could not connect to the remote web service.'));
- return FALSE;
- }
-
-
- if (property_exists($data, 'error')) {
- tripal_report_error('tripal_ws', TRIPAL_ERROR,
- 'Remote web services document reports the following error: !error. Using URL: !url',
- ['!error' => $error, '!url' => $ws_url]);
- return FALSE;
- }
-
- $site_doc = drupal_json_decode($data->data);
-
- if (array_key_exists('error', $data)) {
- $error = '</pre>' . print_r($data['error'], TRUE) . '</pre>';
- tripal_report_error('tripal_ws', TRIPAL_ERROR,
- 'Tripal Remote web services document reports the following error: !error. Using URL: !url',
- ['!error' => $error, '!url' => $ws_url]);
- return FALSE;
- }
- cache_set($cache_name, $site_doc);
- return $site_doc;
- }
- function tripal_load_remote_entities($remote_entity_ids, $site_id, $bundle_accession, $field_ids) {
- if (!$remote_entity_ids) {
- throw new Exception('Please provide the list of remote entity ids for the tripal_load_remote_entities function.');
- }
- if (!is_array($remote_entity_ids)) {
- throw new Exception('Please provide an array for the remote entity ids for the tripal_load_remote_entities function.');
- }
- if (!$site_id) {
- throw new Exception('Please provide a numeric site ID for the tripal_load_remote_entities function.');
- }
- if (!$bundle_accession) {
- throw new Exception('Please provide the bundle accession for the tripal_load_remote_entities function.');
- }
- if (!$field_ids) {
- throw new Exception('Please provide the list of field IDs for the tripal_load_remote_entities function.');
- }
- if (!is_array($field_ids)) {
- throw new Exception('Please provide an array for the field IDs for the tripal_load_remote_entities function.');
- }
-
- $site_doc = tripal_get_remote_API_doc($site_id);
-
- $query = 'page=1&limit=' . count($remote_entity_ids) .
- '&ids=' . urlencode(implode(",", $remote_entity_ids)) .
- '&fields=' . urlencode(implode(",", $field_ids));
- $results = tripal_get_remote_content($site_id, $bundle_accession, $query);
-
- if (array_key_exists('error', $results)) {
- return FALSE;
- }
-
- $context_url = $results['@context'];
- $context = tripal_get_remote_content_context($site_id, $context_url, $bundle_accession);
- if (!$context) {
- return $entity;
- }
- $total_items = $results['totalItems'];
- $members = $results['member'];
- $entities = [];
- foreach ($members as $member) {
-
- $entity_id = preg_replace('/^.*?(\d+)$/', '$1', $member['@id']);
- $entity = new stdClass();
- $entity->entityType = 'TripalEntity';
- $entity->entityInfo = [];
- $entity->id = $entity_id;
- $entity->type = 'TripalEntity';
- $entity->bundle = $bundle_accession;
- $entity->site_id = $site_id;
- $member = _tripal_update_remote_entity_field($member, $context, 1);
- foreach ($member as $field_id => $value) {
- $field = tripal_get_remote_field_info($site_id, $bundle_accession, $field_id);
- $instance = tripal_get_remote_field_instance_info($site_id, $bundle_accession, $field_id);
- $field_name = $field['field_name'];
- $entity->{$field_name}['und'][0]['value'] = $value;
- }
- $entities[$entity_id] = $entity;
- }
- return $entities;
- }
- function tripal_load_remote_entity($remote_entity_id, $site_id, $bundle_accession, $field_ids) {
-
- $site_doc = tripal_get_remote_API_doc($site_id);
-
- $remote_entity = tripal_get_remote_content($site_id, $bundle_accession . '/' . $remote_entity_id);
-
- if (array_key_exists('error', $results)) {
- return FALSE;
- }
-
- $entity = new stdClass();
- $entity->entityType = 'TripalEntity';
- $entity->entityInfo = [];
- $entity->id = $remote_entity_id;
- $entity->type = 'TripalEntity';
- $entity->bundle = $bundle_accession;
- $entity->site_id = $site_id;
-
- $context_url = $remote_entity['@context'];
- $context = tripal_get_remote_content_context($site_id, $context_url, $bundle_accession);
- if (!$context) {
- return $entity;
- }
-
- foreach ($field_ids as $field_id) {
- $field = tripal_get_remote_field_info($site_id, $bundle_accession, $field_id);
- $instance = tripal_get_remote_field_instance_info($site_id, $bundle_accession, $field_id);
- $field_name = $field['field_name'];
- $field_key = '';
- foreach ($context as $k => $v) {
- if (!is_array($v) and $v == $field_id) {
- $field_key = $k;
- }
- }
-
- if (!$field_key) {
- $entity->{$field_name}['und'][0]['value'] = '';
- continue;
- }
- if (!array_key_exists($field_key, $remote_entity)) {
- $entity->{$field_name}['und'][0]['value'] = '';
- continue;
- }
-
-
- $attached = TRUE;
- if (array_key_exists($field_id, $context) and is_array($context[$field_id]) and
- array_key_exists('@type', $context[$field_id]) and $context[$field_id]['@type'] == '@id') {
- $attached = FALSE;
- }
-
- $value = '';
- if (is_array($remote_entity[$field_key])) {
- $value = _tripal_update_remote_entity_field($remote_entity[$field_key], $context, 1);
- }
- else {
- $value = $remote_entity[$field_key];
- }
-
- if (!$attached) {
- $field_data = drupal_http_request($value);
- if (!$field_data) {
- tripal_report_error('tripal_ws', TRIPAL_ERROR,
- 'There was a poblem retrieving the unattached field, "!field:", for the remote entity: !entity_id.',
- ['!field' => $field_id, '!entity_id' => $remote_entity_id]);
- $value = '';
- }
- $field_data = drupal_json_decode($field_data->data);
-
-
- $field_context_url = $field_data['@context'];
- $field_context = tripal_get_remote_content_context($site_id, $field_context_url, $bundle_accession, $field_id);
- if (!$field_context) {
- continue;
- }
- $value = _tripal_update_remote_entity_field($field_data, $field_context);
- }
- $entity->{$field_name}['und'][0]['value'] = $value;
- }
- return $entity;
- }
- function _tripal_update_remote_entity_field($field_data, $context, $depth = 0) {
-
- if ($field_data['@type'] == 'Collection') {
- $members = [];
- foreach ($field_data['member'] as $member) {
- $next_depth = $depth + 1;
- $members[] = _tripal_update_remote_entity_field($member, $context, $next_depth);
- }
-
-
-
- if ($field_data['totalItems'] == 1) {
- return $members[0];
- }
- else {
- return $members;
- }
- }
- $value = [];
- foreach ($field_data as $k => $v) {
-
- if ($k == '@id' or $k == '@type' or $k == '@context') {
- continue;
- }
-
-
- $accession = $context[$k];
- if (is_array($v)) {
- $next_depth = $depth + 1;
- $subvalue = _tripal_update_remote_entity_field($v, $context, $next_depth);
- $value[$accession] = $subvalue;
- }
- else {
- $value[$accession] = $v;
- }
- }
- return $value;
- }
- function tripal_get_remote_field_info($site_id, $bundle_accession, $field_accession) {
-
- $site_doc = tripal_get_remote_API_doc($site_id);
-
- $property = tripal_get_remote_field_doc($site_id, $bundle_accession, $field_accession);
-
- list($vocab, $accession) = explode(':', $field_accession);
- $field_name = 'tripal_remote_site_' . $site_id . '_' . $field_accession;
- $field = [
- 'field_name' => $field_name,
- 'type' => $field_name,
- 'storage' => [
- 'type' => 'tripal_remote_site',
- ],
- ];
- return $field;
- }
- function tripal_get_remote_field_instance_info($site_id, $bundle_accession, $field_accession) {
-
- $site_doc = tripal_get_remote_API_doc($site_id);
-
- $property = tripal_get_remote_field_doc($site_id, $bundle_accession, $field_accession);
- list($vocab, $accession) = explode(':', $field_accession);
- $field_name = 'tripal_remote_site_' . $site_id . '_' . $field_accession;
- list($vocab, $accession) = explode(':', $field_accession);
- $instance = [
- 'label' => $property['hydra:title'],
- 'description' => $property['hydra:description'],
- 'formatters' => $property['tripal_formatters'],
- 'settings' => [
- 'term_vocabulary' => $vocab,
- 'term_accession' => $accession,
- ],
- 'field_name' => $field_name,
- 'entity_type' => 'TripalEntity',
- 'bundle_name' => $bundle_accession,
- ];
- return $instance;
- }
- function tripal_get_remote_content_doc($site_id, $bundle_accession) {
-
- $site_doc = tripal_get_remote_API_doc($site_id);
-
- $classes = $site_doc['supportedClass'];
- $class = NULL;
- foreach ($classes as $item) {
- if ($item['@id'] == $bundle_accession) {
- return $item;
- }
- }
- return NULL;
- }
- function tripal_get_remote_field_doc($site_id, $bundle_accession, $field_accession) {
-
- $site_doc = tripal_get_remote_API_doc($site_id);
- $class = tripal_get_remote_content_doc($site_id, $bundle_accession);
- $properties = $class['supportedProperty'];
- foreach ($properties as $item) {
- if ($item['property'] == $field_accession) {
- return $item;
- }
- }
- return NULL;
- }
- function tripal_get_remote_field_formatters($site_id, $bundle_accession, $field_accession) {
- $flist = [];
-
- $site_doc = tripal_get_remote_API_doc($site_id);
- $property = tripal_get_remote_field_doc($site_id, $bundle_accession, $field_accession);
- if (!$property) {
- return $flist;
- }
- $formatters = $property['tripal_formatters'];
- foreach ($formatters as $formatter) {
- if (tripal_load_include_downloader_class($formatter)) {
- $flist[$formatter] = $formatter::$full_label;
- }
- }
- return $flist;
- }
|