You are here

apachesolr.install in Apache Solr Search 5.2

Install and related hooks for apachesolr_search.

File

apachesolr.install
View source
<?php

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

/**
 * Implementation of hook_requirements().
 */
function apachesolr_requirements($phase) {
  $requirements = array();
  $file_exists = file_exists(dirname(__FILE__) . '/SolrPhpClient/Apache/Solr/Service.php');

  // Ensure translations don't break at install time
  $t = get_t();
  if ($phase == 'runtime' && $file_exists) {
    $host = variable_get('apachesolr_host', 'localhost');
    $port = variable_get('apachesolr_port', 8983);
    $path = variable_get('apachesolr_path', '/solr');
    $ping = FALSE;
    try {
      $solr = apachesolr_get_solr();
      $ping = @$solr
        ->ping(variable_get('apachesolr_ping_timeout', 4));

      // If there is no $solr object, there is no server 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())), 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;
    $description = theme('item_list', array(
      $t('Host: %host', array(
        '%host' => $host,
      )),
      $t('Port: %port', array(
        '%port' => $port,
      )),
      $t('Path: %path', array(
        '%path' => $path,
      )),
    ));
    $requirements['apachesolr'] = array(
      'title' => $t('Apache Solr'),
      'value' => $value,
      'description' => $description,
      'severity' => $severity,
    );
  }

  // All phases
  $title = $t('Apache Solr PHP Client Library');
  if ($file_exists) {
    $expected_revision = 'Revision: 22';
    require_once 'SolrPhpClient/Apache/Solr/Service.php';
    $revision = defined('Apache_Solr_Service::SVN_REVISION') ? trim(Apache_Solr_Service::SVN_REVISION, '$ ') : '';
    if ($revision == $expected_revision) {
      $severity = REQUIREMENT_OK;
      $value = $t('Correct version "@expected".', array(
        '@expected' => $expected_revision,
      ));
      $description = NULL;
    }
    else {
      $value = $t('Incorrect version "@version". See the instructions in README.txt.', array(
        '@version' => $revision,
      ));
      $description = $t('The version of the library in the SolrPhpClient directory is "@version" compared to the expected "@expected"', array(
        '@version' => $revision,
        '@expected' => $expected_revision,
      ));
      $severity = REQUIREMENT_ERROR;
    }
    $requirements['SolrPhpClient'] = array(
      'title' => $title,
      'value' => $value,
      'description' => $description,
      'severity' => $severity,
    );
  }
  else {
    $requirements['SolrPhpClient'] = array(
      'title' => $title,
      'value' => $t('<em>Missing</em>.  See the instructions in README.txt'),
      'description' => $t('The Solr PHP library must be present in a sub-directory named SolrPhpClient.'),
      'severity' => REQUIREMENT_ERROR,
    );
  }
  return $requirements;
}

/**
 * Implementation of hook_install().
 */
function apachesolr_install() {

  // Create tables.
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      db_query("CREATE TABLE {apachesolr_search_node} (\n        nid int(10) unsigned NOT NULL COMMENT 'The primary identifier for a node.',\n        status int(11) NOT NULL default 1 COMMENT 'Boolean indicating whether the node is published (visible to non-administrators).',\n        changed int(11) NOT NULL default 0 COMMENT 'The Unix timestamp when a node property was changed.',\n        PRIMARY KEY (nid),\n        KEY changed (changed, status)\n      ) TYPE=MyISAM COMMENT='Stores a record of when a node property changed to determine if it needs indexing by Solr.' /*!40100 DEFAULT CHARACTER SET utf8 */;");
      db_query("CREATE TABLE {cache_apachesolr} (\n        cid varchar(255) NOT NULL default '',\n        data longblob,\n        expire int NOT NULL default '0',\n        created int NOT NULL default '0',\n        headers text,\n        PRIMARY KEY (cid),\n        INDEX expire (expire)\n      ) /*!40100 DEFAULT CHARACTER SET UTF8 */ ");
      break;
    case 'pgsql':

      // PostgreSQL DB
      db_query("CREATE TABLE {apachesolr_search_node} (\n        nid numeric(10) NOT NULL default 0,\n        status numeric(11) NOT NULL default 1,\n        changed numeric(11) NOT NULL default 0,\n        PRIMARY KEY (nid)\n      )");
      db_query("CREATE INDEX {apachesolr_search_node}_changed_idx ON {apachesolr_search_node} (changed)");
      drupal_set_message("Created Table: apachesolr_search_node");
      db_query("CREATE TABLE {cache_apachesolr} (\n        cid varchar(255) NOT NULL default '',\n        data bytea,\n        expire numeric(11) NOT NULL default 0,\n        created numeric(11) NOT NULL default 0,\n        headers text,\n        PRIMARY KEY (cid)\n      )");
      db_query("CREATE INDEX {cache_apachesolr}_expire_idx ON {cache_apachesolr} (expire)");
      drupal_set_message("Created Table: cache_apachesolr");
      break;
  }

  // Create one MLT block.
  require_once drupal_get_path('module', 'apachesolr') . '/apachesolr.admin.inc';
  apachesolr_mlt_save_block(array(
    'name' => t('More like this'),
  ));
  drupal_set_message(t('Search is enabled. Your site is <a href="!index_settings_link">currently 0% indexed</a>.', array(
    '!index_settings_link' => url('admin/settings/apachesolr/index'),
  )));
}

