You are here

public function SearchApiDenormalizedEntityDataSourceController::cleanTable in Search API Grouping 7.2

Ensures the consistency of the table.

It can happen that an entity deletion happens unrecognized which then creates inconsistencies in the tracking table. In the worst case this can lead to a WSOD e.g. if a non existing entity is loaded.

File

includes/datasource_denormalized_entity.inc, line 115
Contains the SearchApiDenormalizedEntityDataSourceController class.

Class

SearchApiDenormalizedEntityDataSourceController
Data source for all entities known to the Entity API.

Code

public function cleanTable() {
  $count = 0;

  // Fetch all available entity types in the table.
  $entity_types = db_select($this->table)
    ->fields($this->table, array(
    'entity_type',
  ))
    ->groupBy('entity_type')
    ->execute()
    ->fetchCol();

  // Now iterate over all entity types and check table for orphaned ids.
  if (!empty($entity_types)) {
    foreach ($entity_types as $entity_type) {
      $entity_info = entity_get_info($entity_type);
      $entity_type_primary_key = $entity_info['entity keys']['id'];
      $query = db_select($this->table)
        ->fields($this->table, array(
        $this->itemIdColumn,
      ))
        ->condition('entity_type', $entity_type);
      $query
        ->addJoin('LEFT', $entity_info['base table'], NULL, $entity_type_primary_key . ' = ' . $this->table . '.etid');
      $query
        ->isNull($entity_type_primary_key);
      $orphaned_ids = $query
        ->execute()
        ->fetchCol();

      // Get rid of orphaned ids.
      if (!empty($orphaned_ids)) {
        $count += count($orphaned_ids);
        search_api_track_item_delete($this->type, $orphaned_ids);
      }
    }
  }
  return $count;
}