You are here

public function GarbageCollector::processItem in Simple XML sitemap (Views integration) 8

Works on a single queue item.

Parameters

mixed $data: The data that was passed to \Drupal\Core\Queue\QueueInterface::createItem() when the item was queued.

Throws

\Drupal\Core\Queue\RequeueException Processing is not yet finished. This will allow another process to claim the item immediately.

\Exception A QueueWorker plugin may throw an exception to indicate there was a problem. The cron process will log the exception, and leave the item in the queue to be processed again later.

\Drupal\Core\Queue\SuspendQueueException More specifically, a SuspendQueueException should be thrown when a QueueWorker plugin is aware that the problem will affect all subsequent workers of its queue. For example, a callback that makes HTTP requests may find that the remote server is not responding. The cron process will behave as with a normal Exception, and in addition will not attempt to process further items from the current item's queue during the current cron run.

Overrides QueueWorkerInterface::processItem

See also

\Drupal\Core\Cron::processQueues()

File

src/Plugin/QueueWorker/GarbageCollector.php, line 78
Contains queue worker for garbage collection.

Class

GarbageCollector
Executes garbage collection in the simple_sitemap_views table.

Namespace

Drupal\simple_sitemap_views\Plugin\QueueWorker

Code

public function processItem($data) {
  $view_id = $data['view_id'];

  /** @var \Drupal\views\ViewEntityInterface $view_entity */
  $view_entity = $this->viewStorage
    ->load($view_id);
  $display_ids = [];

  // Check that the view exists and it is enabled.
  if ($view_entity && $view_entity
    ->status()) {
    $view = $view_entity
      ->getExecutable();
    $displays = array_filter($view_entity
      ->get('display'), [
      $this->simpleSitemapViews,
      'isValidDisplay',
    ]);
    foreach ($displays as $display_id => $display) {

      // Ensure the display was correctly set.
      if (!$view
        ->setDisplay($display_id)) {
        $view
          ->destroy();
        continue;
      }

      // Check that the display is enabled and has indexable arguments.
      if ($view->display_handler
        ->isEnabled() && ($args_ids = $this->simpleSitemapViews
        ->getIndexableArguments($view))) {
        $display_ids[] = $display_id;

        // Delete records about sets of arguments that are no longer indexed.
        $args_ids = $this->simpleSitemapViews
          ->getArgumentsStringVariations($args_ids);
        $condition = new Condition('AND');
        $condition
          ->condition('view_id', $view_id);
        $condition
          ->condition('display_id', $display_id);
        $condition
          ->condition('arguments_ids', $args_ids, 'NOT IN');
        $this->simpleSitemapViews
          ->removeArgumentsFromIndex($condition);

        // Check if the records limit for display is exceeded.
        $settings = $this->simpleSitemapViews
          ->getSitemapSettings($view);
        $max_links = is_numeric($settings['max_links']) ? $settings['max_links'] : 0;
        if ($max_links > 0) {
          $condition = new Condition('AND');
          $condition
            ->condition('view_id', $view_id);
          $condition
            ->condition('display_id', $display_id);

          // Delete records that exceed the limit.
          if ($index_id = $this->simpleSitemapViews
            ->getIndexIdByPosition($max_links, $condition)) {
            $condition
              ->condition('id', $index_id, '>');
            $this->simpleSitemapViews
              ->removeArgumentsFromIndex($condition);
          }
        }
      }
    }

    // Delete records about view displays that do not exist or are disabled.
    if (!empty($display_ids)) {
      $condition = new Condition('AND');
      $condition
        ->condition('view_id', $view_id);
      $condition
        ->condition('display_id', $display_ids, 'NOT IN');
      $this->simpleSitemapViews
        ->removeArgumentsFromIndex($condition);
    }

    // Destroy a view instance.
    $view
      ->destroy();
  }

  // Delete records about the view, if it does not exist, is disabled or it
  // does not have a display whose arguments are indexed.
  if (empty($display_ids)) {
    $condition = new Condition('AND');
    $condition
      ->condition('view_id', $view_id);
    $this->simpleSitemapViews
      ->removeArgumentsFromIndex($condition);
  }
}