You are here

ws_search_api.module in Web Service Data 7

Defines core functionality for web service integration with Search API

File

modules/ws_search_api/ws_search_api.module
View source
<?php

/**
 * @file
 * Defines core functionality for web service integration with Search API
 */

/**
 * Implements hook_cron().
 */
function ws_search_api_cron() {

  // Selects the expired entities and whom haven't been recently indexed
  $result = db_select('ws_search_api_item', 'w')
    ->fields('w', array(
    'entity_type',
    'entity_id',
  ))
    ->condition('expire', time(), '<')
    ->execute();
  $reindex = array();
  while ($record = $result
    ->fetchAssoc()) {
    $reindex[$record['entity_type']][$record['entity_id']] = $record['entity_id'];
  }

  // Marks them for reindexing
  foreach ($reindex as $type => $ids) {
    db_delete('ws_search_api_item')
      ->condition('entity_type', $type)
      ->condition('entity_id', $ids, 'IN')
      ->execute();
    search_api_track_item_change($type, $ids);
  }
}

/**
 * Implements hook_wsfields_entity_expires().
 */
function ws_search_api_wsfields_entity_expires($entity_type, $entity_id, $expire) {

  // Tell search api we've received new information
  search_api_track_item_change($entity_type, array(
    $entity_id,
  ));
  $record = array(
    'entity_id' => $entity_id,
    'entity_type' => $entity_type,
    'expire' => $expire,
  );
  ws_search_api_update_record($record);
}

/**
 * Implements hook_search_api_index_items_alter().
 */
function ws_search_api_search_api_index_items_alter(array &$items, SearchApiIndex $index) {
  if (!wsfields_storage_entity_has_wsfields($index->item_type)) {
    return;
  }
  $ids = array_keys($items);
  foreach ($ids as $id) {
    ws_search_api_update_record(array(
      'entity_id' => $id,
      'entity_type' => $index->item_type,
      'indexed' => time(),
    ));
  }
}
function ws_search_api_update_record($record) {

  // Log the expires time
  $exists = db_select('ws_search_api_item')
    ->condition('entity_id', $record['entity_id'])
    ->condition('entity_type', $record['entity_type'])
    ->countQuery()
    ->execute()
    ->fetchField();
  if ($exists > 0) {
    return drupal_write_record('ws_search_api_item', $record, array(
      'entity_id',
      'entity_type',
    ));
  }
  else {
    return drupal_write_record('ws_search_api_item', $record);
  }
}