Example #1
0
 /**
  * {@inheritdoc}
  */
 public function import(Row $row, array $old_destination_id_values = array())
 {
     $file = $row->getSourceProperty($this->configuration['source_path_property']);
     $destination = $row->getDestinationProperty($this->configuration['destination_path_property']);
     $source = $this->configuration['source_base_path'] . $file;
     // Ensure the source file exists, if it's a local URI or path.
     if ($this->isLocalUri($source) && !file_exists($source)) {
         throw new MigrateException(SafeMarkup::format('File @source does not exist.', ['@source' => $source]));
     }
     // If the start and end file is exactly the same, there is nothing to do.
     if ($this->isLocationUnchanged($source, $destination)) {
         return parent::import($row, $old_destination_id_values);
     }
     $replace = $this->getOverwriteMode($row);
     $success = $this->writeFile($source, $destination, $replace);
     if (!$success) {
         $dir = $this->getDirectory($destination);
         if (file_prepare_directory($dir, FILE_CREATE_DIRECTORY)) {
             $success = $this->writeFile($source, $destination, $replace);
         } else {
             throw new MigrateException(SafeMarkup::format('Could not create directory @dir', ['@dir' => $dir]));
         }
     }
     if ($success) {
         return parent::import($row, $old_destination_id_values);
     } else {
         throw new MigrateException(SafeMarkup::format('File %source could not be copied to %destination.', ['%source' => $source, '%destination' => $destination]));
     }
 }
 /**
  * {@inheritdoc}
  */
 public function import(Row $row, array $old_destination_id_values = array())
 {
     $source = $this->configuration['source_base_path'] . $row->getSourceProperty($this->configuration['source_path_property']);
     $destination = $row->getDestinationProperty($this->configuration['destination_path_property']);
     $replace = FILE_EXISTS_REPLACE;
     if (!empty($this->configuration['rename'])) {
         $entity_id = $row->getDestinationProperty($this->getKey('id'));
         if (!empty($entity_id) && ($entity = $this->storage->load($entity_id))) {
             $replace = FILE_EXISTS_RENAME;
         }
     }
     $dirname = drupal_dirname($destination);
     if (!file_prepare_directory($dirname, FILE_CREATE_DIRECTORY)) {
         throw new MigrateException(t('Could not create directory %dirname', array('%dirname' => $dirname)));
     }
     if ($this->configuration['move']) {
         $copied = file_unmanaged_move($source, $destination, $replace);
     } else {
         // Determine whether we can perform this operation based on overwrite rules.
         $original_destination = $destination;
         $destination = file_destination($destination, $replace);
         if ($destination === FALSE) {
             throw new MigrateException(t('File %file could not be copied because a file by that name already exists in the destination directory (%destination)', array('%file' => $source, '%destination' => $original_destination)));
         }
         $source = $this->urlencode($source);
         $copied = copy($source, $destination);
     }
     if ($copied) {
         return parent::import($row, $old_destination_id_values);
     } else {
         throw new MigrateException(t('File %source could not be copied to %destination.', array('%source' => $source, '%destination' => $destination)));
     }
 }
Example #3
0
 /**
  * {@inheritdoc}
  */
 protected function getEntity(Row $row, array $old_destination_id_values)
 {
     if ($row->isStub()) {
         $row->setDestinationProperty('name', $this->t('Stub name for source tid:') . $row->getSourceProperty('tid'));
     }
     return parent::getEntity($row, $old_destination_id_values);
 }
 /**
  * Builds an user entity destination.
  *
  * @param array $configuration
  *   A configuration array containing information about the plugin instance.
  * @param string $plugin_id
  *   The plugin_id for the plugin instance.
  * @param mixed $plugin_definition
  *   The plugin implementation definition.
  * @param MigrationInterface $migration
  *   The migration.
  * @param EntityStorageInterface $storage
  *   The storage for this entity type.
  * @param array $bundles
  *   The list of bundles this entity type has.
  * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
  *   The entity manager service.
  * @param \Drupal\Core\Password\PasswordInterface $password
  *   The password service.
  */
 public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, EntityStorageInterface $storage, array $bundles, EntityManagerInterface $entity_manager, FieldTypePluginManagerInterface $field_type_manager, PasswordInterface $password)
 {
     parent::__construct($configuration, $plugin_id, $plugin_definition, $migration, $storage, $bundles, $entity_manager, $field_type_manager);
     // Since password records from the earlier schema already was hashed we
     // disable hashing so that passwords stay intact.
     $this->password = $password;
     $this->password->disablePasswordHashing();
     $this->storage->resetCache();
 }
