View source
<?php
namespace Drupal\migrate\Plugin\migrate\id_map;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Plugin\PluginBase;
use Drupal\migrate\Entity\MigrationInterface;
use Drupal\migrate\Event\MigrateIdMapMessageEvent;
use Drupal\migrate\MigrateException;
use Drupal\migrate\MigrateMessageInterface;
use Drupal\migrate\Plugin\MigrateIdMapInterface;
use Drupal\migrate\Row;
use Drupal\migrate\Event\MigrateEvents;
use Drupal\migrate\Event\MigrateMapSaveEvent;
use Drupal\migrate\Event\MigrateMapDeleteEvent;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
class Sql extends PluginBase implements MigrateIdMapInterface, ContainerFactoryPluginInterface {
protected $eventDispatcher;
protected $mapTableName;
protected $messageTableName;
protected $message;
protected $database;
protected $query;
protected $migration;
protected $sourceIdFields;
protected $destinationIdFields;
protected $initialized;
protected $result = NULL;
protected $sourceIds = array();
protected $destinationIds = array();
protected $currentRow = NULL;
protected $currentKey = array();
public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, EventDispatcherInterface $event_dispatcher) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->migration = $migration;
$this->eventDispatcher = $event_dispatcher;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
return new static($configuration, $plugin_id, $plugin_definition, $migration, $container
->get('event_dispatcher'));
}
protected function sourceIdFields() {
if (!isset($this->sourceIdFields)) {
$this->sourceIdFields = array();
$count = 1;
foreach ($this->migration
->getSourcePlugin()
->getIds() as $field => $schema) {
$this->sourceIdFields[$field] = 'sourceid' . $count++;
}
}
return $this->sourceIdFields;
}
protected function destinationIdFields() {
if (!isset($this->destinationIdFields)) {
$this->destinationIdFields = array();
$count = 1;
foreach ($this->migration
->getDestinationPlugin()
->getIds() as $field => $schema) {
$this->destinationIdFields[$field] = 'destid' . $count++;
}
}
return $this->destinationIdFields;
}
public function mapTableName() {
$this
->init();
return $this->mapTableName;
}
public function messageTableName() {
$this
->init();
return $this->messageTableName;
}
public function getQualifiedMapTableName() {
return $this
->getDatabase()
->getFullQualifiedTableName($this->mapTableName);
}
public function getDatabase() {
if (!isset($this->database)) {
$this->database = \Drupal::database();
}
$this
->init();
return $this->database;
}
protected function init() {
if (!$this->initialized) {
$this->initialized = TRUE;
$machine_name = str_replace(':', '__', $this->migration
->id());
$prefix_length = strlen($this
->getDatabase()
->tablePrefix());
$this->mapTableName = 'migrate_map_' . Unicode::strtolower($machine_name);
$this->mapTableName = Unicode::substr($this->mapTableName, 0, 63 - $prefix_length);
$this->messageTableName = 'migrate_message_' . Unicode::strtolower($machine_name);
$this->messageTableName = Unicode::substr($this->messageTableName, 0, 63 - $prefix_length);
$this
->ensureTables();
}
}
public function setMessage(MigrateMessageInterface $message) {
$this->message = $message;
}
protected function ensureTables() {
if (!$this
->getDatabase()
->schema()
->tableExists($this->mapTableName)) {
$count = 1;
$source_id_schema = array();
$pks = array();
foreach ($this->migration
->getSourcePlugin()
->getIds() as $id_definition) {
$mapkey = 'sourceid' . $count++;
$source_id_schema[$mapkey] = $this
->getFieldSchema($id_definition);
$source_id_schema[$mapkey]['not null'] = TRUE;
if (isset($source_id_schema[$mapkey]['type']) && $source_id_schema[$mapkey]['type'] == 'varchar') {
$source_id_schema[$mapkey]['type'] = 'varchar_ascii';
}
$pks[] = $mapkey;
}
$fields = $source_id_schema;
$count = 1;
foreach ($this->migration
->getDestinationPlugin()
->getIds() as $id_definition) {
$mapkey = 'destid' . $count++;
$fields[$mapkey] = $this
->getFieldSchema($id_definition);
$fields[$mapkey]['not null'] = FALSE;
}
$fields['source_row_status'] = array(
'type' => 'int',
'size' => 'tiny',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => MigrateIdMapInterface::STATUS_IMPORTED,
'description' => 'Indicates current status of the source row',
);
$fields['rollback_action'] = array(
'type' => 'int',
'size' => 'tiny',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => MigrateIdMapInterface::ROLLBACK_DELETE,
'description' => 'Flag indicating what to do for this item on rollback',
);
$fields['last_imported'] = array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
'description' => 'UNIX timestamp of the last time this row was imported',
);
$fields['hash'] = array(
'type' => 'varchar',
'length' => '64',
'not null' => FALSE,
'description' => 'Hash of source row data, for detecting changes',
);
$schema = array(
'description' => 'Mappings from source identifier value(s) to destination identifier value(s).',
'fields' => $fields,
);
if ($pks) {
$schema['primary key'] = $pks;
}
$this
->getDatabase()
->schema()
->createTable($this->mapTableName, $schema);
if (!$this
->getDatabase()
->schema()
->tableExists($this
->messageTableName())) {
$fields = array();
$fields['msgid'] = array(
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
);
$fields += $source_id_schema;
$fields['level'] = array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 1,
);
$fields['message'] = array(
'type' => 'text',
'size' => 'medium',
'not null' => TRUE,
);
$schema = array(
'description' => 'Messages generated during a migration process',
'fields' => $fields,
'primary key' => array(
'msgid',
),
);
if ($pks) {
$schema['indexes']['sourcekey'] = $pks;
}
$this
->getDatabase()
->schema()
->createTable($this
->messageTableName(), $schema);
}
}
else {
if (!$this
->getDatabase()
->schema()
->fieldExists($this->mapTableName, 'rollback_action')) {
$this
->getDatabase()
->schema()
->addField($this->mapTableName, 'rollback_action', array(
'type' => 'int',
'size' => 'tiny',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
'description' => 'Flag indicating what to do for this item on rollback',
));
}
if (!$this
->getDatabase()
->schema()
->fieldExists($this->mapTableName, 'hash')) {
$this
->getDatabase()
->schema()
->addField($this->mapTableName, 'hash', array(
'type' => 'varchar',
'length' => '64',
'not null' => FALSE,
'description' => 'Hash of source row data, for detecting changes',
));
}
}
}
protected function getFieldSchema(array $id_definition) {
$type_parts = explode('.', $id_definition['type']);
if (count($type_parts) == 1) {
$type_parts[] = 'value';
}
$schema = BaseFieldDefinition::create($type_parts[0])
->getColumns();
return $schema[$type_parts[1]];
}
public function getRowBySource(array $source_id_values) {
$query = $this
->getDatabase()
->select($this
->mapTableName(), 'map')
->fields('map');
foreach ($this
->sourceIdFields() as $field_name => $source_id) {
$query
->condition("map.{$source_id}", $source_id_values[$field_name], '=');
}
$result = $query
->execute();
return $result
->fetchAssoc();
}
public function getRowByDestination(array $destination_id_values) {
$query = $this
->getDatabase()
->select($this
->mapTableName(), 'map')
->fields('map');
foreach ($this
->destinationIdFields() as $field_name => $destination_id) {
$query
->condition("map.{$destination_id}", $destination_id_values[$field_name], '=');
}
$result = $query
->execute();
return $result
->fetchAssoc();
}
public function getRowsNeedingUpdate($count) {
$rows = array();
$result = $this
->getDatabase()
->select($this
->mapTableName(), 'map')
->fields('map')
->condition('source_row_status', MigrateIdMapInterface::STATUS_NEEDS_UPDATE)
->range(0, $count)
->execute();
foreach ($result as $row) {
$rows[] = $row;
}
return $rows;
}
public function lookupSourceID(array $destination_id_values) {
$source_id_fields = $this
->sourceIdFields();
$query = $this
->getDatabase()
->select($this
->mapTableName(), 'map');
foreach ($source_id_fields as $source_field_name => $idmap_field_name) {
$query
->addField('map', $idmap_field_name, $source_field_name);
}
foreach ($this
->destinationIdFields() as $field_name => $destination_id) {
$query
->condition("map.{$destination_id}", $destination_id_values[$field_name], '=');
}
$result = $query
->execute();
return $result
->fetchAssoc() ?: [];
}
public function lookupDestinationId(array $source_id_values) {
if (empty($source_id_values)) {
return array();
}
$query = $this
->getDatabase()
->select($this
->mapTableName(), 'map')
->fields('map', $this
->destinationIdFields());
$have_keys = !isset($source_id_values[0]);
foreach ($this
->sourceIdFields() as $field_name => $source_id) {
$query
->condition("map.{$source_id}", $have_keys ? $source_id_values[$field_name] : array_shift($source_id_values), '=');
}
$result = $query
->execute();
$destination_id = $result
->fetchAssoc();
return array_values($destination_id ?: array());
}
public function saveIdMapping(Row $row, array $destination_id_values, $source_row_status = MigrateIdMapInterface::STATUS_IMPORTED, $rollback_action = MigrateIdMapInterface::ROLLBACK_DELETE) {
$source_id_values = $row
->getSourceIdValues();
$keys = array();
foreach ($this
->sourceIdFields() as $field_name => $key_name) {
if (!isset($source_id_values[$field_name])) {
$this->message
->display(t('Could not save to map table due to NULL value for key field @field', array(
'@field' => $field_name,
)), 'error');
return;
}
$keys[$key_name] = $source_id_values[$field_name];
}
$fields = array(
'source_row_status' => (int) $source_row_status,
'rollback_action' => (int) $rollback_action,
'hash' => $row
->getHash(),
);
$count = 0;
foreach ($destination_id_values as $dest_id) {
$fields['destid' . ++$count] = $dest_id;
}
if ($count && $count != count($this
->destinationIdFields())) {
$this->message
->display(t('Could not save to map table due to missing destination id values'), 'error');
return;
}
if ($this->migration
->get('trackLastImported')) {
$fields['last_imported'] = time();
}
if ($keys) {
$this->eventDispatcher
->dispatch(MigrateEvents::MAP_SAVE, new MigrateMapSaveEvent($this, $keys + $fields));
$this
->getDatabase()
->merge($this
->mapTableName())
->key($keys)
->fields($fields)
->execute();
}
}
public function saveMessage(array $source_id_values, $message, $level = MigrationInterface::MESSAGE_ERROR) {
foreach ($this
->sourceIdFields() as $field_name => $source_id) {
if (!isset($source_id_values[$field_name])) {
return;
}
$fields[$source_id] = $source_id_values[$field_name];
}
$fields['level'] = $level;
$fields['message'] = $message;
$this
->getDatabase()
->insert($this
->messageTableName())
->fields($fields)
->execute();
$this->eventDispatcher
->dispatch(MigrateEvents::IDMAP_MESSAGE, new MigrateIdMapMessageEvent($this->migration, $source_id_values, $message, $level));
}
public function getMessageIterator(array $source_id_values = [], $level = NULL) {
$query = $this
->getDatabase()
->select($this
->messageTableName(), 'msg')
->fields('msg');
foreach ($this
->sourceIdFields() as $field_name => $source_id) {
if (isset($source_id_values[$field_name])) {
$query
->condition($source_id, $source_id_values[$field_name]);
}
}
if ($level) {
$query
->condition('level', $level);
}
return $query
->execute();
}
public function prepareUpdate() {
$this
->getDatabase()
->update($this
->mapTableName())
->fields(array(
'source_row_status' => MigrateIdMapInterface::STATUS_NEEDS_UPDATE,
))
->execute();
}
public function processedCount() {
return $this
->getDatabase()
->select($this
->mapTableName())
->countQuery()
->execute()
->fetchField();
}
public function importedCount() {
return $this
->getDatabase()
->select($this
->mapTableName())
->condition('source_row_status', array(
MigrateIdMapInterface::STATUS_IMPORTED,
MigrateIdMapInterface::STATUS_NEEDS_UPDATE,
), 'IN')
->countQuery()
->execute()
->fetchField();
}
public function updateCount() {
return $this
->countHelper(MigrateIdMapInterface::STATUS_NEEDS_UPDATE);
}
public function errorCount() {
return $this
->countHelper(MigrateIdMapInterface::STATUS_FAILED);
}
public function messageCount() {
return $this
->countHelper(NULL, $this
->messageTableName());
}
protected function countHelper($status, $table = NULL) {
$query = $this
->getDatabase()
->select($table ?: $this
->mapTableName());
if (isset($status)) {
$query
->condition('source_row_status', $status);
}
return $query
->countQuery()
->execute()
->fetchField();
}
public function delete(array $source_id_values, $messages_only = FALSE) {
if (empty($source_id_values)) {
throw new MigrateException('Without source identifier values it is impossible to find the row to delete.');
}
if (!$messages_only) {
$map_query = $this
->getDatabase()
->delete($this
->mapTableName());
}
$message_query = $this
->getDatabase()
->delete($this
->messageTableName());
foreach ($this
->sourceIdFields() as $field_name => $source_id) {
if (!$messages_only) {
$map_query
->condition($source_id, $source_id_values[$field_name]);
}
$message_query
->condition($source_id, $source_id_values[$field_name]);
}
if (!$messages_only) {
$this->eventDispatcher
->dispatch(MigrateEvents::MAP_DELETE, new MigrateMapDeleteEvent($this, $source_id_values));
$map_query
->execute();
}
$message_query
->execute();
}
public function deleteDestination(array $destination_id_values) {
$map_query = $this
->getDatabase()
->delete($this
->mapTableName());
$message_query = $this
->getDatabase()
->delete($this
->messageTableName());
$source_id_values = $this
->lookupSourceID($destination_id_values);
if (!empty($source_id_values)) {
foreach ($this
->destinationIdFields() as $field_name => $destination_id) {
$map_query
->condition($destination_id, $destination_id_values[$field_name]);
}
$this->eventDispatcher
->dispatch(MigrateEvents::MAP_DELETE, new MigrateMapDeleteEvent($this, $source_id_values));
$map_query
->execute();
$count = 1;
foreach ($this
->sourceIdFields() as $field_name => $source_id) {
$message_query
->condition($source_id, $source_id_values[$field_name]);
$count++;
}
$message_query
->execute();
}
}
public function setUpdate(array $source_id_values) {
if (empty($source_id_values)) {
throw new MigrateException('No source identifiers provided to update.');
}
$query = $this
->getDatabase()
->update($this
->mapTableName())
->fields(array(
'source_row_status' => MigrateIdMapInterface::STATUS_NEEDS_UPDATE,
));
foreach ($this
->sourceIdFields() as $field_name => $source_id) {
$query
->condition($source_id, $source_id_values[$field_name]);
}
$query
->execute();
}
public function clearMessages() {
$this
->getDatabase()
->truncate($this
->messageTableName())
->execute();
}
public function destroy() {
$this
->getDatabase()
->schema()
->dropTable($this
->mapTableName());
$this
->getDatabase()
->schema()
->dropTable($this
->messageTableName());
}
public function rewind() {
$this->currentRow = NULL;
$fields = array();
foreach ($this
->sourceIdFields() as $field) {
$fields[] = $field;
}
foreach ($this
->destinationIdFields() as $field) {
$fields[] = $field;
}
$this->result = $this
->getDatabase()
->select($this
->mapTableName(), 'map')
->fields('map', $fields)
->execute();
$this
->next();
}
public function current() {
return $this->currentRow;
}
public function key() {
return serialize($this->currentKey);
}
public function currentDestination() {
if ($this
->valid()) {
$result = array();
foreach ($this
->destinationIdFields() as $destination_field_name => $idmap_field_name) {
$result[$destination_field_name] = $this->currentRow[$idmap_field_name];
}
return $result;
}
else {
return NULL;
}
}
public function next() {
$this->currentRow = $this->result
->fetchAssoc();
$this->currentKey = array();
if ($this->currentRow) {
foreach ($this
->sourceIdFields() as $map_field) {
$this->currentKey[$map_field] = $this->currentRow[$map_field];
unset($this->currentRow[$map_field]);
}
}
}
public function valid() {
return $this->currentRow !== FALSE;
}
}