You are here

apachesolr.install in Apache Solr Search 6.3

Install and related hooks for apachesolr_search.

File

apachesolr.install
View source
<?php

/**
 * @file
 *   Install and related hooks for apachesolr_search.
 */

/**
 * Implements hook_requirements().
 */
function apachesolr_requirements($phase) {
  $requirements = array();
  if ($phase != 'runtime') {
    return $requirements;
  }

  // Ensure translations don't break at install time
  $t = get_t();
  $has_settings = FALSE;
  $id = apachesolr_default_environment();
  $environment = apachesolr_environment_load($id);
  if (!$environment || empty($environment['url'])) {
    $requirements['apachesolr'] = array(
      'title' => $t('Apache Solr'),
      'value' => $t('Missing environment configuration'),
      'description' => $t('Missing or invalid Solr environment record for the default environment ID %id.', array(
        '%id' => $id,
      )),
      'severity' => REQUIREMENT_ERROR,
    );
  }
  else {
    $has_settings = TRUE;
  }
  if ($has_settings) {
    $ping = FALSE;
    try {
      $solr = apachesolr_get_solr($id);
      $ping = @$solr
        ->ping(variable_get('apachesolr_ping_timeout', 4));

      // If there is no $solr object, there is no instance available, so don't continue.
      if (!$ping) {
        throw new Exception(t('No Solr instance available when checking requirements.'));
      }
    } catch (Exception $e) {
      watchdog('Apache Solr', nl2br(check_plain($e
        ->getMessage())), NULL, WATCHDOG_ERROR);
    }
    $value = $ping ? $t('Your site has contacted the Apache Solr server.') : $t('Your site was unable to contact the Apache Solr server.');
    $severity = $ping ? REQUIREMENT_OK : REQUIREMENT_ERROR;
    $requirements['apachesolr'] = array(
      'title' => $t('Apache Solr'),
      'value' => $value,
      'description' => $t('Default environment url: <br/> %url', array(
        '%url' => $environment['url'],
      )),
      'severity' => $severity,
    );
  }
  return $requirements;
}

/**
 * Implements hook_install().
 */
function apachesolr_install() {
  drupal_load('module', 'content');
  module_load_include('inc', 'apachesolr', 'apachesolr_search.admin');
  module_load_include('module', 'apachesolr', 'apachesolr');
  module_load_include('inc', 'apachesolr', 'apachesolr.index');

  // Create tables.
  drupal_install_schema('apachesolr');

  // Create one MLT block.
  apachesolr_search_mlt_save_block(array(
    'name' => t('More like this'),
  ));

  // Insert our default environment
  db_query("INSERT INTO {apachesolr_environment} (env_id, name, url, service_class)\n     VALUES ('%s', '%s', '%s', '%s')", array(
    'solr',
    'localhost server',
    'http://localhost:8983/solr',
    '',
  ));

  // Initialize the entities to index. We enable all node types by default
  $env_id = apachesolr_default_environment();
  $bundles = content_types();
  apachesolr_index_set_bundles($env_id, 'node', array_keys($bundles));
  drupal_set_message(t('Apache Solr is installed. Visit the <a href="@settings_link">settings page</a>.', array(
    '@settings_link' => url('admin/settings/apachesolr'),
  )));
}

/**
 * Implements hook_enable().
 */
function apachesolr_enable() {
  module_load_include('inc', 'apachesolr', 'apachesolr.index');

  // Completely build the index table.
  $env_id = apachesolr_default_environment();
  apachesolr_index_mark_for_reindex($env_id);
}

/**
 * Implements hook_schema().
 */
