$sync_map_type) { // Check if sync is enabled for this Drupal content type if (!array_key_exists('enabled', $sync_map_type) || !$sync_map_type['enabled']) { continue; } try { // Handle CMIS updates _cmis_sync_cmis_drupal_handle_updates($repository, $sync_map_type, $node_type); // Handle CMIS deletes if ($sync_map_type['deletes']) { _cmis_sync_cmis_drupal_handle_deletes($repository, $sync_map_type, $node_type); } // Update CMIS sync setting if ($sync_map_type['full_sync_next_cron']) { $sync_map[$node_type]['full_sync_next_cron'] = 0; $sync_map_changed = TRUE; } } catch (CMISException $e) { cmis_error_handler('cmis_sync_cron', $e); } } // Save CMIS sync settings if ($sync_map_changed) { variable_set('cmis_sync_map', $sync_map); } } /** * Creates/updates Drupal nodes with CMIS content. * * @param $repository * @param $sync_map_type * @param $node_type */ function _cmis_sync_cmis_drupal_handle_updates($repository, $sync_map_type, $node_type) { // Get CMIS object properties $cmis_folder = cmisapi_getProperties($repository->repositoryId, drupal_urlencode($sync_map_type['cmis_root'])); // Select updated objects $sync_subfolders_rule = $sync_map_type['subfolders']?'IN_TREE':'IN_FOLDER'; $sync_full_rule = $sync_map_type['full_sync_next_cron']?'':sprintf('AND cmis:lastModificationDate > \'%s\'', date_create('12 hour ago')->format('Y-m-d\TH:i:s.000-00:00')); // Grab last updates $cmis_query = sprintf('SELECT * FROM %s WHERE %s(\'%s\') %s', $sync_map_type['cmis_type'], $sync_subfolders_rule, $cmis_folder->id, $sync_full_rule); $cmis_updates = cmisapi_query($repository->respositoryId, $cmis_query); foreach ($cmis_updates as $cmis_update) { // Build/lookup Drupal node $drupal_node = _cmis_sync_cmis_drupal_prepare($repository, $sync_map_type, $node_type, $cmis_update); // Unable to map current CMIS object to any Drupal content type if (FALSE === $drupal_node) { continue; } // Mark the Drupal node in order to bypass nodeapi cmis_sync hook $drupal_node->cmis_sync_disabled = TRUE; // Save Drupal node node_save($drupal_node); // Update/insert changed timestamp if (db_fetch_object(db_query('SELECT nid FROM {cmis_sync_node} WHERE cmis_objectId = \'%s\'', $cmis_update->id))) { db_query('UPDATE {cmis_sync_node} SET changed_timestamp=%d, nid=%d WHERE cmis_objectId = \'%s\'', $_SERVER['REQUEST_TIME'], $drupal_node->nid, $cmis_update->id); watchdog('cmis_sync_cron', 'Updated nid @nid', array('@nid' => $drupal_node->nid)); } else { db_query('INSERT INTO {cmis_sync_node} (nid, cmis_objectId, changed_timestamp) VALUES (%d, \'%s\', %d)', $drupal_node->nid, $cmis_update->id, $_SERVER['REQUEST_TIME']); watchdog('cmis_sync_cron', 'Added nid @nid', array('@nid' => $drupal_node->nid)); } } } /** * Deletes Drupal nodes referencing to CMIS deleted objects. * * @param $repository * @param $sync_map_type */ function _cmis_sync_cmis_drupal_handle_deletes($repository, $sync_map_type, $node_type) { // Get node list $sync_nodes = array(); $sync_nodes_query = db_query('SELECT nid, cmis_objectId FROM {cmis_sync_node}'); while ($sync_node = db_fetch_object($sync_nodes_query)) { if (node_load($sync_node->nid)->type == $node_type) { $sync_nodes[$sync_node->cmis_objectId] = $sync_node->nid; } } if (count($sync_nodes)) { // Identify existing CMIS objects $cmis_objects = cmisapi_query($repository, sprintf('SELECT ObjectId FROM %s WHERE ObjectId IN (\'%s\')', $sync_map_type['cmis_type'], join('\',\'', array_keys($sync_nodes)))); foreach ($cmis_objects as $cmis_object) { if (array_key_exists($cmis_object->id, $sync_nodes)) { unset($sync_nodes[$cmis_object->id]); } } // Delete CMIS - Drupal reference db_query('DELETE FROM {cmis_sync_node} WHERE nid IN (\'%s\')', join('\',\'', array_values($sync_nodes))); // Delete Drupal nodes foreach ($sync_nodes as $cmis_objectId => $drupal_nid) { node_delete($drupal_nid); } } } /** * Maps a cmis_object to a drupal node. * * @param $cmis_repository * @param $sync_map_type Sync rules for current type * @param $cmis_object * @return $drupal_node * * @todo * Add workflow properties */ function _cmis_sync_cmis_drupal_prepare($repository, $sync_map_type, $node_type, $cmis_object) { module_load_include('api.inc', 'cmis'); if ($sync_map_type['enabled']) { module_load_include('drupal.inc', 'cmis_sync'); $drupal_nid = NULL; // Identify Drupal nid if (!array_key_exists('nid', $sync_map_type['fields'])) { if ($cmis_sync_node = db_fetch_object(db_query('SELECT nid FROM {cmis_sync_node} WHERE cmis_objectId = \'%s\'', $cmis_object->id))) { $drupal_nid = $cmis_sync_node->nid; } } else { $drupal_nid = $cmis_object->properties[$sync_map_type['fields']['nid']]; } // Load Drupal node $node = node_load($drupal_nid); $node->type = $node_type; // Map cmis properties to drupal node fields foreach ($sync_map_type['fields'] as $node_field => $cmis_field) { if (is_string($cmis_field)) { _cmis_sync_node_field_value($node, $node_field, $cmis_object->properties[$cmis_field]); } elseif (is_array($cmis_field)) { if (array_key_exists('cmis to drupal', $cmis_field) && $cmis_field['cmis to drupal'] === False) { continue; } _cmis_sync_drupal_node_field_value($node, $cmis_field['drupal'], $cmis_object->properties[$cmis_field['cmis']]); } else { throw new CMISException(t('Unknown field map type. Expects "string" or "array". Received "@type"', array('@type' => gettype($cmis_field)))); } } // Load content field if (array_key_exists('content_field', $sync_map_type)) { _cmis_sync_drupal_node_field_value($node, $sync_map_type['content_field'], cmisapi_getContentStream($repository->repositoryId, $cmis_object->id), array('cmis' => $cmis_object)); } return $node; } return FALSE; }