You are here

function apachesolr_update_7012 in Apache Solr Search 8

Same name and namespace in other branches
  1. 7 apachesolr.install \apachesolr_update_7012()

Rename some variables and update the database tables

File

./apachesolr.install, line 650
Install and related hooks for apachesolr_search.

Code

function apachesolr_update_7012() {
  if (variable_get('apachesolr_update_from_6303', FALSE)) {
    return NULL;
  }

  // @see: drupal_load()
  if (!function_exists('apachesolr_default_environment')) {
    include_once dirname(__FILE__) . '/apachesolr.module';
  }
  $env_id = apachesolr_default_environment();

  // Variable changed from integer to array with environment integers
  $stored = variable_get('apachesolr_index_last', array());
  if (isset($stored['apachesolr'])) {
    $stored[$env_id]['node']['last_entity_id'] = $stored['apachesolr']['last_nid'];
    $stored[$env_id]['node']['last_changed'] = $stored['apachesolr']['last_change'];
    unset($stored['apachesolr']);
    variable_set('apachesolr_index_last', $stored);
  }
  $last = variable_get('apachesolr_index_updated', NULL);
  if (isset($last)) {
    variable_set('apachesolr_index_updated', array(
      $env_id => (int) $last,
    ));
  }

  // Change namespace to environment id
  $excluded_types = apachesolr_environment_variable_get('apachesolr', 'apachesolr_search_excluded_types', array());
  if (!empty($excluded_types)) {
    apachesolr_environment_variable_set($env_id, 'apachesolr_search_excluded_types', $excluded_types);
    apachesolr_environment_variable_del('apachesolr', 'apachesolr_search_excluded_types');
  }

  // Install the new schema

  //Predefine an amount of types that get their own table
  $types = array(
    'other' => 'apachesolr_index_entities',
    'node' => 'apachesolr_index_entities_node',
  );
  foreach ($types as $type => $table) {
    $schema[$table] = array(
      'description' => 'Stores a record of when an entity changed to determine if it needs indexing by Solr.',
      'fields' => array(
        'entity_type' => array(
          'description' => 'The type of entity.',
          'type' => 'varchar',
          'length' => 32,
          'not null' => TRUE,
        ),
        'entity_id' => array(
          'description' => 'The primary identifier for an entity.',
          'type' => 'int',
          'unsigned' => TRUE,
          'not null' => TRUE,
        ),
        'bundle' => array(
          'description' => 'The bundle to which this entity belongs.',
          'type' => 'varchar',
          'length' => 128,
          'not null' => TRUE,
        ),
        'status' => array(
          'description' => 'Boolean indicating whether the entity is visible to non-administrators (eg, published for nodes).',
          'type' => 'int',
          'not null' => TRUE,
          'default' => 1,
        ),
        'changed' => array(
          'description' => 'The Unix timestamp when an entity was changed.',
          'type' => 'int',
          'not null' => TRUE,
          'default' => 0,
        ),
      ),
      'indexes' => array(
        'changed' => array(
          'bundle',
          'status',
          'changed',
        ),
      ),
      'primary key' => array(
        'entity_id',
      ),
    );
    if ($type == 'other') {

      // Need the entity type also in the pkey for multiple entities in one table.
      $schema[$table]['primary key'][] = 'entity_type';
    }

    // Create the table
    db_create_table($table, $schema[$table]);
  }
  $schema['apachesolr_index_bundles'] = array(
    'description' => 'Records what bundles we should be indexing for a given environment.',
    'fields' => array(
      'env_id' => array(
        'description' => 'The name of the environment.',
        'type' => 'varchar',
        'length' => 64,
        'not null' => TRUE,
      ),
      'entity_type' => array(
        'description' => 'The type of entity.',
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
      ),
      'bundle' => array(
        'description' => 'The bundle to index.',
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
      ),
    ),
    'primary key' => array(
      'env_id',
      'entity_type',
      'bundle',
    ),
  );
  db_create_table('apachesolr_index_bundles', $schema['apachesolr_index_bundles']);

  // Move the data from apachesolr_search_node to apachesolr_index_entities_node
  $select = db_select('apachesolr_search_node', 'asn');
  $select
    ->join('node', 'n', 'asn.nid = n.nid');
  $select
    ->addField('n', 'nid', 'entity_id');
  $select
    ->addField('n', 'type', 'bundle');
  $select
    ->addField('asn', 'status', 'status');
  $select
    ->addField('asn', 'changed', 'changed');
  $select
    ->addExpression("'node'", 'entity_type');
  $return_value = db_insert('apachesolr_index_entities_node')
    ->fields(array(
    'entity_id',
    'bundle',
    'status',
    'changed',
    'entity_type',
  ))
    ->from($select)
    ->execute();

  // Drop the table apachesolr_search_node
  db_drop_table('apachesolr_search_node');
  $environments = apachesolr_load_all_environments();
  foreach ($environments as $env_id => $environment) {
    $excluded_types = apachesolr_environment_variable_get($env_id, 'apachesolr_search_excluded_types', array());

    // Get indexable entity types
    $options = array();
    foreach (entity_get_info() as $entity_type => $entity_info) {
      if ($entity_type == 'node') {
        foreach ($entity_info['bundles'] as $key => $info) {

          // See if it was excluded & only of entity node. We will not enable
          // other entity types by default
          if (empty($excluded_types[$key])) {
            $options[$entity_type][$key] = $key;
          }
        }
      }
    }

    // Set all except the excluded types
    // @see apachesolr_index_set_bundles()
    foreach ($options as $entity_type => $bundles) {
      db_delete('apachesolr_index_bundles')
        ->condition('env_id', $env_id)
        ->condition('entity_type', $entity_type)
        ->execute();
      if ($bundles) {
        $insert = db_insert('apachesolr_index_bundles')
          ->fields(array(
          'env_id',
          'entity_type',
          'bundle',
        ));
        foreach ($bundles as $bundle) {
          $insert
            ->values(array(
            'env_id' => $env_id,
            'entity_type' => $entity_type,
            'bundle' => $bundle,
          ));
        }
        $insert
          ->execute();
      }
    }

    // Remove the excluded types
    apachesolr_environment_variable_del($env_id, 'apachesolr_search_excluded_types');
  }
}