5 * User page callbacks for the linkchecker module.
9 * Menu callback for general reporting.
14 function linkchecker_admin_report_page() {
16 $ignore_response_codes = preg_split('/(\r\n?|\n)/', variable_get('linkchecker_ignore_response_codes', "200\n206\n302\n304\n401\n403"));
18 // Search for broken links in nodes and comments and blocks of all users.
19 // @todo Try to make UNION'ed subselect resultset smaller.
20 $subquery4 = db_select('linkchecker_node', 'ln')
22 ->fields('ln', array('lid'));
24 $subquery3 = db_select('linkchecker_comment', 'lc')
26 ->fields('lc', array('lid'));
28 $subquery2 = db_select('linkchecker_block_custom', 'lb')
30 ->fields('lb', array('lid'));
32 // UNION all linkchecker type tables.
33 $subquery1 = db_select($subquery2->union($subquery3)->union($subquery4), 'q1')->fields('q1', array('lid'));
36 $query = db_select('linkchecker_link', 'll')->extend('PagerDefault')->extend('TableSort');
37 $query->innerJoin($subquery1, 'q2', 'q2.lid = ll.lid');
39 $query->condition('ll.last_checked', 0, '<>');
40 $query->condition('ll.status', 1);
41 $query->condition('ll.code', $ignore_response_codes, 'NOT IN');
43 return _linkchecker_report_page($query);
47 * Menu callback for author specific reporting.
49 * @param object $account
55 function linkchecker_user_report_page($account) {
56 drupal_set_title($account->name);
58 $ignore_response_codes = preg_split('/(\r\n?|\n)/', variable_get('linkchecker_ignore_response_codes', "200\n206\n302\n304\n401\n403"));
60 // Build query for broken links in nodes of the current user.
61 $subquery2 = db_select('node', 'n');
62 $subquery2->innerJoin('node_revision', 'r', 'r.vid = n.vid');
63 $subquery2->innerJoin('linkchecker_node', 'ln', 'ln.nid = n.nid');
64 $subquery2->innerJoin('linkchecker_link', 'll', 'll.lid = ln.lid');
65 $subquery2->condition('ll.last_checked', 0, '<>');
66 $subquery2->condition('ll.status', 1);
67 $subquery2->condition('ll.code', $ignore_response_codes, 'NOT IN');
68 $subquery2->condition(db_or()
69 ->condition('n.uid', $account->uid)
70 ->condition('r.uid', $account->uid)
72 $subquery2->distinct();
73 $subquery2->fields('ll', array('lid'));
75 $comment_types = linkchecker_scan_comment_types();
76 if (!empty($comment_types)) {
77 // Build query for broken links in nodes and comments of the current user.
78 $subquery3 = db_select('comment', 'c');
79 $subquery3->innerJoin('linkchecker_comment', 'lc', 'lc.cid = c.cid');
80 $subquery3->innerJoin('linkchecker_link', 'll', 'll.lid = lc.lid');
81 $subquery3->condition('ll.last_checked', 0, '<>');
82 $subquery3->condition('ll.status', 1);
83 $subquery3->condition('ll.code', $ignore_response_codes, 'NOT IN');
84 $subquery3->condition('c.uid', $account->uid);
85 $subquery3->distinct();
86 $subquery3->fields('ll', array('lid'));
88 // UNION the linkchecker_node and linkchecker_comment tables.
89 $subquery1 = db_select($subquery2->union($subquery3), 'q1')->fields('q1', array('lid'));
92 // Build query for broken links in nodes of the current user.
93 $subquery1 = db_select($subquery2, 'q1')->fields('q1', array('lid'));
97 $query = db_select('linkchecker_link', 'll')->extend('PagerDefault')->extend('TableSort');
98 $query->innerJoin($subquery1, 'q2', 'q2.lid = ll.lid');
100 $query->condition('ll.last_checked', 0, '<>');
101 $query->condition('ll.status', 1);
102 $query->condition('ll.code', $ignore_response_codes, 'NOT IN');
104 return _linkchecker_report_page($query, $account);
108 * Builds the HTML report page table with pager.
110 * @param SelectQueryInterface $query
111 * The pager query for the report page. Can be per user report or global.
112 * @param object|null $account
113 * The user account object.
116 * Themed report page.
118 function _linkchecker_report_page($query, $account = NULL) {
120 $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();
121 if ($links_unchecked > 0) {
122 $links_all = db_query('SELECT COUNT(1) FROM {linkchecker_link} WHERE status = :status', array(':status' => 1))->fetchField();
123 drupal_set_message(format_plural($links_unchecked,
124 'There is 1 unchecked link of about @links_all links in the database. Please be patient until all links have been checked via cron.',
125 'There are @count unchecked links of about @links_all links in the database. Please be patient until all links have been checked via cron.',
126 array('@links_all' => $links_all)), 'warning');
130 array('data' => t('URL'), 'field' => 'url', 'sort' => 'desc'),
131 array('data' => t('Response'), 'field' => 'code', 'sort' => 'desc'),
132 array('data' => t('Error'), 'field' => 'error'),
133 array('data' => t('Operations')),
138 ->orderByHeader($header)
141 // Evaluate permission once for performance reasons.
142 $access_edit_link_settings = user_access('edit link settings');
143 $access_administer_blocks = user_access('administer blocks');
144 $access_administer_redirects = user_access('administer redirects');
147 foreach ($result as $link) {
148 // Get the node, block and comment IDs that refer to this broken link and
149 // that the current user has access to.
150 $nids = _linkchecker_link_node_ids($link, $account);
151 $cids = _linkchecker_link_comment_ids($link, $account);
152 $bids = _linkchecker_link_block_ids($link);
154 // If the user does not have access to see this link anywhere, do not
155 // display it, for reasons explained in _linkchecker_link_access(). We
156 // still need to fill the table row, though, so as not to throw off the
157 // number of items in the pager.
158 if (empty($nids) && empty($cids) && empty($bids)) {
159 $rows[] = array(array('data' => t('Permission restrictions deny you access to this broken link.'), 'colspan' => count($header)));
165 // Show links to link settings.
166 if ($access_edit_link_settings) {
167 $links[] = l(t('Edit link settings'), 'linkchecker/' . $link->lid . '/edit', array('query' => drupal_get_destination()));
170 // Show link to nodes having this broken link.
171 foreach ($nids as $nid) {
172 $links[] = l(t('Edit node @node', array('@node' => $nid)), 'node/' . $nid . '/edit', array('query' => drupal_get_destination()));
175 // Show link to comments having this broken link.
176 $comment_types = linkchecker_scan_comment_types();
177 if (module_exists('comment') && !empty($comment_types)) {
178 foreach ($cids as $cid) {
179 $links[] = l(t('Edit comment @comment', array('@comment' => $cid)), 'comment/' . $cid . '/edit', array('query' => drupal_get_destination()));
183 // Show link to blocks having this broken link.
184 if ($access_administer_blocks) {
185 foreach ($bids as $bid) {
186 $links[] = l(t('Edit block @block', array('@block' => $bid)), 'admin/structure/block/manage/block/' . $bid . '/configure', array('query' => drupal_get_destination()));
190 // Show link to redirect this broken internal link.
191 if (module_exists('redirect') && $access_administer_redirects && _linkchecker_is_internal_url($link)) {
192 $links[] = l(t('Create redirection'), 'admin/config/search/redirect/add', array('query' => array('source' => $link->internal, drupal_get_destination())));
195 // Create table data for output.
198 l(_filter_url_trim($link->url, 40), $link->url),
200 check_plain($link->error),
201 theme('item_list', array('items' => $links)),
206 $build['linkchecker_table'] = array(
208 '#header' => $header,
210 '#empty' => t('No broken links have been found.'),
212 $build['linkchecker_pager'] = array('#theme' => 'pager');
218 * Check if the link is an internal URL or not.
220 * @param object $link
224 * TRUE if link is internal, otherwise FALSE.
226 function _linkchecker_is_internal_url(&$link) {
229 if (strpos($link->url, $base_url) === 0) {
230 $link->internal = trim(substr($link->url, strlen($base_url)), " \t\r\n\0\\/");