Example #1
0
<?php

if (elgg_get_plugin_setting('reindex_running', 'elgg_solr')) {
    register_error(elgg_echo('elgg_solr:error:index_running'));
    forward(REFERER);
}
$is_elgg18 = elgg_solr_is_elgg18();
$starttime = get_input('starttime');
$endtime = get_input('endtime');
$type = get_input('type');
switch ($type) {
    case 'comments':
        // only applies to 1.8
        elgg_register_event_handler('shutdown', 'system', 'elgg_solr_comment_reindex');
        break;
    case '':
    case 'full':
        //vroomed
        elgg_register_event_handler('shutdown', 'system', 'elgg_solr_reindex');
        if ($is_elgg18) {
            elgg_register_event_handler('shutdown', 'system', 'elgg_solr_comment_reindex');
        }
        break;
    default:
        // set up options to use instead of all registered types
        $types = array($type => get_input('subtype', array()));
        elgg_set_config('elgg_solr_reindex_options', $types);
        elgg_register_event_handler('shutdown', 'system', 'elgg_solr_reindex');
        break;
}
if ($starttime && $endtime) {
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
/**
 *  Init elgg_solr plugin
 */
function elgg_solr_init()
{
    elgg_extend_view('css/admin', 'css/admin/elgg_solr');
    // if the plugin is not configured lets leave search alone
    if (!elgg_solr_has_settings()) {
        return true;
    }
    $is_elgg18 = elgg_solr_is_elgg18();
    elgg_register_library('Solarium', dirname(__FILE__) . '/vendor/autoload.php');
    elgg_register_library('elgg_solr:upgrades', dirname(__FILE__) . '/lib/upgrades.php');
    if (elgg_get_plugin_setting('use_solr', 'elgg_solr') != 'no') {
        // unregister default search hooks
        elgg_unregister_plugin_hook_handler('search', 'object', 'search_objects_hook');
        elgg_unregister_plugin_hook_handler('search', 'user', 'search_users_hook');
        elgg_unregister_plugin_hook_handler('search', 'group', 'search_groups_hook');
        elgg_unregister_plugin_hook_handler('search', 'tags', 'search_tags_hook');
        elgg_register_plugin_hook_handler('search', 'object:file', 'elgg_solr_file_search');
        elgg_register_plugin_hook_handler('search', 'object', 'elgg_solr_object_search');
        elgg_register_plugin_hook_handler('search', 'user', 'elgg_solr_user_search');
        elgg_register_plugin_hook_handler('search', 'group', 'elgg_solr_group_search');
        elgg_register_plugin_hook_handler('search', 'tags', 'elgg_solr_tag_search');
        if ($is_elgg18) {
            // this is elgg 1.8 need to handle comments as annotations
            elgg_unregister_plugin_hook_handler('search', 'comments', 'search_comments_hook');
            elgg_register_plugin_hook_handler('search', 'comments', 'elgg_solr_comment_search');
        }
    }
    elgg_register_plugin_hook_handler('cron', 'daily', 'elgg_solr_daily_cron');
    elgg_register_event_handler('create', 'all', 'elgg_solr_add_update_entity', 1000);
    elgg_register_event_handler('update', 'all', 'elgg_solr_add_update_entity', 1000);
    elgg_register_event_handler('delete', 'all', 'elgg_solr_delete_entity', 1000);
    elgg_register_event_handler('create', 'metadata', 'elgg_solr_metadata_update');
    elgg_register_event_handler('update', 'metadata', 'elgg_solr_metadata_update');
    elgg_register_event_handler('upgrade', 'system', 'elgg_solr_upgrades');
    elgg_register_event_handler('disable', 'all', 'elgg_solr_disable_entity');
    elgg_register_event_handler('enable', 'all', 'elgg_solr_enable_entity');
    elgg_register_event_handler('shutdown', 'system', 'elgg_solr_entities_sync');
    if ($is_elgg18) {
        elgg_register_event_handler('create', 'all', 'elgg_solr_add_update_annotation', 1000);
        elgg_register_event_handler('update', 'all', 'elgg_solr_add_update_annotation', 1000);
        elgg_register_event_handler('delete', 'annotations', 'elgg_solr_delete_annotation', 1000);
        elgg_register_event_handler('shutdown', 'system', 'elgg_solr_annotations_sync');
    }
    elgg_set_config('elgg_solr_sync', array());
    elgg_set_config('elgg_solr_delete', array());
    // when to update the user index
    elgg_register_plugin_hook_handler('usersettings:save', 'user', 'elgg_solr_user_settings_save', 1000);
    elgg_register_event_handler('profileupdate', 'user', 'elgg_solr_profile_update', 1000);
    // register functions for indexing
    elgg_solr_register_solr_entity_type('object', 'file', 'elgg_solr_add_update_file');
    elgg_solr_register_solr_entity_type('user', 'default', 'elgg_solr_add_update_user');
    elgg_solr_register_solr_entity_type('object', 'default', 'elgg_solr_add_update_object_default');
    elgg_solr_register_solr_entity_type('group', 'default', 'elgg_solr_add_update_group_default');
    elgg_register_action('elgg_solr/reindex', dirname(__FILE__) . '/actions/reindex.php', 'admin');
    elgg_register_action('elgg_solr/delete_index', dirname(__FILE__) . '/actions/delete_index.php', 'admin');
    elgg_register_action('elgg_solr/reindex_unlock', dirname(__FILE__) . '/actions/reindex_unlock.php', 'admin');
    elgg_register_action('elgg_solr/settings/save', dirname(__FILE__) . '/actions/plugin_settings.php', 'admin');
    elgg_register_action('elgg_solr/restart_reindex', dirname(__FILE__) . '/actions/restart_reindex.php', 'admin');
    elgg_register_action('elgg_solr/stop_reindex', dirname(__FILE__) . '/actions/stop_reindex.php', 'admin');
    elgg_register_admin_menu_item('administer', 'solr_index', 'administer_utilities');
    elgg_register_ajax_view('elgg_solr/ajax/progress');
}