示例#1
0
 /**
  * Given an array of revision IDs and UUIDs and an entity, searches through
  * our site for a matching revision UUID. If there are no matching UUIDs,
  * this function returns false.
  *
  * @param array  $revisions The list of revisions. Each item contains an ID and
  *                          UUID field.
  * @param Entity $entity    The entity on our side to check for revisions.
  *
  * @return array|bool
  */
 protected function findMatchingRevision($revisions, Entity $entity)
 {
     $revision_uuids = array();
     foreach ($revisions as $revision) {
         $revision_uuids[] = $revision['uuid'];
     }
     // Get the revisions on our end and find the nearest one that matches.
     $our_revisions = Entity::getAllRevisions($entity->id(), $entity->type());
     if (!$our_revisions || !is_array($our_revisions)) {
         return false;
     }
     foreach ($our_revisions as $revision) {
         if (in_array($revision['uuid'], $revision_uuids)) {
             return $revision['uuid'];
         }
     }
     return false;
 }
示例#2
0
 public function getRevisionHistory($start_revision = false, $end_revision = false)
 {
     // Make sure we have a revisions table.
     if (!$this->revisions_table) {
         return false;
     }
     // Get the keys we need.
     if (array_key_exists('id', $this->entity_keys)) {
         $id_key = $this->entity_keys['id'];
     } else {
         return false;
     }
     if (array_key_exists('revision', $this->entity_keys)) {
         $revision_key = $this->entity_keys['revision'];
     } else {
         return false;
     }
     // Reset the start revision if it has a false value.
     if (!$start_revision) {
         $start_revision = -1;
     }
     // Query the database between the start revision and end revision.
     $query = \db_select($this->revisions_table, 'revision');
     $query->addField('revision', $revision_key, 'revision');
     $query->orderBy('revision.' . $revision_key);
     $query->condition('revision.' . $revision_key, $start_revision, '>');
     if ($end_revision !== false) {
         $query->condition('revision.' . $revision_key, $end_revision, '<=');
     }
     $query->condition('revision.' . $id_key, $this->entity->id());
     $result = $query->execute()->fetchAll();
     $revisions = array();
     foreach ($result as $row) {
         $revisions[] = $row->revision;
     }
     return $revisions;
 }
示例#3
0
 protected function deleteRevision(Entity $entity, $revision_id)
 {
     // First, delete the revision using the entity API.
     entity_revision_delete($entity->type(), $revision_id);
     // Now, if workbench moderation is installed, remove the node history for the specific revision.
     if (module_exists('workbench_moderation') && $entity->type() == 'node') {
         db_delete('workbench_moderation_node_history')->condition('nid', $entity->id())->condition('vid', $revision_id)->execute();
     }
 }
示例#4
0
 /**
  * Create reference definition.
  *
  * Creates an entity reference definition, storing the UUID, VUUID and entity
  * type for the provided entity.
  *
  * @param Entity $entity       The entity to generate the definition for.
  *
  * @return array The generated definition.
  */
 public static function createReferenceDefinition(Entity $entity)
 {
     $value = array();
     $value['uuid'] = $entity->uuid();
     $value['vuuid'] = $entity->vuuid();
     $value['entity_type'] = $entity->type();
     $value['original'] = $entity->id();
     $value['original_revision'] = $entity->revision();
     $value['revisions'] = Entity::getAllRevisions($entity->id(), $entity->type());
     return $value;
 }