/**
 * Implementation of hook_enable().
 */
function apachesolr_enable() {

  // Completely build the index table.
  apachesolr_rebuild_index_table();
}

/**
 * Implementation of hook_uninstall().
 */
function apachesolr_uninstall() {
  variable_del('apachesolr_host');
  variable_del('apachesolr_port');
  variable_del('apachesolr_path');
  variable_del('apachesolr_rows');
  variable_del('apachesolr_facet_query_limits');
  variable_del('apachesolr_facet_query_limit_default');
  variable_del('apachesolr_site_hash');
  variable_del('apachesolr_index_last');
  variable_del('apachesolr_mlt_blocks');
  variable_del('apachesolr_cron_limit');
  variable_del('apachesolr_enabled_facets');

  // Remove tables.
  if (db_table_exists('apachesolr_search_node')) {
    db_query("DROP TABLE {apachesolr_search_node}");
  }

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

/**
 * Create node indexing table.
 */
function apachesolr_update_5000() {
  set_time_limit(0);

  // Create table.
  $ret = array();
  if (!db_table_exists('apachesolr_search_node')) {

    // Create tables.
    switch ($GLOBALS['db_type']) {
      case 'mysql':
      case 'mysqli':
        $ret[] = update_sql("CREATE TABLE {apachesolr_search_node} (\n                    nid int(10) unsigned NOT NULL COMMENT 'The primary identifier for a node.',\n                    status int(11) NOT NULL default 1 COMMENT 'Boolean indicating whether the node is published (visible to non-administrators).',\n                    changed int(11) NOT NULL default 0 COMMENT 'The Unix timestamp when a node property was changed.',\n                    PRIMARY KEY (nid),\n                    KEY changed (changed, status)\n                 ) TYPE=MyISAM COMMENT='Stores a record of when a node property changed to determine if it needs indexing by Solr.' /*!40100 DEFAULT CHARACTER SET utf8 */;");
        break;
      case 'pgsql':

        // PostgreSQL DB
        $ret[] = update_sql("CREATE TABLE {apachesolr_search_node} (\n                    nid numeric(10) NOT NULL default 0,\n                    status numeric(11) NOT NULL default 1,\n                    changed numeric(11) NOT NULL default 0,\n                    PRIMARY KEY (nid)\n                  )");
        $ret[] = update_sql("CREATE INDEX {apachesolr_search_node}_changed_idx ON {apachesolr_search_node} (changed)");
        break;
    }
  }

  // Populate table
  $ret[] = update_sql("INSERT INTO {apachesolr_search_node} (nid, status, changed)\n                       SELECT n.nid, n.status, GREATEST(n.created, n.changed, COALESCE(c.last_comment_timestamp, 0)) AS changed\n                       FROM {node} n LEFT JOIN {apachesolr_search_node} asn ON n.nid = asn.nid\n                       LEFT JOIN {node_comment_statistics} c ON n.nid = c.nid\n                       WHERE asn.changed IS NULL");
  return $ret;
}

/**
 * Fix block caching settings.
 * This was backported from 6.x
 * and was keept for numering reasosns
 */
function apachesolr_update_5001() {
  $ret = array();
  return $ret;
}

/**
 *  Make sure no nodes have a timestamp that's in the future
 */
function apachesolr_update_5002() {
  $ret = array();

  // Make sure no nodes end up with a timestamp that's in the future.
  $time = (int) time();
  $ret[] = update_sql("UPDATE {apachesolr_search_node} SET changed = {$time} WHERE changed > {$time}");
  return $ret;
}

/**
 *  Re-index nodes in books.
 */
function apachesolr_update_5003() {
  $ret = array();
  if (module_exists('book')) {
    $time = (int) time();
    $ret[] = update_sql("UPDATE {apachesolr_search_node} SET changed = {$time} WHERE nid IN (SELECT nid from {book})");
  }
  return $ret;
}

/**
 *  Subsume MLT functionality..
 */
function apachesolr_update_5004() {
  $ret = array();
  if (db_table_exists("apachesolr_mlt")) {
    require_once drupal_get_path('module', 'apachesolr') . '/apachesolr.admin.inc';
    $result = db_query('SELECT id, data FROM {apachesolr_mlt} ORDER BY id ASC');
    while ($row = db_fetch_array($result)) {
      $delta = sprintf('mlt-%03d', $row['id']);
      apachesolr_mlt_save_block(unserialize($row['data']), $delta);
      $ret[] = update_sql("UPDATE {blocks} SET module = 'apachesolr', delta = '" . $delta . "' WHERE module = 'apachesolr_mlt' AND delta ='" . $row['id'] . "'");
    }
    $ret[] = update_sql("DROP TABLE {apachesolr_mlt}");
  }
  $ret[] = update_sql("DELETE FROM {system} WHERE name = 'apachesolr_mlt'");
  return $ret;
}

/**
 * Add ApacheSolr's own cache table.
 * @see http://drupal.org/node/630798
 */
function apachesolr_update_5005() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("CREATE TABLE {cache_apachesolr} (\n        cid varchar(255) NOT NULL default '',\n        data longblob,\n        expire int NOT NULL default '0',\n        created int NOT NULL default '0',\n        headers text,\n        PRIMARY KEY (cid),\n        INDEX expire (expire)\n      ) /*!40100 DEFAULT CHARACTER SET UTF8 */ ");
      break;
    case 'pgsql':
      $ret[] = update_sql("CREATE TABLE {cache_apachesolr} (\n        cid varchar(255) NOT NULL default '',\n        data bytea,\n        expire numeric(11) NOT NULL default 0,\n        created numeric(11) NOT NULL default 0,\n        headers text,\n        PRIMARY KEY (cid)\n      )");
      $ret[] = update_sql("CREATE INDEX {cache_apachesolr}_expire_idx ON {cache_apachesolr} (expire)");
      break;
  }
  return $ret;
}

Functions

Namesort descending Description
apachesolr_enable Implementation of hook_enable().
apachesolr_install Implementation of hook_install().
apachesolr_requirements Implementation of hook_requirements().
apachesolr_uninstall Implementation of hook_uninstall().
apachesolr_update_5000 Create node indexing table.
apachesolr_update_5001 Fix block caching settings. This was backported from 6.x and was keept for numering reasosns
apachesolr_update_5002 Make sure no nodes have a timestamp that's in the future
apachesolr_update_5003 Re-index nodes in books.
apachesolr_update_5004 Subsume MLT functionality..
apachesolr_update_5005 Add ApacheSolr's own cache table.