You are here

abstract class MigrateFileBase in Migrate 7.2

Hierarchy

Expanded class hierarchy of MigrateFileBase

File

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

View source
abstract class MigrateFileBase implements MigrateFileInterface {

  /**
   * 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).
   */
  const FILE_EXISTS_REUSE = -1;

  /**
   * An optional file object to use as a default starting point for building the
   * file entity.
   *
   * @var stdClass
   */
  protected $defaultFile;

  /**
   * How to handle destination filename collisions.
   *
   * @var int
   */
  protected $fileReplace = FILE_EXISTS_RENAME;

  /**
   * Set to TRUE to prevent file deletion on rollback.
   *
   * @var bool
   */
  protected $preserveFiles = FALSE;
  public function __construct($arguments = array(), $default_file = NULL) {
    if (isset($arguments['preserve_files'])) {
      $this->preserveFiles = $arguments['preserve_files'];
    }
    if (isset($arguments['file_replace'])) {
      $this->fileReplace = $arguments['file_replace'];
    }
    if ($default_file) {
      $this->defaultFile = $default_file;
    }
    else {
      $this->defaultFile = new stdClass();
    }
  }

  /**
   * Default implementation of MigrateFileInterface::fields().
   *
   * @return array
   */
  public static function fields() {
    return array(
      'preserve_files' => t('Option: <a href="@doc">Boolean indicating whether files should be preserved or deleted on rollback</a>', array(
        '@doc' => 'http://drupal.org/node/1540106#preserve_files',
      )),
    );
  }

  /**
   * Setup a file entity object suitable for saving.
   *
   * @param $destination
   *  Path to the Drupal copy of the file.
   * @param $owner
   *  Uid of the file owner.
   *
   * @return stdClass
   *  A file object ready to be saved.
   */
  protected function createFileEntity($destination, $owner) {
    $file = clone $this->defaultFile;
    $file->uri = $destination;
    $file->uid = $owner;
    if (!isset($file->filename)) {
      $file->filename = drupal_basename($destination);
    }
    if (!isset($file->filemime)) {
      $file->filemime = file_get_mimetype(urldecode($destination));
    }
    if (!isset($file->status)) {
      $file->status = FILE_STATUS_PERMANENT;
    }
    if (empty($file->type) || $file->type == 'file') {

      // Try to determine the file type.
      if (module_exists('file_entity')) {
        $type = file_get_type($file);
      }
      elseif ($slash_pos = strpos($file->filemime, '/')) {
        $type = substr($file->filemime, 0, $slash_pos);
      }
      $file->type = isset($type) ? $type : 'file';
    }

    // If we are replacing or reusing an existing filesystem entry,
    // also re-use its database record.
    if ($this->fileReplace == FILE_EXISTS_REPLACE || $this->fileReplace == self::FILE_EXISTS_REUSE) {
      $existing_files = file_load_multiple(array(), array(
        'uri' => $destination,
      ));
      if (count($existing_files)) {
        $existing = reset($existing_files);
        $file->fid = $existing->fid;
        $file->filename = $existing->filename;
      }
    }
    return $file;
  }

  /**
   * If asked to preserve files from deletion on rollback, add a file_usage
   * entry.
   *
   * @param $fid
   */
  protected function markForPreservation($fid) {
    if (!empty($this->preserveFiles)) {

      // We do this directly instead of calling file_usage_add, to force the
      // count to 1 - otherwise, updates will increment the counter and the file
      // will never be deletable
      db_merge('file_usage')
        ->key(array(
        'fid' => $fid,
        'module' => 'migrate',
        'type' => 'file',
        'id' => $fid,
      ))
        ->fields(array(
        'count' => 1,
      ))
        ->execute();
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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::fields public static function Default implementation of MigrateFileInterface::fields(). Overrides MigrateFileInterface::fields 1
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.
MigrateFileBase::__construct public function 1
MigrateFileInterface::processFile public function Create or link to a Drupal file entity. 3