You are here

class LinkCheckerCommands in Link checker 8

Drush 9 commands for Linkchecker module.

Hierarchy

Expanded class hierarchy of LinkCheckerCommands

1 string reference to 'LinkCheckerCommands'
drush.services.yml in ./drush.services.yml
drush.services.yml
1 service uses LinkCheckerCommands
linkchecker.command in ./drush.services.yml
Drupal\linkchecker\Commands\LinkCheckerCommands

File

src/Commands/LinkCheckerCommands.php, line 16

Namespace

Drupal\linkchecker\Commands
View source
class LinkCheckerCommands extends DrushCommands {
  use StringTranslationTrait;

  /**
   * The linkchecker settings.
   *
   * @var \Drupal\Core\Config\ImmutableConfig
   */
  protected $linkcheckerSetting;

  /**
   * The logger.channel.linkchecker service.
   *
   * @var \Psr\Log\LoggerInterface
   */
  protected $logger;

  /**
   * The extractor batch helper.
   *
   * @var \Drupal\linkchecker\LinkExtractorBatch
   */
  protected $extractorBatch;

  /**
   * The checker batch helper.
   *
   * @var \Drupal\linkchecker\LinkCheckerBatch
   */
  protected $checkerBatch;

  /**
   * The link clean up.
   *
   * @var \Drupal\linkchecker\LinkCleanUp
   */
  protected $linkCleanUp;

  /**
   * LinkCheckerCommands constructor.
   */
  public function __construct(ConfigFactoryInterface $configFactory, LoggerInterface $logger, LinkExtractorBatch $extractorBatch, LinkCheckerBatch $checkerBatch, LinkCleanUp $linkCleanUp) {
    parent::__construct();
    $this->linkcheckerSetting = $configFactory
      ->get('linkchecker.settings');
    $this->logger = $logger;
    $this->extractorBatch = $extractorBatch;
    $this->checkerBatch = $checkerBatch;
    $this->linkCleanUp = $linkCleanUp;
  }

  /**
   * Reanalyzes content for links. Recommended after module has been upgraded.
   *
   * @command linkchecker:analyze
   *
   * @aliases lca
   */
  public function analyze() {
    $this
      ->analyzeCheckParams();
    $total = $this->extractorBatch
      ->getTotalEntitiesToProcess();
    if (!empty($total)) {
      $this->extractorBatch
        ->batch();
      drush_backend_batch_process();
    }
    else {
      $this
        ->logger()
        ->warning('No content configured for link analysis.');
    }
  }

  /**
   * Clears all link data and analyze content for links.
   *
   * WARNING: Custom link check settings are deleted.
   *
   * @command linkchecker:clear
   *
   * @aliases lccl
   */
  public function clear() {
    $this
      ->analyzeCheckParams();
    $total = $this->extractorBatch
      ->getTotalEntitiesToProcess();
    if (!empty($total)) {
      $this->linkCleanUp
        ->removeAllBatch();
      $this->extractorBatch
        ->batch();
      drush_backend_batch_process();
    }
    else {
      $this
        ->logger()
        ->warning('No content configured for link analysis.');
    }
  }

  /**
   * Check link status.
   *
   * @command linkchecker:check
   *
   * @aliases lcch
   */
  public function check() {
    $this
      ->logger()
      ->info('Starting link checking...');

    // Always rebuild queue on Drush command run to check all links.
    if (empty($this->checkerBatch
      ->getTotalLinksToProcess())) {
      $this->logger
        ->notice($this
        ->t('There are no links to check.'));
      return;
    }
    $this->checkerBatch
      ->batch();
    drush_backend_batch_process();
  }

  /**
   * Throws an exception if base url is not set.
   *
   * @throws \Exception
   */
  protected function analyzeCheckParams() {
    $alias = $this
      ->getConfig()
      ->getContext('alias');
    $options = $alias
      ->get('options');
    if (!isset($options['uri'])) {
      $httpProtocol = $this->linkcheckerSetting
        ->get('default_url_scheme');
      $baseUrl = $httpProtocol . $this->linkcheckerSetting
        ->get('base_path');
    }
    else {
      $baseUrl = $options['uri'];
    }
    if (empty($baseUrl)) {
      throw new \Exception($this
        ->t('You MUST configure the site base_url or provide --uri parameter.'));
    }
    if (mb_strpos($baseUrl, 'http') !== 0) {
      throw new \Exception($this
        ->t('Base url should start with http scheme (http:// or https://)'));
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LinkCheckerCommands::$checkerBatch protected property The checker batch helper.
LinkCheckerCommands::$extractorBatch protected property The extractor batch helper.
LinkCheckerCommands::$linkcheckerSetting protected property The linkchecker settings.
LinkCheckerCommands::$linkCleanUp protected property The link clean up.
LinkCheckerCommands::$logger protected property The logger.channel.linkchecker service.
LinkCheckerCommands::analyze public function Reanalyzes content for links. Recommended after module has been upgraded.
LinkCheckerCommands::analyzeCheckParams protected function Throws an exception if base url is not set.
LinkCheckerCommands::check public function Check link status.
LinkCheckerCommands::clear public function Clears all link data and analyze content for links.
LinkCheckerCommands::__construct public function LinkCheckerCommands constructor.
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.