class Sql extends PluginBase implements MigrateIdMapInterface, ContainerFactoryPluginInterface, HighestIdInterface (View source)

Defines the sql based ID map implementation.

It creates one map and one message table per migration entity to store the relevant information.

Traits

Wrapper methods for \Drupal\Core\StringTranslation\TranslationInterface.

Provides dependency injection friendly methods for serialization.

Provides a trait for the messenger service.

Constants

DERIVATIVE_SEPARATOR

A string which is used to separate base plugin IDs from the derivative ID.

SOURCE_IDS_HASH

Column name of hashed source id values.

Properties

protected string $pluginId

The plugin_id.

from  PluginBase
protected array $pluginDefinition

The plugin implementation definition.

from  PluginBase
protected array $configuration

Configuration information passed into the plugin.

from  PluginBase
protected TranslationInterface $stringTranslation

The string translation service.

from  StringTranslationTrait
protected array $_serviceIds

An array of service IDs keyed by property name used for serialization.

from  DependencySerializationTrait
protected array $_entityStorages

An array of entity type IDs keyed by the property name of their storages.

from  DependencySerializationTrait
protected MessengerInterface $messenger

The messenger.

from  MessengerTrait
protected EventDispatcherInterface $eventDispatcher

An event dispatcher instance to use for map events.

protected string $mapTableName

The migration map table name.

protected string $messageTableName

The message table name.

protected MigrateMessageInterface $message

The migrate message service.

protected Connection $database

The database connection for the map/message tables on the destination.

protected SelectInterface $query

The select query.

protected MigrationInterface $migration

The migration being done.

protected array $sourceIdFields

The source ID fields.

protected array $destinationIdFields

The destination ID fields.

protected bool $initialized

Whether the plugin is already initialized.

protected StatementInterface $result

The result.

protected array $sourceIds

The source identifiers.

protected array $destinationIds

The destination identifiers.

protected array|false|null $currentRow

The current row.

protected array $currentKey

The current key.

Methods

__construct(array $configuration, string $plugin_id, mixed $plugin_definition, MigrationInterface $migration, EventDispatcherInterface $event_dispatcher)

Constructs an SQL object.

string
getPluginId()

Gets the plugin_id of the plugin instance.

string
getBaseId()

Gets the base_plugin_id of the plugin instance.

string|null
getDerivativeId()

Gets the derivative_id of the plugin instance.

array
getPluginDefinition()

Gets the definition of the plugin implementation.

bool
isConfigurable()

Determines if the plugin is configurable.

t(string $string, array $args = [], array $options = [])

Translates a string to the current language or to a given language.

formatPlural($count, $singular, $plural, array $args = [], array $options = [])

Formats a string containing a count of items.

getNumberOfPlurals($langcode = NULL)

Returns the number of plurals supported by a given language.

getStringTranslation()

Gets the string translation service.

$this
setStringTranslation(TranslationInterface $translation)

Sets the string translation service to use.

__sleep()

{@inheritdoc}

__wakeup()

{@inheritdoc}

setMessenger(MessengerInterface $messenger)

Sets the messenger.

messenger()

Gets the messenger.

create(ContainerInterface $container, array $configuration, string $plugin_id, mixed $plugin_definition, MigrationInterface $migration = NULL)

Creates an instance of the plugin.

string
getSourceIdsHash(array $source_id_values)

Retrieves the hash of the source identifier values.

array
sourceIdFields()

The source ID fields.

array
destinationIdFields()

The destination ID fields.

string
mapTableName()

The name of the database map table.

string
messageTableName()

The name of the database message table.

getQualifiedMapTableName()

Get the fully qualified map table name.

getDatabase()

Gets the database connection.

init()

Initialize the plugin.

setMessage(MigrateMessageInterface $message)

Sets the migrate message service.

ensureTables()

Create the map and message tables if they don't already exist.

array
getFieldSchema(array $id_definition)

Creates schema from an ID definition.

array
getRowBySource(array $source_id_values)

Retrieves a row from the map table based on source identifier values.

array
getRowByDestination(array $destination_id_values)

Retrieves a row by the destination identifiers.

array
getRowsNeedingUpdate(int $count)

Retrieves an array of map rows marked as needing update.

array
lookupSourceId(array $destination_id_values)

Looks up the source identifier.

array
lookupDestinationIds(array $source_id_values)

Looks up the destination identifiers corresponding to a source key.

