Migration.php 8.99 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
<?php

/**
 * @file
 * Contains \Drupal\migrate\Entity\Migration.
 */

namespace Drupal\migrate\Entity;

use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\migrate\MigrateException;
use Drupal\migrate\Plugin\MigrateIdMapInterface;
13
use Drupal\migrate\Plugin\RequirementsInterface;
14 15 16 17 18 19 20

/**
 * Defines the Migration entity.
 *
 * The migration entity stores the information about a single migration, like
 * the source, process and destination plugins.
 *
21
 * @ConfigEntityType(
22 23 24
 *   id = "migration",
 *   label = @Translation("Migration"),
 *   module = "migrate",
25
 *   handlers = {
26
 *     "storage" = "Drupal\migrate\MigrationStorage"
27 28 29 30
 *   },
 *   entity_keys = {
 *     "id" = "id",
 *     "label" = "label",
31
 *     "weight" = "weight"
32 33 34
 *   }
 * )
 */
35
class Migration extends ConfigEntityBase implements MigrationInterface, RequirementsInterface {
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80

  /**
   * The migration ID (machine name).
   *
   * @var string
   */
  public $id;

  /**
   * The human-readable label for the migration.
   *
   * @var string
   */
  public $label;

  /**
   * The plugin ID for the row.
   *
   * @var string
   */
  public $row;

  /**
   * The source configuration, with at least a 'plugin' key.
   *
   * Used to initialize the $sourcePlugin.
   *
   * @var array
   */
  public $source;

  /**
   * The source plugin.
   *
   * @var \Drupal\migrate\Plugin\MigrateSourceInterface
   */
  protected $sourcePlugin;

  /**
   * The configuration describing the process plugins.
   *
   * @var array
   */
  public $process;

81 82 83 84 85 86 87
  /**
   * The configuration describing the load plugins.
   *
   * @var array
   */
  public $load;

88 89 90 91 92 93 94
  /**
   * The cached process plugins.
   *
   * @var array
   */
  protected $processPlugins = array();

95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
  /**
   * The destination configuration, with at least a 'plugin' key.
   *
   * Used to initialize $destinationPlugin.
   *
   * @var array
   */
  public $destination;

  /**
   * The destination plugin.
   *
   * @var \Drupal\migrate\Plugin\MigrateDestinationInterface
   */
  protected $destinationPlugin;

  /**
   * The identifier map data.
   *
   * Used to initialize $idMapPlugin.
   *
   * @var string
   */
  public $idMap = array();

  /**
   * The identifier map.
   *
   * @var \Drupal\migrate\Plugin\MigrateIdMapInterface
   */
  protected $idMapPlugin;

  /**
   * The source identifiers.
   *
   * An array of source identifiers: the keys are the name of the properties,
   * the values are dependent on the ID map plugin.
   *
   * @var array
   */
  public $sourceIds = array();

  /**
   * The destination identifiers.
   *
   * An array of destination identifiers: the keys are the name of the
   * properties, the values are dependent on the ID map plugin.
   *
   * @var array
   */
145
  public $destinationIds = FALSE;
146 147

  /**
148
   * Information on the high water mark.
149 150 151
   *
   * @var array
   */
152
  public $highWaterProperty;
153 154 155 156 157 158 159 160 161 162 163 164 165 166

  /**
   * Indicate whether the primary system of record for this migration is the
   * source, or the destination (Drupal). In the source case, migration of
   * an existing object will completely replace the Drupal object with data from
   * the source side. In the destination case, the existing Drupal object will
   * be loaded, then changes from the source applied; also, rollback will not be
   * supported.
   *
   * @var string
   */
  public $systemOfRecord = self::SOURCE;

  /**
167
   * Specify value of source_row_status for current map row. Usually set by
168 169 170 171
   * MigrateFieldHandler implementations.
   *
   * @var int
   */
172
  public $sourceRowStatus = MigrateIdMapInterface::STATUS_IMPORTED;
173 174 175 176

  /**
   * @var \Drupal\Core\KeyValueStore\KeyValueStoreInterface
   */
177
  protected $highWaterStorage;
178 179 180 181 182 183

  /**
   * @var bool
   */
  public $trackLastImported = FALSE;

184
  /**
185
   * These migrations must be already executed before this migration can run.
186
   *
187
   * @var array
188
   */
189
  protected $requirements = array();
190 191

  /**
192
   * These migrations, if ran at all, must be executed before this migration.
193 194 195
   *
   * @var array
   */
196
  public $migration_dependencies = array();
197

198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
  /**
   * {@inheritdoc}
   */
  public function getSourcePlugin() {
    if (!isset($this->sourcePlugin)) {
      $this->sourcePlugin = \Drupal::service('plugin.manager.migrate.source')->createInstance($this->source['plugin'], $this->source, $this);
    }
    return $this->sourcePlugin;
  }

