listDirs = $list_dirs; $this->baseDir = $base_dir; $this->fileMask = $file_mask; $this->directoryOptions = $options; } /** * 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) { $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; } } /** * Implementation of MigrateItem, for retrieving a file from the file system * based on source directory and an ID provided by a MigrateList class. */ class MigrateItemFile extends MigrateItem { protected $baseDir; protected $getContents; /** * 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) { parent::__construct(); $this->baseDir = $base_dir; $this->getContents = $get_contents; } /** * Return an object representing a file. * * @param $id * The file id, which is the file URI. * * @return object * The item object for migration. */ public function getItem($id) { $item_uri = $this->baseDir . $id; // Get the file data at the specified URI $data = $this->loadFile($item_uri); if (is_string($data)) { $return = new stdClass; $return->filedata = $data; return $return; } elseif ($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; } }