Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / linkchecker / src / Commands / LinkCheckerCommands.php
1 <?php
2
3 namespace Drupal\linkchecker\Commands;
4
5 use Drupal\Core\Config\ConfigFactoryInterface;
6 use Drupal\Core\StringTranslation\StringTranslationTrait;
7 use Drush\Commands\DrushCommands;
8 use Psr\Log\LoggerInterface;
9
10 /**
11  * Drush 9 commands for Linkchecker module.
12  */
13 class LinkCheckerCommands extends DrushCommands {
14
15   use StringTranslationTrait;
16
17   /**
18    * A config factory for retrieving required config settings.
19    *
20    * @var \Drupal\Core\Config\ConfigFactoryInterface
21    */
22   protected $config;
23
24   /**
25    * The logger.channel.linkchecker service.
26    *
27    * @var \Psr\Log\LoggerInterface
28    */
29   protected $logger;
30
31   /**
32    * LinkCheckerCommands constructor.
33    *
34    * @param \Drupal\Core\Config\ConfigFactoryInterface $config
35    *   A config factory object for retrieving configuration settings.
36    * @param \Psr\Log\LoggerInterface $logger
37    *   The logger service.
38    */
39   public function __construct(
40     ConfigFactoryInterface $config,
41     LoggerInterface $logger
42   ) {
43     parent::__construct();
44     $this->config = $config;
45     $this->logger = $logger;
46   }
47
48   /**
49    * Reanalyzes content for links. Recommended after module has been upgraded.
50    *
51    * @command linkchecker:analyze
52    *
53    * @aliases lca
54    */
55   public function analyze() {
56     // @fixme
57     global $base_url;
58     if ($base_url == 'http://default') {
59       $this->logger()->error('You MUST configure the site $base_url or provide --uri parameter.');
60     }
61
62     // @fixme
63     module_load_include('admin.inc', 'linkchecker');
64
65     // Fake $form_state to leverage _submit function.
66     $form_state = [
67       'values' => ['op' => $this->t('Analyze content for links')],
68       'buttons' => [],
69     ];
70
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();
75     }
76     else {
77       $this->logger()->warning('No content configured for link analysis.');
78     }
79   }
80
81   /**
82    * Clears all link data and analyze content for links.
83    *
84    * WARNING: Custom link check settings are deleted.
85    *
86    * @command linkchecker:clear
87    *
88    * @aliases lccl
89    */
90   public function clear() {
91     // @fixme
92     global $base_url;
93     if ($base_url == 'http://default') {
94       $this->logger()->error('You MUST configure the site $base_url or provide --uri parameter.');
95       return;
96     }
97
98     // @fixme
99     module_load_include('admin.inc', 'linkchecker');
100
101     // Fake $form_state to leverage _submit function.
102     $form_state = [
103       'values' => ['op' => $this->t('Clear link data and analyze content for links')],
104       'buttons' => [],
105     ];
106
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();
112     }
113     else {
114       $this->logger()->warning('No content configured for link analysis.');
115     }
116   }
117
118   /**
119    * Check link status.
120    *
121    * @command linkchecker:check
122    *
123    * @aliases lcch
124    */
125   public function check() {
126     $this->logger()->info('Starting link checking...');
127     $run = _linkchecker_check_links();
128     if (!$run) {
129       $this->logger()->warning('Attempted to re-run link checks while they are already running.');
130     }
131     else {
132       $this->logger()->info('Finished checking links.');
133     }
134   }
135
136 }