You are here

class LinkCheckerBatch in Link checker 8

Helper service to handle links checking.

Hierarchy

Expanded class hierarchy of LinkCheckerBatch

1 file declares its use of LinkCheckerBatch
LinkCheckerCommands.php in src/Commands/LinkCheckerCommands.php
1 string reference to 'LinkCheckerBatch'
linkchecker.services.yml in ./linkchecker.services.yml
linkchecker.services.yml
1 service uses LinkCheckerBatch
linkchecker.checker_batch in ./linkchecker.services.yml
Drupal\linkchecker\LinkCheckerBatch

File

src/LinkCheckerBatch.php, line 18

Namespace

Drupal\linkchecker
View source
class LinkCheckerBatch {
  use DependencySerializationTrait;
  use MessengerTrait;
  use StringTranslationTrait;

  /**
   * The link checker.
   *
   * @var \Drupal\linkchecker\LinkCheckerService
   */
  protected $checker;

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The lock.
   *
   * @var \Drupal\Core\Lock\LockBackendInterface
   */
  protected $lock;

  /**
   * The queue factory.
   *
   * @var \Drupal\Core\Queue\QueueFactory
   */
  protected $queueFactory;

  /**
   * The queue worker manager.
   *
   * @var \Drupal\Core\Queue\QueueWorkerManagerInterface
   */
  protected $queueWorkerManager;

  /**
   * LinkExtractorBatch constructor.
   */
  public function __construct(LinkCheckerService $checker, EntityTypeManagerInterface $entityTypeManager, LockBackendInterface $lock, QueueFactory $queueFactory, QueueWorkerManagerInterface $queueWorkerManager) {
    $this->checker = $checker;
    $this->entityTypeManager = $entityTypeManager;
    $this->lock = $lock;
    $this->queueFactory = $queueFactory;
    $this->queueWorkerManager = $queueWorkerManager;
  }

  /**
   * Process next item in queue.
   */
  public function processQueueItem() {
    $item = $this->queueFactory
      ->get('linkchecker_check')
      ->claimItem();
    if (!empty($item)) {
      $this->queueWorkerManager
        ->createInstance('linkchecker_check')
        ->processItem($item->data);
    }
  }

  /**
   * Sets a batch to extract links from entities.
   */
  public function batch() {

    // Get max_execution_time from configuration, override 0 with 240 seconds.
    $maxExecutionTime = ini_get('max_execution_time') == 0 ? 240 : ini_get('max_execution_time');

    // Make sure we have enough time to validate all of the links.
    Environment::setTimeLimit($maxExecutionTime);

    // Make sure this is the only process trying to run this function.
    if (!$this->lock
      ->acquire('linkchecker_check', $maxExecutionTime) && FALSE) {
      $this
        ->messenger()
        ->addWarning($this
        ->t('Attempted to re-run link checks while they are already running.'));
      return;
    }
    $batch = new BatchBuilder();
    $batch
      ->setTitle('Check links')
      ->addOperation([
      $this,
      'batchProcessQueue',
    ])
      ->setProgressive()
      ->setFinishCallback([
      $this,
      'batchFinished',
    ]);
    batch_set($batch
      ->toArray());
  }

  /**
   * Gets total number of links to process.
   *
   * @return int
   *   Total number of links.
   */
  public function getTotalLinksToProcess() {
    return $this->checker
      ->queueLinks(TRUE);
  }

  /**
   * Process linkchecker_check queue.
   *
   * @param mixed $context
   *   Context data from batch API.
   */
  public function batchProcessQueue(&$context) {
    if (!isset($context['sandbox']['total'])) {
      $context['sandbox']['total'] = $this
        ->getTotalLinksToProcess();
      $context['sandbox']['current'] = 0;
    }
    $this
      ->processQueueItem();
    $context['sandbox']['current']++;
    if (!empty($context['sandbox']['total'])) {
      $context['finished'] = $context['sandbox']['current'] / $context['sandbox']['total'];
    }
    else {
      $context['finished'] = 1;
    }
  }

  /**
   * Finished callback for batch.
   */
  public function batchFinished($success) {
    if ($success) {
      $this
        ->messenger()
        ->addStatus($this
        ->t('Links were successfully checked.'));
    }
    else {
      $this
        ->messenger()
        ->addError($this
        ->t('Links were not checked.'));
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
LinkCheckerBatch::$checker protected property The link checker.
LinkCheckerBatch::$entityTypeManager protected property The entity type manager.
LinkCheckerBatch::$lock protected property The lock.
LinkCheckerBatch::$queueFactory protected property The queue factory.
LinkCheckerBatch::$queueWorkerManager protected property The queue worker manager.
LinkCheckerBatch::batch public function Sets a batch to extract links from entities.
LinkCheckerBatch::batchFinished public function Finished callback for batch.
LinkCheckerBatch::batchProcessQueue public function Process linkchecker_check queue.
LinkCheckerBatch::getTotalLinksToProcess public function Gets total number of links to process.
LinkCheckerBatch::processQueueItem public function Process next item in queue.
LinkCheckerBatch::__construct public function LinkExtractorBatch constructor.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
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.