linkchecker.install | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ linkchecker.module | 31 +++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) diff --git a/linkchecker.install b/linkchecker.install index 84686c0..9a467bf 100644 --- a/linkchecker.install +++ b/linkchecker.install @@ -6,6 +6,58 @@ */ /** + * Implements hook_requirements(). + */ +function linkchecker_requirements($phase) { + $requirements = array(); + if ($phase == 'runtime') { + $t = get_t(); + module_load_include('module', 'linkchecker'); + $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_INFO : REQUIREMENT_OK, + 'description' => $description, + 'title' => $t('Link Checker'), + ); + } + return $requirements; +} + +/** * Implements hook_install(). */ function linkchecker_install() { diff --git a/linkchecker.module b/linkchecker.module index a05d4c6..f1dceb3 100644 --- a/linkchecker.module +++ b/linkchecker.module @@ -2505,3 +2505,34 @@ 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; +} +