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