You are here

search_api_et.install in Search API Entity Translation 7.2

Install, update and uninstall functions for the Search API ET module.

File

search_api_et.install
View source
<?php

/**
 * @file
 * Install, update and uninstall functions for the Search API ET module.
 */

/**
 * Implements hook_schema().
 */
function search_api_et_schema() {
  $schema['search_api_et_item'] = array(
    'description' => 'Stores the items which should be indexed for each index, and their status.',
    'fields' => array(
      'item_id' => array(
        'description' => "The item's ID, with language prefix.",
        'type' => 'varchar',
        'length' => 53,
        'not null' => TRUE,
      ),
      'index_id' => array(
        'description' => 'The {search_api_index}.id this item belongs to.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'changed' => array(
        'description' => 'Either a flag or a timestamp to indicate if or when the item was changed since it was last indexed.',
        'type' => 'int',
        'size' => 'big',
        'not null' => TRUE,
        'default' => 1,
      ),
    ),
    'indexes' => array(
      'indexing' => array(
        'index_id',
        'changed',
      ),
    ),
    'primary key' => array(
      'item_id',
      'index_id',
    ),
  );
  return $schema;
}

/**
 * Implements hook_install().
 *
 * Creates a default multilingual node index if the module is installed manually.
 */
function search_api_et_install() {

  // In case the module is installed via an installation profile, a batch is
  // active and we skip that.
  if (batch_get()) {
    return;
  }
  $name = 'Default multilingual node index';
  $values = array(
    'name' => st($name),
    'machine_name' => preg_replace('/[^a-z0-9]+/', '_', drupal_strtolower($name)),
    'description' => st('An automatically created search index for indexing multilingual node data. Might be configured to specific needs.'),
    'server' => NULL,
    'item_type' => 'search_api_et_node',
    'options' => array(
      'index_directly' => 1,
      'cron_limit' => '50',
      'data_alter_callbacks' => array(
        'search_api_alter_node_access' => array(
          'status' => 1,
          'weight' => '0',
          'settings' => array(),
        ),
      ),
      'processors' => array(
        'search_api_case_ignore' => array(
          'status' => 1,
          'weight' => '0',
          'settings' => array(
            'strings' => 0,
          ),
        ),
        'search_api_html_filter' => array(
          'status' => 1,
          'weight' => '10',
          'settings' => array(
            'title' => 0,
            'alt' => 1,
            'tags' => "h1 = 5\n" . "h2 = 3\n" . "h3 = 2\n" . "strong = 2\n" . "b = 2\n" . "em = 1.5\n" . "u = 1.5",
          ),
        ),
        'search_api_tokenizer' => array(
          'status' => 1,
          'weight' => '20',
          'settings' => array(
            'spaces' => '[^\\p{L}\\p{N}]',
            'ignorable' => '[-]',
          ),
        ),
      ),
      'fields' => array(
        'type' => array(
          'type' => 'string',
        ),
        'title' => array(
          'type' => 'text',
          'boost' => '5.0',
        ),
        'promote' => array(
          'type' => 'boolean',
        ),
        'sticky' => array(
          'type' => 'boolean',
        ),
        'created' => array(
          'type' => 'date',
        ),
        'changed' => array(
          'type' => 'date',
        ),
        'author' => array(
          'type' => 'integer',
          'entity_type' => 'user',
        ),
        'comment_count' => array(
          'type' => 'integer',
        ),
        'search_api_language' => array(
          'type' => 'string',
        ),
        'body:value' => array(
          'type' => 'text',
        ),
      ),
    ),
  );
  search_api_index_insert($values);
  drupal_set_message(st('The Search API Entity Translation module was installed. A new default multilingual node index was created.'));
}

/**
 * Clean {search_api_et_item} table from outdated items.
 */
function search_api_et_update_7200(&$sandbox) {
  $indexes = db_select('search_api_index', 'sai')
    ->fields('sai', array(
    'id',
  ))
    ->distinct('distinct index.id')
    ->execute()
    ->fetchAllAssoc('id');
  $index_ids = array_keys($indexes);
  $res = db_delete('search_api_et_item')
    ->condition('index_id', $index_ids, 'NOT IN')
    ->execute();
}

/**
 * Migrate the old ItemID format to the newer one.
 */
function search_api_et_update_7201(&$sandbox) {
  if (!isset($sandbox['progress'])) {
    $sandbox['progress'] = 0;
    $sandbox['max'] = db_query('SELECT COUNT(*) FROM {search_api_et_item}')
      ->fetchField();
  }
  $items = db_select('search_api_et_item', 'item')
    ->fields('item', array(
    'item_id',
    'index_id',
  ))
    ->range($sandbox['progress'], 500)
    ->orderBy('item_id', 'ASC')
    ->execute();
  foreach ($items as $item) {

    // Get the old item_id format: "{LANG}_{ENTITY-ID}".
    $parts = explode('_', $item->item_id);
    if (is_array($parts) && count($parts) == 2) {

      // Build the new item_id format: "{ENTITY-ID}/{LANG}".
      $new_item_id = $parts[1] . '/' . $parts[0];
      db_update('search_api_et_item')
        ->fields(array(
        'item_id' => $new_item_id,
      ))
        ->condition('item_id', $item->item_id)
        ->execute();
    }
    $sandbox['progress']++;
  }

  // Inform on the progression.
  $sandbox['#finished'] = empty($sandbox['max']) ? 1 : $sandbox['progress'] / $sandbox['max'];
  if ($sandbox['#finished'] != 1) {
    return t('SearchAPI ET: Updated !items of !total total.', array(
      '!items' => $sandbox['progress'],
      '!total' => $sandbox['max'],
    ));
  }
  else {
    return t('SearchAPI ET: Migrate the old ItemID format to the newer one completed. You will need to run a re-index on search_api_et indexes.');
  }
}

Functions

Namesort descending Description
search_api_et_install Implements hook_install().
search_api_et_schema Implements hook_schema().
search_api_et_update_7200 Clean {search_api_et_item} table from outdated items.
search_api_et_update_7201 Migrate the old ItemID format to the newer one.