Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / migrate / src / Plugin / migrate / id_map / Sql.php
index 5949b59fbfa3952d575ec2b60b335593eb316bb1..0de83485ffa9a1c9f1bc38f1bb732ace7d4a637e 100644 (file)
@@ -2,7 +2,7 @@
 
 namespace Drupal\migrate\Plugin\migrate\id_map;
 
-use Drupal\Component\Utility\Unicode;
+use Drupal\Core\Database\DatabaseException;
 use Drupal\Core\Field\BaseFieldDefinition;
 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
 use Drupal\Core\Plugin\PluginBase;
@@ -161,6 +161,18 @@ class Sql extends PluginBase implements MigrateIdMapInterface, ContainerFactoryP
     $this->migration = $migration;
     $this->eventDispatcher = $event_dispatcher;
     $this->message = new MigrateMessage();
+
+    if (!isset($this->database)) {
+      $this->database = \Drupal::database();
+    }
+
+    // Default generated table names, limited to 63 characters.
+    $machine_name = str_replace(':', '__', $this->migration->id());
+    $prefix_length = strlen($this->database->tablePrefix());
+    $this->mapTableName = 'migrate_map_' . mb_strtolower($machine_name);
+    $this->mapTableName = mb_substr($this->mapTableName, 0, 63 - $prefix_length);
+    $this->messageTableName = 'migrate_message_' . mb_strtolower($machine_name);
+    $this->messageTableName = mb_substr($this->messageTableName, 0, 63 - $prefix_length);
   }
 
   /**
@@ -187,7 +199,7 @@ class Sql extends PluginBase implements MigrateIdMapInterface, ContainerFactoryP
    * @return string
    *   An hash containing the hashed values of the source identifiers.
    */
