You are here

apachesolr_nan.module in Apache Solr Not-A-Node 7

Same filename and directory in other branches
  1. 7.2 apachesolr_nan.module

Provides hook and common functions for non-node searching.

File

apachesolr_nan.module
View source
<?php

/**
 * @file
 * Provides hook and common functions for non-node searching.
 */

/**
 * Implements hook_menu().
 */
function apachesolr_nan_menu() {
  $items['admin/config/search/apachesolr-nan'] = array(
    'title' => t('Apache Solr Not-a-Node'),
    'description' => t('Configure non-node index settings'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'apachesolr_nan_nan_settings_form',
    ),
    'access arguments' => array(
      'administer search',
    ),
    'file' => 'apachesolr_nan.admin.inc',
  );
  return $items;
}

/**
 * Implements hook_cron().
 *
 * Find non-nodes that should be indexed and pass them to solr.
 */
function apachesolr_nan_cron() {
  $query = db_select('apachesolr_nan_index_paths', 'p');
  $query
    ->fields('p', array(
    'id',
    'path',
    'description',
    'title',
  ));
  $query
    ->where('(:time - p.last_indexed) > p.frequency', array(
    ':time' => REQUEST_TIME,
  ));
  $results = $query
    ->execute();
  while ($data = $results
    ->fetchAssoc()) {
    apachesolr_nan_process_solr_indexes($data['path'], $data['title'], $data['description'], $data['id']);
    watchdog('ApacheSolr NAN search', 'The apache solr non-node item %id from path %path was indexed.', array(
      '%id' => $data['id'],
      '%path' => $data['path'],
    ));
    $data['last_indexed'] = REQUEST_TIME;
    drupal_write_record('apachesolr_nan_index_paths', $data, array(
      'id',
    ));
  }
}

/**
 * Send information to solr for indexing.
 *
 * @param string $path
 *   A string representing the path to index.
 * @param string $title
 *   A string representing the title as it should appear in the search index.
 * @param string $description
 *   A string representing additional search terms or a description that will be
 *   helpful for users to find the page.
 * @param int $fnid
 *   An interger that will be used to designate the node id of the non-node. The
 *   "f" stands for fake. This should be a negative number to keep it out of the
 *   normal node id space. If a positive integer is received it will be
 *   multiplied by negative one.
 */
function apachesolr_nan_process_solr_indexes($path, $title, $description, $fnid) {
  if ($fnid > 0 && is_numeric($fnid)) {
    $fnid = (int) $fnid * -1;
  }
  global $user;
  drupal_save_session(FALSE);
  $saved_user = $user;

  // build the content for the index as an anonymous user to avoid exposing
  // restricted fields and such. By setting a variable, indexing can take place
  // as a different user
  $uid = variable_get('apachesolr_index_user', 0);
  if ($uid == 0) {
    $user = drupal_anonymous_user();
  }
  else {
    $user = user_load($uid);
  }
  module_load_include('inc', 'apachesolr', 'apachesolr.index');
  global $base_url;
  $env_id = variable_get('apachesolr_nan_nan_env', apachesolr_default_environment());
  $entity_type = 'node';
  $bundle = 'basic_page';
  $bundle_name = 'Basic Page';
  $item = menu_get_item(drupal_get_normal_path($path));
  if (empty($item['access'])) {
    apachesolr_index_delete_entity_from_index($env_id, 'node', $fnid);
    watchdog('ApacheSolr NAN search', 'Access to %id from path %path is not allowed to be viewed by the indexing user and has been removed..', array(
      '%id' => $fnid,
      '%path' => $path,
    ));
    return FALSE;
  }
  $function = $item['page_callback'];
  if (!empty($item['include_file'])) {
    require_once $item['include_file'];
  }
  $content = call_user_func_array($function, $item['page_arguments']);
  $content = render($content);
  $document = new ApacheSolrDocument();
  $document->id = apachesolr_document_id($fnid, $entity_type);
  $document->site = $base_url;
  $document->hash = apachesolr_site_hash();
  $document->entity_id = $fnid;
  $document->entity_type = $entity_type;
  $document->bundle = $bundle;
  $document->bundle_name = $bundle_name;
  $document->ss_language = LANGUAGE_NONE;
  $document->path = $path;
  $document->url = url($path);
  $document->path_alias = $path;
  $document->label = $title;
  $document->content = apachesolr_clean_text($description . ' ' . $content);
  $document->teaser = truncate_utf8($document->content, 300, TRUE);
  $documents[] = $document;
  apachesolr_index_send_to_solr($env_id, $documents);

  // Restore the user.
  $user = $saved_user;
  drupal_save_session(TRUE);
}

Functions

Namesort descending Description
apachesolr_nan_cron Implements hook_cron().
apachesolr_nan_menu Implements hook_menu().
apachesolr_nan_process_solr_indexes Send information to solr for indexing.