view sites/all/modules/custom/solrsearch/solrsearch_search.install @ 0:015d06b10d37 default tip

initial
author dwinter
date Wed, 31 Jul 2013 13:49:13 +0200
parents
children
line wrap: on
line source

<?php

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

/**
 * Implements hook_install().
 */
function solrsearch_search_install() {
  // Add a taxonomy search page to the database
  $settings = array(
    'solrsearch_search_search_type' => 'tid',
    'solrsearch_search_per_page' => 10,
    'solrsearch_search_browse' => 'results',
    'solrsearch_search_spellcheck' => FALSE,
    'solrsearch_search_search_box' => FALSE,
  );
  $settings = serialize($settings);

  $fields = array(
    'page_id' => 'taxonomy_search',
    'label' => 'Taxonomy Search',
    'description' => 'Search all items with given term',
    'search_path' => 'taxonomy/term/%',
    'env_id' => '',
    'page_title' => '%value',
    'settings' => $settings,
  );
  db_insert('solrsearch_search_page')->fields($fields)->execute();
}

/**
 * Implements hook_enable().
 */
function solrsearch_search_enable() {
  // Make sure the default core search page is installed.
  //dpm("ENABLE");
  $search_page = solrsearch_search_page_load('core_solr_search');
  //dpm($search_page);
  if (empty($search_page)) {
    // Add Default search page (core search)
    // This is a duplication from update_7004 but it is intended
    // so future changes are possible without breaking the update
    $settings = array(
      'solrsearch_search_search_type' => 'custom',
      'solrsearch_search_per_page' => 10,
      'solrsearch_search_browse' => 'browse',
      'solrsearch_search_spellcheck' => TRUE,
      'solrsearch_search_not_removable' => TRUE,
      'solrsearch_search_search_box' => TRUE,
    );
    $settings = serialize($settings);

    $fields = array(
      'page_id' => 'core_solr_search',
      'label' => 'Core Solr Search',
      'description' => 'Core SolrSearch',
      'search_path' => 'solrsearch/site',
      'env_id' => 'searchecho',
      'page_title' => 'echo',
      'settings' => $settings,
    );
    db_insert('solrsearch_search_page')->fields($fields)->execute();
  }


  #$active = variable_get('search_active_modules', array('node', 'user'));
  #$active[] = 'solrsearch_search';
  #variable_set('search_active_modules', array_unique($active));
}

/**
 * Implements hook_schema().
 */
function solrsearch_search_schema() {
  $schema = array();

  $schema['solrsearch_search_page'] = array(
    'description' => 'Apache Solr Search search page settings.',
    'export' => array(
      // Environment machine name.
      'key' => 'page_id',
      // Description of key.
      'key name' => 'search page machine name',
      // Variable name to use in exported code.
      'identifier' => 'searcher',
      // Use the same hook as the API name below.
      'default hook' => 'solrsearch_search_default_searchers',
      'status' => 'solrsearch_search_page_status',
      // CTools API implementation.
      'api' => array(
        'owner' => 'solrsearch_search',
        'api' => 'solrsearch_search_defaults',
        'minimum_version' => 3,
        'current_version' => 3,
      ),
      // Includes all search page specific configurations.
      'export callback' => 'solrsearch_search_page_settings_export',
    ),
    'fields' => array(
      'page_id' => array(
        'description' => 'The machine readable name of the search page.',
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
        'default' => '',
      ),
      'label' => array(
        'description' => 'The human readable name of the search page.',
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
        'default' => '',
      ),
      'description' => array(
        'description' => 'The description of the search page.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'search_path' => array(
        'description' => 'The path to the search page.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'page_title' => array(
        'description' => 'The title of the search page.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'env_id' => array(
        'description' => 'The machine name of the search environment.',
        'type' => 'varchar',
        'length' => 64,
        'not null' => TRUE,
        'default' => '',
      ),
      'settings' => array(
        'description' => 'Serialized storage of general settings.',
        'type' => 'text',
        'serialize' => TRUE,
      ),
    ),
    'primary key' => array('page_id'),
    'indexes' => array(
      'env_id' => array('env_id'),
    ),
  );

  return $schema;
}

/**
 * Implements hook_uninstall().
 */
function solrsearch_search_uninstall() {
  $stored = variable_get('solrsearch_index_last', array());
  unset($stored['solrsearch_search']);
  variable_set('solrsearch_index_last', $stored);

  $active = variable_get('search_active_modules', array('node', 'user'));
  $idx = array_search('solrsearch_search', $active);
  if ($idx !== FALSE) {
    unset($active[$idx]);
    variable_set('search_active_modules', $active);
  }
  // Remove variables.
  variable_del('solrsearch_search_spellcheck');
  variable_del('solrsearch_search_mlt_blocks');
  variable_del('solrsearch_search_default_search_page');
  // Remove blocks.
  db_delete('block')->condition('module', 'solrsearch_search')->execute();
}

/**
 * Various updates for Drupal 7.
 */
function solrsearch_search_update_7000() {
  $taxo_links = variable_get('solrsearch_search_taxonomy_links', 0);
  // TODO - enable the new contrib module?
  variable_del('solrsearch_search_taxonomy_links');
  // TODO - possibly rename block deltas, etc.
  $active = variable_get('search_active_modules', array('node', 'user'));
  $active[] = 'solrsearch_search';
  variable_set('search_active_modules', array_unique($active));
  if (variable_get('solrsearch_search_make_default', 0)) {
    variable_set('search_default_module', 'solrsearch_search');
  }
  variable_del('solrsearch_search_make_default');
}

