5 * User page callbacks for the linkchecker module.
8 use Drupal\Component\Utility\Html;
11 * Menu callback for general reporting.
16 function linkchecker_admin_report_page() {
18 $ignore_response_codes = preg_split('/(\r\n?|\n)/', \Drupal::config('linkchecker.settings')->get('linkchecker_ignore_response_codes'));
20 // Search for broken links in nodes and comments and blocks of all users.
21 // @todo Try to make UNION'ed subselect resultset smaller.
22 $subquery4 = db_select('linkchecker_node', 'ln')
24 ->fields('ln', ['lid']);
26 $subquery3 = db_select('linkchecker_comment', 'lc')
28 ->fields('lc', ['lid']);
30 $subquery2 = db_select('linkchecker_block_custom', 'lb')
32 ->fields('lb', ['lid']);
34 // UNION all linkchecker type tables.
35 $subquery1 = db_select($subquery2->union($subquery3)->union($subquery4), 'q1')->fields('q1', ['lid']);
38 $query = db_select('linkchecker_link', 'll')->extend('PagerDefault')->extend('TableSort');
39 $query->innerJoin($subquery1, 'q2', 'q2.lid = ll.lid');
41 $query->condition('ll.last_checked', 0, '<>');
42 $query->condition('ll.status', 1);
43 $query->condition('ll.code', $ignore_response_codes, 'NOT IN');
45 return _linkchecker_report_page($query);
49 * Menu callback for author specific reporting.
51 * @param object $account
57 function linkchecker_user_report_page($account) {
58 drupal_set_title($account->name);
60 $ignore_response_codes = preg_split('/(\r\n?|\n)/', \Drupal::config('linkchecker.settings')->get('linkchecker_ignore_response_codes'));
62 // Build query for broken links in nodes of the current user.
63 $subquery2 = db_select('node', 'n');
64 $subquery2->innerJoin('node_revision', 'r', 'r.vid = n.vid');
65 $subquery2->innerJoin('linkchecker_node', 'ln', 'ln.nid = n.nid');
66 $subquery2->innerJoin('linkchecker_link', 'll', 'll.lid = ln.lid');
67 $subquery2->condition('ll.last_checked', 0, '<>');
68 $subquery2->condition('ll.status', 1);
69 $subquery2->condition('ll.code', $ignore_response_codes, 'NOT IN');
70 $subquery2->condition(db_or()
71 ->condition('n.uid', $account->uid)
72 ->condition('r.uid', $account->uid)
74 $subquery2->distinct();
75 $subquery2->fields('ll', ['lid']);
77 $comment_types = linkchecker_scan_comment_types();
78 if (!empty($comment_types)) {
79 // Build query for broken links in nodes and comments of the current user.
80 $subquery3 = db_select('comment', 'c');
81 $subquery3->innerJoin('linkchecker_comment', 'lc', 'lc.cid = c.cid');
82 $subquery3->innerJoin('linkchecker_link', 'll', 'll.lid = lc.lid');
83 $subquery3->condition('ll.last_checked', 0, '<>');
84 $subquery3->condition('ll.status', 1);
85 $subquery3->condition('ll.code', $ignore_response_codes, 'NOT IN');
86 $subquery3->condition('c.uid', $account->uid);
87 $subquery3->distinct();
88 $subquery3->fields('ll', ['lid']);
90 // UNION the linkchecker_node and linkchecker_comment tables.
91 $subquery1 = db_select($subquery2->union($subquery3), 'q1')->fields('q1', ['lid']);
94 // Build query for broken links in nodes of the current user.
95 $subquery1 = db_select($subquery2, 'q1')->fields('q1', ['lid']);
99 $query = db_select('linkchecker_link', 'll')->extend('PagerDefault')->extend('TableSort');
100 $query->innerJoin($subquery1, 'q2', 'q2.lid = ll.lid');
101 $query->fields('ll');
102 $query->condition('ll.last_checked', 0, '<>');
103 $query->condition('ll.status', 1);
104 $query->condition('ll.code', $ignore_response_codes, 'NOT IN');
106 return _linkchecker_report_page($query, $account);
110 * Builds the HTML report page table with pager.
112 * @param SelectQueryInterface $query
113 * The pager query for the report page. Can be per user report or global.
114 * @param object|null $account
115 * The user account object.
118 * Themed report page.
120 function _linkchecker_report_page($query, $account = NULL) {
122 $links_unchecked = db_query('SELECT COUNT(1) FROM {linkchecker_link} WHERE last_checked = :last_checked AND status = :status', [':last_checked' => 0, ':status' => 1])->fetchField();
123 if ($links_unchecked > 0) {
124 $links_all = db_query('SELECT COUNT(1) FROM {linkchecker_link} WHERE status = :status', [':status' => 1])->fetchField();
125 drupal_set_message(\Drupal::translation()->formatPlural($links_unchecked,
126 'There is 1 unchecked link of about @links_all links in the database. Please be patient until all links have been checked via cron.',
127 'There are @count unchecked links of about @links_all links in the database. Please be patient until all links have been checked via cron.',
128 ['@links_all' => $links_all]), 'warning');
132 ['data' => t('URL'), 'field' => 'url', 'sort' => 'desc'],
133 ['data' => t('Response'), 'field' => 'code', 'sort' => 'desc'],
134 ['data' => t('Error'), 'field' => 'error'],
135 ['data' => t('Operations')],
140 ->orderByHeader($header)
143 // Evaluate permission once for performance reasons.
144 $access_edit_link_settings = \Drupal::currentUser()->hasPermission('edit link settings');
145 $access_administer_blocks = \Drupal::currentUser()->hasPermission('administer blocks');
146 $access_administer_redirects = \Drupal::currentUser()->hasPermission('administer redirects');
149 foreach ($result as $link) {
150 // Get the node, block and comment IDs that refer to this broken link and
151 // that the current user has access to.
152 $nids = _linkchecker_link_node_ids($link, $account);
153 $cids = _linkchecker_link_comment_ids($link, $account);
154 $bids = _linkchecker_link_block_ids($link);
156 // If the user does not have access to see this link anywhere, do not
157 // display it, for reasons explained in _linkchecker_link_access(). We
158 // still need to fill the table row, though, so as not to throw off the
159 // number of items in the pager.
160 if (empty($nids) && empty($cids) && empty($bids)) {
161 $rows[] = [['data' => t('Permission restrictions deny you access to this broken link.'), 'colspan' => count($header)]];
167 // Show links to link settings.
168 if ($access_edit_link_settings) {
169 $links[] = l(t('Edit link settings'), 'linkchecker/' . $link->lid . '/edit', ['query' => drupal_get_destination()]);
172 // Show link to nodes having this broken link.
173 foreach ($nids as $nid) {
174 $links[] = l(t('Edit node @node', ['@node' => $nid]), 'node/' . $nid . '/edit', ['query' => drupal_get_destination()]);
177 // Show link to comments having this broken link.
178 $comment_types = linkchecker_scan_comment_types();
179 if (\Drupal::moduleHandler()->moduleExists('comment') && !empty($comment_types)) {
180 foreach ($cids as $cid) {
181 $links[] = l(t('Edit comment @comment', ['@comment' => $cid]), 'comment/' . $cid . '/edit', ['query' => drupal_get_destination()]);
185 // Show link to blocks having this broken link.
186 if ($access_administer_blocks) {
187 foreach ($bids as $bid) {
188 $links[] = l(t('Edit block @block', ['@block' => $bid]), 'admin/structure/block/manage/block/' . $bid . '/configure', ['query' => drupal_get_destination()]);
192 // Show link to redirect this broken internal link.
193 if (\Drupal::moduleHandler()->moduleExists('redirect') && $access_administer_redirects && _linkchecker_is_internal_url($link)) {
194 $links[] = l(t('Create redirection'), 'admin/config/search/redirect/add', ['query' => ['source' => $link->internal, drupal_get_destination()]]);
197 // Create table data for output.
200 l(_filter_url_trim($link->url, 40), $link->url),
202 Html::escape($link->error),
203 theme('item_list', ['items' => $links]),
208 $build['linkchecker_table'] = [
210 '#header' => $header,
212 '#empty' => t('No broken links have been found.'),
214 $build['linkchecker_pager'] = ['#theme' => 'pager'];
220 * Check if the link is an internal URL or not.
222 * @param object $link
226 * TRUE if link is internal, otherwise FALSE.
228 function _linkchecker_is_internal_url(&$link) {
231 if (strpos($link->url, $base_url) === 0) {
232 $link->internal = trim(substr($link->url, strlen($base_url)), " \t\r\n\0\\/");