You are here

public function SearchApiCombinedEntityDataSourceController::startTracking in Search API 7

Initializes tracking of the index status of items for the given indexes.

All currently known items of this data source's type should be inserted into the tracking table for the given indexes, with status "changed". If items were already present, these should also be set to "changed" and not be inserted again.

Parameters

SearchApiIndex[] $indexes: The SearchApiIndex objects for which item tracking should be initialized.

Throws

SearchApiDataSourceException If any error state was encountered.

Overrides SearchApiAbstractDataSourceController::startTracking

File

includes/datasource_multiple.inc, line 152
Contains SearchApiCombinedEntityDataSourceController.

Class

SearchApiCombinedEntityDataSourceController
Provides a datasource for indexing multiple types of entities.

Code

public function startTracking(array $indexes) {
  if (!$this->table) {
    return;
  }

  // We first clear the tracking table for all indexes, so we can just insert
  // all items again without any key conflicts.
  $this
    ->stopTracking($indexes);
  foreach ($indexes as $index) {
    $types = $this
      ->getEntityTypes($index);

    // Wherever possible, use a sub-select instead of the much slower
    // entity_load().
    foreach ($types as $type) {
      $entity_info = entity_get_info($type);
      if (!empty($entity_info['base table'])) {

        // Assumes that all entities use the "base table" property and the
        // "entity keys[id]" in the same way as the default controller.
        $id_field = $entity_info['entity keys']['id'];
        $table = $entity_info['base table'];

        // Select all entity ids.
        $query = db_select($table, 't');
        $query
          ->addExpression("CONCAT(:prefix, t.{$id_field})", 'item_id', array(
          ':prefix' => $type . '/',
        ));
        $query
          ->addExpression(':index_id', 'index_id', array(
          ':index_id' => $index->id,
        ));
        $query
          ->addExpression('1', 'changed');

        // INSERT ... SELECT ...
        db_insert($this->table)
          ->from($query)
          ->execute();
        unset($types[$type]);
      }
    }

    // In the absence of a "base table", use the slow entity_load().
    if ($types) {
      foreach ($types as $type) {
        $query = new EntityFieldQuery();
        $query
          ->entityCondition('entity_type', $type);
        $result = $query
          ->execute();
        $ids = !empty($result[$type]) ? array_keys($result[$type]) : array();
        if ($ids) {
          foreach ($ids as $i => $id) {
            $ids[$i] = $type . '/' . $id;
          }
          $this
            ->trackItemInsert($ids, array(
            $index,
          ), TRUE);
        }
      }
    }
  }
}