You are here

apachesolr_user.module in Apachesolr User 6

Same filename and directory in other branches
  1. 7 apachesolr_user.module

File

apachesolr_user.module
View source
<?php

/**
 *  This module will index users and there profile information in Solr.
 *  As this is a D6 module we are treating them like nodes
 */
function apachesolr_user_update_index() {
  $cron_limit = 5;

  //variable_get('apachesolr_cron_limit', 50);
  $rows = apachesolr_user_get_users_to_index('apachesolr_user', $cron_limit);
  apachesolr_user_index_users($rows, 'apachesolr_user');
}

//apachesolr_document_id($id, 'user');
function apachesolr_user_user($op, &$edit, &$account) {
  switch ($op) {
    case 'update':
      apachesolr_user_user_update($account->uid, time());
      break;
  }
}
function apachesolr_user_get_users_to_index($namespace, $limit) {
  $rows = array();
  if (variable_get('apachesolr_read_only', 0)) {
    return $rows;
  }
  extract(apachesolr_get_last_index($namespace));
  $args = array(
    $last_change,
    $last_change,
    $last_nid,
  );

  // @TODO Extend this to support indexing of only certain roles etc
  $result = db_query_range("SELECT asu.uid as nid, asu.changed FROM {apachesolr_search_user} asu WHERE (asu.changed > %d OR (asu.changed = %d AND asu.uid > %d)) AND asu.status = 1 ORDER BY asu.changed ASC, asu.uid ASC", $args, 0, $limit);
  while ($row = db_fetch_object($result)) {
    $rows[] = $row;
  }
  return $rows;
}
function apachesolr_user_index_users($rows, $namespace = '', $callback = 'apachesolr_user_add_user_document') {
  if (!$rows) {

    // Nothing to do.
    return FALSE;
  }
  try {

    // Get the $solr object
    $solr = apachesolr_get_solr();

    // If there is no server available, don't continue.
    if (!$solr
      ->ping(variable_get('apachesolr_ping_timeout', 4))) {
      throw new Exception(t('No Solr instance available during indexing.'));
    }
  } catch (Exception $e) {
    watchdog('Apache Solr', nl2br(check_plain($e
      ->getMessage())), NULL, WATCHDOG_ERROR);
    return FALSE;
  }
  $documents = array();
  $old_position = apachesolr_get_last_index($namespace);
  $position = $old_position;
  foreach ($rows as $row) {
    try {
      $callback($documents, $row->nid, $namespace);

      // Variables to track the last item changed.
      $position['last_change'] = $row->changed;
      $position['last_nid'] = $row->nid;
    } catch (Exception $e) {

      // Something bad happened - don't continue.
      watchdog('Apache Solr', 'Error constructing documents to index: <br /> !message', array(
        '!message' => nl2br(strip_tags($e
          ->getMessage())),
      ), WATCHDOG_ERROR);
      break;
    }
  }
  if (count($documents)) {
    try {
      watchdog('Apache Solr', 'Adding @count documents.', array(
        '@count' => count($documents),
      ));

      // Chunk the adds by 20s
      $docs_chunk = array_chunk($documents, 20);
      foreach ($docs_chunk as $docs) {
        $solr
          ->addDocuments($docs);
      }

      // Set the timestamp to indicate an index update.
      apachesolr_index_updated(time());
    } catch (Exception $e) {
      $nids = array();
      if (!empty($docs)) {
        foreach ($docs as $doc) {
          $nids[] = $doc->nid;
        }
      }
      watchdog('Apache Solr', 'Indexing failed on one of the following nodes: @nids <br /> !message', array(
        '@nids' => implode(', ', $nids),
        '!message' => nl2br(strip_tags($e
          ->getMessage())),
      ), WATCHDOG_ERROR);
      return FALSE;
    }
  }

  // Save the new position in case it changed.
  if ($namespace && $position != $old_position) {
    $stored = variable_get('apachesolr_index_last', array());
    $stored[$namespace] = $position;
    variable_set('apachesolr_index_last', $stored);
  }
  return $position;
}

/**
 * Truncate and rebuild the apachesolr_search_user table, reset the apachesolr_index_last variable.
 * This is the most complete way to force reindexing, or to build the indexing table for the
 * first time.
 *
 */
function apachesolr_user_rebuild_index_table() {
  db_query("DELETE FROM {apachesolr_search_user} WHERE uid IN (SELECT uid FROM {users})");

  // Populate table
  db_query("INSERT INTO {apachesolr_search_user} (uid, status, changed)\n              SELECT u.uid, u.status, %d AS changed\n              FROM {users} u ", time());
  $time = time();
  db_query("UPDATE {apachesolr_search_node} SET changed = %d WHERE changed > %d", $time, $time);
  apachesolr_clear_last_index('apachesolr_user');
}
function apachesolr_user_add_user_document(&$documents, $uid, $namespace) {
  if ($document = apachesolr_user_user_to_document($uid, $namespace)) {
    $documents[] = $document;
  }
}

