You are here

class MigrateSourceMongoDB in Migrate 7.2

Implementation of MigrateSource, to handle imports from MongoDB connections.

Hierarchy

Expanded class hierarchy of MigrateSourceMongoDB

File

plugins/sources/mongodb.inc, line 11
Define a MigrateSource for importing from MongoDB connections

View source
class MigrateSourceMongoDB extends MigrateSource {

  /**
   * The mongodb collection object.
   *
   * @var MongoCollection
   */
  protected $collection;

  /**
   * The mongodb cursor object.
   *
   * @var MongoCursor
   */
  protected $cursor;

  /**
   * The mongodb query.
   *
   * @var array
   */
  protected $query;

  /**
   * List of available source fields.
   *
   * @var array
   */
  protected $fields = array();

  /**
   * Simple initialization.
   */
  public function __construct(MongoCollection $collection, array $query, array $fields = array(), array $sort = array(
    '_id' => 1,
  ), array $options = array()) {
    parent::__construct($options);
    $this->collection = $collection;
    $this->query = $query;
    $this->sort = $sort;
    $this->fields = $fields;
  }

  /**
   * Returns a list of fields available to be mapped from the source query.
   *
   * @return array
   *  Keys: machine names of the fields (to be passed to addFieldMapping)
   *  Values: Human-friendly descriptions of the fields.
   */
  public function fields() {

    // The fields are passed to the constructor for this plugin.
    return $this->fields;
  }

  /**
   * Return a count of all available source records.
   */
  public function computeCount() {
    return $this->cursor
      ->count(TRUE);
  }

  /**
   * Implementation of MigrateSource::getNextRow().
   *
   * @return object
   */
  public function getNextRow() {
    $row = $this->cursor
      ->getNext();
    if ($row) {
      return (object) $row;
    }
    return NULL;
  }

  /**
   * Implementation of MigrateSource::performRewind().
   *
   * @return void
   */
  public function performRewind() {
    $keys = $this
      ->getSourceKeyNameAndType();

    // If we have an existing idlist we use it.
    if ($this->idList) {
      foreach ($this->idList as $key => $id) {

        // Try make new ObjectID.
        $this->idList[$key] = $this
          ->getMongoId($id, $keys);
      }
      $this->query[$keys[0]['name']]['$in'] = $this->idList;
    }
    migrate_instrument_start('MigrateSourceMongoDB execute');
    try {
      $this->cursor = $this->collection
        ->find($this->query)
        ->sort($this->sort);
      $this->cursor
        ->timeout(-1);
    } catch (MongoCursorException $e) {
      Migration::displayMessage($e
        ->getMessage(), 'error');
    }
    migrate_instrument_stop('MigrateSourceMongoDB execute');
  }

  /**
   * Return a string representing the source query.
   *
   * @return string
   */
  public function __toString() {
    if (is_null($this->cursor)) {
      $this->cursor = $this->collection
        ->find($this->query)
        ->sort($this->sort);
      $this->cursor
        ->timeout(-1);
    }
    $query_info = $this->cursor
      ->info();
    $query = 'query:    ' . drupal_json_encode($query_info['query']['$query']);
    $sort = 'order by: ' . drupal_json_encode($query_info['query']['$orderby']);
    $fields = 'fields:   ' . drupal_json_encode($query_info['fields']);
    return $query . PHP_EOL . $sort . PHP_EOL . $fields . PHP_EOL;
  }

  /**
   * Check if given document id is a mongo ObjectId and return mongo ObjectId
   * or simple value.
   *
   * @param mixed $document_id
   *   Document key value.
   * @param array $keys
   *   List of keys.
   *
   * @return type
   */
  public function getMongoId($document_id, $keys) {
    if ($keys[0]['name'] != '_id') {
      switch ($keys[0]['type']) {
        case 'int':
          return (int) $document_id;
          break;
        default:
          return $document_id;
      }
    }

    // Trying create Mongo ObjectId
    $mongoid = new MongoId($document_id);

    // If (string) $mongoid == $document_id we return $mongoid object
    if ((string) $mongoid == $document_id) {
      return $mongoid;
    }
    return $document_id;
  }

