Example #1
0
function elgg_solr_entities_sync()
{
    $access = access_get_show_hidden_status();
    access_show_hidden_entities(true);
    $guids = elgg_get_config('elgg_solr_sync');
    if ($guids) {
        $options = array('guids' => array_keys($guids), 'limit' => false);
        $batch_size = elgg_get_plugin_setting('reindex_batch_size', 'elgg_solr');
        $entities = new ElggBatch('elgg_get_entities', $options, null, $batch_size);
        foreach ($entities as $e) {
            elgg_solr_add_update_entity(null, null, $e);
        }
    }
    $delete_guids = elgg_get_config('elgg_solr_delete');
    if (is_array($delete_guids)) {
        foreach ($delete_guids as $g => $foo) {
            $client = elgg_solr_get_client();
            $query = $client->createUpdate();
            $query->addDeleteById($g);
            $query->addCommit();
            try {
                $client->update($query);
            } catch (Exception $ex) {
                //something went wrong, lets cache the id and try again on cron
                elgg_get_site_entity()->annotate('elgg_solr_delete_cache', $g, ACCESS_PUBLIC);
            }
        }
    }
    access_show_hidden_entities($access);
}
Example #2
0
function elgg_solr_reindex()
{
    set_time_limit(0);
    $is_elgg18 = elgg_solr_is_elgg18();
    $guid_getter = 'elgg_solr_get_entity_guids';
    if ($is_elgg18) {
        $guid_getter = 'elgg_solr_get_entity_guids_18';
    }
    $ia = elgg_set_ignore_access(true);
    $show_hidden = access_get_show_hidden_status();
    access_show_hidden_entities(true);
    // lock the function
    elgg_set_plugin_setting('reindex_running', 1, 'elgg_solr');
    if (!file_exists(elgg_get_config('dataroot') . 'elgg_solr')) {
        mkdir(elgg_get_config('dataroot') . 'elgg_solr');
    }
    $logtime = elgg_get_config('elgg_solr_restart_logtime');
    if (!$logtime) {
        $logtime = time();
    }
    $log = elgg_get_config('dataroot') . 'elgg_solr/' . $logtime . '.txt';
    elgg_set_plugin_setting('current_log', $logtime, 'elgg_solr');
    // initialize the csv
    $report = array('percent' => '', 'count' => 0, 'typecount' => 0, 'fullcount' => 0, 'type' => '', 'querytime' => 0, 'message' => 'Initializing Reindex', 'date' => date('Y-M-j H:i:s'), 'logtime' => $logtime);
    file_put_contents($log, json_encode($report) . "\n", FILE_APPEND);
    $debug = get_input('debug', false);
    if ($debug) {
        elgg_set_config('elgg_solr_debug', 1);
    }
    $registered_types = elgg_get_config('elgg_solr_reindex_options');
    if (!$registered_types) {
        $registered_types = get_registered_entity_types();
    }
    // build our options and cache them in case we need to restart it
    $cacheoptions = array('types' => $registered_types);
    $options = array();
    $time = elgg_get_config('elgg_solr_time_options');
    if ($time && is_array($time)) {
        $options['wheres'] = array("e.time_created >= {$time['starttime']}", "e.time_created <= {$time['endtime']}");
        $cacheoptions['starttime'] = $time['starttime'];
        $cacheoptions['endtime'] = $time['endtime'];
    }
    elgg_set_config('elgg_solr_nocommit', true);
    // tell our indexer not to commit right away
    $fullcount = 0;
    foreach ($registered_types as $type => $subtypes) {
        $options['type'] = $type;
        $options['limit'] = false;
        $restart_time = elgg_get_config('elgg_solr_restart_time');
        if ($restart_time) {
            elgg_set_config('elgg_solr_restart_time', false);
            $options['wheres'][1] = "e.time_created <= {$restart_time}";
        } elseif ($time['endtime']) {
            $options['wheres'][1] = "e.time_created <= {$time['endtime']}";
        }
        if ($subtypes) {
            if (!is_array($subtypes)) {
                $subtypes = array($subtypes);
            }
            $options['subtypes'] = $subtypes;
        }
        // this iteration fixes a bug https://github.com/Elgg/Elgg/issues/7561
        // uses a custom getter which only fetches the guids in a single large-batch query
        // which is much more efficient than standard egef
        $batch_size = elgg_get_plugin_setting('reindex_batch_size', 'elgg_solr');
        $entities = new ElggBatch($guid_getter, $options, null, $batch_size);
        $final_count = $guid_getter(array_merge($options, array('count' => true)));
        elgg_set_config('elgg_solr_nocommit', true);
        // disable committing on each entity for performance
        $count = 0;
        $fetch_time_start = microtime(true);
        foreach ($entities as $e) {
            $count++;
            $fullcount++;
            $first_entity = (bool) ($count % $batch_size == 1);
            $last_entity = (bool) ($count % $batch_size == 0);
            if ($first_entity) {
                // this is the first entity in the new batch
                $fetch_time = microtime(true) - $fetch_time_start;
                // the query time in seconds
            }
            $entity = get_entity($e->guid);
            if ($entity) {
                elgg_solr_add_update_entity(null, null, $entity);
                elgg_set_config('elgg_solr_nocommit', true);
            }
            if (!($count % 200)) {
                $qtime = round($fetch_time, 4);
                $percent = round($count / $final_count * 100);
                if ($entity) {
                    $restart_time = $entity->time_created;
                }
                $report = array('percent' => $percent, 'count' => $count, 'typecount' => $final_count, 'fullcount' => $fullcount, 'type' => $type, 'querytime' => $qtime, 'message' => '', 'date' => date('Y-M-j H:i:s'), 'cacheoptions' => $cacheoptions, 'logtime' => $logtime, 'restart_time' => $restart_time);
                file_put_contents($log, json_encode($report) . "\n", FILE_APPEND);
                elgg_set_config('elgg_solr_nocommit', false);
                // push a commit on this one
                // check for the termination signal
                if ($logtime == elgg_get_plugin_setting('stop_reindex', 'elgg_solr')) {
                    $report = array('percent' => $percent, 'count' => $count, 'typecount' => $final_count, 'fullcount' => $fullcount, 'type' => $type, 'querytime' => $qtime, 'message' => 'Reindex has been stopped', 'date' => date('Y-M-j H:i:s'), 'cacheoptions' => $cacheoptions, 'logtime' => $logtime, 'restart_time' => $restart_time);
                    file_put_contents($log, json_encode($report) . "\n", FILE_APPEND);
                    error_log('Stopping reindex due to termination signal');
                    exit;
                }
            }
            if ($last_entity) {
                $fetch_time_start = microtime(true);
            }
        }
        // we've finished this type, unset from the cache options
        unset($cacheoptions['types'][$type]);
    }
    $report = array('percent' => '', 'count' => 0, 'typecount' => 0, 'fullcount' => 0, 'type' => '', 'querytime' => 0, 'message' => 'Reindex complete', 'date' => date('Y-M-j H:i:s'), 'logtime' => $logtime);
    file_put_contents($log, json_encode($report) . "\n", FILE_APPEND);
    elgg_set_plugin_setting('reindex_running', 0, 'elgg_solr');
    // commit the last of the entities
    $client = elgg_solr_get_client();
    $query = $client->createUpdate();
    $query->addCommit();
    try {
        $client->update($query);
    } catch (Exception $e) {
        error_log($e->getMessage());
        return false;
    }
    access_show_hidden_entities($show_hidden);
    elgg_set_ignore_access($ia);
}
Example #3
0
function plugin_release_update($event, $type, $release)
{
    if (!elgg_instanceof($release, 'object', 'plugin_release')) {
        return true;
    }
    $project = $release->getProject();
    // reindex the project when the release is updated or created
    elgg_solr_add_update_entity('update', $project->type, $project);
}
Example #4
0
/**
 * Delete relationship event
 *
 * @todo: this fires before the relationship is actually deleted
 * but there's no :after event for relationships
 * add to core, or relationships deleted on shutdown will be incorrectly indexed
 *
 * @param string           $event        "delete"
 * @param string           $type         "relationship"
 * @param ElggRelationship $relationship Relationship
 * @return void
 */
function elgg_solr_relationship_delete($event, $type, $relationship)
{
    if ($GLOBALS['shutdown_flag']) {
        $entity1 = get_entity($relationship->guid_one);
        $entity2 = get_entity($relationship->guid_two);
        elgg_solr_add_update_entity(null, null, $entity1);
        elgg_solr_add_update_entity(null, null, $entity2);
    } else {
        elgg_solr_defer_index_update($relationship->guid_one);
        elgg_solr_defer_index_update($relationship->guid_two);
    }
}