-  public function getSourceIDsHash(array $source_id_values) {
+  public function getSourceIdsHash(array $source_id_values) {
     // When looking up the destination ID we require an array with both the
     // source key and value, e.g. ['nid' => 41]. In this case, $source_id_values
     // need to be ordered the same order as $this->sourceIdFields().
@@ -246,7 +258,6 @@ class Sql extends PluginBase implements MigrateIdMapInterface, ContainerFactoryP
    *   The map table name.
    */
   public function mapTableName() {
-    $this->init();
     return $this->mapTableName;
   }
 
@@ -257,7 +268,6 @@ class Sql extends PluginBase implements MigrateIdMapInterface, ContainerFactoryP
    *   The message table name.
    */
   public function messageTableName() {
-    $this->init();
     return $this->messageTableName;
   }
 
@@ -278,9 +288,6 @@ class Sql extends PluginBase implements MigrateIdMapInterface, ContainerFactoryP
    *   The database connection object.
    */
   public function getDatabase() {
-    if (!isset($this->database)) {
-      $this->database = \Drupal::database();
-    }
     $this->init();
     return $this->database;
   }
@@ -291,13 +298,6 @@ class Sql extends PluginBase implements MigrateIdMapInterface, ContainerFactoryP
   protected function init() {
     if (!$this->initialized) {
       $this->initialized = TRUE;
-      // Default generated table names, limited to 63 characters.
-      $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();
     }
   }
@@ -491,7 +491,7 @@ class Sql extends PluginBase implements MigrateIdMapInterface, ContainerFactoryP
   public function getRowBySource(array $source_id_values) {
     $query = $this->getDatabase()->select($this->mapTableName(), 'map')
       ->fields('map');
-    $query->condition(static::SOURCE_IDS_HASH, $this->getSourceIDsHash($source_id_values));
+    $query->condition(static::SOURCE_IDS_HASH, $this->getSourceIdsHash($source_id_values));
     $result = $query->execute();
     return $result->fetchAssoc();
   }
@@ -528,7 +528,7 @@ class Sql extends PluginBase implements MigrateIdMapInterface, ContainerFactoryP
   /**
    * {@inheritdoc}
    */
-  public function lookupSourceID(array $destination_id_values) {
+  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) {
@@ -562,9 +562,13 @@ class Sql extends PluginBase implements MigrateIdMapInterface, ContainerFactoryP
     $conditions = [];
     foreach ($this->sourceIdFields() as $field_name => $db_field) {
       if ($is_associative) {
-        // Associative $source_id_values can have fields out of order.
-        if (isset($source_id_values[$field_name])) {
-          $conditions[$db_field] = $source_id_values[$field_name];
+        // Ensure to handle array elements with a NULL value.
+        if (array_key_exists($field_name, $source_id_values)) {
+          // Associative $source_id_values can have fields out of order.
+          if (isset($source_id_values[$field_name])) {
+            // Only add a condition if the value is not NULL.
+            $conditions[$db_field] = $source_id_values[$field_name];
+          }
           unset($source_id_values[$field_name]);
         }
       }
@@ -579,14 +583,15 @@ class Sql extends PluginBase implements MigrateIdMapInterface, ContainerFactoryP
     }
 
     if (!empty($source_id_values)) {
-      throw new MigrateException("Extra unknown items in source IDs");
+      $var_dump = var_export($source_id_values, TRUE);
+      throw new MigrateException(sprintf("Extra unknown items in source IDs: %s", $var_dump));
     }
 
     $query = $this->getDatabase()->select($this->mapTableName(), 'map')
       ->fields('map', $this->destinationIdFields());
     if (count($this->sourceIdFields()) === count($conditions)) {
       // Optimization: Use the primary key.
-      $query->condition(self::SOURCE_IDS_HASH, $this->getSourceIDsHash(array_values($conditions)));
+      $query->condition(self::SOURCE_IDS_HASH, $this->getSourceIdsHash(array_values($conditions)));
     }
     else {
       foreach ($conditions as $db_field => $value) {
@@ -636,7 +641,7 @@ class Sql extends PluginBase implements MigrateIdMapInterface, ContainerFactoryP
     if ($this->migration->getTrackLastImported()) {
       $fields['last_imported'] = time();
     }
-    $keys = [static::SOURCE_IDS_HASH => $this->getSourceIDsHash($source_id_values)];
+    $keys = [static::SOURCE_IDS_HASH => $this->getSourceIdsHash($source_id_values)];
     // Notify anyone listening of the map row we're about to save.
     $this->eventDispatcher->dispatch(MigrateEvents::MAP_SAVE, new MigrateMapSaveEvent($this, $fields));
     $this->getDatabase()->merge($this->mapTableName())
@@ -655,7 +660,7 @@ class Sql extends PluginBase implements MigrateIdMapInterface, ContainerFactoryP
         return;
       }
     }
-    $fields[static::SOURCE_IDS_HASH] = $this->getSourceIDsHash($source_id_values);
+    $fields[static::SOURCE_IDS_HASH] = $this->getSourceIdsHash($source_id_values);
     $fields['level'] = $level;
     $fields['message'] = $message;
     $this->getDatabase()->insert($this->messageTableName())
@@ -674,7 +679,7 @@ class Sql extends PluginBase implements MigrateIdMapInterface, ContainerFactoryP
     $query = $this->getDatabase()->select($this->messageTableName(), 'msg')
       ->fields('msg');
     if ($source_id_values) {
-      $query->condition(static::SOURCE_IDS_HASH, $this->getSourceIDsHash($source_id_values));
+      $query->condition(static::SOURCE_IDS_HASH, $this->getSourceIdsHash($source_id_values));
     }
 
     if ($level) {
@@ -696,21 +701,17 @@ class Sql extends PluginBase implements MigrateIdMapInterface, ContainerFactoryP
    * {@inheritdoc}
    */
   public function processedCount() {
-    return (int) $this->getDatabase()->select($this->mapTableName())
-      ->countQuery()
-      ->execute()
-      ->fetchField();
+    return $this->countHelper(NULL, $this->mapTableName());
   }
 
   /**
    * {@inheritdoc}
    */
   public function importedCount() {
-    return (int) $this->getDatabase()->select($this->mapTableName())
-      ->condition('source_row_status', [MigrateIdMapInterface::STATUS_IMPORTED, MigrateIdMapInterface::STATUS_NEEDS_UPDATE], 'IN')
-      ->countQuery()
-      ->execute()
-      ->fetchField();
+    return $this->countHelper([
+      MigrateIdMapInterface::STATUS_IMPORTED,
+      MigrateIdMapInterface::STATUS_NEEDS_UPDATE,
+    ]);
   }
 
   /**
@@ -737,20 +738,28 @@ class Sql extends PluginBase implements MigrateIdMapInterface, ContainerFactoryP
   /**
    * Counts records in a table.
    *
-   * @param int $status
-   *   An integer for the source_row_status column.
+   * @param int|array $status
+   *   (optional) Status code(s) to filter the source_row_status column.
    * @param string $table
    *   (optional) The table to work. Defaults to NULL.
    *
    * @return int
    *   The number of records.
    */
-  protected function countHelper($status, $table = NULL) {
-    $query = $this->getDatabase()->select($table ?: $this->mapTableName());
+  protected function countHelper($status = NULL, $table = NULL) {
+    // Use database directly to avoid creating tables.
+    $query = $this->database->select($table ?: $this->mapTableName());
     if (isset($status)) {
-      $query->condition('source_row_status', $status);
+      $query->condition('source_row_status', $status, is_array($status) ? 'IN' : '=');
+    }
+    try {
+      $count = (int) $query->countQuery()->execute()->fetchField();
+    }
+    catch (DatabaseException $e) {
+      // The table does not exist, therefore there are no records.
+      $count = 0;
     }
-    return (int) $query->countQuery()->execute()->fetchField();
+    return $count;
   }
 
   /**
@@ -763,13 +772,13 @@ class Sql extends PluginBase implements MigrateIdMapInterface, ContainerFactoryP
 
     if (!$messages_only) {
       $map_query = $this->getDatabase()->delete($this->mapTableName());
-      $map_query->condition(static::SOURCE_IDS_HASH, $this->getSourceIDsHash($source_id_values));
+      $map_query->condition(static::SOURCE_IDS_HASH, $this->getSourceIdsHash($source_id_values));
       // Notify anyone listening of the map row we're about to delete.
       $this->eventDispatcher->dispatch(MigrateEvents::MAP_DELETE, new MigrateMapDeleteEvent($this, $source_id_values));
       $map_query->execute();
     }
     $message_query = $this->getDatabase()->delete($this->messageTableName());
-    $message_query->condition(static::SOURCE_IDS_HASH, $this->getSourceIDsHash($source_id_values));
+    $message_query->condition(static::SOURCE_IDS_HASH, $this->getSourceIdsHash($source_id_values));
     $message_query->execute();
   }
 
@@ -779,7 +788,7 @@ class Sql extends PluginBase implements MigrateIdMapInterface, ContainerFactoryP
   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);
+    $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]);
@@ -788,7 +797,7 @@ class Sql extends PluginBase implements MigrateIdMapInterface, ContainerFactoryP
       $this->eventDispatcher->dispatch(MigrateEvents::MAP_DELETE, new MigrateMapDeleteEvent($this, $source_id_values));
       $map_query->execute();
 
-      $message_query->condition(static::SOURCE_IDS_HASH, $this->getSourceIDsHash($source_id_values));
+      $message_query->condition(static::SOURCE_IDS_HASH, $this->getSourceIdsHash($source_id_values));
       $message_query->execute();
     }
   }
@@ -976,8 +985,9 @@ class Sql extends PluginBase implements MigrateIdMapInterface, ContainerFactoryP
     // Get the highest id from the list of map tables.
     $ids = [0];
     foreach ($map_tables as $map_table) {
+      // If the map_table does not exist then continue on to the next map_table.
       if (!$this->getDatabase()->schema()->tableExists($map_table)) {
-        break;
+        continue;
       }
 
       $query = $this->getDatabase()->select($map_table, 'map')