linkchecker.module | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 81 insertions(+)
diff --git a/linkchecker.module b/linkchecker.module
index 9a22ee9..941a8e0 100644
--- a/linkchecker.module
+++ b/linkchecker.module
@@ -2488,3 +2488,84 @@ function _linkchecker_array_values_recursive(array $array) {
return $array_values;
}
+
+function _linkchecker_broken_links_query() {
+ $ignore_response_codes = preg_split('/(\r\n?|\n)/', variable_get('linkchecker_ignore_response_codes', "200\n206\n302\n304\n401\n403"));
+
+ // Search for broken links in nodes and comments and blocks of all users.
+ // @todo Try to make UNION'ed subselect resultset smaller.
+ $subquery4 = db_select('linkchecker_node', 'ln')
+ ->distinct()
+ ->fields('ln', array('lid'));
+
+ $subquery3 = db_select('linkchecker_comment', 'lc')
+ ->distinct()
+ ->fields('lc', array('lid'));
+
+ $subquery2 = db_select('linkchecker_block_custom', 'lb')
+ ->distinct()
+ ->fields('lb', array('lid'));
+
+ // UNION all linkchecker type tables.
+ $subquery1 = db_select($subquery2->union($subquery3)->union($subquery4), 'q1')->fields('q1', array('lid'));
+
+ // Build query.
+ $query = db_select('linkchecker_link', 'll');
+ $query->innerJoin($subquery1, 'q2', 'q2.lid = ll.lid');
+ $query->fields('ll');
+ $query->condition('ll.last_checked', 0, '<>');
+ $query->condition('ll.status', 1);
+ $query->condition('ll.code', $ignore_response_codes, 'NOT IN');
+ return $query;
+}
+
+/**
+ * Implements hook_requirements().
+ */
+function linkchecker_requirements($phase) {
+ $requirements = array();
+ if ($phase == 'runtime') {
+ $links_all = db_query('SELECT COUNT(1) FROM {linkchecker_link} WHERE status = :status', array(':status' => 1))->fetchField();
+ $links_unchecked = db_query('SELECT COUNT(1) FROM {linkchecker_link} WHERE last_checked = :last_checked AND status = :status', array(':last_checked' => 0, ':status' => 1))->fetchField();
+ $query = _linkchecker_broken_links_query();
+ $links_broken = $query->countQuery()->execute()->fetchField();
+
+ $description = '';
+ if ($links_all > 0 && $links_broken == 0 && $links_unchecked == 0) {
+ $msg = format_plural($links_all,
+ '@count link in the database is checked OK.',
+ '@count links in the database are checked OK.'
+ );
+ }
+ else {
+ $msg = ($links_all > 0)
+ ? format_plural($links_all,
+ '@links_broken broken and @links_unchecked unchecked of total @count link in the database.',
+ '@links_broken broken and @links_unchecked unchecked of total @count links in the database.',
+ array(
+ '@links_unchecked' => $links_unchecked,
+ '@links_broken' => $links_broken,
+ ))
+ : t('There are no links in the database.');
+ if ($links_unchecked > 0) {
+ $description .= t('Please be patient until all links have been checked via cron.');
+ $description .= ' ' . t('You can run cron manually.', array('@cron' => url('admin/reports/status/run-cron')));
+ }
+ if ($links_broken > 0) {
+ if (!empty($description)) {
+ $description .= '
';
+ }
+ $description .= t('See Broken links report.', array('@report' => url('admin/reports/linkchecker')));
+ }
+ }
+
+ $requirements['linkchecker'] = array(
+ 'value' => $msg,
+ 'severity' => ($links_broken > 0) ? REQUIREMENT_WARNING : (($links_unchecked > 0) ? REQUIREMENT_INFO : REQUIREMENT_OK),
+ 'description' => $description,
+ 'title' => t('Link Checker'),
+ );
+ }
+ return $requirements;
+}
+