Back to FinderIndexCommand class

Method index

private void
index
()
Run the indexer.
Returns
  • void
Since
  • 2.5

Method index - Source code

/**
 * Run the indexer.
 *
 * @return  void
 *
 * @since   2.5
 */
private function index()
{
    // Disable caching.
    $app = $this->getApplication();
    $app->set('caching', 0);
    $app->set('cache_handler', 'file');
    // Reset the indexer state.
    Indexer::resetState();
    // Import the plugins.
    PluginHelper::importPlugin('system');
    PluginHelper::importPlugin('finder');
    // Starting Indexer.
    $this->ioStyle->text(Text::_('FINDER_CLI_STARTING_INDEXER'));
    // Trigger the onStartIndex event.
    $app->triggerEvent('onStartIndex');
    // Remove the script time limit.
    @set_time_limit(0);
    // Get the indexer state.
    $state = Indexer::getState();
    // Setting up plugins.
    $this->ioStyle->text(Text::_('FINDER_CLI_SETTING_UP_PLUGINS'));
    // Trigger the onBeforeIndex event.
    $app->triggerEvent('onBeforeIndex');
    // Startup reporting.
    $this->ioStyle->text(Text::sprintf('FINDER_CLI_SETUP_ITEMS', $state->totalItems, round(microtime(true) - $this->time, 3)));
    // Get the number of batches.
    $t = (int) $state->totalItems;
    $c = (int) ceil($t / $state->batchSize);
    $c = $c === 0 ? 1 : $c;
    try {
        // Process the batches.
        for ($i = 0; $i < $c; $i++) {
            // Set the batch start time.
            $this->qtime = microtime(true);
            // Reset the batch offset.
            $state->batchOffset = 0;
            // Trigger the onBuildIndex event.
            Factory::getApplication()->triggerEvent('onBuildIndex');
            // Batch reporting.
            $text = Text::sprintf('FINDER_CLI_BATCH_COMPLETE', $i + 1, $processingTime = round(microtime(true) - $this->qtime, 3));
            $this->ioStyle->text($text);
            if ($this->pause !== 0) {
                // Pausing Section
                $skip = !($processingTime >= $this->minimumBatchProcessingTime);
                $pause = 0;
                if ($this->pause === 'division' && $this->divisor > 0) {
                    if (!$skip) {
                        $pause = round($processingTime / $this->divisor);
                    } else {
                        $pause = 1;
                    }
                } elseif ($this->pause > 0) {
                    $pause = $this->pause;
                }
                if ($pause > 0 && !$skip) {
                    $this->ioStyle->text(Text::sprintf('FINDER_CLI_BATCH_PAUSING', $pause));
                    sleep($pause);
                    $this->ioStyle->text(Text::_('FINDER_CLI_BATCH_CONTINUING'));
                }
                if ($skip) {
                    $this->ioStyle->text(Text::sprintf('FINDER_CLI_SKIPPING_PAUSE_LOW_BATCH_PROCESSING_TIME', $processingTime, $this->minimumBatchProcessingTime));
                }
                // End of Pausing Section
            }
        }
    } catch (Exception $e) {
        // Display the error
        $this->ioStyle->error($e->getMessage());
        // Reset the indexer state.
        Indexer::resetState();
        // Close the app
        $app->close($e->getCode());
    }
    // Reset the indexer state.
    Indexer::resetState();
}