Esempio n. 1
0
function elgg_solr_add_update_user($entity)
{
    if (!elgg_instanceof($entity, 'user')) {
        return false;
    }
    if (!is_registered_entity_type($entity->type, $entity->getSubtype())) {
        return false;
    }
    // lump public profile fields in with description
    $profile_fields = elgg_get_config('profile_fields');
    $desc = '';
    if (is_array($profile_fields) && sizeof($profile_fields) > 0) {
        $walled = elgg_get_config('walled_garden');
        foreach ($profile_fields as $shortname => $valtype) {
            $md = elgg_get_metadata(array('guid' => $entity->guid, 'metadata_names' => array($shortname)));
            foreach ($md as $m) {
                if ($m->access_id == ACCESS_PUBLIC || $walled && $m->access_id == ACCESS_LOGGED_IN) {
                    $desc .= $m->value . ' ';
                }
            }
        }
    }
    $client = elgg_solr_get_client();
    $commit = elgg_get_config('elgg_solr_nocommit') ? false : true;
    $query = $client->createUpdate();
    $subtype = $entity->getSubtype() ? $entity->getSubtype() : '';
    // add document
    $doc = $query->createDocument();
    $doc->id = $entity->guid;
    $doc->type = $entity->type;
    $doc->subtype = $subtype;
    $doc->owner_guid = $entity->owner_guid;
    $doc->container_guid = $entity->container_guid;
    $doc->access_id = $entity->access_id;
    $doc->title = elgg_strip_tags($entity->title);
    $doc->name = elgg_strip_tags($entity->name);
    $doc->username = $entity->username;
    $doc->description = elgg_strip_tags($desc);
    $doc->time_created = $entity->time_created;
    $doc = elgg_solr_add_tags($doc, $entity);
    $doc->enabled = $entity->enabled;
    $params = array('entity' => $entity);
    $doc = elgg_trigger_plugin_hook('elgg_solr:index', $entity->type, $params, $doc);
    if (!$doc) {
        return true;
        // a plugin has stopped the index
    }
    $query->addDocument($doc, true);
    if ($commit) {
        $query->addCommit();
    }
    // this executes the query and returns the result
    try {
        $client->update($query);
    } catch (Exception $exc) {
        error_log($exc->getMessage());
    }
    return true;
}
Esempio n. 2
0
/**
 * Populates entity doc with default index fields
 *
 * @param DocumentInterface $doc    Solr document
 * @param ElggEntity        $entity Elgg entity
 * @return DocumentInterface
 */
function elgg_solr_prepare_entity_doc(DocumentInterface $doc, ElggEntity $entity)
{
    $doc->id = $entity->guid;
    $doc->type = $entity->type;
    $doc->subtype = (string) $entity->getSubtype();
    $doc->owner_guid = $entity->owner_guid;
    $doc->container_guid = $entity->container_guid;
    $doc->access_id = $entity->access_id;
    $doc->title = elgg_strip_tags($entity->title);
    $doc->name = elgg_strip_tags($entity->name);
    $doc->description = elgg_strip_tags($entity->description);
    $doc->time_created = $entity->time_created;
    $doc->time_updated_i = $entity->time_updated;
    $doc = elgg_solr_add_tags($doc, $entity);
    $doc->enabled = $entity->enabled;
    if (is_callable([$entity, 'hasIcon'])) {
        $doc->has_icon_b = $entity->hasIcon('small');
    } else {
        $doc->has_icon_b = (bool) $entity->icontime;
    }
    if ($entity instanceof ElggFile) {
        $mimetype = (string) $entity->getMimeType();
        $doc->simpletype_s = (string) elgg_get_file_simple_type($mimetype);
        $doc->mimetype_s = $mimetype;
        $doc->originalfilename_s = $entity->originalfilename;
        if ($entity->exists()) {
            $doc->exists_b = true;
            $doc->filestorename_s = $entity->getFilenameOnFilestore();
            $doc->filesize_i = (int) $entity->getSize();
        } else {
            $doc->exists_b = false;
        }
    }
    // Store comment/reply thread information to allow grouping
    if ($entity instanceof ElggComment) {
        $container = $entity->getContainerEntity();
        while ($container instanceof ElggComment) {
            $container = $container->getContainerEntity();
        }
        $doc->responses_thread_i = $container->guid;
    } else {
        $doc->responses_thread_i = $entity->guid;
    }
    // Store comment/reply guids
    $responses = [];
    $responses_batch = new ElggBatch('elgg_get_entities', ['types' => 'object', 'subtypes' => ['comment', 'discussion_reply'], 'container_guid' => $entity->guid, 'limit' => 0, 'callback' => false]);
    foreach ($responses_batch as $response) {
        $responses[] = $response->guid;
    }
    $doc->responses_is = $responses;
    $doc->responses_count_i = count($responses);
    $doc->likes_i = $entity->countAnnotations('likes');
    $params = array('entity' => $entity);
    $doc = elgg_trigger_plugin_hook('elgg_solr:index', $entity->type, $params, $doc);
    if ($entity->getSubtype()) {
        $doc = elgg_trigger_plugin_hook('elgg_solr:index', "{$entity->type}:{$entity->getSubtype()}", $params, $doc);
    }
    return $doc;
}