3 namespace Drupal\linkchecker\Commands;
5 use Drupal\Core\Config\ConfigFactoryInterface;
6 use Drupal\Core\StringTranslation\StringTranslationTrait;
7 use Drush\Commands\DrushCommands;
8 use Psr\Log\LoggerInterface;
11 * Drush 9 commands for Linkchecker module.
13 class LinkcheckerCommands extends DrushCommands {
15 use StringTranslationTrait;
18 * A config factory for retrieving required config settings.
20 * @var \Drupal\Core\Config\ConfigFactoryInterface
25 * The logger.channel.linkchecker service.
27 * @var \Psr\Log\LoggerInterface
32 * LinkcheckerCommands constructor.
34 * @param \Drupal\Core\Config\ConfigFactoryInterface $config
35 * A config factory object for retrieving configuration settings.
36 * @param \Psr\Log\LoggerInterface $logger
39 public function __construct(
40 ConfigFactoryInterface $config,
41 LoggerInterface $logger
43 parent::__construct();
44 $this->config = $config;
45 $this->logger = $logger;
49 * Reanalyzes content for links. Recommended after module has been upgraded.
51 * @command linkchecker:analyze
55 public function analyze() {
58 if ($base_url == 'http://default') {
59 $this->logger()->error('You MUST configure the site $base_url or provide --uri parameter.');
63 module_load_include('admin.inc', 'linkchecker');
65 // Fake $form_state to leverage _submit function.
67 'values' => ['op' => $this->t('Analyze content for links')],
71 $node_types = linkchecker_scan_node_types();
72 if (!empty($node_types) || \Drupal::config('linkchecker.settings')->get('scan_blocks')) {
73 linkchecker_analyze_links_submit(NULL, $form_state);
74 drush_backend_batch_process();
77 $this->logger()->warning('No content configured for link analysis.');
82 * Clears all link data and analyze content for links.
84 * WARNING: Custom link check settings are deleted.
86 * @command linkchecker:clear
90 public function clear() {
93 if ($base_url == 'http://default') {
94 $this->logger()->error('You MUST configure the site $base_url or provide --uri parameter.');
99 module_load_include('admin.inc', 'linkchecker');
101 // Fake $form_state to leverage _submit function.
103 'values' => ['op' => $this->t('Clear link data and analyze content for links')],
107 $node_types = linkchecker_scan_node_types();
108 if (!empty($node_types) || \Drupal::config('linkchecker.settings')
109 ->get('scan_blocks')) {
110 linkchecker_clear_analyze_links_submit(NULL, $form_state);
111 drush_backend_batch_process();
114 $this->logger()->warning('No content configured for link analysis.');
121 * @command linkchecker:check
125 public function check() {
126 $this->logger()->info('Starting link checking...');
127 $run = _linkchecker_check_links();
129 $this->logger()->warning('Attempted to re-run link checks while they are already running.');
132 $this->logger()->info('Finished checking links.');