You are here

class MigrateItemFile in Migrate 7.2

Same name and namespace in other branches
  1. 6.2 plugins/sources/files.inc \MigrateItemFile

Implementation of MigrateItem, for retrieving a file from the file system based on source directory and an ID provided by a MigrateList class.

Hierarchy

Expanded class hierarchy of MigrateItemFile

File

plugins/sources/files.inc, line 254
Support for migration from files sources.

View source
class MigrateItemFile extends MigrateItem {
  protected $baseDir;
  protected $getContents;
  protected $parser;

  /**
   * Constructor.
   *
   * @param $base_dir
   *   The base directory from which all file paths are calculated.
   * @param $get_contents
   *   TRUE if we should try load the contents of each file (in the case
   *   of a text file), or FALSE if we just want to confirm it exists (binary).
   */
  public function __construct($base_dir, $get_contents = TRUE, MigrateContentParser $parser = NULL) {
    parent::__construct();
    if (!$parser) {
      $parser = new MigrateSimpleContentParser();
    }
    $this->baseDir = $base_dir;
    $this->getContents = $get_contents;
    $this->parser = $parser;
  }

  /**
   * Return an object representing a file or piece thereof.
   *
   * @param $id
   *   The file id, which is the file URI.
   *
   * @return object
   *   The item object for migration.
   */
  public function getItem($id) {
    $pieces = explode(MIGRATE_CHUNK_SEPARATOR, $id);
    $item_uri = $this->baseDir . $pieces[0];
    $chunk = !empty($pieces[1]) ? $pieces[1] : '';

    // Get the file data at the specified URI
    $data = $this
      ->loadFile($item_uri);
    if (is_string($data)) {
      $this->parser
        ->setContent($data, $pieces[0]);
      $return = new stdClass();
      $return->filedata = $this->parser
        ->getChunk($chunk);
      return $return;
    }
    else {
      if ($data === TRUE) {
        $return = new stdClass();
        return $return;
      }
      else {
        $migration = Migration::currentMigration();
        $message = t('Loading of !objecturi failed:', array(
          '!objecturi' => $item_uri,
        ));
        $migration
          ->getMap()
          ->saveMessage(array(
          $id,
        ), $message, MigrationBase::MESSAGE_ERROR);
        return NULL;
      }
    }
  }

  /**
   * Default file loader.
   */
  protected function loadFile($item_uri) {

    // Only try load the contents if we have this flag set.
    if ($this->getContents) {
      $data = file_get_contents($item_uri);
    }
    else {
      $data = file_exists($item_uri);
    }
    return $data;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MigrateItemFile::$baseDir protected property
MigrateItemFile::$getContents protected property
MigrateItemFile::$parser protected property
MigrateItemFile::getItem public function Return an object representing a file or piece thereof. Overrides MigrateItem::getItem
MigrateItemFile::loadFile protected function Default file loader.
MigrateItemFile::__construct public function Constructor. Overrides MigrateItem::__construct