  /**
   * {@inheritdoc}
   */
  public function getProcessPlugins(array $process = NULL) {
    if (!isset($process)) {
      $process = $this->process;
    }
215 216
    $index = serialize($process);
    if (!isset($this->processPlugins[$index])) {
217
      $this->processPlugins[$index] = array();
218 219 220 221 222 223 224 225 226 227 228 229 230
      foreach ($this->getProcessNormalized($process) as $property => $configurations) {
        $this->processPlugins[$index][$property] = array();
        foreach ($configurations as $configuration) {
          if (isset($configuration['source'])) {
            $this->processPlugins[$index][$property][] = \Drupal::service('plugin.manager.migrate.process')->createInstance('get', $configuration, $this);
          }
          // Get is already handled.
          if ($configuration['plugin'] != 'get') {
            $this->processPlugins[$index][$property][] = \Drupal::service('plugin.manager.migrate.process')->createInstance($configuration['plugin'], $configuration, $this);
          }
          if (!$this->processPlugins[$index][$property]) {
            throw new MigrateException("Invalid process configuration for $property");
          }
231 232 233
        }
      }
    }
234
    return $this->processPlugins[$index];
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
  }

  /**
   * Resolve shorthands into a list of plugin configurations.
   *
   * @param array $process
   *   A process configuration array.
   *
   * @return array
   *   The normalized process configuration.
   */
  protected function getProcessNormalized(array $process) {
    $normalized_configurations = array();
    foreach ($process as $destination => $configuration) {
      if (is_string($configuration)) {
        $configuration = array(
          'plugin' => 'get',
          'source' => $configuration,
        );
      }
      if (isset($configuration['plugin'])) {
        $configuration = array($configuration);
      }
      $normalized_configurations[$destination] = $configuration;
    }
    return $normalized_configurations;
  }

  /**
   * {@inheritdoc}
   */
  public function getDestinationPlugin() {
    if (!isset($this->destinationPlugin)) {
      $this->destinationPlugin = \Drupal::service('plugin.manager.migrate.destination')->createInstance($this->destination['plugin'], $this->destination, $this);
    }
    return $this->destinationPlugin;
  }

  /**
   * {@inheritdoc}
   */
  public function getIdMap() {
    if (!isset($this->idMapPlugin)) {
      $configuration = $this->idMap;
      $plugin = isset($configuration['plugin']) ? $configuration['plugin'] : 'sql';
      $this->idMapPlugin = \Drupal::service('plugin.manager.migrate.id_map')->createInstance($plugin, $configuration, $this);
    }
    return $this->idMapPlugin;
  }

  /**
286
   * Get the high water storage object.
287
   *
288
   * @return \Drupal\Core\KeyValueStore\KeyValueStoreInterface
289
   *   The storage object.
290 291
   */
  protected function getHighWaterStorage() {
292 293
    if (!isset($this->highWaterStorage)) {
      $this->highWaterStorage = \Drupal::keyValue('migrate:high_water');
294
    }
295
    return $this->highWaterStorage;
296 297
  }

298 299 300
  /**
   * {@inheritdoc}
   */
301
  public function getHighWater() {
302 303 304
    return $this->getHighWaterStorage()->get($this->id());
  }

305 306 307
  /**
   * {@inheritdoc}
   */
308 309
  public function saveHighWater($high_water) {
    $this->getHighWaterStorage()->set($this->id(), $high_water);
310
  }
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326

  /**
   * {@inheritdoc}
   */
  public function checkRequirements() {
    // Check whether the current migration source and destination plugin
    // requirements are met or not.
    try {
      if ($this->getSourcePlugin() instanceof RequirementsInterface && !$this->getSourcePlugin()->checkRequirements()) {
        return FALSE;
      }
      if ($this->getDestinationPlugin() instanceof RequirementsInterface && !$this->getDestinationPlugin()->checkRequirements()) {
        return FALSE;
      }

      /** @var \Drupal\migrate\Entity\MigrationInterface[] $required_migrations */
327
      $required_migrations = \Drupal::entityManager()->getStorage('migration')->loadMultiple($this->requirements);
328 329
      // Check if the dependencies are in good shape.
      foreach ($required_migrations as $required_migration) {
330
        if (!$required_migration->isComplete()) {
331 332 333 334 335 336 337 338 339 340 341
          return FALSE;
        }
      }
    }
    catch (\Exception $e) {
      return FALSE;
    }

    return TRUE;
  }

342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
  /**
   * {@inheritdoc}
   */
  public function setMigrationResult($result) {
    $migrate_result_store = \Drupal::keyValue('migrate_result');
    $migrate_result_store->set($this->id(), $result);
  }

  /**
   * {@inheritdoc}
   */
  public function getMigrationResult() {
    $migrate_result_store = \Drupal::keyValue('migrate_result');
    return $migrate_result_store->get($this->id(), static::RESULT_INCOMPLETE);
  }

  /**
   * {@inheritdoc}
   */
  public function isComplete() {
    return $this->getMigrationResult() === static::RESULT_COMPLETED;
  }

365
}