function apachesolr_schema() {
  $table = drupal_get_schema_unprocessed('system', 'cache');
  $table['description'] = 'Cache table for apachesolr to store Luke data and indexing information.';
  $schema['cache_apachesolr'] = $table;
  $schema['apachesolr_environment'] = array(
    'description' => 'The Solr search environment table.',
    // Enable CTools exportables based on this table.
    'export' => array(
      // Environment machine name.
      'key' => 'env_id',
      // Description of key.
      'key name' => 'Environment machine name',
      // Apache Solr doesn't allow disabling environments.
      'can disable' => FALSE,
      // Variable name to use in exported code.
      'identifier' => 'environment',
      // Thin wrapper for the environment save callback.
      'save callback' => 'apachesolr_ctools_environment_save',
      // Thin wrapper for the environment delete callback.
      'delete callback' => 'apachesolr_ctools_environment_delete',
      // Includes the environment variables in 'conf' as well as the fields in this table.
      'export callback' => 'apachesolr_ctools_environment_export',
      // Use the same hook as the API name below.
      'default hook' => 'apachesolr_environments',
      // CTools API implementation.
      'api' => array(
        'owner' => 'apachesolr',
        'api' => 'apachesolr_environments',
        'minimum_version' => 1,
        'current_version' => 1,
      ),
    ),
    'fields' => array(
      'env_id' => array(
        'description' => 'Unique identifier for the environment',
        'type' => 'varchar',
        'length' => 64,
        'not null' => TRUE,
      ),
      'name' => array(
        'description' => 'Human-readable name for the server',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'url' => array(
        'description' => 'Full url for the server',
        'type' => 'varchar',
        'length' => 1000,
        'not null' => TRUE,
      ),
      'service_class' => array(
        'description' => 'Optional class name to use for connection',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
    ),
    'primary key' => array(
      'env_id',
    ),
  );
  $schema['apachesolr_environment_variable'] = array(
    'description' => 'Variable values for each Solr search environment.',
    'fields' => array(
      'env_id' => array(
        'description' => 'Unique identifier for the environment',
        'type' => 'varchar',
        'length' => 64,
        'not null' => TRUE,
      ),
      'name' => array(
        'description' => 'The name of the variable.',
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
        'default' => '',
      ),
      'value' => array(
        'description' => 'The value of the variable.',
        'type' => 'blob',
        'not null' => TRUE,
        'size' => 'big',
      ),
    ),
    'primary key' => array(
      'env_id',
      'name',
    ),
  );

  // Technically the entity system does not require an integer ID.
  // However, documentation mentions :
  // id: The name of the property that contains the primary id of the
  // entity. Every entity object passed to the Field API must have this
  // property and its value must be numeric.

  //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 should be in the index.',
          '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(
        'bundle_changed' => array(
          'bundle',
          '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';
    }
  }
  $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',
    ),
  );
  return $schema;
}

/**
 * Implements hook_uninstall().
 * @todo : Remove the blocks with a query
 */
function apachesolr_uninstall() {

  // Remove variables.
  variable_del('apachesolr_default_environment');
  variable_del('apachesolr_rows');
  variable_del('apachesolr_site_hash');
  variable_del('apachesolr_index_last');
  variable_del('apachesolr_search_mlt_blocks');
  variable_del('apachesolr_cron_limit');
  variable_del('apachesolr_failure');
  variable_del('apachesolr_index_updated');
  variable_del('apachesolr_read_only');
  variable_del('apachesolr_set_nodeapi_messages');
  variable_del('apachesolr_last_optimize');
  variable_del('apachesolr_update_from_6303');

  // Remove tables.
  drupal_uninstall_schema('apachesolr');

  // Remove blocks.
  db_query('DELETE FROM {blocks} WHERE module = "apachesolr"');
}

/**
 * Remove status from the key.
 */
function apachesolr_update_6301() {
  $ret = array();

  // check if old tables are present and remove them
  if (!db_table_exists('apachesolr_search_node')) {
    $types = array(
      'other' => 'apachesolr_index_entities',
      'node' => 'apachesolr_index_entities_node',
    );
    foreach ($types as $type => $table) {
      db_drop_index($ret, $table, 'changed');
      db_add_index($ret, $table, 'bundle_changed', array(
        'bundle',
        'changed',
      ));
    }
  }
  return $ret;
}

