Hi all,

I have to migrate a folder of images in media images in my new site.

The problem is that I don't have a sql, or json or xml source, simply I have an array containing all images filenames.

Please can you suggest me what kind of migrate source class I have to use?

Here's what I did:

class myImagesMigration extends myMigration {
  public function __construct() {
    parent::__construct();
    $this->description = t('Embedded images in articles body');

    $this->map = new MigrateSQLMap($this->machineName,
      array(
        'filename' => array(
          'type' => 'varchar',
          'length' => 255,
          'not null' => TRUE,
          'description' => 'Image filename',
        )
      ),
      MigrateDestinationMedia::getKeySchema()
    );

    // Source fields:.
    $fields = array(
      'filename' => t('Image filename, relative to the source directory'),
    );

    // All source images were previous copied in a defined folder:
    $dir_path = '/var/www/mysite/sites/default/files/old_server/source_files';
    
    // source folder contains only .jpg files: grab all image names with scandir:
    $images = array_diff(scandir($dir_path), array('..', '.')); 

    $this->source = new Migrate...... ($images); <= WHAT MIGRATE SOURCE?!?!?!**********************

    $this->destination = new MigrateDestinationMedia('image');

    $this->addFieldMapping('source_dir')
            ->defaultValue($dir_path);
    
    
    $this->addFieldMapping('value', 'filename');


    $this->addFieldMapping('uid')
            ->defaultValue(1);

    $this->addUnmigratedDestinations(array('field_image_description:format',
      'field_image_description:language', 'destination_dir', 'destination_file',
      'file_replace', 'preserve_files', 'timestamp'));

  }
}

Thank you very much for helping me

Comments

dropfen’s picture

Why are you trying to use Migrate for this case. As I understood, you want create just file objects managed by media, right? Maybe it's better to use something like this...

http://drupal.org/project/bulk_media_upload

MXT’s picture

Thank you for your suggestion dropfen, but

Why are you trying to use Migrate for this case

Because:

  1. I have to map filenames and then reuse "myImages" class as source for a new node migrate class (the which one that will create "articles" nodes with the above images embedded directly in body field, see for example: http://previousnext.com.au/blog/how-handle-embedded-images-when-migratin... )
  2. All the rest (95%) of my data migration is already managed with migrate module: I miss this little piece only!

Does someone have any idea to resolve what I'm asking ?

Thank you very much

mikeryan’s picture

Status: Active » Postponed (maintainer needs more info)

If you really want to literally use a memory array as a source, then that would require implementing a source class that would accept the array and iterate over it (which would actually be about just about the simplest source class possible, but no one's done that yet).

However, what you're really trying to do is to process files in a directory, you can use MigrateListFiles and MigrateItemFile (see plugins/sources/files.inc). I haven't used them myself (they were a community contribution), but I think you should be able to encapsulate the directory listing in MigrateListFiles and reference the individual file names in MigrateItemFile.

mikeryan’s picture

Status: Postponed (maintainer needs more info) » Closed (works as designed)

No further response.