comparison modules/dblog/dblog.admin.inc @ 1:c1f4ac30525a 6.0

Drupal 6.0
author Franck Deroche <webmaster@defr.org>
date Tue, 23 Dec 2008 14:28:28 +0100
parents
children 589fb7c02327
comparison
equal deleted inserted replaced
0:5a113a1c4740 1:c1f4ac30525a
1 <?php
2 // $Id: dblog.admin.inc,v 1.6 2008/01/08 10:35:41 goba Exp $
3
4 /**
5 * @file
6 * Administrative page callbacks for the dblog module.
7 */
8
9 /**
10 * dblog module settings form.
11 *
12 * @ingroup forms
13 * @see system_settings_form()
14 */
15 function dblog_admin_settings() {
16 $form['dblog_row_limit'] = array(
17 '#type' => 'select',
18 '#title' => t('Discard log entries above the following row limit'),
19 '#default_value' => variable_get('dblog_row_limit', 1000),
20 '#options' => drupal_map_assoc(array(100, 1000, 10000, 100000, 1000000)),
21 '#description' => t('The maximum number of rows to keep in the database log. Older entries will be automatically discarded. (Requires a correctly configured <a href="@cron">cron maintenance task</a>.)', array('@cron' => url('admin/reports/status')))
22 );
23
24 return system_settings_form($form);
25 }
26
27 /**
28 * Menu callback; displays a listing of log messages.
29 */
30 function dblog_overview() {
31 $filter = dblog_build_filter_query();
32 $rows = array();
33 $icons = array(
34 WATCHDOG_NOTICE => '',
35 WATCHDOG_WARNING => theme('image', 'misc/watchdog-warning.png', t('warning'), t('warning')),
36 WATCHDOG_ERROR => theme('image', 'misc/watchdog-error.png', t('error'), t('error')),
37 );
38 $classes = array(
39 WATCHDOG_NOTICE => 'dblog-notice',
40 WATCHDOG_WARNING => 'dblog-warning',
41 WATCHDOG_ERROR => 'dblog-error',
42 );
43
44 $output = drupal_get_form('dblog_filter_form');
45
46 $header = array(
47 ' ',
48 array('data' => t('Type'), 'field' => 'w.type'),
49 array('data' => t('Date'), 'field' => 'w.wid', 'sort' => 'desc'),
50 t('Message'),
51 array('data' => t('User'), 'field' => 'u.name'),
52 array('data' => t('Operations')),
53 );
54
55 $sql = "SELECT w.wid, w.uid, w.severity, w.type, w.timestamp, w.message, w.variables, w.link, u.name FROM {watchdog} w INNER JOIN {users} u ON w.uid = u.uid";
56 $tablesort = tablesort_sql($header);
57 if (!empty($filter['where'])) {
58 $result = pager_query($sql ." WHERE ". $filter['where'] . $tablesort, 50, 0, NULL, $filter['args']);
59 }
60 else {
61 $result = pager_query($sql . $tablesort, 50);
62 }
63
64 while ($dblog = db_fetch_object($result)) {
65 $rows[] = array('data' =>
66 array(
67 // Cells
68 $icons[$dblog->severity],
69 t($dblog->type),
70 format_date($dblog->timestamp, 'small'),
71 l(truncate_utf8(_dblog_format_message($dblog), 56, TRUE, TRUE), 'admin/reports/event/'. $dblog->wid, array('html' => TRUE)),
72 theme('username', $dblog),
73 $dblog->link,
74 ),
75 // Attributes for tr
76 'class' => "dblog-". preg_replace('/[^a-z]/i', '-', $dblog->type) .' '. $classes[$dblog->severity]
77 );
78 }
79
80 if (!$rows) {
81 $rows[] = array(array('data' => t('No log messages available.'), 'colspan' => 6));
82 }
83
84 $output .= theme('table', $header, $rows, array('id' => 'admin-dblog'));
85 $output .= theme('pager', NULL, 50, 0);
86
87 return $output;
88 }
89
90 /**
91 * Menu callback; generic function to display a page of the most frequent
92 * dblog events of a specified type.
93 */
94 function dblog_top($type) {
95
96 $header = array(
97 array('data' => t('Count'), 'field' => 'count', 'sort' => 'desc'),
98 array('data' => t('Message'), 'field' => 'message')
99 );
100
101 $result = pager_query("SELECT COUNT(wid) AS count, message, variables FROM {watchdog} WHERE type = '%s' GROUP BY message, variables ". tablesort_sql($header), 30, 0, "SELECT COUNT(DISTINCT(message)) FROM {watchdog} WHERE type = '%s'", $type);
102
103 $rows = array();
104 while ($dblog = db_fetch_object($result)) {
105 $rows[] = array($dblog->count, truncate_utf8(_dblog_format_message($dblog), 56, TRUE, TRUE));
106 }
107
108 if (empty($rows)) {
109 $rows[] = array(array('data' => t('No log messages available.'), 'colspan' => 2));
110 }
111
112 $output = theme('table', $header, $rows);
113 $output .= theme('pager', NULL, 30, 0);
114
115 return $output;
116 }
117
118 /**
119 * Menu callback; displays details about a log message.
120 */
121 function dblog_event($id) {
122 $severity = watchdog_severity_levels();
123 $output = '';
124 $result = db_query('SELECT w.*, u.name, u.uid FROM {watchdog} w INNER JOIN {users} u ON w.uid = u.uid WHERE w.wid = %d', $id);
125 if ($dblog = db_fetch_object($result)) {
126 $rows = array(
127 array(
128 array('data' => t('Type'), 'header' => TRUE),
129 t($dblog->type),
130 ),
131 array(
132 array('data' => t('Date'), 'header' => TRUE),
133 format_date($dblog->timestamp, 'large'),
134 ),
135 array(
136 array('data' => t('User'), 'header' => TRUE),
137 theme('username', $dblog),
138 ),
139 array(
140 array('data' => t('Location'), 'header' => TRUE),
141 l($dblog->location, $dblog->location),
142 ),
143 array(
144 array('data' => t('Referrer'), 'header' => TRUE),
145 l($dblog->referer, $dblog->referer),
146 ),
147 array(
148 array('data' => t('Message'), 'header' => TRUE),
149 _dblog_format_message($dblog),
150 ),
151 array(
152 array('data' => t('Severity'), 'header' => TRUE),
153 $severity[$dblog->severity],
154 ),
155 array(
156 array('data' => t('Hostname'), 'header' => TRUE),
157 check_plain($dblog->hostname),
158 ),
159 array(
160 array('data' => t('Operations'), 'header' => TRUE),
161 $dblog->link,
162 ),
163 );
164 $attributes = array('class' => 'dblog-event');
165 $output = theme('table', array(), $rows, $attributes);
166 }
167 return $output;
168 }
169
170 /**
171 * Build query for dblog administration filters based on session.
172 */
173 function dblog_build_filter_query() {
174 if (empty($_SESSION['dblog_overview_filter'])) {
175 return;
176 }
177
178 $filters = dblog_filters();
179
180 // Build query
181 $where = $args = array();
182 foreach ($_SESSION['dblog_overview_filter'] as $key => $filter) {
183 $filter_where = array();
184 foreach ($filter as $value) {
185 $filter_where[] = $filters[$key]['where'];
186 $args[] = $value;
187 }
188 if (!empty($filter_where)) {
189 $where[] = '('. implode(' OR ', $filter_where) .')';
190 }
191 }
192 $where = !empty($where) ? implode(' AND ', $where) : '';
193
194 return array(
195 'where' => $where,
196 'args' => $args,
197 );
198 }
199
200
201 /**
202 * List dblog administration filters that can be applied.
203 */
204 function dblog_filters() {
205 $filters = array();
206
207 foreach (_dblog_get_message_types() as $type) {
208 $types[$type] = $type;
209 }
210
211 if (!empty($types)) {
212 $filters['type'] = array(
213 'title' => t('Type'),
214 'where' => "w.type = '%s'",
215 'options' => $types,
216 );
217 }
218
219 $filters['severity'] = array(
220 'title' => t('Severity'),
221 'where' => 'w.severity = %d',
222 'options' => watchdog_severity_levels(),
223 );
224
225 return $filters;
226 }
227
228 /**
229 * Formats a log message for display.
230 *
231 * @param $dblog
232 * An object with at least the message and variables properties
233 */
234 function _dblog_format_message($dblog) {
235 // Legacy messages and user specified text
236 if ($dblog->variables === 'N;') {
237 return $dblog->message;
238 }
239 // Message to translate with injected variables
240 else {
241 return t($dblog->message, unserialize($dblog->variables));
242 }
243 }
244
245
246 /**
247 * Return form for dblog administration filters.
248 *
249 * @ingroup forms
250 * @see dblog_filter_form_submit()
251 * @see dblog_filter_form_validate()
252 */
253 function dblog_filter_form() {
254 $session = &$_SESSION['dblog_overview_filter'];
255 $session = is_array($session) ? $session : array();
256 $filters = dblog_filters();
257
258 $form['filters'] = array(
259 '#type' => 'fieldset',
260 '#title' => t('Filter log messages'),
261 '#theme' => 'dblog_filters',
262 '#collapsible' => TRUE,
263 '#collapsed' => empty($session),
264 );
265 foreach ($filters as $key => $filter) {
266 $form['filters']['status'][$key] = array(
267 '#title' => $filter['title'],
268 '#type' => 'select',
269 '#multiple' => TRUE,
270 '#size' => 8,
271 '#options' => $filter['options'],
272 );
273 if (!empty($session[$key])) {
274 $form['filters']['status'][$key]['#default_value'] = $session[$key];
275 }
276 }
277
278 $form['filters']['buttons']['submit'] = array(
279 '#type' => 'submit',
280 '#value' => t('Filter'),
281 );
282 if (!empty($session)) {
283 $form['filters']['buttons']['reset'] = array(
284 '#type' => 'submit',
285 '#value' => t('Reset')
286 );
287 }
288
289 return $form;
290 }
291
292 /**
293 * Validate result from dblog administration filter form.
294 */
295 function dblog_filter_form_validate($form, &$form_state) {
296 if ($form_state['values']['op'] == t('Filter') && empty($form_state['values']['type']) && empty($form_state['values']['severity'])) {
297 form_set_error('type', t('You must select something to filter by.'));
298 }
299 }
300
301 /**
302 * Process result from dblog administration filter form.
303 */
304 function dblog_filter_form_submit($form, &$form_state) {
305 $op = $form_state['values']['op'];
306 $filters = dblog_filters();
307 switch ($op) {
308 case t('Filter'):
309 foreach ($filters as $name => $filter) {
310 if (isset($form_state['values'][$name])) {
311 $_SESSION['dblog_overview_filter'][$name] = $form_state['values'][$name];
312 }
313 }
314 break;
315 case t('Reset'):
316 $_SESSION['dblog_overview_filter'] = array();
317 break;
318 }
319 return 'admin/reports/dblog';
320 }