You are here

class DevelController in Search API Solr 4.x

Same name and namespace in other branches
  1. 8.3 modules/search_api_solr_devel/src/Controller/DevelController.php \Drupal\search_api_solr_devel\Controller\DevelController
  2. 8.2 search_api_solr_devel/src/Controller/DevelController.php \Drupal\search_api_solr_devel\Controller\DevelController

Returns responses for devel module routes.

Hierarchy

Expanded class hierarchy of DevelController

File

modules/search_api_solr_devel/src/Controller/DevelController.php, line 24

Namespace

Drupal\search_api_solr_devel\Controller
View source
class DevelController extends ControllerBase {

  /**
   * The server storage controller.
   *
   * @var \Drupal\Core\Entity\EntityStorageInterface
   */
  protected $storage;

  /**
   * The backend plugin manager.
   *
   * @var \Drupal\search_api\Backend\BackendPluginManager
   */
  protected $backendPluginManager;

  /**
   * The Devel dumper manager.
   *
   * @var \Drupal\devel\DevelDumperManagerInterface
   */
  protected $develDumperManager;

  /**
   * The fields helper.
   *
   * @var \Drupal\search_api\Utility\FieldsHelperInterface
   */
  protected $fieldsHelper;

  /**
   * The module handler service.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * The date formatter service.
   *
   * @var \Drupal\Core\Datetime\DateFormatterInterface
   */
  protected $dateFormatter;

  /**
   * The time service.
   *
   * @var \Drupal\Component\Datetime\TimeInterface
   */
  protected $time;

  /**
   * Constructs a DevelController object.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\search_api\Backend\BackendPluginManager $backend_plugin_manager
   *   The backend plugin manager.
   * @param \Drupal\devel\DevelDumperManagerInterface $devel_dumper_manager
   *   The Devel dumper manager.
   * @param \Drupal\search_api\Utility\FieldsHelperInterface $fields_helper
   *   The Search API Fields Helper.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler.
   * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
   *   The date formatter service.
   * @param \Drupal\Component\Datetime\TimeInterface $time
   *   The time service.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, BackendPluginManager $backend_plugin_manager, DevelDumperManagerInterface $devel_dumper_manager, FieldsHelperInterface $fields_helper, ModuleHandlerInterface $module_handler, DateFormatterInterface $date_formatter, TimeInterface $time) {
    $this->storage = $entity_type_manager
      ->getStorage('search_api_server');
    $this->backendPluginManager = $backend_plugin_manager;
    $this->develDumperManager = $devel_dumper_manager;
    $this->fieldsHelper = $fields_helper;
    $this->moduleHandler = $module_handler;
    $this->dateFormatter = $date_formatter;
    $this->time = $time;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('entity_type.manager'), $container
      ->get('plugin.manager.search_api.backend'), $container
      ->get('devel.dumper'), $container
      ->get('search_api.fields_helper'), $container
      ->get('module_handler'), $container
      ->get('date.formatter'), $container
      ->get('datetime.time'));
  }

  /**
   * Returns all available Solr backend plugins.
   *
   * @return string[]
   *   An associative array mapping backend plugin IDs to their (HTML-escaped)
   *   labels.
   */
  protected function getBackends() {
    $backends = [];
    $plugin_definitions = $this->backendPluginManager
      ->getDefinitions();
    foreach ($plugin_definitions as $plugin_id => $plugin_definition) {
      if (is_a($plugin_definition['class'], $plugin_definitions['search_api_solr']['class'], TRUE)) {
        $backends[] = $plugin_id;
      }
    }
    return $backends;
  }

