You are here

function cmis_sync_cron in CMIS API 6

Same name and namespace in other branches
  1. 6.4 cmis_sync/cmis_sync.module \cmis_sync_cron()
  2. 6.2 cmis_sync/cmis_sync.module \cmis_sync_cron()
  3. 6.3 cmis_sync/cmis_sync.module \cmis_sync_cron()
  4. 7.2 cmis_sync/cmis_sync.module \cmis_sync_cron()
  5. 7 cmis_sync/cmis_sync.module \cmis_sync_cron()

Implementation of hook_cron @todo: handle SQL injection for CMIS query calls. Low priority.

File

cmis_sync/cmis_sync.module, line 46

Code

function cmis_sync_cron() {
  $cmis_sync_root_folder = variable_get("cmis_sync_root_folder", null);
  if (empty($cmis_sync_root_folder)) {
    watchdog(WATCHDOG_ERROR, '"cmis_sync_root_folder" not set');
    drupal_set_message(t('"cmis_sync_root_folder" not set. Synconization with CMIS repository will not work as espected.'), 'error');
    return false;
  }
  module_load_include('api.inc', 'cmis');
  module_load_include('inc', 'node', 'node.pages');
  $repository = cmisapi_getRepositoryInfo();
  $folderId = cmisapi_getProperties($repository->repositoryId, drupal_urlencode($cmis_sync_root_folder));
  $cmis_updates = cmisapi_query($repository->respositoryId, sprintf('SELECT * FROM document WHERE IN_FOLDER(\'%s\') AND LastModificationDate > \'%s\'', $folderId->id, date_create('today')
    ->format("Y-m-d\\TH:i:s.000-00:00")));
  foreach ($cmis_updates as $cmis_update) {
    if (isset($cmis_update->versionSeriesCheckedOutBy)) {
      continue;
    }
    if ($cmis_node_sync = db_fetch_object(db_query('SELECT nid, changed_timestamp FROM {cmis_sync_node} WHERE cmis_objectId = \'%s\'', $cmis_update->id))) {
      if (date('Y-m-d\\TH:i', $cmis_node_sync->changed_timestamp) < $cmis_update->updated
        ->format('Y-m-d\\TH:i')) {
        $node = node_load($cmis_node_sync->nid);
        $node->cmis_objectId = $cmis_update->id;
        $node->title = $cmis_update->title;
        $node->body = cmisapi_getContentStream($repository->repositoryId, $cmis_update->id);
        node_save($node);
        db_query('UPDATE {cmis_sync_node} SET changed_timestamp=%d WHERE nid = %d', $_SERVER['REQUEST_TIME'], $node->nid);
        drupal_set_message('Updated nid:' . $node->nid);
      }
    }
    else {
      $new_node = new stdClass();
      $new_node->cmis_objectId = $cmis_update->id;
      $new_node->title = $cmis_update->title;
      $new_node->body = cmisapi_getContentStream($repository->repositoryId, $cmis_update->id);
      $new_node->type = variable_get('cmis_sync_node_type', 'story');
      $new_node->status = variable_get('cmis_sync_node_status', 1);
      $new_node->promote = variable_get('cmis_sync_node_promote', 1);
      $new_node->sticky = variable_get('cmis_sync_node_sticky', 0);
      node_object_prepare($new_node);
      node_save($new_node);
      db_query('INSERT INTO {cmis_sync_node} (nid, cmis_objectId, changed_timestamp) VALUES (%d, \'%s\', %d)', $new_node->nid, $cmis_update->id, $_SERVER['REQUEST_TIME']);
      drupal_set_message('Added nid:' . $new_node->nid);
    }
  }
}