Example #1
0
 /**
  *
  */
 public function sync()
 {
     $this->opa_processed_records = array();
     $this->opa_processed_self_relations = array();
     $o_config = Configuration::load(__CA_CONF_DIR__ . "/synchronization.conf");
     $o_dm = Datamodel::load();
     $va_sources = $o_config->getAssoc('sources');
     foreach ($va_sources as $vs_code => $va_source) {
         $vs_base_url = $va_source['baseUrl'];
         $vs_search_expression = $va_source['searchExpression'];
         $vs_username = $va_source['username'];
         $vs_password = $va_source['password'];
         $vs_table = $va_source['table'];
         if (!$vs_base_url) {
             print "ERROR: You must pass a valid CollectiveAccess url\n";
             exit(-1);
         }
         print "[Notice] Processing {$vs_base_url}/{$vs_code}\n";
         if (!($t_instance = $o_dm->getInstanceByTableName($vs_table, false))) {
             die("Invalid table '{$vs_table}'\n");
         }
         //
         // Set up HTTP client for REST calls
         //
         $o_client = new RestClient($vs_base_url . "/service.php/search/Search/rest");
         //
         // Authenticate
         //
         $o_res = $o_client->auth($vs_username, $vs_password)->get();
         if (!$o_res->isSuccess()) {
             die("Could not authenticate to service for authentication\n");
         }
         //
         // Get userID
         //
         $o_res = $o_client->getUserID()->get();
         if (!$o_res->isSuccess()) {
             die("Could not fetch user_id\n");
         }
         $vn_user_id = (int) $o_res->getUserID->response;
         //
         // Get date/time on server
         //
         $o_res = $o_client->getServerTime()->get();
         if (!$o_res->isSuccess()) {
             die("Could not fetch server time\n");
         }
         $vn_server_timestamp = (int) $o_res->getServerTime->response;
         // Get last event
         $va_last_event = ca_data_import_events::getLastEventForSourceAndType($vs_base_url, 'SYNC');
         $vs_last_event_timestamp = null;
         if (is_array($va_last_event)) {
             if (preg_match('!\\[([^\\]]+)!', $va_last_event['description'], $va_matches)) {
                 $vs_last_event_timestamp = $va_matches[1];
                 $vs_search_expression = "({$vs_search_expression}) AND (modified:\"after {$vs_last_event_timestamp}\")";
             }
         } else {
             $vs_search_expression = "({$vs_search_expression}) AND (modified:\"after June 15 2012\")";
         }
         //$vs_search_expression = "modified:\"after 2/24/2013\"";
         //$vs_search_expression = "ca_objects:idno:10180";
         $vs_search_expression = "ca_objects.type_id:48";
         print "\t[Notice] Search is for [{$vs_search_expression}]\n";
         try {
             $o_res = $o_client->queryRest($vs_table, $vs_search_expression, array("ca_objects.status" => array("convertCodesToDisplayText" => 1)))->get();
         } catch (exception $e) {
             print "\t[Error] Search failed: " . $e->getMessage() . "\n";
             continue;
         }
         // TODO: check for errors here
         //parse results
         $vs_pk = $t_instance->primaryKey();
         $va_items = array();
         $o_xml = $o_res->CaSearchResult;
         foreach ($o_xml->children() as $vn_i => $o_item) {
             $o_attributes = $o_item->attributes();
             $vn_id = (int) $o_attributes->{$vs_pk};
             $vs_idno = (string) $o_item->idno;
             $vs_label = (string) $o_item->ca_labels->ca_label[0];
             $va_items[$vs_table . '/' . $vn_id] = array('table' => $vs_table, 'id' => $vn_id, 'idno' => $vs_idno);
         }
         print "\t\t[Notice] Found " . sizeof($va_items) . " items\n";
         //print_R($va_items);
         // Ok... now fetch and import each
         $o_client->setUri($vs_base_url . "/service.php/iteminfo/ItemInfo/rest");
         $this->fetchAndImport($va_items, $o_client, $va_source, array(), $vs_code);
         // TODO: handle deletes
         // Create new import event
         ca_data_import_events::newEvent($vn_user_id, 'SYNC', $vs_base_url, 'Sync process synchronization at [' . date("c", $vn_server_timestamp) . ']');
     }
 }