  /**
   * Prints the document structure to be indexed by Solr.
   *
   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
   *   A RouteMatch object.
   *
   * @return array
   *   Array of page elements to render.
   */
  public function entitySolr(RouteMatchInterface $route_match) {
    $output_details = [];
    $table_rows = [];
    $num = 0;
    $parameter_name = $route_match
      ->getRouteObject()
      ->getOption('_devel_entity_type_id');
    $entity = $route_match
      ->getParameter($parameter_name);
    if ($entity && $entity instanceof EntityInterface) {
      foreach ($this
        ->getBackends() as $backend_id) {

        /** @var \Drupal\search_api\ServerInterface[] $servers */
        $servers = $this->storage
          ->loadByProperties([
          'backend' => $backend_id,
          'status' => TRUE,
        ]);
        foreach ($servers as $server) {

          /** @var \Drupal\search_api\ServerInterface $server */

          /** @var \Drupal\search_api_solr\SolrBackendInterface $backend */
          $backend = $server
            ->getBackend();

          /** @var \Drupal\search_api\IndexInterface[] $indexes */
          $indexes = $server
            ->getIndexes();
          $solr = $backend
            ->getSolrConnector();
          foreach ($indexes as $index) {
            if ($index
              ->status()) {
              foreach ($index
                ->getDatasourceIds() as $datasource_id) {
                list(, $entity_type) = Utility::splitPropertyPath($datasource_id);
                if ($entity
                  ->getEntityTypeId() == $entity_type) {
                  foreach (array_keys($entity
                    ->getTranslationLanguages()) as $langcode) {

                    // @todo improve that ID generation?
                    $item_id = $datasource_id . '/' . $entity
                      ->id() . ':' . $langcode;
                    $items = [];
                    $base_summary_row = $this
                      ->getBaseRow($server, $index, $datasource_id, $entity, $langcode, $item_id);

                    // @TODO: Run a timer on this process and report it?
                    $items[$item_id] = $this->fieldsHelper
                      ->createItemFromObject($index, $entity
                      ->getTranslation($langcode)
                      ->getTypedData(), $item_id);

                    // Alter and preprocess the items to indexed.
                    $index
                      ->alterIndexedItems($items);
                    $this->moduleHandler
                      ->alter('search_api_index_items', $index, $items);
                    $index
                      ->preprocessIndexItems($items);

                    // Gather documents generated by Search API.
                    $documents = $backend
                      ->getDocuments($index, $items);
                    foreach ($documents as $document) {
                      $summary_row = $base_summary_row;
                      $summary_row['num'] = $num + 1;
                      $fields = $document
                        ->getFields();
                      $summary_row['object_size'] = format_size(strlen(json_encode($fields)));
                      ksort($fields);
                      $details_id = $fields['id'];
                      $output_details[$details_id] = [
                        '#type' => 'details',
                        '#title' => $this
                          ->t('Row #@num: local and Solr indexing data', [
                          '@num' => $num + 1,
                        ]),
                      ];
                      $output_details[$details_id][] = [
                        '#markup' => '<h3>' . $this
                          ->t('Locally-generated data that would be sent to Solr during indexing:') . '</h3>',
                      ];
                      $output_details[$details_id][] = [
                        '#markup' => $this->develDumperManager
                          ->dumpOrExport($fields, $this
                          ->t('Locally-generated data'), TRUE),
                      ];

                      // Show current data for this item from the Solr backend.

                      /** @var \Drupal\search_api_solr\SolrConnectorInterface $solr */

                      /** @var \Solarium\QueryType\Select\Query\Query $query */
                      $output_details[$details_id][] = [
                        '#markup' => '<h3>' . $this
                          ->t('Current data in Solr for this item:') . '</h3>',
                      ];
                      $summary_row['solr_id'] = $fields['id'];
                      $query = $solr
                        ->getSelectQuery();
                      $query
                        ->setQuery('id:"' . $fields['id'] . '"');
                      $query
                        ->setFields('*');
                      try {

                        // @TODO: Run a timer on this process and report it?
                        $results = $solr
                          ->execute($query, $backend
                          ->getCollectionEndpoint($index));
                        $num_found = $results
                          ->getNumFound();
                        $summary_row['solr_exists'] = $this
                          ->t('yes');
                      } catch (\Exception $e) {
                        $results = [];
                        $num_found = -1;
                        $output_details[$details_id][] = [
                          '#markup' => $this
                            ->t('Error querying the Solr server!') . '<pre>' . $e
                            ->getMessage() . '</pre>',
                        ];
                        $summary_row['solr_exists'] = $this
                          ->t('error');
                      }

                      // If no item found in Solr, report it.
                      if ($num_found == 0) {
                        $summary_row['solr_exists'] = $this
                          ->t('no');
                        $output_details[$details_id][] = [
                          '#markup' => $this
                            ->t('No Solr document found with the ID %id', [
                            '%id' => $fields['id'],
                          ]),
                        ];
                      }
                      if ($num_found == 1) {

                        // Show Solr documents for this item.
                        $solr_documents = $results
                          ->getDocuments();
                        $fields = $solr_documents[0]
                          ->getFields();
                        $summary_row['solr_size'] = format_size(strlen(json_encode($fields)));
                        if (!empty($fields['timestamp'])) {
                          $summary_row['solr_changed'] = $this
                            ->showTimeAndTimeAgo(strtotime($fields['timestamp']));
                        }
                        ksort($fields);
                        $output_details[$details_id][] = [
                          '#markup' => $this->develDumperManager
                            ->dumpOrExport($fields, $this
                            ->t('Solr data'), TRUE),
                        ];
                      }
                      $table_rows[$num++] = $summary_row;
                    }
                  }
                }
              }
            }
          }
        }
      }
    }

    // Message for no output.
    if (empty($output_details)) {
      return [
        '#markup' => $this
          ->t('No enabled indexes with a Solr backend that contain this item were found.'),
      ];
    }
    return array_merge([
      '#title' => $this
        ->t('Search API Solr devel status'),
      'header' => [
        '#markup' => $this
          ->t('This page shows Search API and Solr indexing data for the current entity.'),
      ],
      'summary_table' => [
        '#type' => 'table',
        '#prefix' => '<h3>' . $this
          ->t('Summary') . '</h3><p>' . $this
          ->t("Each row in the table represents an item generated from this entity according to the Search API index configuration (data sources, fields, processors, hook implementations, etc.). The first columns correspond to the Search API tracking information (kept in the site's database), and the rest of the columns show information about the corresponding Solr document for each item. See the <a href=\"https://www.drupal.org/docs/8/modules/search-api/developer-documentation\">developer documentation</a>.") . '</p>',
        '#header' => $this
          ->summaryTableHeader(),
        '#rows' => $table_rows,
      ],
    ], $output_details);
  }

