You are here

similar.module in Similar Entries 6.2

Same filename and directory in other branches
  1. 5 similar.module
  2. 6 similar.module
  3. 7.2 similar.module
  4. 7 similar.module

Module that shows a block listing similar entries. NOTE: Uses MySQL's FULLTEXT indexing for MyISAM tables.

@author David Kent Norman http://deekayen.net/ @author Arnab Nandi http://arnab.org/ @author Jordan Halterman jordan.halterman@gmail.com

File

similar.module
View source
<?php

/**
 * @file
 * Module that shows a block listing similar entries.
 * NOTE: Uses MySQL's FULLTEXT indexing for MyISAM tables.
 *
 * @author David Kent Norman http://deekayen.net/
 * @author Arnab Nandi http://arnab.org/
 * @author Jordan Halterman jordan.halterman@gmail.com
 */

/**
 * Implements hook_help().
 */
function similar_help($path, $arg) {
  switch ($path) {
    case 'admin/help#similar':
      return '<p>' . t('Lists the most similar nodes to the current node.') . '</p>';
  }
}

/**
 * Implements hook_views_api().
 */
function similar_views_api() {
  return array(
    'api' => 2,
    'path' => drupal_get_path('module', 'similar') . '/views',
  );
}

/**
 * Resets all Similar Entries indexes.
 */
function similar_reset_indices() {
  variable_del('similar_indices');
  similar_cron();
}

/**
 * Implements hook_cron().
 *
 * Checks if an index rebuild is needed as determined in Drupal
 * variables. If the similar_index variable is not empty it will
 * have an array of field names which were added on the last
 * cron run. If field settings have changed since that last cron
 * run then the old index will be deleted and a new one added that
 * includes all the relevant fields in the table. This is because
 * ideally we want a single index for multiple fields in a table.
 *
 * @TODO: Ensure that the index sticks with the field when CCK
 * fields are edited. If not, we may have to implement
 * hook_content_fieldapi()'s update operations to reset the index.
 */
function similar_cron() {
  if (module_exists('content')) {
    $indices = similar_get_indices();
    $data = array();

    // Get all columns for each field.
    foreach (content_fields() as $field) {
      if ($field['type'] == 'text') {
        $info = content_database_info($field);
        foreach ($info['columns'] as $column) {
          $data[$info['table']][$column['column']] = $column['column'];
        }
      }
    }

    // Check and reindex each table for each content type if necessary.
    foreach ($data as $table => $columns) {
      foreach ($columns as $column) {
        _similar_add_index($table, $column);
      }
    }
  }
}

/**
 * Adds FULLTEXT indexes to CCK database columns.
 *
 * @param $table
 *   A string representing the table to index.
 * @param $column
 *   A column name to be indexed.
 */
function _similar_add_index($table, $column) {
  if (isset($indexed[$table][$column])) {
    return;
  }
  $indexed = similar_get_indices();
  if (db_table_exists($table)) {
    if (!empty($column)) {
      $index = 'similar_' . $column;
      db_query("ALTER TABLE {" . $table . "} ENGINE = MYISAM");
      db_query("ALTER TABLE {" . $table . "} ADD FULLTEXT `{$index}` (`%s`)", $column);
      $indexed[$table][$column] = $column;
    }
  }
  elseif (isset($index[$table])) {
    unset($indexed[$table]);
  }
  variable_set('similar_indices', $indexed);
}

/**
 * Returns data about what fields are currently indexed.
 *
 * Indexed tables are fields are stored in a Drupal variable. Something
 * to do is look into ways to reset the variable upon reasonable events
 * like a CCK module install/uninstall or a cache clearance.
 *
 * @param $table
 *   An optional string identifying a specific table's indices to return.
 * @return
 *   An array of sub-arrays where the key is a table name and the value
 *   is an array of fields which are currently indexed in the table.
 */
function similar_get_indices($table = NULL) {
  $indices = variable_get('similar_indices', array());
  if (!empty($table)) {
    return isset($indices[$table]) ? $indices[$table] : FALSE;
  }
  else {
    return $indices;
  }
}

Functions

Namesort descending Description
similar_cron Implements hook_cron().
similar_get_indices Returns data about what fields are currently indexed.
similar_help Implements hook_help().
similar_reset_indices Resets all Similar Entries indexes.
similar_views_api Implements hook_views_api().
_similar_add_index Adds FULLTEXT indexes to CCK database columns.