You are here

class GarbageCollector 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

Executes garbage collection in the simple_sitemap_views table.

Plugin annotation


@QueueWorker(
  id = "simple_sitemap.views.garbage_collector",
  title = @Translation("Garbage collection in the simple_sitemap_views table"),
  cron = {"time" = 30}
)

Hierarchy

Expanded class hierarchy of GarbageCollector

File

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

Namespace

Drupal\simple_sitemap_views\Plugin\QueueWorker
View source
class GarbageCollector extends QueueWorkerBase implements ContainerFactoryPluginInterface {

  /**
   * View entities storage.
   *
   * @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface
   */
  protected $viewStorage;

  /**
   * Views sitemap data.
   *
   * @var \Drupal\simple_sitemap_views\SimpleSitemapViews
   */
  protected $sitemapViews;

  /**
   * GarbageCollector constructor.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   Entity type manager.
   * @param \Drupal\simple_sitemap_views\SimpleSitemapViews $sitemap_views
   *   Views sitemap data.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, SimpleSitemapViews $sitemap_views) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->viewStorage = $entity_type_manager
      ->getStorage('view');
    $this->sitemapViews = $sitemap_views;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('entity_type.manager'), $container
      ->get('simple_sitemap.views'));
  }

  /**
   * {@inheritdoc}
   */
  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);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
GarbageCollector::$sitemapViews protected property Views sitemap data.
GarbageCollector::$viewStorage protected property View entities storage.
GarbageCollector::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
GarbageCollector::processItem public function Works on a single queue item. Overrides QueueWorkerInterface::processItem
GarbageCollector::__construct public function GarbageCollector constructor. Overrides PluginBase::__construct
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.