  /**
   * Given a timestamp it returns both a human-readable date + "time ago".
   *
   * @param int $timestamp
   *   A UNIX timestamp to display.
   *
   * @return \Drupal\Core\StringTranslation\TranslatableMarkup
   *   The formatted timestamp.
   */
  protected function showTimeAndTimeAgo($timestamp) {
    return $this
      ->t('%time (%time_ago ago)', [
      '%time' => $this->dateFormatter
        ->format($timestamp),
      '%time_ago' => $this->dateFormatter
        ->formatDiff($timestamp, $this->time
        ->getRequestTime()),
    ]);
  }

  /**
   * Returns header for summary table.
   *
   * @return string[]
   *   An array of table headers.
   */
  protected function summaryTableHeader() {
    return [
      'num' => '#',
      'server' => $this
        ->t('Search API server'),
      'index' => $this
        ->t('Search API index'),
      'id' => $this
        ->t('Item Datasource & ID'),
      'langcode' => $this
        ->t('Item langcode'),
      'tracked' => $this
        ->t('Is item tracked in SAPI Index?'),
      'changed' => $this
        ->t('Last time item was marked to be indexed'),
      'status' => $this
        ->t('Has item been sent to Solr?'),
      'object_size' => $this
        ->t('Local: item size'),
      'solr_id' => $this
        ->t('Solr: document ID'),
      'solr_exists' => $this
        ->t('Solr: document exists?'),
      'solr_changed' => $this
        ->t('Solr: time indexed'),
      'solr_size' => $this
        ->t('Solr: document size'),
    ];
  }

