You are here

abstract class MigrateFile in Migrate 7.2

Base class for creating core file entities.

Hierarchy

Expanded class hierarchy of MigrateFile

File

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

View source
abstract class MigrateFile extends MigrateFileBase {

  /**
   * The destination directory within Drupal.
   *
   * @var string
   */
  protected $destinationDir = 'public://';

  /**
   * The filename relative to destinationDir to which to save the current file.
   *
   * @var string
   */
  protected $destinationFile = '';
  public function __construct($arguments = array(), $default_file = NULL) {
    parent::__construct($arguments, $default_file);
    if (isset($arguments['destination_dir'])) {
      $this->destinationDir = $arguments['destination_dir'];
    }
    if (isset($arguments['destination_file'])) {
      $this->destinationFile = $arguments['destination_file'];
    }
  }

  /**
   * Implementation of MigrateFileInterface::fields().
   *
   * @return array
   */
  public static function fields() {
    return parent::fields() + array(
      'destination_dir' => t('Subfield: <a href="@doc">Path within Drupal files directory to store file</a>', array(
        '@doc' => 'http://drupal.org/node/1540106#destination_dir',
      )),
      'destination_file' => t('Subfield: <a href="@doc">Path within destination_dir to store the file.</a>', array(
        '@doc' => 'http://drupal.org/node/1540106#destination_file',
      )),
      'file_replace' => t('Option: <a href="@doc">Value of $replace in that file function. Defaults to FILE_EXISTS_RENAME.</a>', array(
        '@doc' => 'http://drupal.org/node/1540106#file_replace',
      )),
    );
  }

  /**
   * By whatever appropriate means, put the file in the right place.
   *
   * @param $destination
   *  Destination path within Drupal.
   *
   * @return bool
   *  TRUE if the file is successfully saved, FALSE otherwise.
   */
  protected abstract function copyFile($destination);

  /**
   * Default implementation of MigrateFileInterface::processFiles().
   *
   * @param $value
   *  The URI or local filespec of a file to be imported.
   * @param $owner
   *  User ID (uid) to be the owner of the file.
   *
   * @return object
   *  The file entity being created or referenced.
   */
  public function processFile($value, $owner) {
    $migration = Migration::currentMigration();

    // Determine the final path we want in Drupal - start with our preferred path.
    $destination = file_stream_wrapper_uri_normalize($this->destinationDir . '/' . ltrim($this->destinationFile, "/\\"));

    // Our own file_replace behavior - if the file exists, use it without
    // replacing it
    if ($this->fileReplace == self::FILE_EXISTS_REUSE) {

      // See if we this file already (we'll reuse and resave a file entity if it exists).
      if (file_exists($destination)) {
        $file = $this
          ->createFileEntity($destination, $owner);
        $file = file_save($file);
        $this
          ->markForPreservation($file->fid);
        return $file;
      }

      // No existing one to reuse, reset to REPLACE
      $this->fileReplace = FILE_EXISTS_REPLACE;
    }

    // Prepare the destination directory.
    $destdir = drupal_dirname($destination);
    if (!file_prepare_directory($destdir, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) {
      $migration
        ->saveMessage(t('Could not create destination directory for !dest', array(
        '!dest' => $destination,
      )));
      return FALSE;
    }

    // Determine whether we can perform this operation based on overwrite rules.
    $destination = file_destination($destination, $this->fileReplace);
    if ($destination === FALSE) {
      $migration
        ->saveMessage(t('The file could not be copied because file %dest already exists in the destination directory.', array(
        '%dest' => $destination,
      )));
      return FALSE;
    }

    // Make sure the .htaccess files are present.
    file_ensure_htaccess();

    // Put the file where it needs to be.
    if (!$this
      ->copyFile($destination)) {
      return FALSE;
    }

    // Set the permissions on the new file.
    drupal_chmod($destination);

    // Create and save the file entity.
    $file = file_save($this
      ->createFileEntity($destination, $owner));

    // Prevent deletion of the file on rollback if requested.
    if (is_object($file)) {
      $this
        ->markForPreservation($file->fid);
      return $file;
    }
    else {
      return FALSE;
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MigrateFile::$destinationDir protected property The destination directory within Drupal.
MigrateFile::$destinationFile protected property The filename relative to destinationDir to which to save the current file.
MigrateFile::copyFile abstract protected function By whatever appropriate means, put the file in the right place. 2
MigrateFile::fields public static function Implementation of MigrateFileInterface::fields(). Overrides MigrateFileBase::fields 1
MigrateFile::processFile public function Default implementation of MigrateFileInterface::processFiles(). Overrides MigrateFileInterface::processFile 2
MigrateFile::__construct public function Overrides MigrateFileBase::__construct 1
MigrateFileBase::$defaultFile protected property An optional file object to use as a default starting point for building the file entity.
MigrateFileBase::$fileReplace protected property How to handle destination filename collisions.
MigrateFileBase::$preserveFiles protected property Set to TRUE to prevent file deletion on rollback.
MigrateFileBase::createFileEntity protected function Setup a file entity object suitable for saving.
MigrateFileBase::FILE_EXISTS_REUSE constant Extension of the core FILE_EXISTS_* constants, offering an alternative to reuse the existing file if present as-is (core only offers the options of replacing it or renaming to avoid collision).
MigrateFileBase::markForPreservation protected function If asked to preserve files from deletion on rollback, add a file_usage entry.