You are here

public function GarbageCollector::processItem in Simple XML sitemap 8.3

Same name and namespace in other branches
  1. 4.x modules/simple_sitemap_views/src/Plugin/QueueWorker/GarbageCollector.php \Drupal\simple_sitemap_views\Plugin\QueueWorker\GarbageCollector::processItem()

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

modules/simple_sitemap_views/src/Plugin/QueueWorker/GarbageCollector.php, line 73

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();
    foreach ($this->sitemapViews
      ->getRouterDisplayIds($view_entity) as $display_id) {

      // Ensure the display was correctly set.
      // Check that the display is enabled.
      if (!$view
        ->setDisplay($display_id) || !$view->display_handler
        ->isEnabled()) {
        continue;
      }
      $variants = $this->sitemapViews
        ->getIndexableVariants($view);
      $variants = array_keys($variants);
      $args_ids = [];
      foreach ($variants as $variant) {
        $variant_args_ids = $this->sitemapViews
          ->getIndexableArguments($view, $variant);
        if (count($variant_args_ids) > count($args_ids)) {
          $args_ids = $variant_args_ids;
        }
      }

      // Check that the display has indexable arguments.
      if (empty($args_ids)) {
        continue;
      }
      $display_ids[] = $display_id;

      // Delete records about sets of arguments that are no longer indexed.
      $args_ids = $this->sitemapViews
        ->getArgumentsStringVariations($args_ids);
      $condition = Database::getConnection()
        ->condition('AND');
      $condition
        ->condition('view_id', $view_id);
      $condition
        ->condition('display_id', $display_id);
      $condition
        ->condition('arguments_ids', $args_ids, 'NOT IN');
      $this->sitemapViews
        ->removeArgumentsFromIndex($condition);
      $max_links = 0;
      foreach ($variants as $variant) {
        $settings = $this->sitemapViews
          ->getSitemapSettings($view, $variant);
        $variant_max_links = is_numeric($settings['max_links']) ? $settings['max_links'] : 0;
        if ($variant_max_links == 0) {
          $max_links = 0;
          break;
        }
        elseif ($variant_max_links > $max_links) {
          $max_links = $variant_max_links;
        }
      }

      // Check if the records limit for display is exceeded.
      if ($max_links > 0) {
        $condition = Database::getConnection()
          ->condition('AND');
        $condition
          ->condition('view_id', $view_id);
        $condition
          ->condition('display_id', $display_id);

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

    // Delete records about view displays that do not exist or are disabled.
    if (!empty($display_ids)) {
      $condition = Database::getConnection()
        ->condition('AND');
      $condition
        ->condition('view_id', $view_id);
      $condition
        ->condition('display_id', $display_ids, 'NOT IN');
      $this->sitemapViews
        ->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 = Database::getConnection()
      ->condition('AND');
    $condition
      ->condition('view_id', $view_id);
    $this->sitemapViews
      ->removeArgumentsFromIndex($condition);
  }
}