  /**
   * Return a base row for the summary table, which will be modified later on.
   *
   * @param \Drupal\search_api\ServerInterface $server
   *   The Search API server entity.
   * @param \Drupal\search_api\IndexInterface $index
   *   The Search API index entity.
   * @param string $datasource_id
   *   The ID of the datasource.
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   The entity for which to return the base row.
   * @param string $langcode
   *   The language code.
   * @param string $item_id
   *   The internal item ID of the entity.
   *
   * @return string[]
   *   An associative array of strings for the base row.
   */
  protected function getBaseRow(ServerInterface $server, IndexInterface $index, $datasource_id, EntityInterface $entity, $langcode, $item_id) {

    // Build table row.
    $base_row = [
      'num' => 0,
      'server' => $this
        ->t('<a href=":url">@id</a>', [
        '@id' => $server
          ->id(),
        ':url' => Url::fromRoute('entity.search_api_server.canonical', [
          'search_api_server' => $server
            ->id(),
        ])
          ->toString(),
      ]),
      'index' => $this
        ->t('<a href=":index_url">@index_id</a><br />(%read_write_mode mode)', [
        '@index_id' => $index
          ->id(),
        ':index_url' => Url::fromRoute('entity.search_api_index.canonical', [
          'search_api_index' => $index
            ->id(),
        ])
          ->toString(),
        '%read_write_mode' => $index
          ->isReadOnly() ? $this
          ->t('read-only') : $this
          ->t('read-write'),
      ]),
      'id' => $datasource_id . '/' . $entity
        ->id(),
      'langcode' => $langcode,
      'tracked' => '',
      'changed' => '-',
      'status' => '-',
      'object_size' => '-',
      'solr_id' => '-',
      'solr_exists' => '',
      'solr_changed' => '-',
      'solr_size' => '-',
    ];

    // Fetch tracker information.
    $tracker = $index
      ->getTrackerInstance();
    $select = $tracker
      ->getDatabaseConnection()
      ->select('search_api_item', 'sai');
    $select
      ->condition('index_id', $index
      ->id());
    $select
      ->condition('datasource', $datasource_id);
    $select
      ->condition('item_id', $item_id);
    $select
      ->fields('sai', [
      'item_id',
      'status',
      'changed',
    ]);
    $tracker_data = $select
      ->execute()
      ->fetch();

    // Add tracker information to row.
    if ($tracker_data) {
      $base_row['tracked'] = $this
        ->t('yes');
      $base_row['changed'] = $this
        ->showTimeAndTimeAgo($tracker_data->changed);
      $base_row['status'] = $tracker_data->status ? $this
        ->t('no') : $this
        ->t('yes');
    }
    else {
      $base_row['tracked'] = $this
        ->t('no');
    }
    return $base_row;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ControllerBase::$configFactory protected property The configuration factory.
ControllerBase::$currentUser protected property The current user service. 1
ControllerBase::$entityFormBuilder protected property The entity form builder.
ControllerBase::$entityTypeManager protected property The entity type manager.
ControllerBase::$formBuilder protected property The form builder. 2
ControllerBase::$keyValue protected property The key-value storage. 1
ControllerBase::$languageManager protected property The language manager. 1
ControllerBase::$stateService protected property The state service.
ControllerBase::cache protected function Returns the requested cache bin.
ControllerBase::config protected function Retrieves a configuration object.
ControllerBase::container private function Returns the service container.
ControllerBase::currentUser protected function Returns the current user. 1
ControllerBase::entityFormBuilder protected function Retrieves the entity form builder.
ControllerBase::entityTypeManager protected function Retrieves the entity type manager.
ControllerBase::formBuilder protected function Returns the form builder service. 2
ControllerBase::keyValue protected function Returns a key/value storage collection. 1
ControllerBase::languageManager protected function Returns the language manager service. 1
ControllerBase::moduleHandler protected function Returns the module handler. 2
ControllerBase::redirect protected function Returns a redirect response object for the specified route.
ControllerBase::state protected function Returns the state storage service.
DevelController::$backendPluginManager protected property The backend plugin manager.
DevelController::$dateFormatter protected property The date formatter service.
DevelController::$develDumperManager protected property The Devel dumper manager.
DevelController::$fieldsHelper protected property The fields helper.
DevelController::$moduleHandler protected property The module handler service. Overrides ControllerBase::$moduleHandler
DevelController::$storage protected property The server storage controller.
DevelController::$time protected property The time service.
DevelController::create public static function Instantiates a new instance of this class. Overrides ControllerBase::create
DevelController::entitySolr public function Prints the document structure to be indexed by Solr.
DevelController::getBackends protected function Returns all available Solr backend plugins.
DevelController::getBaseRow protected function Return a base row for the summary table, which will be modified later on.
DevelController::showTimeAndTimeAgo protected function Given a timestamp it returns both a human-readable date + "time ago".
DevelController::summaryTableHeader protected function Returns header for summary table.
DevelController::__construct public function Constructs a DevelController object.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 27
MessengerTrait::messenger public function Gets the messenger. 27
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.