/**
 * This function cleans up the old apachesolr tables. There is no magic upgrade
 * pat. The old facet and MLT blocks will be gone
 */
function apachesolr_update_6302() {
  $ret = array();

  // check if old tables are present and remove them
  if (db_table_exists('apachesolr_search_node')) {
    drupal_load('module', 'content');
    module_load_include('inc', 'apachesolr', 'apachesolr_search.admin');
    module_load_include('module', 'apachesolr', 'apachesolr');
    module_load_include('inc', 'apachesolr', 'apachesolr.index');

    // Remove the old tracking table
    $ret[] = update_sql('DROP TABLE {apachesolr_search_node}');

    // Remove cache table, will be freshly installed
    $ret[] = update_sql('DROP TABLE {cache_apachesolr}');

    // Delete all old facet blocks
    $ret[] = update_sql("DELETE FROM {blocks} WHERE 'module' = 'apachesolr'");

    // Delete old variables
    variable_del('apachesolr_enabled_facets');
    variable_del('apachesolr_facet_missing');
    variable_del('apachesolr_facet_query_initial_limits');
    variable_del('apachesolr_facet_query_limits');
    variable_del('apachesolr_index_last');
    variable_del('apachesolr_index_updated');
    variable_del('apachesolr_last_optimize');
    variable_del('apachesolr_host');
    variable_del('apachesolr_path');
    variable_del('apachesolr_port');
    variable_del('apachesolr_set_nodeapi_messages');
    variable_del('apachesolr_site_hash');

    // Create tables.
    drupal_install_schema('apachesolr');

    // Create one MLT block.
    apachesolr_search_mlt_save_block(array(
      'name' => t('More like this'),
    ));

    // Insert our default environment
    $ret[] = db_query("INSERT INTO {apachesolr_environment} (env_id, name, url, service_class)\n       VALUES ('%s', '%s', '%s', '%s')", array(
      'solr',
      'localhost server',
      'http://localhost:8983/solr',
      '',
    ));

    // Initialize the entities to index. We enable all node types by default
    $env_id = apachesolr_default_environment();
    $bundles = content_types();
    apachesolr_index_set_bundles($env_id, 'node', array_keys($bundles));
    apachesolr_enable();
  }
  return $ret;
}

/**
 * Leave a flag for 7.x updates so we have an upgrade path.
 */
function apachesolr_update_6303() {
  variable_set('apachesolr_update_from_6303', TRUE);
  return array();
}

/**
* Turn global variables in environment specific ones.
*/
function apachesolr_update_6304() {
  $stored = variable_get('apachesolr_index_last', array());
  foreach ($stored as $env_id => $entity_info) {
    apachesolr_environment_variable_set($env_id, 'apachesolr_index_last', $entity_info);
  }
  variable_del('apachesolr_index_last');
  $updated = variable_get('apachesolr_index_updated', array());
  $optimized = variable_get('apachesolr_last_optimize', 0);
  foreach ($updated as $env_id => $timestamp) {
    apachesolr_environment_variable_set($env_id, 'apachesolr_index_updated', $timestamp);
    apachesolr_environment_variable_set($env_id, 'apachesolr_last_optimize', $optimized);
  }
  variable_del('apachesolr_index_updated');
  variable_del('apachesolr_last_optimize');
}

Functions

Namesort descending Description
apachesolr_enable Implements hook_enable().
apachesolr_install Implements hook_install().
apachesolr_requirements Implements hook_requirements().
apachesolr_schema Implements hook_schema().
apachesolr_uninstall Implements hook_uninstall(). @todo : Remove the blocks with a query
apachesolr_update_6301 Remove status from the key.
apachesolr_update_6302 This function cleans up the old apachesolr tables. There is no magic upgrade pat. The old facet and MLT blocks will be gone
apachesolr_update_6303 Leave a flag for 7.x updates so we have an upgrade path.
apachesolr_update_6304 Turn global variables in environment specific ones.