saveIdMapping(Row $row, array $destination_id_values, $source_row_status = MigrateIdMapInterface::STATUS_IMPORTED, int $rollback_action = MigrateIdMapInterface::ROLLBACK_DELETE)

Saves a mapping from the source identifiers to the destination identifiers.

saveMessage(array $source_id_values, string $message, int $level = MigrationInterface::MESSAGE_ERROR)

Saves a message related to a source record in the migration message table.

getMessages(array $source_id_values = [], int $level = NULL)

Retrieves a traversable object of messages related to source records.

prepareUpdate()

Prepares to run a full update.

int
processedCount()

Returns the number of processed items in the map.

int
importedCount()

Returns the number of imported items in the map.

int
updateCount()

Returns a count of items which are marked as needing update.

int
errorCount()

Returns the number of items that failed to import.

int
messageCount()

Returns the number of messages saved.

int
countHelper(int|array $status = NULL, string $table = NULL)

Counts records in a table.

delete(array $source_id_values, bool $messages_only = FALSE)

Deletes the map and message entries for a given source record.

deleteDestination(array $destination_id_values)

Deletes the map and message table entries for a given destination row.

setUpdate(array $source_id_values)

Sets a specified record to be updated, if it exists.

clearMessages()

Clears all messages from the map.

destroy()

Removes any persistent storage used by this map.

rewind()

Implementation of \Iterator::rewind().

current()

Implementation of \Iterator::current().

key()

Implementation of \Iterator::key().

array
currentDestination()

Looks up the destination identifier currently being iterated.

array
currentSource()

Looks up the source identifier(s) currently being iterated.

next()

Implementation of \Iterator::next().

valid()

Implementation of \Iterator::valid().

getMigrationPluginManager()

Returns the migration plugin manager.

int
getHighestId()

Returns the highest ID tracked by the implementing plugin.

Details

__construct(array $configuration, string $plugin_id, mixed $plugin_definition, MigrationInterface $migration, EventDispatcherInterface $event_dispatcher)

Constructs an SQL object.

Sets up the tables and builds the maps,

Parameters

array $configuration

A configuration array containing information about the plugin instance.

string $plugin_id

The plugin_id for the plugin instance.

mixed $plugin_definition

The plugin implementation definition.

MigrationInterface $migration

The migration to do.

EventDispatcherInterface $event_dispatcher

The event dispatcher.

string getPluginId()

Gets the plugin_id of the plugin instance.

Return Value

string

The plugin_id of the plugin instance.

string getBaseId()

Gets the base_plugin_id of the plugin instance.

Return Value

string

The base_plugin_id of the plugin instance.

string|null getDerivativeId()

Gets the derivative_id of the plugin instance.

Return Value

string|null

The derivative_id of the plugin instance NULL otherwise.

array getPluginDefinition()

Gets the definition of the plugin implementation.

Return Value

array

The plugin definition, as returned by the discovery object used by the plugin manager.

bool isConfigurable()

Determines if the plugin is configurable.

Return Value

bool

A boolean indicating whether the plugin is configurable.

protected TranslatableMarkup t(string $string, array $args = [], array $options = [])

Translates a string to the current language or to a given language.

See \Drupal\Core\StringTranslation\TranslatableMarkup::__construct() for important security information and usage guidelines.

In order for strings to be localized, make them available in one of the ways supported by the

Parameters

string $string

A string containing the English text to translate.

array $args

(optional) An associative array of replacements to make after translation. Based on the first character of the key, the value is escaped and/or themed. See \Drupal\Component\Render\FormattableMarkup::placeholderFormat() for details.

array $options

(optional) An associative array of additional options, with the following elements:

  • 'langcode' (defaults to the current language): A language code, to translate to a language other than what is used to display the page.
  • 'context' (defaults to the empty context): The context the source string belongs to. See the @link i18n Internationalization topic @endlink for more information about string contexts.

Return Value

TranslatableMarkup

An object that, when cast to a string, returns the translated string.

See also

FormattableMarkup::placeholderFormat
TranslatableMarkup::__construct

protected formatPlural($count, $singular, $plural, array $args = [], array $options = [])

Formats a string containing a count of items.

Parameters

$count
$singular
$plural
array $args
array $options

See also

TranslationInterface::formatPlural

protected getNumberOfPlurals($langcode = NULL)

Returns the number of plurals supported by a given language.

Parameters

$langcode

See also

PluralFormulaInterface::getNumberOfPlurals

