Example #1
0
 /**
  * 
  *
  * @param string $ps_field
  * @param string $ps_target
  * @return bool
  */
 public function replicateMedia($ps_field, $ps_target)
 {
     global $AUTH_CURRENT_USER_ID;
     $va_targets = $this->getMediaReplicationTargets($ps_field, 'original');
     $va_target_info = $va_targets[$ps_target];
     // TODO: generalize this metadata? Perhaps let the plugin deal with it?
     $pa_data = array('title' => $va_target_info['options']['title'] ? caProcessTemplateForIDs($va_target_info['options']['title'], $this->tableNum(), array($this->getPrimaryKey()), array('requireLinkTags' => true)) : "", 'description' => $va_target_info['options']['description'] ? caProcessTemplateForIDs($va_target_info['options']['description'], $this->tableNum(), array($this->getPrimaryKey()), array('requireLinkTags' => true)) : "", 'tags' => $va_target_info['options']['tags'] ? caProcessTemplateForIDs($va_target_info['options']['tags'], $this->tableNum(), array($this->getPrimaryKey()), array('requireLinkTags' => true)) : "", 'category' => $va_target_info['options']['category']);
     $vs_version = $va_target_info['version'];
     if (!in_array($vs_version, $this->getMediaVersions($ps_field))) {
         $vs_version = 'original';
     }
     $o_replicator = new MediaReplicator();
     if ($this->getAppConfig()->get('queue_enabled')) {
         // Queue replication
         $o_tq = new TaskQueue();
         $vs_row_key = join("/", array($this->tableName(), $this->getPrimaryKey()));
         $vs_entity_key = join("/", array($this->tableName(), $ps_field, $this->getPrimaryKey(), $vs_version));
         if (!$o_tq->cancelPendingTasksForEntity($vs_entity_key)) {
             //$this->postError(560, _t("Could not cancel pending tasks: %1", $this->error),"BaseModel->replicateMedia()");
             //return false;
         }
         if ($o_tq->addTask('mediaReplication', array("FIELD" => $ps_field, "TARGET" => $ps_target, "TABLE" => $this->tableName(), "VERSION" => $vs_version, "TARGET_INFO" => $va_target_info, "DATA" => $pa_data, "PK" => $this->primaryKey(), "PK_VAL" => $this->getPrimaryKey()), array("priority" => 100, "entity_key" => $vs_entity_key, "row_key" => $vs_row_key, 'user_id' => $AUTH_CURRENT_USER_ID))) {
             $va_media_info = $this->getMediaInfo($ps_field);
             $va_media_info['REPLICATION_STATUS'][$ps_target] = __CA_MEDIA_REPLICATION_STATE_PENDING__;
             // pending means it's queued
             $va_media_info['REPLICATION_LOG'][$ps_target][] = array('STATUS' => __CA_MEDIA_REPLICATION_STATE_PENDING__, 'DATETIME' => time());
             $va_media_info['REPLICATION_KEYS'][$ps_target] = null;
             $this->setMediaInfo($ps_field, $va_media_info);
             $this->setMode(ACCESS_WRITE);
             $this->update();
             return null;
         } else {
             $this->postError(100, _t("Couldn't queue mirror using '%1' for version '%2'", 'mediaReplication', $vs_version), "BaseModel->replicateMedia()");
         }
         $vs_key = null;
     } else {
         // Do replication right now, in-process
         // (mark replication as started)
         $va_media_info = $this->getMediaInfo($ps_field);
         $va_media_info['REPLICATION_STATUS'][$ps_target] = __CA_MEDIA_REPLICATION_STATE_UPLOADING__;
         $va_media_info['REPLICATION_LOG'][$ps_target][] = array('STATUS' => __CA_MEDIA_REPLICATION_STATE_UPLOADING__, 'DATETIME' => time());
         $va_media_info['REPLICATION_KEYS'][$ps_target] = null;
         $this->setMediaInfo($ps_field, $va_media_info);
         $this->setMode(ACCESS_WRITE);
         $this->update(array('processingMediaForReplication' => true));
         if (!is_array($va_media_desc = $this->_FIELD_VALUES[$ps_field])) {
             throw new Exception(_t("Could not record replication status: %1 has no media description data", $ps_field));
         }
         try {
             if ($vs_key = $o_replicator->replicateMedia($this->getMediaPath($ps_field, $vs_version), $va_target_info, $pa_data)) {
                 $vn_status = $o_replicator->getReplicationStatus($va_target_info, $vs_key);
                 $va_media_info['REPLICATION_STATUS'][$ps_target] = $vn_status;
                 $va_media_info['REPLICATION_LOG'][$ps_target][] = array('STATUS' => $vn_status, 'DATETIME' => time());
                 $va_media_info['REPLICATION_KEYS'][$ps_target] = $vs_key;
                 $this->setMediaInfo($ps_field, $va_media_info);
                 $this->update(array('processingMediaForReplication' => true));
             } else {
                 $va_media_info['REPLICATION_STATUS'][$ps_target] = __CA_MEDIA_REPLICATION_STATE_ERROR__;
                 $va_media_info['REPLICATION_LOG'][$ps_target][] = array('STATUS' => __CA_MEDIA_REPLICATION_STATE_ERROR__, 'DATETIME' => time());
                 $va_media_info['REPLICATION_KEYS'][$ps_target] = null;
                 $this->setMediaInfo($ps_field, $va_media_info);
                 $this->update(array('processingMediaForReplication' => true));
                 $this->postError(3300, _t('Media replication for %1 failed', $va_target_info['name']), 'BaseModel->replicateMedia', $this->tableName() . '.' . $ps_field);
                 return null;
             }
         } catch (Exception $e) {
             $va_media_info['REPLICATION_STATUS'][$ps_target] = __CA_MEDIA_REPLICATION_STATE_ERROR__;
             $va_media_info['REPLICATION_LOG'][$ps_target][] = array('STATUS' => __CA_MEDIA_REPLICATION_STATE_ERROR__, 'DATETIME' => time());
             $va_media_info['REPLICATION_KEYS'][$ps_target] = null;
             $this->setMediaInfo($ps_field, $va_media_info);
             $this->update(array('processingMediaForReplication' => true));
             $this->postError(3300, _t('Media replication for %1 failed: %2', $va_target_info['name'], $e->getMessage()), 'BaseModel->replicateMedia', $this->tableName() . '.' . $ps_field);
             return null;
         }
     }
     return $vs_key;
 }
 public function process($pa_parameters)
 {
     // Check status of replication and either (a) start replication (b) check replication
     // if checking replication and it's done then write data back to database
     $va_report = array();
     $o_eventlog = new EventLog();
     $o_replicator = new MediaReplicator();
     $o_dm = Datamodel::load();
     if (!($t_instance = $o_dm->getInstanceByTableName($pa_parameters['TABLE'], true))) {
         $o_eventlog->log(array("CODE" => "ERR", "SOURCE" => "TaskQueue->mediaReplication->process()", "MESSAGE" => _t("Table %1 is invalid", $pa_parameters['TABLE'])));
         $va_report['errors'][] = _t("Table %1 is invalid", $pa_parameters['TABLE']);
         return false;
     }
     if (!$t_instance->load($pa_parameters['PK_VAL'])) {
         $o_eventlog->log(array("CODE" => "ERR", "SOURCE" => "TaskQueue->mediaReplication->process()", "MESSAGE" => _t("Row_id %1 is invalid for table %2", $pa_parameters['PK_VAL'], $pa_parameters['TABLE'])));
         $va_report['errors'][] = _t("Row_id %1 is invalid for table %2", $pa_parameters['PK_VAL'], $pa_parameters['TABLE']);
         return false;
     }
     $ps_field = $pa_parameters['FIELD'];
     $ps_target = $pa_parameters['TARGET'];
     $ps_version = $pa_parameters['VERSION'];
     $pa_target_info = $pa_parameters['TARGET_INFO'];
     $pa_data = $pa_parameters['DATA'];
     $va_media_info = $t_instance->getMediaInfo($ps_field);
     $va_media_info['REPLICATION_STATUS'][$ps_target] = __CA_MEDIA_REPLICATION_STATE_UPLOADING__;
     $va_media_info['REPLICATION_LOG'][$ps_target][] = array('STATUS' => __CA_MEDIA_REPLICATION_STATE_UPLOADING__, 'DATETIME' => time());
     $va_media_info['REPLICATION_KEYS'][$ps_target] = null;
     $t_instance->setMediaInfo($ps_field, $va_media_info);
     $t_instance->setMode(ACCESS_WRITE);
     $t_instance->update(array('processingMediaForReplication' => true));
     if (!is_array($va_media_desc = $t_instance->_FIELD_VALUES[$ps_field])) {
         $o_eventlog->log(array("CODE" => "ERR", "SOURCE" => "TaskQueue->mediaReplication->process()", "MESSAGE" => _t("Could not record replication status: %1 has no media description data", $ps_field)));
         $va_report['errors'][] = _t("Could not record replication status: %1 has no media description data", $ps_field);
         return false;
     }
     try {
         if ($vs_key = $o_replicator->replicateMedia($t_instance->getMediaPath($ps_field, $ps_version), $pa_target_info, $pa_data)) {
             $va_media_info['REPLICATION_STATUS'][$ps_target] = __CA_MEDIA_REPLICATION_STATE_PROCESSING__;
             $va_media_info['REPLICATION_LOG'][$ps_target][] = array('STATUS' => __CA_MEDIA_REPLICATION_STATE_PROCESSING__, 'DATETIME' => time());
             $va_media_info['REPLICATION_KEYS'][$ps_target] = $vs_key;
             $t_instance->setMediaInfo($ps_field, $va_media_info);
             $t_instance->update(array('processingMediaForReplication' => true));
             $o_eventlog->log(array("CODE" => "DEBG", "SOURCE" => "TaskQueue->mediaReplication->process()", "MESSAGE" => _t('Media replicated to %1 for processing', $pa_target_info['name'])));
             $va_report['notes'][] = _t('Media replicated to %1 for processing', $pa_target_info['name']);
         } else {
             $va_media_info['REPLICATION_STATUS'][$ps_target] = __CA_MEDIA_REPLICATION_STATE_ERROR__;
             $va_media_info['REPLICATION_LOG'][$ps_target][] = array('STATUS' => __CA_MEDIA_REPLICATION_STATE_ERROR__, 'DATETIME' => time());
             $va_media_info['REPLICATION_KEYS'][$ps_target] = null;
             $t_instance->setMediaInfo($ps_field, $va_media_info);
             $t_instance->update(array('processingMediaForReplication' => true));
             $o_eventlog->log(array("CODE" => "ERR", "SOURCE" => "TaskQueue->mediaReplication->process()", "MESSAGE" => _t('Media replication for %1 failed', $pa_target_info['name'])));
             $va_report['errors'][] = _t('Media replication for %1 failed', $pa_target_info['name']);
         }
     } catch (Exception $e) {
         $va_media_info['REPLICATION_STATUS'][$ps_target] = __CA_MEDIA_REPLICATION_STATE_ERROR__;
         $va_media_info['REPLICATION_LOG'][$ps_target][] = array('STATUS' => __CA_MEDIA_REPLICATION_STATE_ERROR__, 'DATETIME' => time());
         $va_media_info['REPLICATION_KEYS'][$ps_target] = null;
         $t_instance->setMediaInfo($ps_field, $va_media_info);
         $t_instance->update(array('processingMediaForReplication' => true));
         $o_eventlog->log(array("CODE" => "ERR", "SOURCE" => "TaskQueue->mediaReplication->process()", "MESSAGE" => _t('Media replication for %1 failed: %2', $pa_target_info['name'], $e->getMessage())));
         $va_report['errors'][] = _t('Media replication for %1 failed: %2', $pa_target_info['name'], $e->getMessage());
     }
     return $va_report;
 }