You are here

public function MigrateSource::next in Migrate 6.2

Same name and namespace in other branches
  1. 7.2 includes/source.inc \MigrateSource::next()

Implementation of Iterator::next() - subclasses of MigrateSource should implement getNextRow() to retrieve the next valid source rocord to process.

1 call to MigrateSource::next()
MigrateSource::rewind in includes/source.inc
Implementation of Iterator::rewind() - subclasses of MigrateSource should implement performRewind() to do any class-specific setup for iterating source records.

File

includes/source.inc, line 248
Define base for migration sources.

Class

MigrateSource
Abstract base class for source handling.

Code

public function next() {
  $this->currentKey = NULL;
  $this->currentRow = NULL;
  migrate_instrument_start(get_class($this) . ' getNextRow');
  while ($row = $this
    ->getNextRow()) {
    migrate_instrument_stop(get_class($this) . ' getNextRow');

    // Populate the source key for this row
    $this->currentKey = $this->activeMigration
      ->prepareKey($this->activeMap
      ->getSourceKey(), $row);

    // Pick up the existing map row, if any, unless getNextRow() did it.
    if (!$this->mapRowAdded) {
      $map_row = $this->activeMap
        ->getRowBySource($this->currentKey);

      // Add map info to the row, if present
      if ($map_row) {
        foreach ($map_row as $field => $value) {
          $field = 'migrate_map_' . $field;
          $row->{$field} = $value;
        }
      }
    }

    // First, determine if this row should be passed to prepareRow(), or skipped
    // entirely. The rules are:
    // 1. If there's an explicit idlist, that's all we care about (ignore
    //    highwaters and map rows).
    $prepared = FALSE;
    if (!empty($this->idList)) {
      if (in_array(reset($this->currentKey), $this->idList)) {

        // In the list, fall through.
      }
      else {

        // Not in the list, skip it
        $this->currentRow = NULL;
        continue;
      }
    }
    elseif (!isset($row->migrate_map_sourceid1)) {

      // Fall through
    }
    elseif ($row->migrate_map_needs_update == MigrateMap::STATUS_NEEDS_UPDATE) {

      // Fall through
    }
    elseif (empty($this->highwaterField)) {

      // No highwater, skip
      $this->currentRow = NULL;
      continue;
    }
    elseif ($this->activeMigration
      ->getHighwater() === '') {

      // Fall through
    }
    else {

      // Call prepareRow() here, in case the highwaterField needs preparation
      if ($this
        ->prepareRow($row) !== FALSE) {
        if ($row->{$this->highwaterField['name']} > $this->activeMigration
          ->getHighwater()) {
          $this->currentRow = $row;
          break;
        }
        else {

          // Skip
          $this->currentRow = NULL;
          continue;
        }
      }
      else {
        $this->currentRow = NULL;
      }
      $prepared = TRUE;
    }

    // Allow the Migration to prepare this row. prepareRow() can return boolean
    // FALSE to ignore this row.
    if (!$prepared) {
      if ($this
        ->prepareRow($row) !== FALSE) {

        // Finally, we've got a keeper.
        $this->currentRow = $row;
        break;
      }
      else {
        $this->currentRow = NULL;
      }
    }
  }
  migrate_instrument_stop(get_class($this) . ' getNextRow');
  if (!$this->currentRow) {
    $this->currentKey = NULL;
  }
}