protected TranslationInterface getStringTranslation()

Gets the string translation service.

Return Value

TranslationInterface

The string translation service.

$this setStringTranslation(TranslationInterface $translation)

Sets the string translation service to use.

Parameters

TranslationInterface $translation

The string translation service.

Return Value

$this

__sleep()

{@inheritdoc}

__wakeup()

{@inheritdoc}

setMessenger(MessengerInterface $messenger)

Sets the messenger.

Parameters

MessengerInterface $messenger

The messenger.

MessengerInterface messenger()

Gets the messenger.

Return Value

MessengerInterface

The messenger.

static ContainerFactoryPluginInterface create(ContainerInterface $container, array $configuration, string $plugin_id, mixed $plugin_definition, MigrationInterface $migration = NULL)

Creates an instance of the plugin.

Parameters

ContainerInterface $container

The container to pull out services used in the plugin.

array $configuration

A configuration array containing information about the plugin instance.

string $plugin_id

The plugin ID for the plugin instance.

mixed $plugin_definition

The plugin implementation definition.

MigrationInterface $migration

Return Value

ContainerFactoryPluginInterface

Returns an instance of this plugin.

string getSourceIdsHash(array $source_id_values)

internal  
 

Retrieves the hash of the source identifier values.

Parameters

array $source_id_values

The source identifiers

Return Value

string

A hash containing the hashed values of the source identifiers.

protected array sourceIdFields()

The source ID fields.

Return Value

array

The source ID fields.

protected array destinationIdFields()

The destination ID fields.

Return Value

array

The destination ID fields.

string mapTableName()

The name of the database map table.

Return Value

string

The map table name.

string messageTableName()

The name of the database message table.

Return Value

string

The message table name.

getQualifiedMapTableName()

Get the fully qualified map table name.

Connection getDatabase()

Gets the database connection.

Return Value

Connection

The database connection object.

protected init()

Initialize the plugin.

setMessage(MigrateMessageInterface $message)

Sets the migrate message service.

Parameters

MigrateMessageInterface $message

The migrate message service.

protected ensureTables()

Create the map and message tables if they don't already exist.

Exceptions

DatabaseException

protected array getFieldSchema(array $id_definition)

Creates schema from an ID definition.

Parameters

array $id_definition

The definition of the field having the structure as the items returned by MigrateSourceInterface or MigrateDestinationInterface::getIds().

Return Value

array

The database schema definition.

See also

MigrateSourceInterface::getIds
MigrateDestinationInterface::getIds

array getRowBySource(array $source_id_values)

Retrieves a row from the map table based on source identifier values.

Parameters

array $source_id_values

The source identifier keyed values of the record, e.g. ['nid' => 5].

Return Value

array

The raw row data as an associative array.

array getRowByDestination(array $destination_id_values)

Retrieves a row by the destination identifiers.

Parameters

array $destination_id_values

The destination identifier keyed values of the record, e.g. ['nid' => 5].

Return Value

array

The row(s) of data or an empty array when there is no matching map row.

array getRowsNeedingUpdate(int $count)

Retrieves an array of map rows marked as needing update.

Parameters

int $count

The maximum number of rows to return.

Return Value

array

Array of map row objects that need updating.

array lookupSourceId(array $destination_id_values)

Looks up the source identifier.

Given a (possibly multi-field) destination identifier value, return the (possibly multi-field) source identifier value mapped to it.

Parameters

array $destination_id_values

The destination identifier keyed values of the record, e.g. ['nid' => 5].

Return Value

array

The source identifier keyed values of the record, e.g. ['nid' => 5], or an empty array on failure.

array lookupDestinationIds(array $source_id_values)

Looks up the destination identifiers corresponding to a source key.

This can look up a subset of source keys if only some are provided, and will return all destination keys that match.

Parameters

array $source_id_values

The source identifier keyed values of the records, e.g. ['nid' => 5]. If unkeyed, the first count($source_id_values) keys will be assumed.

Return Value

array

An array of arrays of destination identifier values.

Exceptions

MigrateException

saveIdMapping(Row $row, array $destination_id_values, $source_row_status = MigrateIdMapInterface::STATUS_IMPORTED, int $rollback_action = MigrateIdMapInterface::ROLLBACK_DELETE)

Saves a mapping from the source identifiers to the destination identifiers.

Called upon import of one row, we record a mapping from the source ID to the destination ID. Also may be called, setting the third parameter to NEEDS_UPDATE, to signal an existing record should be re-migrated.

