You are here

searchindex_wipe.module in Search Index Wipe 7

Same filename and directory in other branches
  1. 8 searchindex_wipe.module

Enables wipe of search index.

File

searchindex_wipe.module
View source
<?php

/**
 * @file
 * Enables wipe of search index.
 */

/**
 * Implements hook_menu().
 */
function searchindex_wipe_menu() {

  // Confirmation form to wipe search index.
  $items['admin/searchindex_wipe'] = array(
    'title' => 'Clear Index',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'searchindex_wipe_confirm',
    ),
    'access arguments' => array(
      'wipe search index',
    ),
    'type' => MENU_CALLBACK,
  );
  return $items;
}

/**
 * Implements hook_permission().
 */
function searchindex_wipe_permission() {
  return array(
    'wipe search index' => array(
      'title' => t('Wipe Search Index'),
      'description' => t('Permission to wipe search index.'),
    ),
  );
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function searchindex_wipe_form_search_admin_settings_alter(&$form, &$form_state) {
  if (user_access('wipe search index')) {
    $form['status']['clear_index'] = array(
      '#value' => t('Wipe Index'),
      '#type' => 'submit',
      '#submit' => array(
        'searchindex_wipe_all',
      ),
      '#description' => t('Drops all the index build by search module'),
    );
  }
}

/**
 * Redirect to confirmation link.
 */
function searchindex_wipe_all(&$form, &$form_state) {
  $form_state['redirect'] = array(
    'admin/searchindex_wipe',
  );
}

/**
 * Implements hook_help().
 */
function searchindex_wipe_help($path, $arg) {
  switch ($path) {

    // Main module help for the block module.
    case 'admin/config/search/settings':
      return '<p>' . t('Enables clearing of Search related indexes, so that search related tables do not grow large. To Re-index the site, You first need to click on Wipe Index button.') . '</p>';
  }
}

/**
 * Confirmation form.
 */
function searchindex_wipe_confirm($form, $form_state) {
  return confirm_form($form, t('Are you sure you want to clear Search index?'), "admin/config/search/settings", t('This action cannot be undone.'), t('Delete'), t('Cancel'));
}

/**
 * Confirmation form submit. Truncates all search related tables.
 */
function searchindex_wipe_confirm_submit($form, &$form_state) {
  if ($form_state['values']['confirm']) {
    searchindex_wipe_truncate_table();
  }
  $form_state['redirect'] = 'admin/config/search/settings';
}
function searchindex_wipe_truncate_table() {

  // Taken code from drupal_uninstall_schema.
  $schema = drupal_get_schema_unprocessed('search');
  foreach ($schema as $table_name => $table) {

    // Truncate search related tables.
    db_truncate($table_name)
      ->execute();
  }
  drupal_set_message(t('Search Index Cleared, Please rebuilt the index by running cron.'));
}

Functions

Namesort descending Description
searchindex_wipe_all Redirect to confirmation link.
searchindex_wipe_confirm Confirmation form.
searchindex_wipe_confirm_submit Confirmation form submit. Truncates all search related tables.
searchindex_wipe_form_search_admin_settings_alter Implements hook_form_FORM_ID_alter().
searchindex_wipe_help Implements hook_help().
searchindex_wipe_menu Implements hook_menu().
searchindex_wipe_permission Implements hook_permission().
searchindex_wipe_truncate_table