Exemplo n.º 1
0
 /**
  * {@inheritdoc}
  */
 public function saveIdMapping(Row $row, array $destination_id_values, $source_row_status = MigrateIdMapInterface::STATUS_IMPORTED, $rollback_action = MigrateIdMapInterface::ROLLBACK_DELETE)
 {
     // Construct the source key.
     $source_id_values = $row->getSourceIdValues();
     // Construct the source key and initialize to empty variable keys.
     $keys = array();
     foreach ($this->sourceIdFields() as $field_name => $key_name) {
         // A NULL key value will fail.
         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) {
         // Notify anyone listening of the map row we're about to save.
         $this->eventDispatcher->dispatch(MigrateEvents::MAP_SAVE, new MigrateMapSaveEvent($this, $keys + $fields));
         $this->getDatabase()->merge($this->mapTableName())->key($keys)->fields($fields)->execute();
     }
 }
Exemplo n.º 2
0
 /**
  * Tests the import method with a regular Exception being thrown.
  */
 public function testImportWithValidRowWithException()
 {
     $exception_message = $this->getRandomGenerator()->string();
     $source = $this->getMockSource();
     $row = $this->getMockBuilder('Drupal\\migrate\\Row')->disableOriginalConstructor()->getMock();
     $row->expects($this->once())->method('getSourceIdValues')->will($this->returnValue(array('id' => 'test')));
     $source->expects($this->once())->method('current')->will($this->returnValue($row));
     $this->executable->setSource($source);
     $this->migration->expects($this->once())->method('getProcessPlugins')->will($this->returnValue(array()));
     $destination = $this->getMock('Drupal\\migrate\\Plugin\\MigrateDestinationInterface');
     $destination->expects($this->once())->method('import')->with($row, array('test'))->will($this->throwException(new \Exception($exception_message)));
     $this->migration->expects($this->once())->method('getDestinationPlugin')->will($this->returnValue($destination));
     $this->idMap->expects($this->once())->method('saveIdMapping')->with($row, array(), MigrateIdMapInterface::STATUS_FAILED, NULL);
     $this->idMap->expects($this->once())->method('saveMessage');
     $this->idMap->expects($this->once())->method('lookupDestinationId')->with(array('id' => 'test'))->will($this->returnValue(array('test')));
     $this->message->expects($this->once())->method('display')->with($exception_message);
     $this->assertSame(MigrationInterface::RESULT_COMPLETED, $this->executable->import());
 }
Exemplo n.º 3
0
 /**
  * Tests whether we've exceeded the desired memory threshold.
  *
  * If so, output a message.
  *
  * @return bool
  *   TRUE if the threshold is exceeded, otherwise FALSE.
  */
 protected function memoryExceeded()
 {
     $usage = $this->getMemoryUsage();
     $pct_memory = $usage / $this->memoryLimit;
     if (!($threshold = $this->memoryThreshold)) {
         return FALSE;
     }
     if ($pct_memory > $threshold) {
         $this->message->display($this->t('Memory usage is @usage (@pct% of limit @limit), reclaiming memory.', array('@pct' => round($pct_memory * 100), '@usage' => $this->formatSize($usage), '@limit' => $this->formatSize($this->memoryLimit))), 'warning');
         $usage = $this->attemptMemoryReclaim();
         $pct_memory = $usage / $this->memoryLimit;
         // Use a lower threshold - we don't want to be in a situation where we keep
         // coming back here and trimming a tiny amount
         if ($pct_memory > 0.9 * $threshold) {
             $this->message->display($this->t('Memory usage is now @usage (@pct% of limit @limit), not enough reclaimed, starting new batch', array('@pct' => round($pct_memory * 100), '@usage' => $this->formatSize($usage), '@limit' => $this->formatSize($this->memoryLimit))), 'warning');
             return TRUE;
         } else {
             $this->message->display($this->t('Memory usage is now @usage (@pct% of limit @limit), reclaimed enough, continuing', array('@pct' => round($pct_memory * 100), '@usage' => $this->formatSize($usage), '@limit' => $this->formatSize($this->memoryLimit))), 'warning');
             return FALSE;
         }
     } else {
         return FALSE;
     }
 }
Exemplo n.º 4
0
 /**
  * Logs a message using the Migrate message service.
  *
  * @param string $message
  *   The message to log.
  * @param string $type
  *   The type of message, for example: status or warning.
  */
 public function logMessage($message, $type = 'status')
 {
     $this->message->display($message, $type);
 }