Parameters

Row $row

The raw source data. We use the ID map derived from the source object to get the source identifier values.

array $destination_id_values

An array of destination identifier values.

$source_row_status
int $rollback_action

(optional) How to handle the destination object on rollback. Defaults to self::ROLLBACK_DELETE.

saveMessage(array $source_id_values, string $message, int $level = MigrationInterface::MESSAGE_ERROR)

Saves a message related to a source record in the migration message table.

Parameters

array $source_id_values

The source identifier keyed values of the record, e.g. ['nid' => 5].

string $message

The message to record.

int $level

(optional) The message severity. Defaults to MigrationInterface::MESSAGE_ERROR.

Traversable getMessages(array $source_id_values = [], int $level = NULL)

Retrieves a traversable object of messages related to source records.

Parameters

array $source_id_values

(optional) The source identifier keyed values of the record, e.g. ['nid' => 5]. If empty (the default), all messages are retrieved.

int $level

(optional) Message severity. If NULL (the default), retrieve messages of all severities.

Return Value

Traversable

Retrieves a traversable object of message objects of unspecified class. Each object has the following public properties:

  • source_row_hash: the hash of the entire serialized source row data.
  • message: the text of the message.
  • level: one of MigrationInterface::MESSAGE_ERROR, MigrationInterface::MESSAGE_WARNING, MigrationInterface::MESSAGE_NOTICE, MigrationInterface::MESSAGE_INFORMATIONAL.

prepareUpdate()

Prepares to run a full update.

Prepares this migration to run as an update - that is, in addition to un-migrated content (source records not in the map table) being imported, previously-migrated content will also be updated in place by marking all previously-imported content as ready to be re-imported.

int processedCount()

Returns the number of processed items in the map.

Return Value

int

The count of records in the map table.

int importedCount()

Returns the number of imported items in the map.

Return Value

int

The number of imported items.

int updateCount()

Returns a count of items which are marked as needing update.

Return Value

int

The number of items which need updating.

int errorCount()

Returns the number of items that failed to import.

Return Value

int

The number of items that failed to import.

int messageCount()

Returns the number of messages saved.

Return Value

int

The number of messages.

protected int countHelper(int|array $status = NULL, string $table = NULL)

Counts records in a table.

Parameters

int|array $status

(optional) Status code(s) to filter the source_row_status column.

string $table

(optional) The table to work. Defaults to NULL.

Return Value

int

The number of records.

delete(array $source_id_values, bool $messages_only = FALSE)

Deletes the map and message entries for a given source record.

Parameters

array $source_id_values

The source identifier keyed values of the record, e.g. ['nid' => 5].

bool $messages_only

(optional) TRUE to only delete the migrate messages. Defaults to FALSE.

deleteDestination(array $destination_id_values)

Deletes the map and message table entries for a given destination row.

Parameters

array $destination_id_values

The destination identifier key value pairs we should do the deletes for.

setUpdate(array $source_id_values)

Sets a specified record to be updated, if it exists.

Parameters

array $source_id_values

The source identifier values of the record.

clearMessages()

Clears all messages from the map.

destroy()

Removes any persistent storage used by this map.

For example, remove the map and message tables.

rewind()

Implementation of \Iterator::rewind().

This is called before beginning a foreach loop.

current()

Implementation of \Iterator::current().

This is called when entering a loop iteration, returning the current row.

key()

Implementation of \Iterator::key().

This is called when entering a loop iteration, returning the key of the current row. It must be a scalar - we will serialize to fulfill the requirement, but using getCurrentKey() is preferable.

array currentDestination()

Looks up the destination identifier currently being iterated.

Return Value

array

The destination identifier values of the record, or NULL on failure.

array currentSource()

Looks up the source identifier(s) currently being iterated.

Return Value

array

The source identifier values of the record, or NULL on failure.

next()

Implementation of \Iterator::next().

This is called at the bottom of the loop implicitly, as well as explicitly from rewind().

valid()

Implementation of \Iterator::valid().

This is called at the top of the loop, returning TRUE to process the loop and FALSE to terminate it.

protected MigrationPluginManagerInterface getMigrationPluginManager()

Returns the migration plugin manager.

Inject as a dependency in https://www.drupal.org/node/2919158.

Return Value

MigrationPluginManagerInterface

The migration plugin manager.

int getHighestId()

Returns the highest ID tracked by the implementing plugin.

Return Value

int

The highest ID.