Further modules included.
[yaffs-website] / web / modules / contrib / linkchecker / linkchecker.pages.inc
1 <?php
2
3 /**
4  * @file
5  * User page callbacks for the linkchecker module.
6  */
7
8 /**
9  * Menu callback for general reporting.
10  *
11  * @return string
12  *   Themed report page.
13  */
14 function linkchecker_admin_report_page() {
15
16   $ignore_response_codes = preg_split('/(\r\n?|\n)/', variable_get('linkchecker_ignore_response_codes', "200\n206\n302\n304\n401\n403"));
17
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')
21     ->distinct()
22     ->fields('ln', array('lid'));
23
24   $subquery3 = db_select('linkchecker_comment', 'lc')
25     ->distinct()
26     ->fields('lc', array('lid'));
27
28   $subquery2 = db_select('linkchecker_block_custom', 'lb')
29     ->distinct()
30     ->fields('lb', array('lid'));
31
32   // UNION all linkchecker type tables.
33   $subquery1 = db_select($subquery2->union($subquery3)->union($subquery4), 'q1')->fields('q1', array('lid'));
34
35   // Build pager query.
36   $query = db_select('linkchecker_link', 'll')->extend('PagerDefault')->extend('TableSort');
37   $query->innerJoin($subquery1, 'q2', 'q2.lid = ll.lid');
38   $query->fields('ll');
39   $query->condition('ll.last_checked', 0, '<>');
40   $query->condition('ll.status', 1);
41   $query->condition('ll.code', $ignore_response_codes, 'NOT IN');
42
43   return _linkchecker_report_page($query);
44 }
45
46 /**
47  * Menu callback for author specific reporting.
48  *
49  * @param object $account
50  *   The user account.
51  *
52  * @return string
53  *   Themed report page.
54  */
55 function linkchecker_user_report_page($account) {
56   drupal_set_title($account->name);
57
58   $ignore_response_codes = preg_split('/(\r\n?|\n)/', variable_get('linkchecker_ignore_response_codes', "200\n206\n302\n304\n401\n403"));
59
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)
71   );
72   $subquery2->distinct();
73   $subquery2->fields('ll', array('lid'));
74
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'));
87
88     // UNION the linkchecker_node and linkchecker_comment tables.
89     $subquery1 = db_select($subquery2->union($subquery3), 'q1')->fields('q1', array('lid'));
90   }
91   else {
92     // Build query for broken links in nodes of the current user.
93     $subquery1 = db_select($subquery2, 'q1')->fields('q1', array('lid'));
94   }
95
96   // Build pager query.
97   $query = db_select('linkchecker_link', 'll')->extend('PagerDefault')->extend('TableSort');
98   $query->innerJoin($subquery1, 'q2', 'q2.lid = ll.lid');
99   $query->fields('ll');
100   $query->condition('ll.last_checked', 0, '<>');
101   $query->condition('ll.status', 1);
102   $query->condition('ll.code', $ignore_response_codes, 'NOT IN');
103
104   return _linkchecker_report_page($query, $account);
105 }
106
107 /**
108  * Builds the HTML report page table with pager.
109  *
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.
114  *
115  * @return string
116  *   Themed report page.
117  */
118 function _linkchecker_report_page($query, $account = NULL) {
119
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');
127   }
128
129   $header = array(
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')),
134   );
135
136   $result = $query
137     ->limit(50)
138     ->orderByHeader($header)
139     ->execute();
140
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');
145
146   $rows = array();
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);
153
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)));
160       continue;
161     }
162
163     $links = array();
164
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()));
168     }
169
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()));
173     }
174
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()));
180       }
181     }
182
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()));
187       }
188     }
189
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())));
193     }
194
195     // Create table data for output.
196     $rows[] = array(
197       'data' => array(
198         l(_filter_url_trim($link->url, 40), $link->url),
199         $link->code,
200         check_plain($link->error),
201         theme('item_list', array('items' => $links)),
202       ),
203     );
204   }
205
206   $build['linkchecker_table'] = array(
207     '#theme' => 'table',
208     '#header' => $header,
209     '#rows' => $rows,
210     '#empty' => t('No broken links have been found.'),
211   );
212   $build['linkchecker_pager'] = array('#theme' => 'pager');
213
214   return $build;
215 }
216
217 /**
218  * Check if the link is an internal URL or not.
219  *
220  * @param object $link
221  *   Link object.
222  *
223  * @return bool
224  *   TRUE if link is internal, otherwise FALSE.
225  */
226 function _linkchecker_is_internal_url(&$link) {
227   global $base_url;
228
229   if (strpos($link->url, $base_url) === 0) {
230     $link->internal = trim(substr($link->url, strlen($base_url)), " \t\r\n\0\\/");
231     return TRUE;
232   }
233 }