/**
 * Add solrsearch_search_page table.
 */
function solrsearch_search_update_7001() {
  // Moved to 7002
}

/**
 * Add solrsearch_search_page table for real.
 */
function solrsearch_search_update_7002() {

  $schema['solrsearch_search_page'] = array(
    'description' => 'Apache Solr Search search page settings.',
    'export' => array(
      'key' => 'page_id',
      'identifier' => 'searcher',
      'default hook' => 'solrsearch_search_default_searchers',
      'status' => 'solrsearch_search_page_status',
      'api' => array(
        'owner' => 'solrsearch_search',
        'api' => 'solrsearch_search_defaults',
        'minimum_version' => 3,
        'current_version' => 3,
      ),
      'export callback' => 'solrsearch_search_page_settings_export',
    ),
    'fields' => array(
      'page_id' => array(
        'description' => 'The machine readable name of the search page.',
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
        'default' => '',
      ),
      'label' => array(
        'description' => 'The human readable name of the search page.',
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
        'default' => '',
      ),
      'description' => array(
        'description' => 'The description of the search page.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'search_path' => array(
        'description' => 'The path to the search page.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'page_title' => array(
        'description' => 'The title of the search page.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'env_id' => array(
        'description' => 'The machine name of the search environment.',
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
        'default' => '',
      ),
      'settings' => array(
        'description' => 'Serialized storage of general settings.',
        'type' => 'text',
        'serialize' => TRUE,
      ),
    ),
    'primary key' => array('page_id'),
    'indexes' => array(
      'env_id' => array('env_id'),
    ),
  );
  if (db_table_exists('solrsearch_search_page')) {
    // Just in case you are chasing HEAD.
    db_drop_table('solrsearch_search_page');
  }
  db_create_table('solrsearch_search_page', $schema['solrsearch_search_page']);
}


/**
 * Delete all Apache Solr Search blocks - they moved to Facet API.
 */
function solrsearch_search_update_7003() {
  // Remove blocks.
  db_delete('block')->condition('module', 'solrsearch_search')->execute();
}

/**
 * Add a default search page for core
 * Add a taxonomy page if the taxonomy module was ever active
 */
function solrsearch_search_update_7004() {
  // Add Default search page (core search)
  $settings = array(
    'solrsearch_search_search_type' => 'custom',
    'solrsearch_search_per_page' => variable_get('solrsearch_rows', 10),
    'solrsearch_search_browse' => variable_get('solrsearch_search_browse', 'browse'),
    'solrsearch_search_spellcheck' => variable_get('solrsearch_search_spellcheck', TRUE),
    'solrsearch_search_not_removable' => TRUE,
    'solrsearch_search_search_box' => TRUE,
  );
  $settings = serialize($settings);

  $fields = array(

      'page_id' => 'core_solr_search',
      'label' => 'Core Solr Search',
      'description' => 'Core SolrSearch',
      'search_path' => 'solrsearch/site',
      'env_id' => 'searchecho',
      'page_title' => 'echo',
      'settings' => $settings,
  );
  db_insert('solrsearch_search_page')->fields($fields)->execute();
  // Remove variables.
  variable_del('solrsearch_search_spellcheck');
  variable_del('solrsearch_search_browse');

  // Add this taxonomy search page to the database
  $settings = array(
    'solrsearch_search_search_type' => 'tid',
    'solrsearch_search_per_page' => 10,
    'solrsearch_search_browse' => 'results',
    'solrsearch_search_spellcheck' => FALSE,
    'solrsearch_search_search_box' => FALSE,
  );
  $settings = serialize($settings);

  $fields = array(
    'page_id' => 'taxonomy_search',
    'label' => 'Taxonomy Search',
    'description' => 'Search all items with given term',
    'search_path' => 'taxonomy/term/%',
    'env_id' => '',
    'page_title' => '%value',
    'settings' => $settings,
  );
  db_insert('solrsearch_search_page')->fields($fields)->execute();

  // Check if the taxonomy module was ever present
  $status = db_query("SELECT 1 FROM {system} WHERE name = 'solrsearch_taxonomy'")->fetchField();
  if ($status) {
    $message  = t('If you had the solrsearch_taxonomy module enabled please go to the !link and enable the Taxonomy Term page', array('!link' => l('Apache Solr custom pages', 'admin/config/search/solrsearch/search-pages')));
    drupal_set_message($message, 'warning');
  }
}

/**
 * Make the env_id length on the solrsearch_search_page table 64 characters
 * to match the length of the env_id on all other tables
 */
function solrsearch_search_update_7005(&$sandbox) {
  db_drop_index('solrsearch_search_page', 'env_id');
  db_change_field('solrsearch_search_page', 'env_id', 'env_id',
    array(
        'description' => 'The machine name of the search environment.',
        'type' => 'varchar',
        'length' => 64,
        'not null' => TRUE,
        'default' => '',
      ),
      array(
        'indexes' => array(
          'env_id' => array('env_id'),
        )
      )
    );
}

/**
 * Remove all solrsearch_search env variables for show_facets if it is zero
 */
function solrsearch_search_update_7006() {
  module_load_include('module', 'solrsearch');
  $environments = solrsearch_load_all_environments();
  foreach ($environments as $environment) {
    $show_facets = solrsearch_environment_variable_get($environment['env_id'], 'solrsearch_search_show_facets', 0);
    if ($show_facets === 0) {
      solrsearch_environment_variable_del($environment['env_id'], 'solrsearch_search_show_facets');
    }
  }
}