You are here

public function MigrateDestinationFile::import in Migrate 7.2

Import a single file record.

Parameters

$file: File object to build. Prefilled with any fields mapped in the Migration.

$row: Raw source data object - passed through to prepare/complete handlers.

Return value

array Array of key fields (fid only in this case) of the file that was saved if successful. FALSE on failure.

Overrides MigrateDestination::import

File

plugins/destinations/file.inc, line 671
Support for file entity as destination. Note that File Fields have their own destination in fields.inc

Class

MigrateDestinationFile
Destination class implementing migration into the files table.

Code

public function import(stdClass $file, stdClass $row) {

  // Updating previously-migrated content?
  $migration = Migration::currentMigration();
  if (isset($row->migrate_map_destid1)) {
    if (isset($file->fid)) {
      if ($file->fid != $row->migrate_map_destid1) {
        throw new MigrateException(t("Incoming fid !fid and map destination fid !destid1 don't match", array(
          '!fid' => $file->fid,
          '!destid1' => $row->migrate_map_destid1,
        )));
      }
    }
    else {
      $file->fid = $row->migrate_map_destid1;
    }
  }
  if ($migration
    ->getSystemOfRecord() == Migration::DESTINATION) {
    if (!isset($file->fid)) {
      throw new MigrateException(t('System-of-record is DESTINATION, but no destination fid provided'));
    }

    // @todo: Support DESTINATION case
    $old_file = file_load($file->fid);
  }

  // 'type' is the bundle property on file entities. It must be set here for
  // the sake of the prepare handlers, although it may be overridden later
  // based on the detected mime type.
  if (empty($file->type)) {

    // If a bundle was specified in the constructor we use it for filetype.
    if ($this->bundle != 'file') {
      $file->type = $this->bundle;
    }
    else {
      $file->type = 'file';
    }
  }

  // Invoke migration prepare handlers
  $this
    ->prepare($file, $row);
  if (isset($file->fid)) {
    $updating = TRUE;
  }
  else {
    $updating = FALSE;
  }
  if (!isset($file->uid)) {
    $file->uid = 1;
  }

  // file_save() unconditionally sets timestamp - if we have an explicit
  // value we want, we need to set it manually after file_save.
  if (isset($file->timestamp)) {
    $timestamp = MigrationBase::timestamp($file->timestamp);
  }

  // Don't pass preserve_files through to the file class, which will add
  // file_usage - we will handle it ourselves in rollback().
  $file->preserve_files = FALSE;
  $file_class = $this->fileClass;
  $source = new $file_class((array) $file, $file);
  $file = $source
    ->processFile($file->value, $file->uid);
  if (is_object($file) && isset($file->fid)) {
    $this
      ->complete($file, $row);
    if (isset($timestamp)) {
      db_update('file_managed')
        ->fields(array(
        'timestamp' => $timestamp,
      ))
        ->condition('fid', $file->fid)
        ->execute();
      $file->timestamp = $timestamp;
    }
    $return = array(
      $file->fid,
    );
    if ($updating) {
      $this->numUpdated++;
    }
    else {
      $this->numCreated++;
    }
  }
  else {
    $return = FALSE;
  }
  return $return;
}