4

I'm working on a Migration class to import from a depreciated WordPress plugin and everything's just about there -- everything succeeds when importing and there aren't any errors; taking the SQL from Migrate UI and plugging it into MySQL yields the data table I need.

However, the images aren't importing at all. None of the relevant tables -- file_managed, field_data_field_photo -- are updating.

All images are already in /sites/default/files/wp-content/uploads/. The paths are stored like "2012/10/image.jpg" (I.e., year/month/filename) in the source database.

Hugely grateful for any help.

<?php
/**
 * @file
 *  Migrating WordTour WordPress plugin to OpenMusicFestival on Drupal.
 */

abstract class WordTourMigration extends Migration {
  public function __construct() {
    // Always call the parent constructor first for basic setup
    parent::__construct();
  }
}

class ArtistMigration extends WordTourMigration {
  public function __construct() {
    parent::__construct();

    //Set up other database

    Database::addConnectionInfo('wp', 'default', array(
          'driver' => 'mysql',
          'database' => 'db_wp',
          'username' => 'user',
          'password' => 'pass',
          'host' => 'localhost',
          'prefix' => '',
        ));

    $this->description = t('Migrate the artists!');

    $this->map = new MigrateSQLMap($this->machineName,
        array(
          'artist_id' => array(
                           'type' => 'int',
                           'length' => 7,
                           'not null' => TRUE,
                           'description' => 'Artist ID',
                          )
        ),
        MigrateDestinationNode::getKeySchema()
      );

    $query = Database::getConnection('default', 'wp')
           ->select('artists', 'a');
    $query->join('attachment', 'at', 'a.artist_id = at.attachment_target_id');
    $query->join('attachment', 'att', 'a.artist_id = att.attachment_target_id');
    $query->join('postmeta', 'pm', 'pm.post_id = att.attachment_type_id');
    $query->fields('a',
              array(
                  'artist_id',
                  'artist_name',
                  'artist_publish_date',
                  'artist_bio',
                  'artist_record_company',
                  'artist_social_links',
              )
            );
    $query->fields('pm', array('meta_value'));
    //$query->addField('pm', 'meta_value', 'photo');
    $query->addExpression('GROUP_CONCAT(DISTINCT at.attachment_info)', 'genres'); //Pull in genres.
    $query->condition('at.attachment_target', 'artist');
    $query->condition('at.attachment_type', 'genre');
    $query->condition('att.attachment_type', 'thumbnail');
    $query->condition('pm.meta_key', '_wp_attached_file');
    $query->groupBy('a.artist_id');


    // Create a MigrateSource object, which manages retrieving the input data.
    $this->source = new MigrateSourceSQL($query, array(), NULL, array('map_joinable' => FALSE));

    // Set up our destination
    $this->destination = new MigrateDestinationNode('artist', array('text_format' => 'full_html'));

    // Assign mappings TO destination fields FROM source fields.
    $this->addFieldMapping('title', 'artist_name');
    $this->addFieldMapping('uid')
         ->defaultValue(1);
    $this->addFieldMapping('changed', 'artist_publish_date');
    $this->addFieldMapping('status')
         ->defaultValue(1);
    $this->addFieldMapping('promote', '')
         ->defaultValue(0);
    $this->addFieldMapping('sticky', '')
         ->defaultValue(0);
    $this->addFieldMapping('revision')
         ->defaultValue(0);
    $this->addFieldMapping('log')
         ->defaultValue('Migrated to Drupal 7 from WordPress.');
    $this->addFieldMapping('comment')
         ->defaultValue(1);
    $this->addFieldMapping('body', 'artist_bio')
          ->arguments(array('format' => 'full_html'))
    $this->addFieldMapping('created', 'artist_publish_date');
    $this->addFieldMapping('field_labels', 'artist_record_company')
          ->separator(', ')
          ->arguments(array('create_term' => true));
    $this->addFieldMapping('field_photo', 'meta_value');
    $this->addFieldMapping('field_photo:source_dir')
         ->defaultValue('/Users/aendrew/Sites/test/sites/default/files/wp-content/uploads/');
    $this->addFieldMapping('field_photo:preserve_files')
         ->defaultValue(true);
    $this->addFieldMapping('field_photo:destination_file', 'meta_value');
    $this->addFieldMapping('field_photo:file_replace')
         ->defaultValue(MigrateFile::FILE_EXISTS_REUSE);
    $this->addFieldMapping('field_photo:alt', 'artist_name');
    $this->addFieldMapping('field_photo:title', 'artist_name');
    $this->addFieldMapping('field_links', 'artist_social_links')
         ->description('See prepare()'); //Needs to be unserialized
    $this->addFieldMapping('field_genres', 'genres')
          ->separator(',')
          ->arguments(array('create_term' => true));
  }

  public function prepareRow($row) {
    watchdog('migrate', 'Filename is ' . $row->meta_value); // This is reporting correctly.
  }

  public function prepare(stdClass $node, stdClass $row) {
    //Set up the links
    $links = unserialize($row->artist_social_links);
    $empty = 'a:10:{s:13:"artist_flickr";s:0:"";s:14:"artist_youtube";s:0:"";s:12:"artist_vimeo";s:0:"";s:15:"artist_facebook";s:0:"";s:14:"artist_twitter";s:0:"";s:13:"artist_lastfm";s:0:"";s:14:"artist_myspace";s:0:"";s:15:"artist_bandcamp";s:0:"";s:13:"artist_tumblr";s:0:"";s:19:"artist_reverbnation";s:0:"";}';
    if ($node->field_links[LANGUAGE_NONE][0]['url'] == $empty) {
      unset($node->field_links);
    } else {
      $i = 0;
      foreach ($links as $site => $link) {
        if (!empty($link)) {
          $site_name = ucfirst(str_replace('artist_', '', $site));
          if ($site_name == "Youtube") $site_name = "YouTube";
          if ($site_name == "Lastfm") $site_name = "Last.fm";
          if ($site_name == "Youtube") $site_name = "YouTube";
          if ($site_name == "Myspace") $site_name = "MySpace";
          $node->field_links[LANGUAGE_NONE][$i]['title'] = $site_name;
          $node->field_links[LANGUAGE_NONE][$i]['url'] = urldecode($link);
          $i++;
        }
      }
    }
  }
}

1 Answer 1

5

Apparently having the same source and destination directory mucks things up — I moved all the images outside of Drupal and suddenly everything works great!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.