You are here

public function WordPressMigrationGenerator::createMigrations in WordPress Migrate 8.3

Creates a set of WordPress import migrations based on configuration settings.

Throws

\Drupal\Core\Entity\EntityStorageException

\Exception

File

src/WordPressMigrationGenerator.php, line 85

Class

WordPressMigrationGenerator
Functionality to construct WordPress migrations from broad configuration settings.

Namespace

Drupal\wordpress_migrate

Code

public function createMigrations() {

  // Create the migration group.
  $group_configuration = [
    'id' => $this->configuration['group_id'],
    // @todo: Add Wordpress site title in here.
    // @link https://www.drupal.org/node/2742287
    'label' => 'Imports from WordPress site',
    'source_type' => 'WordPress',
    'shared_configuration' => [
      'source' => [
        // @todo: Dynamically populate from the source XML.
        // @link https://www.drupal.org/node/2742287
        'namespaces' => [
          'wp' => 'http://wordpress.org/export/1.2/',
          'excerpt' => 'http://wordpress.org/export/1.2/excerpt/',
          'content' => 'http://purl.org/rss/1.0/modules/content/',
          'wfw' => 'http://wellformedweb.org/CommentAPI/',
          'dc' => 'http://purl.org/dc/elements/1.1/',
        ],
        'urls' => [
          $this->configuration['file_uri'],
        ],
      ],
    ],
  ];
  MigrationGroup::create($group_configuration)
    ->save();

  // Determine the uid mappings, creating an author migration if needed.
  if ($this->configuration['default_author']) {
    $account = user_load_by_name($this->configuration['default_author']);
    if ($account) {
      $this->uidMapping = [
        'plugin' => 'default_value',
        'default_value' => $account
          ->id(),
      ];
    }
    else {
      throw new \Exception('Username @name does not exist.', [
        '@name' => $this->configuration['default_author'],
      ]);
    }
  }
  else {
    $this->authorID = $this->configuration['prefix'] . 'wordpress_authors';
    $migration = static::createEntityFromPlugin('wordpress_authors', $this->authorID);
    $migration
      ->set('migration_group', $this->configuration['group_id']);
    $migration
      ->save();
    $this->uidMapping = [
      'plugin' => 'migration_lookup',
      'migration' => $this->authorID,
      'source' => 'creator',
    ];
  }

  // Set up the attachment migration.
  $this->attachmentID = $this->configuration['prefix'] . 'wordpress_attachments';
  $migration = static::createEntityFromPlugin('wordpress_attachments', $this->attachmentID);
  $migration
    ->set('migration_group', $this->configuration['group_id']);
  $process = $migration
    ->get('process');
  $process['uid'] = $this->uidMapping;
  $migration
    ->set('process', $process);
  $migration
    ->save();

  // Setup vocabulary migrations if requested.
  if ($this->configuration['tag_vocabulary']) {
    $this->tagsID = $this->configuration['prefix'] . 'wordpress_tags';
    $migration = static::createEntityFromPlugin('wordpress_tags', $this->tagsID);
    $migration
      ->set('migration_group', $this->configuration['group_id']);
    $process = $migration
      ->get('process');
    $process['vid'] = [
      'plugin' => 'default_value',
      'default_value' => $this->configuration['tag_vocabulary'],
    ];
    $migration
      ->set('process', $process);
    $migration
      ->save();
  }
  if ($this->configuration['category_vocabulary']) {
    $this->categoriesID = $this->configuration['prefix'] . 'wordpress_categories';
    $migration = static::createEntityFromPlugin('wordpress_categories', $this->categoriesID);
    $migration
      ->set('migration_group', $this->configuration['group_id']);
    $process = $migration
      ->get('process');
    $process['vid'] = [
      'plugin' => 'default_value',
      'default_value' => $this->configuration['category_vocabulary'],
    ];
    $migration
      ->set('process', $process);
    $migration
      ->save();
  }

  // Setup the content migrations.
  foreach ([
    'post',
    'page',
  ] as $wordpress_type) {
    if (!empty($this->configuration[$wordpress_type]['type'])) {
      $this
        ->createContentMigration($wordpress_type);
    }
  }
}