  /**
   * Get source keys array.
   */
  public function getSourceKeyNameAndType() {

    // Get the key name, and type.
    $keys = array();
    foreach ($this->activeMap
      ->getSourceKey() as $field_name => $field_schema) {
      $keys[] = array(
        'name' => $field_name,
        'type' => $field_schema['type'],
      );
    }
    return $keys;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MigrateSource::$activeMap protected property The MigrateMap class for the current migration.
MigrateSource::$activeMigration protected property The Migration class currently invoking us, during rewind() and next().
MigrateSource::$cacheCounts protected property Whether this instance should cache the source count.
MigrateSource::$cacheKey protected property Key to use for caching counts.
MigrateSource::$currentKey protected property The primary key of the current row
MigrateSource::$currentRow protected property The current row from the quey
MigrateSource::$highwaterField protected property Information on the highwater mark for the current migration, if any.
MigrateSource::$idList protected property List of source IDs to process.
MigrateSource::$mapRowAdded protected property By default, next() will directly read the map row and add it to the data row. A source plugin implementation may do this itself (in particular, the SQL source can incorporate the map table into the query) - if so, it should set this TRUE so we…
MigrateSource::$multikeySeparator protected property Used in the case of multiple key sources that need to use idlist.
MigrateSource::$numIgnored protected property Number of rows intentionally ignored (prepareRow() returned FALSE)
MigrateSource::$numProcessed protected property Number of rows we've at least looked at. 1
MigrateSource::$originalHighwater protected property The highwater mark at the beginning of the import operation.
MigrateSource::$skipCount protected property Whether this instance should not attempt to count the source.
MigrateSource::$trackChanges protected property If TRUE, we will maintain hashed source rows to determine whether incoming data has changed.
MigrateSource::count public function Return a count of available source records, from the cache if appropriate. Returns -1 if the source is not countable.
MigrateSource::current public function Implementation of Iterator::current() - called when entering a loop iteration, returning the current row
MigrateSource::dataChanged protected function Determine whether this row has changed, and therefore whether it should be processed.
MigrateSource::getCurrentKey public function
MigrateSource::getIgnored public function
MigrateSource::getProcessed public function
MigrateSource::hash protected function Generate a hash of the source row. 3
MigrateSource::key public function Implementation of Iterator::key - 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.
MigrateSource::next public function Implementation of Iterator::next() - subclasses of MigrateSource should implement getNextRow() to retrieve the next valid source rocord to process.
MigrateSource::prepareRow protected function Give the calling migration a shot at manipulating, and possibly rejecting, the source row.
MigrateSource::resetStats public function Reset numIgnored back to 0.
MigrateSource::rewind public function Implementation of Iterator::rewind() - subclasses of MigrateSource should implement performRewind() to do any class-specific setup for iterating source records.
MigrateSource::valid public function Implementation of Iterator::valid() - called at the top of the loop, returning TRUE to process the loop and FALSE to terminate it
MigrateSourceMongoDB::$collection protected property The mongodb collection object.
MigrateSourceMongoDB::$cursor protected property The mongodb cursor object.
MigrateSourceMongoDB::$fields protected property List of available source fields.
MigrateSourceMongoDB::$query protected property The mongodb query.
MigrateSourceMongoDB::computeCount public function Return a count of all available source records.
MigrateSourceMongoDB::fields public function Returns a list of fields available to be mapped from the source query. Overrides MigrateSource::fields
MigrateSourceMongoDB::getMongoId public function Check if given document id is a mongo ObjectId and return mongo ObjectId or simple value.
MigrateSourceMongoDB::getNextRow public function Implementation of MigrateSource::getNextRow().
MigrateSourceMongoDB::getSourceKeyNameAndType public function Get source keys array.
MigrateSourceMongoDB::performRewind public function Implementation of MigrateSource::performRewind().
MigrateSourceMongoDB::__construct public function Simple initialization. Overrides MigrateSource::__construct
MigrateSourceMongoDB::__toString public function Return a string representing the source query.