You are here

class MigrateListFiles in Migrate 6.2

Same name and namespace in other branches
  1. 7.2 plugins/sources/files.inc \MigrateListFiles

Implementation of MigrateList, for retrieving a list of IDs to be migrated from a directory listing. Each item is a file, it's ID is the path.

Hierarchy

Expanded class hierarchy of MigrateListFiles

File

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

View source
class MigrateListFiles extends MigrateList {
  protected $listDirs;
  protected $baseDir;
  protected $fileMask;
  protected $directoryOptions;
  protected $parser;

  /**
   * Constructor.
   *
   * @param $list_dirs
   *   Array of directory paths that will be scanned for files. No trailing
   *   slash. For example:
   *     array(
   *       '/var/html_source/en/news',
   *       '/var/html_source/fr/news',
   *       '/var/html_source/zh/news',
   *     );
   * @param $base_dir
   *   The base dir is the part of the path that will be excluded when making
   *   an ID for each file. To continue the example from above, you want base_dir
   *   to be = '/var/html_source', so that the files will have IDs in the format
   *   '/en/news/news_2011_03_4.html'.
   * @param $file_mask
   *   Passed on and used to filter for certain types of files. Use a regular
   *   expression, for example '/(.*\.htm$|.*\.html$)/i' to match all .htm and
   *   .html files, case insensitive.
   * @param $options
   *   Options that will be passed on to file_scan_directory(). See docs of that
   *   core Drupal function for more information.
   */
  public function __construct($list_dirs, $base_dir, $file_mask = NULL, $options = array(), MigrateContentParser $parser = NULL) {
    if (!$parser) {
      $parser = new MigrateSimpleContentParser();
    }
    parent::__construct();
    $this->listDirs = $list_dirs;
    $this->baseDir = $base_dir;
    $this->fileMask = $file_mask;
    $this->directoryOptions = $options;
    $this->parser = $parser;
  }

  /**
   * Our public face is the directories we're getting items from.
   */
  public function __toString() {
    if (is_array($this->listDirs)) {
      return implode(',', $this->listDirs);
    }
    else {
      return $this->listDirs;
    }
  }

  /**
   * Retrieve a list of files based on parameters passed for the migration.
   */
  public function getIdList() {
    $files = array();
    foreach ($this->listDirs as $dir) {
      migrate_instrument_start("Retrieve {$dir}");
      $files = array_merge(file_scan_directory($dir, $this->fileMask, $this->directoryOptions), $files);
      migrate_instrument_stop("Retrieve {$dir}");
    }
    if (isset($files)) {
      return $this
        ->getIDsFromFiles($files);
    }
    Migration::displayMessage(t('Loading of !listuri failed:', array(
      '!listuri' => $this->listUri,
    )));
    return NULL;
  }

  /**
   * Given an array generated from file_scan_directory(), parse out the IDs for
   * processing and return them as an array.
   */
  protected function getIDsFromFiles(array $files) {
    $ids = array();
    foreach ($files as $file) {
      $contents = file_get_contents($file->uri);
      $this->parser
        ->setContent($contents);
      if ($this->parser
        ->getChunkCount() > 1) {
        foreach ($this->parser
          ->getChunkIDs() as $chunk_id) {
          $ids[] = str_replace($this->baseDir, '', (string) $file->uri) . MIGRATE_CHUNK_SEPARATOR . $chunk_id;
        }
      }
      else {
        $ids[] = str_replace($this->baseDir, '', (string) $file->uri);
      }
    }
    return array_unique($ids);
  }

  /**
   * Return a count of all available IDs from the source listing.
   */
  public function computeCount() {
    $count = 0;
    $files = $this
      ->getIdList();
    if ($files) {
      $count = count($files);
    }
    return $count;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MigrateListFiles::$baseDir protected property
MigrateListFiles::$directoryOptions protected property
MigrateListFiles::$fileMask protected property
MigrateListFiles::$listDirs protected property
MigrateListFiles::$parser protected property
MigrateListFiles::computeCount public function Return a count of all available IDs from the source listing. Overrides MigrateList::computeCount
MigrateListFiles::getIdList public function Retrieve a list of files based on parameters passed for the migration. Overrides MigrateList::getIdList
MigrateListFiles::getIDsFromFiles protected function Given an array generated from file_scan_directory(), parse out the IDs for processing and return them as an array.
MigrateListFiles::__construct public function Constructor. Overrides MigrateList::__construct
MigrateListFiles::__toString public function Our public face is the directories we're getting items from. Overrides MigrateList::__toString