/**
 * Given a node ID, return a document representing that node.
 */
function apachesolr_user_user_to_document($uid, $namespace) {
  include_once drupal_get_path('module', 'apachesolr') . '/apachesolr.index.inc';
  include_once drupal_get_path('module', 'apachesolr') . '/SolrPhpClient/Apache/Solr/Document.php';
  $user = user_load($uid, NULL, TRUE);
  if (empty($user)) {
    return FALSE;
  }
  $document = FALSE;

  // Let any module exclude this node from the index.
  $build_document = TRUE;
  if ($build_document) {

    // Build the node body.
    $node->body = $user->name;
    $node->title = $user->name;
    $text = $node->name;
    $profile = array();
    foreach ($user as $key => $value) {
      if (stristr($key, 'profile_')) {

        // At the moment this can only be a date
        if (is_array($value)) {
          $value = sprintf('%s-%s-%s', $value['year'], $value['month'], $value['day']);
        }
        $profile[$key] = $value;
      }
    }
    $text .= "\n\n" . implode(' ', $profile);
    $document = new Apache_Solr_Document();
    $document->id = apachesolr_document_id($user->uid, 'user');
    $document->hash = apachesolr_site_hash();
    $document->entity = 'user';
    $document->nid = $user->uid;
    $document->uid = $user->uid;
    $document->title = $node->title;
    $document->status = $user->status;
    $document->body = apachesolr_clean_text($text);
    $document->type = 'user';
    $document->type_name = 'user';
    $document->created = apachesolr_date_iso($user->created);
    $document->name = $user->name;
    $path = 'user/' . $user->uid;

    // Picture
    $document->{'ss_picture'} = $user->picute;

    // Signature
    $document->{'ts_signature'} = $user->signature;
    $document->url = url($path, array(
      'absolute' => TRUE,
    ));
    $document->path = $path;

    // Path aliases can have important information about the content.
    // Add them to the index as well.
    if (function_exists('drupal_get_path_alias')) {

      // Add any path alias to the index, looking first for language specific
      // aliases but using language neutral aliases otherwise.
      $language = empty($node->language) ? '' : $node->language;
      $output = drupal_get_path_alias($path, $language);
      if ($output && $output != $path) {
        $document->path_alias = $output;
      }
    }

    // Profile Fields
    foreach ($profile as $key => $value) {
      $profile_info = false;
      $profile_info = apachesolr_user_get_profile_info($key);

      // At the moment we only index items that are visible to the
      if ($profile_info['visibility'] > 1 && !empty($value)) {
        $index_key = apachesolr_user_profile_index_key($profile_info);

        //          if (isset($value['value']) && strlen($value['value'])) {
        //            if ($cck_info['multiple']) {
        //              $document->setMultiValue($index_key, apachesolr_clean_text($value['value']));
        //            }
        //            else {
        if ($profile_info['type'] == 'date') {
          $value = strtotime($value);
          $document->{$index_key} = apachesolr_date_iso($value);
        }
        else {
          $document->{$index_key} = apachesolr_clean_text($value);
        }
      }
    }

    // Let modules add to the document.
    foreach (module_implements('apachesolr_user_update_index') as $module) {
      $function = $module . 'apachesolr_user_update_index';
      $function($document, $node, $namespace);
    }
  }
  return $document;
}
function apachesolr_user_user_update($uid, $time) {
  db_query('UPDATE {apachesolr_search_user} SET changed = %d where uid = %d', $time, $uid);
}
function apachesolr_user_get_profile_info($field_name) {
  static $info = array();
  if (!isset($info[$field_name])) {
    $result = db_query('SELECT type, visibility, options, name from profile_fields where name = "%s"', $field_name);

    // There should only be one
    $info[$field_name] = db_fetch_array($result);
  }
  return $info[$field_name];
}
function apachesolr_user_profile_index_key($field) {
  switch ($field['type']) {
    case 'textfield':
    case 'textarea':
      $type_prefix = 't';
      break;
    case 'checkbox':
      $type_prefix = 'b';
      break;
    case 'date':
      $type_prefix = 'd';
      break;
    default:
      $type_prefix = 's';
  }
  $sm = $field['multiple'] ? 'm_' : 's_';
  return $type_prefix . $sm . $field['name'];
}

Functions

Namesort descending Description
apachesolr_user_add_user_document
apachesolr_user_get_profile_info
apachesolr_user_get_users_to_index
apachesolr_user_index_users
apachesolr_user_profile_index_key
apachesolr_user_rebuild_index_table Truncate and rebuild the apachesolr_search_user table, reset the apachesolr_index_last variable. This is the most complete way to force reindexing, or to build the indexing table for the first time.
apachesolr_user_update_index This module will index users and there profile information in Solr. As this is a D6 module we are treating them like nodes
apachesolr_user_user
apachesolr_user_user_to_document Given a node ID, return a document representing that node.
apachesolr_user_user_update