View source
<?php
function apachesolr_user_update_index() {
$cron_limit = 5;
$rows = apachesolr_user_get_users_to_index('apachesolr_user', $cron_limit);
apachesolr_user_index_users($rows, 'apachesolr_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,
);
$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) {
return FALSE;
}
try {
$solr = apachesolr_get_solr();
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);
$position['last_change'] = $row->changed;
$position['last_nid'] = $row->nid;
} catch (Exception $e) {
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),
));
$docs_chunk = array_chunk($documents, 20);
foreach ($docs_chunk as $docs) {
$solr
->addDocuments($docs);
}
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;
}
}
if ($namespace && $position != $old_position) {
$stored = variable_get('apachesolr_index_last', array());
$stored[$namespace] = $position;
variable_set('apachesolr_index_last', $stored);
}
return $position;
}
function apachesolr_user_rebuild_index_table() {
db_query("DELETE FROM {apachesolr_search_user} WHERE uid IN (SELECT uid FROM {users})");
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;
}
}
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;
$build_document = TRUE;
if ($build_document) {
$node->body = $user->name;
$node->title = $user->name;
$text = $node->name;
$profile = array();
foreach ($user as $key => $value) {
if (stristr($key, 'profile_')) {
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;
$document->{'ss_picture'} = $user->picute;
$document->{'ts_signature'} = $user->signature;
$document->url = url($path, array(
'absolute' => TRUE,
));
$document->path = $path;
if (function_exists('drupal_get_path_alias')) {
$language = empty($node->language) ? '' : $node->language;
$output = drupal_get_path_alias($path, $language);
if ($output && $output != $path) {
$document->path_alias = $output;
}
}
foreach ($profile as $key => $value) {
$profile_info = false;
$profile_info = apachesolr_user_get_profile_info($key);
if ($profile_info['visibility'] > 1 && !empty($value)) {
$index_key = apachesolr_user_profile_index_key($profile_info);
if ($profile_info['type'] == 'date') {
$value = strtotime($value);
$document->{$index_key} = apachesolr_date_iso($value);
}
else {
$document->{$index_key} = apachesolr_clean_text($value);
}
}
}
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);
$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'];
}