Example #5
0
 /**
  * {@inheritdoc}
  */
 public function import(Row $row, array $old_destination_id_values = array())
 {
     if ($row->isStub() && ($state = $this->state->get('comment.maintain_entity_statistics', 0))) {
         $this->state->set('comment.maintain_entity_statistics', 0);
     }
     $return = parent::import($row, $old_destination_id_values);
     if ($row->isStub() && $state) {
         $this->state->set('comment.maintain_entity_statistics', $state);
     }
     return $return;
 }
Example #6
0
 /**
  * {@inheritdoc}
  * @throws \Drupal\migrate\MigrateException
  */
 public function import(Row $row, array $old_destination_id_values = array())
 {
     if ($this->password) {
         if ($this->password instanceof MigratePassword) {
             $this->password->enableMd5Prefixing();
         } else {
             throw new MigrateException('Password service has been altered by another module, aborting.');
         }
     }
     $ids = parent::import($row, $old_destination_id_values);
     if ($this->password) {
         $this->password->disableMd5Prefixing();
     }
     return $ids;
 }
 /**
  * {@inheritdoc}
  * @throws \Drupal\migrate\MigrateException
  */
 public function import(Row $row, array $old_destination_id_values = array())
 {
     if ($this->password) {
         if ($this->password instanceof MigratePassword) {
             $this->password->enableMd5Prefixing();
         } else {
             throw new MigrateException('Password service has been altered by another module, aborting.');
         }
     }
     // Do not overwrite the root account password.
     if ($row->getDestinationProperty('uid') == 1) {
         $row->removeDestinationProperty('pass');
     }
     $ids = parent::import($row, $old_destination_id_values);
     if ($this->password) {
         $this->password->disableMd5Prefixing();
     }
     return $ids;
 }
Example #8
0
 /**
  * {@inheritdoc}
  */
 public function import(Row $row, array $old_destination_id_values = array())
 {
     $source = $this->configuration['source_base_path'] . $row->getSourceProperty($this->configuration['source_path_property']);
     $destination = $row->getDestinationProperty($this->configuration['destination_path_property']);
     $replace = FILE_EXISTS_REPLACE;
     if (!empty($this->configuration['rename'])) {
         $entity_id = $row->getDestinationProperty($this->getKey('id'));
         if (!empty($entity_id) && ($entity = $this->storage->load($entity_id))) {
             $replace = FILE_EXISTS_RENAME;
         }
     }
     $dirname = drupal_dirname($destination);
     file_prepare_directory($dirname, FILE_CREATE_DIRECTORY);
     if ($this->configuration['move']) {
         file_unmanaged_move($source, $destination, $replace);
     } else {
         file_unmanaged_copy($source, $destination, $replace);
     }
     return parent::import($row, $old_destination_id_values);
 }
Example #9
0
 /**
  * {@inheritdoc}
  */
 public function import(Row $row, array $old_destination_id_values = array())
 {
     $file = $row->getSourceProperty($this->configuration['source_path_property']);
     $destination = $row->getDestinationProperty($this->configuration['destination_path_property']);
     // We check the destination to see if this is a temporary file. If it is
     // then we do not prepend the source_base_path because temporary files are
     // already absolute.
     $source = $this->isTempFile($destination) ? $file : $this->configuration['source_base_path'] . $file;
     $dirname = drupal_dirname($destination);
     if (!file_prepare_directory($dirname, FILE_CREATE_DIRECTORY)) {
         throw new MigrateException(t('Could not create directory %dirname', array('%dirname' => $dirname)));
     }
     // If the start and end file is exactly the same, there is nothing to do.
     if (drupal_realpath($source) === drupal_realpath($destination)) {
         return parent::import($row, $old_destination_id_values);
     }
     $replace = FILE_EXISTS_REPLACE;
     if (!empty($this->configuration['rename'])) {
         $entity_id = $row->getDestinationProperty($this->getKey('id'));
         if (!empty($entity_id) && ($entity = $this->storage->load($entity_id))) {
             $replace = FILE_EXISTS_RENAME;
         }
     }
     if ($this->configuration['move']) {
         $copied = file_unmanaged_move($source, $destination, $replace);
     } else {
         // Determine whether we can perform this operation based on overwrite rules.
         $original_destination = $destination;
         $destination = file_destination($destination, $replace);
         if ($destination === FALSE) {
             throw new MigrateException(t('File %file could not be copied because a file by that name already exists in the destination directory (%destination)', array('%file' => $source, '%destination' => $original_destination)));
         }
         $source = $this->urlencode($source);
         $copied = @copy($source, $destination);
     }
     if ($copied) {
         return parent::import($row, $old_destination_id_values);
     } else {
         throw new MigrateException(t('File %source could not be copied to %destination.', array('%source' => $source, '%destination' => $destination)));
     }
 }