示例#5
0
 public function execute(Entity $entity, Remote $remote, array $options, &$context)
 {
     // Get the transaction from the session.
     $transaction_session = TransactionSession::getFromSession();
     if (!$transaction_session) {
         return;
     }
     // Prepare the list of entities.
     $entities = array();
     // Get the dependencies from the entity.
     try {
         if ($entity->supportsRevisions()) {
             $revision = $entity->revision();
             $resolver = new RevisionResolver($entity);
         } else {
             $resolver = new Resolver($entity);
         }
         $dependencies = $resolver->dependencies();
         if (!empty($revision)) {
             $entity->setRevision($revision);
         }
     } catch (ResolverException $ex) {
         $message = t('There was an error processing the dependencies for the <strong>:type</strong> <code>:id</code> to the remote. Skipping.', array(':type' => $entity->type(), ':id' => $entity->id()));
         drupal_set_message($message, 'error');
         watchdog('publisher', $message, array(), WATCHDOG_WARNING);
         $this->updateContextMessages($entity, $context);
         return;
     }
     // Optionally mark the item as forced based on the entity type.
     $force = array_key_exists('force', $options) ? $options['force'] : false;
     drupal_alter('publisher_force_entity', $force, $entity);
     // Loop through each of the dependencies and mark them as forced if the options specify.
     if ($force) {
         foreach ($dependencies as $key => $dependency) {
             $dependencies[$key]['force'] = true;
             $dependencies[$key]['required'] = true;
         }
     }
     // Mark the root dependency as required if it has a status update.
     if (array_key_exists($entity->uuid(), $dependencies)) {
         $dependencies[$entity->uuid()]['source_required'] = false;
         $status = publisher_entity_tracking_get_status($entity->uuid(), $entity->type(), $remote->name);
         if (!$status->date_synced) {
             $dependencies[$entity->uuid()]['source_required'] = true;
         }
     }
     // Add the relationships to the transaction session.
     if ($resolver->relationships()) {
         $relationships = $transaction_session->getRelationships();
         foreach ($resolver->relationships() as $relationship) {
             $relationships[] = $relationship;
         }
         $transaction_session->setRelationships($relationships);
     }
     // Add the metadata to the transaction session.
     if ($metadata = $resolver->metadata()) {
         $transaction_session->setMetadata($metadata);
     }
     // Prepare the post data.
     $post_data = array('dependencies' => $dependencies);
     // Start the transaction.
     $transaction = new Transaction($remote);
     $response = $transaction->send('begin', $post_data);
     if (!$response['success']) {
         $message = t('There was an error sending the <strong>:type</strong> <code>:id</code> to the remote. See recent log messages for more details.', array(':type' => $entity->type(), ':id' => $entity->id()));
         drupal_set_message($message, 'error');
         watchdog('publisher', $message, array(), WATCHDOG_WARNING);
         Debug::wd($response);
     }
     $needs = array();
     if (array_key_exists('dependencies', $response)) {
         $needs = $response['dependencies'];
     }
     // Check to see if the receiving server didn't need anything.
     if (count($needs) <= 0) {
         $message = t('The remote <strong>:remote</strong> already has the latest version of the <strong>:type</strong> <code>:id</code>', array(':remote' => $remote->label, ':type' => $entity->type(), ':id' => $entity->id()));
         drupal_set_message($message);
         watchdog('publisher', $message);
         $this->updateContextMessages($entity, $context);
         return;
     }
     // Make sure each of the needs is valid before adding them to the transaction
     // session.
     foreach ($needs as $need) {
         if (!array_key_exists('uuid', $need) || !array_key_exists('entity_type', $need) || !array_key_exists('need revision', $need) || !array_key_exists('have revision', $need)) {
             continue;
         }
         // Check to see if the entity exists and prepare its payload.
         $entity_need = Entity::loadByUUID($need['uuid'], $need['entity_type']);
         if (!$entity_need) {
             $message = t('One of the entities the destination server needs could not be found. Please check the development log for more details.');
             drupal_set_message($message, 'error');
             watchdog('publisher', $message, array(), WATCHDOG_WARNING);
             Debug::wd($need);
             continue;
         }
         // Generate the payload for the entity.
         $entity_need_payload = publisher_compare_revision_uuid($entity_need, $need['have your revision'], $need['need revision']);
         if ($entity_need_payload === false) {
             $message = t('One or more revisions did not exist for the <strong>:type</strong> <code>:id</code>', array(':type' => $entity_need->type(), ':id' => $entity_need->id()));
             drupal_set_message($message, 'error');
             watchdog('publisher', $message, array(), WATCHDOG_WARNING);
             continue;
         }
         // Add the entity to the entities list in the transaction session.
         $entities[$entity_need->uuid()] = array('entity_type' => $entity_need->type(), 'entity_uuid' => $entity_need->uuid(), 'entity_id' => $entity_need->id(), 'entity_vuuid' => $entity_need->vuuid(), 'have_revision' => $need['have revision'], 'have_your_revision' => $need['have your revision'], 'need_revision' => $need['need revision'], 'required_from_remote' => $need['required_from_remote'], 'original_dependency' => $dependencies[$entity_need->uuid()], 'revisions_payload' => $entity_need_payload);
     }
     // Add the entities to the transaction session.
     $transaction_session->addEntities($entity->uuid(), $entities);
     $transaction_session->storeToSession();
     // Finally, record the context messages.
     $this->updateContextMessages($entity, $context);
 }