bc8709c81ce6e1b1f5eb29a8ae9828759e3ac86f
[yaffs-website] / web / modules / contrib / linkchecker / linkchecker.batch.inc
1 <?php
2
3 /**
4  * @file
5  * Batch API callbacks for the linkchecker module.
6  */
7
8 use \Drupal\node\Entity\Node;
9
10 /**
11  * Batch: Scan nodes for links.
12  */
13 function _linkchecker_batch_import_nodes($node_types = []) {
14   // Get all active {node}.nid's.
15   $result = db_query('SELECT n.nid FROM {node} n WHERE n.status = :status AND n.type IN (:types) ORDER BY n.nid', [':status' => 1, ':types' => $node_types]);
16
17   $operations = [];
18   foreach ($result as $row) {
19     $operations[] = ['_linkchecker_batch_node_import_op', [$row->nid]];
20   }
21   $batch = [
22     'file' => drupal_get_path('module', 'linkchecker') . '/linkchecker.batch.inc',
23     'finished' => '_linkchecker_batch_node_import_finished',
24     'operations' => $operations,
25     'title' => t('Scanning for links'),
26   ];
27
28   return $batch;
29 }
30
31 /**
32  * Batch operation: Scan one by one node for links.
33  */
34 function _linkchecker_batch_node_import_op($nid, &$context) {
35   // Load the node and scan for links.
36   $node = Node::load($nid);
37   _linkchecker_add_node_links($node);
38
39   // Store results for post-processing in the finished callback.
40   $context['results'][] = $node->nid;
41   $context['message'] = t('Content: @title', ['@title' => $node->title]);
42 }
43
44 /**
45  * Output node batch result messages.
46  *
47  * @param bool $success
48  *   If scan completed successfully or not.
49  * @param int $results
50  *   Number of nodes scanned.
51  * @param array $operations
52  *   Array of functions called.
53  */
54 function _linkchecker_batch_node_import_finished($success, $results, array $operations) {
55   if ($success) {
56     $message = \Drupal::translation()->formatPlural(count($results), 'One node has been scanned.', '@count nodes have been scanned.');
57   }
58   else {
59     $message = t('Scanning for links in content has failed with an error.');
60   }
61   drupal_set_message($message);
62 }
63
64 /**
65  * Batch: Scan comments for links.
66  */
67 function _linkchecker_batch_import_comments($node_types = []) {
68   // Get all active {comment}.cid's.
69   $result = db_query('SELECT c.cid FROM {comment} c INNER JOIN {node} n ON c.nid = n.nid WHERE c.status = :cstatus AND n.status = :nstatus AND n.type IN (:types) ORDER BY c.cid', [':cstatus' => COMMENT_PUBLISHED, ':nstatus' => 1, ':types' => $node_types]);
70
71   $operations = [];
72   foreach ($result as $row) {
73     $operations[] = ['_linkchecker_batch_comments_import_op', [$row->cid]];
74   }
75   $batch = [
76     'file' => drupal_get_path('module', 'linkchecker') . '/linkchecker.batch.inc',
77     'finished' => '_linkchecker_batch_comments_import_finished',
78     'operations' => $operations,
79     'title' => t('Scanning for links'),
80   ];
81
82   return $batch;
83 }
84
85 /**
86  * Batch operation: Scan one by one comment for links.
87  */
88 function _linkchecker_batch_comments_import_op($cid, &$context) {
89   // Load the comment and scan for links.
90   $comment = comment_load($cid);
91   _linkchecker_add_comment_links($comment);
92
93   // Store results for post-processing in the finished callback.
94   $context['results'][] = $comment->cid;
95   $context['message'] = t('Comment: @title', ['@title' => $comment->subject]);
96 }
97
98 /**
99  * Output comment batch result messages.
100  *
101  * @param bool $success
102  *   If scan completed successfully or not.
103  * @param int $results
104  *   Number of comments scanned.
105  * @param array $operations
106  *   Array of functions called.
107  */
108 function _linkchecker_batch_comments_import_finished($success, $results, array $operations) {
109   if ($success) {
110     $message = \Drupal::translation()->formatPlural(count($results), 'One comment has been scanned.', '@count comments have been scanned.');
111   }
112   else {
113     $message = t('Scanning for links in comments have failed with an error.');
114   }
115   drupal_set_message($message);
116 }
117
118 /**
119  * Batch: Scan blocks for links.
120  */
121 function _linkchecker_batch_import_block_custom() {
122   // Get all {block_custom}.bid's as block module suxxx and has no usable hooks.
123   $result = db_query('SELECT bid FROM {block_custom} ORDER BY bid');
124
125   $operations = [];
126   foreach ($result as $row) {
127     $operations[] = ['_linkchecker_batch_import_block_custom_op', [$row->bid]];
128   }
129   $batch = [
130     'file' => drupal_get_path('module', 'linkchecker') . '/linkchecker.batch.inc',
131     'finished' => '_linkchecker_batch_block_custom_import_finished',
132     'operations' => $operations,
133     'title' => t('Scanning for links'),
134   ];
135
136   return $batch;
137 }
138
139 /**
140  * Batch operation: Scan one by one block for links.
141  */
142 function _linkchecker_batch_import_block_custom_op($bid, &$context) {
143   // Load the custom block and scan for links.
144   $block_custom = linkchecker_block_custom_block_get($bid);
145   _linkchecker_add_block_custom_links($block_custom, $block_custom->delta);
146
147   // Store some result for post-processing in the finished callback.
148   $context['results'][] = $block_custom->delta;
149   $context['message'] = t('Block: @title', ['@title' => $block_custom->info]);
150 }
151
152 /**
153  * Output block batch result messages.
154  *
155  * @param bool $success
156  *   If scan completed successfully or not.
157  * @param int $results
158  *   Number of blocks scanned.
159  * @param array $operations
160  *   Array of functions called.
161  */
162 function _linkchecker_batch_block_custom_import_finished($success, $results, array $operations) {
163   if ($success) {
164     $message = \Drupal::translation()->formatPlural(count($results), 'One block has been scanned.', '@count blocks have been scanned.');
165   }
166   else {
167     $message = t('Scanning for links in blocks have failed with an error.');
168   }
169   drupal_set_message($message);
170 }
171
172 /**
173  * Recurring scans of a single node via batch API.
174  *
175  * @param int $nid
176  *   The unique node id to scan for links.
177  * @param int $missing_links_count
178  *   The number of links not yet added to linkchecker_links table. By this
179  *   number the re-scan rounds are calulated.
180  *
181  * @return array
182  *   The batch task definition.
183  */
184 function _linkchecker_batch_import_single_node($nid, $missing_links_count) {
185   $operations = [];
186   for ($i = 0; $i <= $missing_links_count; $i = $i + LINKCHECKER_SCAN_MAX_LINKS_PER_RUN) {
187     $operations[] = ['_linkchecker_batch_single_node_import_op', [$nid]];
188   }
189   $batch = [
190     'file' => drupal_get_path('module', 'linkchecker') . '/linkchecker.batch.inc',
191     'finished' => '_linkchecker_batch_single_node_import_finished',
192     'operations' => $operations,
193     'title' => t('Scanning for links'),
194     'progress_message' => t('Remaining @remaining of @total scans.'),
195   ];
196
197   return $batch;
198 }
199
200 /**
201  * Run single node link extraction.
202  *
203  * @param int $nid
204  *   Node ID.
205  * @param array $context
206  *   Batch context array.
207  */
208 function _linkchecker_batch_single_node_import_op($nid, array &$context) {
209   // Load the node and scan for links.
210   $node = node_load($nid, NULL, TRUE);
211   _linkchecker_add_node_links($node, TRUE);
212
213   // Store results for post-processing in the finished callback.
214   $context['results'][] = $node->nid;
215   $context['message'] = t('Content: @title', ['@title' => $node->title]);
216 }
217
218 /**
219  * Output single node batch result messages.
220  *
221  * @param bool $success
222  *   If scan completed successfully or not.
223  * @param int $results
224  *   How often the node has been scanned.
225  * @param array $operations
226  *   Array of functions called.
227  */
228 function _linkchecker_batch_single_node_import_finished($success, $results, array $operations) {
229   if ($success) {
230     $message = \Drupal::translation()->formatPlural(count($results), 'Node @nid has been re-scanned once to collect all links.', 'Node @nid has been re-scanned @count times to collect all links.', ['@nid' => $results[0]]);
231   }
232   else {
233     $message = t('Recurring scanning for links in node @nid has failed with an error.', ['@nid' => $results[0]]);
234   }
235   drupal_set_message($message);
236 }
237
238 /**
239  * Recurring scans of a single comment via batch API.
240  *
241  * @param int $cid
242  *   The unique comment id to scan for links.
243  * @param int $missing_links_count
244  *   The number of links not yet added to linkchecker_links table. By this
245  *   number the re-scan rounds are calulated.
246  *
247  * @return array
248  *   The batch task definition.
249  */
250 function _linkchecker_batch_import_single_comment($cid, $missing_links_count) {
251   $operations = [];
252   for ($i = 0; $i <= $missing_links_count; $i = $i + LINKCHECKER_SCAN_MAX_LINKS_PER_RUN) {
253     $operations[] = ['_linkchecker_batch_single_comment_import_op', [$cid]];
254   }
255   $batch = [
256     'file' => drupal_get_path('module', 'linkchecker') . '/linkchecker.batch.inc',
257     'finished' => '_linkchecker_batch_single_comment_import_finished',
258     'operations' => $operations,
259     'title' => t('Scanning for links'),
260     'progress_message' => t('Remaining @remaining of @total scans.'),
261   ];
262
263   return $batch;
264 }
265
266 /**
267  * Run single comment link extraction.
268  *
269  * @param int $cid
270  *   Comment ID.
271  * @param array $context
272  *   Batch context array.
273  */
274 function _linkchecker_batch_single_comment_import_op($cid, array &$context) {
275   $comment = comment_load($cid);
276   _linkchecker_add_comment_links($comment, TRUE);
277
278   // Store results for post-processing in the finished callback.
279   $context['results'][] = $comment->cid;
280   $context['message'] = t('Comment: @title', ['@title' => $comment->subject]);
281 }
282
283 /**
284  * Output single comment batch result messages.
285  *
286  * @param bool $success
287  *   If scan completed successfully or not.
288  * @param int $results
289  *   How often the comment has been scanned.
290  * @param array $operations
291  *   Array of functions called.
292  */
293 function _linkchecker_batch_single_comment_import_finished($success, $results, array $operations) {
294   if ($success) {
295     $message = \Drupal::translation()->formatPlural(count($results), 'Comment @cid has been re-scanned once to collect all links.', 'Comment @cid has been re-scanned @count times to collect all links.', ['@cid' => $results[0]]);
296   }
297   else {
298     $message = t('Recurring scanning for links in comment @cid has failed with an error.', ['@cid' => $results[0]]);
299   }
300   drupal_set_message($message);
301 }
302
303 /**
304  * Recurring scans of a single block via batch API.
305  *
306  * @param int $bid
307  *   The unique block id to scan for links.
308  * @param int $missing_links_count
309  *   The number of links not yet added to linkchecker_links table. By this
310  *   number the re-scan rounds are calulated.
311  *
312  * @return array
313  *   The batch task definition.
314  */
315 function _linkchecker_batch_import_single_block_custom($bid, $missing_links_count) {
316   $operations = [];
317   for ($i = 0; $i <= $missing_links_count; $i = $i + LINKCHECKER_SCAN_MAX_LINKS_PER_RUN) {
318     $operations[] = ['_linkchecker_batch_single_block_custom_import_op', [$bid]];
319   }
320   $batch = [
321     'file' => drupal_get_path('module', 'linkchecker') . '/linkchecker.batch.inc',
322     'finished' => '_linkchecker_batch_single_block_custom_import_finished',
323     'operations' => $operations,
324     'title' => t('Scanning for links'),
325     'progress_message' => t('Remaining @remaining of @total scans.'),
326   ];
327
328   return $batch;
329 }
330
331 /**
332  * Run single block link extraction.
333  *
334  * @param int $bid
335  *   Node ID.
336  * @param array $context
337  *   Batch context array.
338  */
339 function _linkchecker_batch_single_block_custom_import_op($bid, array &$context) {
340   // Load the custom block and scan for links.
341   $block_custom = linkchecker_block_custom_block_get($bid);
342   _linkchecker_add_block_custom_links($block_custom, $block_custom->delta, TRUE);
343
344   // Store some result for post-processing in the finished callback.
345   $context['results'][] = $block_custom->delta;
346   $context['message'] = t('Block: @title', ['@title' => $block_custom->info]);
347 }
348
349 /**
350  * Output single block batch result messages.
351  *
352  * @param bool $success
353  *   If scan completed successfully or not.
354  * @param int $results
355  *   How often the block has been scanned.
356  * @param array $operations
357  *   Array of functions called.
358  */
359 function _linkchecker_batch_single_block_custom_import_finished($success, $results, array $operations) {
360   if ($success) {
361     $message = \Drupal::translation()->formatPlural(count($results), 'Block @bid has been re-scanned once to collect all links.', 'Block @bid has been re-scanned @count times to collect all links.', ['@bid' => $results[0]]);
362   }
363   else {
364     $message = t('Recurring scanning for links in block @bid has failed with an error.', ['@bid' => $results[0]]);
365   }
366   drupal_set_message($message);
367 }