Example #10
0
 /**
  * {@inheritdoc}
  */
 protected function processStubRow(Row $row)
 {
     // We stub the uri value ourselves so we can create a real stub file for it.
     if (!$row->getDestinationProperty('uri')) {
         $field_definitions = $this->entityManager->getFieldDefinitions($this->storage->getEntityTypeId(), $this->getKey('bundle'));
         $value = UriItem::generateSampleValue($field_definitions['uri']);
         if (empty($value)) {
             throw new MigrateException('Stubbing failed, unable to generate value for field uri');
         }
         // generateSampleValue() wraps the value in an array.
         $value = reset($value);
         // Make it into a proper public file uri, stripping off the existing
         // scheme if present.
         $value = 'public://' . preg_replace('|^[a-z]+://|i', '', $value);
         $value = Unicode::substr($value, 0, $field_definitions['uri']->getSetting('max_length'));
         // Create a real file, so File::preSave() can do filesize() on it.
         touch($value);
         $row->setDestinationProperty('uri', $value);
     }
     parent::processStubRow($row);
 }
Example #11
0
 /**
  * {@inheritdoc}
  */
 protected function processStubRow(Row $row)
 {
     parent::processStubRow($row);
     $stub_commented_entity_type = $row->getDestinationProperty('entity_type');
     // While parent::getEntity() fills the bundle property for stub entities
     // if it's still empty, here we must also make sure entity_id/entity_type
     // are filled (so $comment->getCommentedEntity() always returns a value).
     if (empty($this->stubCommentedEntityIds[$stub_commented_entity_type])) {
         // Fill stub entity id. Any id will do, as long as it exists.
         $entity_type = $this->entityManager->getDefinition($stub_commented_entity_type);
         $id_key = $entity_type->getKey('id');
         $result = $this->entityQuery->get($stub_commented_entity_type)->range(0, 1)->execute();
         if ($result) {
             $this->stubCommentedEntityIds[$stub_commented_entity_type] = array_pop($result);
             $row->setSourceProperty($id_key, $this->stubCommentedEntityIds[$stub_commented_entity_type]);
         } else {
             throw new MigrateException(t('Could not find parent entity to use for comment %id', ['%id' => implode(':', $row->getSourceIdValues())]), MigrationInterface::MESSAGE_ERROR);
         }
     }
     $row->setDestinationProperty('entity_id', $this->stubCommentedEntityIds[$stub_commented_entity_type]);
     $row->setDestinationProperty('entity_type', $stub_commented_entity_type);
     $row->setDestinationProperty('created', REQUEST_TIME);
     $row->setDestinationProperty('changed', REQUEST_TIME);
 }
Example #12
0
 /**
  * {@inheritdoc}
  */
 protected function processStubRow(Row $row)
 {
     parent::processStubRow($row);
     // Neither uid nor name is required in itself, but it is required to set one
     // of them.
     $row->setDestinationProperty('name', 'anonymous_stub');
 }
Example #13
0
 /**
  * {@inheritdoc}
  */
 protected function processStubRow(Row $row)
 {
     parent::processStubRow($row);
     // Email address is not defined as required in the base field definition but
     // is effectively required by the UserMailRequired constraint. This means
     // that Entity::processStubRow() did not populate it - we do it here.
     $field_definitions = $this->entityManager->getFieldDefinitions($this->storage->getEntityTypeId(), $this->getKey('bundle'));
     $mail = EmailItem::generateSampleValue($field_definitions['mail']);
     $row->setDestinationProperty('mail', reset($mail));
     // @todo Work-around for https://www.drupal.org/node/2602066.
     $name = $row->getDestinationProperty('name');
     if (is_array($name)) {
         $name = reset($name);
     }
     if (Unicode::strlen($name) > USERNAME_MAX_LENGTH) {
         $row->setDestinationProperty('name', Unicode::substr($name, 0, USERNAME_MAX_LENGTH));
     }
 }