5 * Batch API callbacks for the linkchecker module.
8 use \Drupal\node\Entity\Node;
11 * Batch: Scan nodes for links.
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]);
18 foreach ($result as $row) {
19 $operations[] = ['_linkchecker_batch_node_import_op', [$row->nid]];
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'),
32 * Batch operation: Scan one by one node for links.
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);
39 // Store results for post-processing in the finished callback.
40 $context['results'][] = $node->nid;
41 $context['message'] = t('Content: @title', ['@title' => $node->title]);
45 * Output node batch result messages.
47 * @param bool $success
48 * If scan completed successfully or not.
50 * Number of nodes scanned.
51 * @param array $operations
52 * Array of functions called.
54 function _linkchecker_batch_node_import_finished($success, $results, array $operations) {
56 $message = \Drupal::translation()->formatPlural(count($results), 'One node has been scanned.', '@count nodes have been scanned.');
59 $message = t('Scanning for links in content has failed with an error.');
61 drupal_set_message($message);
65 * Batch: Scan comments for links.
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]);
72 foreach ($result as $row) {
73 $operations[] = ['_linkchecker_batch_comments_import_op', [$row->cid]];
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'),
86 * Batch operation: Scan one by one comment for links.
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);
93 // Store results for post-processing in the finished callback.
94 $context['results'][] = $comment->cid;
95 $context['message'] = t('Comment: @title', ['@title' => $comment->subject]);
99 * Output comment batch result messages.
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.
108 function _linkchecker_batch_comments_import_finished($success, $results, array $operations) {
110 $message = \Drupal::translation()->formatPlural(count($results), 'One comment has been scanned.', '@count comments have been scanned.');
113 $message = t('Scanning for links in comments have failed with an error.');
115 drupal_set_message($message);
119 * Batch: Scan blocks for links.
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');
126 foreach ($result as $row) {
127 $operations[] = ['_linkchecker_batch_import_block_custom_op', [$row->bid]];
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'),
140 * Batch operation: Scan one by one block for links.
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);
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]);
153 * Output block batch result messages.
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.
162 function _linkchecker_batch_block_custom_import_finished($success, $results, array $operations) {
164 $message = \Drupal::translation()->formatPlural(count($results), 'One block has been scanned.', '@count blocks have been scanned.');
167 $message = t('Scanning for links in blocks have failed with an error.');
169 drupal_set_message($message);
173 * Recurring scans of a single node via batch API.
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.
182 * The batch task definition.
184 function _linkchecker_batch_import_single_node($nid, $missing_links_count) {
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]];
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.'),
201 * Run single node link extraction.
205 * @param array $context
206 * Batch context array.
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);
213 // Store results for post-processing in the finished callback.
214 $context['results'][] = $node->nid;
215 $context['message'] = t('Content: @title', ['@title' => $node->title]);
219 * Output single node batch result messages.
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.
228 function _linkchecker_batch_single_node_import_finished($success, $results, array $operations) {
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]]);
233 $message = t('Recurring scanning for links in node @nid has failed with an error.', ['@nid' => $results[0]]);
235 drupal_set_message($message);
239 * Recurring scans of a single comment via batch API.
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.
248 * The batch task definition.
250 function _linkchecker_batch_import_single_comment($cid, $missing_links_count) {
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]];
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.'),
267 * Run single comment link extraction.
271 * @param array $context
272 * Batch context array.
274 function _linkchecker_batch_single_comment_import_op($cid, array &$context) {
275 $comment = comment_load($cid);
276 _linkchecker_add_comment_links($comment, TRUE);
278 // Store results for post-processing in the finished callback.
279 $context['results'][] = $comment->cid;
280 $context['message'] = t('Comment: @title', ['@title' => $comment->subject]);
284 * Output single comment batch result messages.
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.
293 function _linkchecker_batch_single_comment_import_finished($success, $results, array $operations) {
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]]);
298 $message = t('Recurring scanning for links in comment @cid has failed with an error.', ['@cid' => $results[0]]);
300 drupal_set_message($message);
304 * Recurring scans of a single block via batch API.
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.
313 * The batch task definition.
315 function _linkchecker_batch_import_single_block_custom($bid, $missing_links_count) {
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]];
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.'),
332 * Run single block link extraction.
336 * @param array $context
337 * Batch context array.
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);
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]);
350 * Output single block batch result messages.
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.
359 function _linkchecker_batch_single_block_custom_import_finished($success, $results, array $operations) {
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]]);
364 $message = t('Recurring scanning for links in block @bid has failed with an error.', ['@bid' => $results[0]]);
366 drupal_set_message($message);