Code coverage for /20080809/modules/dblog/dblog.admin.inc

Line #Times calledCode
1
<?php
2
// $Id: dblog.admin.inc,v 1.8 2008/07/19 07:44:45 dries 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
 */
1528
function dblog_admin_settings() {
163
  $form['dblog_row_limit'] = array(
173
    '#type' => 'select',
183
    '#title' => t('Discard log entries above the following row limit'),
193
    '#default_value' => variable_get('dblog_row_limit', 1000),
203
    '#options' => drupal_map_assoc(array(100, 1000, 10000, 100000,
1000000)),
213
    '#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')))
223
  );
23
243
  return system_settings_form($form);
250
}
26
27
/**
28
 * Menu callback; displays a listing of log messages.
29
 */
3028
function dblog_overview() {
316
  $filter = dblog_build_filter_query();
326
  $rows = array();
33
  $icons = array(
346
    WATCHDOG_DEBUG    => '',
356
    WATCHDOG_INFO     => '',
366
    WATCHDOG_NOTICE   => '',
376
    WATCHDOG_WARNING  => theme('image', 'misc/watchdog-warning.png',
t('warning'), t('warning')),
386
    WATCHDOG_ERROR    => theme('image', 'misc/watchdog-error.png',
t('error'), t('error')),
396
    WATCHDOG_CRITICAL => theme('image', 'misc/watchdog-error.png',
t('critical'), t('critical')),
406
    WATCHDOG_ALERT    => theme('image', 'misc/watchdog-error.png',
t('alert'), t('alert')),
416
    WATCHDOG_EMERG    => theme('image', 'misc/watchdog-error.png',
t('emergency'), t('emergency')),
426
  );
43
  $classes = array(
446
    WATCHDOG_DEBUG    => 'dblog-debug',
456
    WATCHDOG_INFO     => 'dblog-info',
466
    WATCHDOG_NOTICE   => 'dblog-notice',
476
    WATCHDOG_WARNING  => 'dblog-warning',
486
    WATCHDOG_ERROR    => 'dblog-error',
496
    WATCHDOG_CRITICAL => 'dblog-critical',
506
    WATCHDOG_ALERT    => 'dblog-alert',
516
    WATCHDOG_EMERG    => 'dblog-emerg',
526
  );
53
546
  $output = drupal_get_form('dblog_filter_form');
55
56
  $header = array(
576
    ' ',
586
    array('data' => t('Type'), 'field' => 'w.type'),
596
    array('data' => t('Date'), 'field' => 'w.wid', 'sort' => 'desc'),
606
    t('Message'),
616
    array('data' => t('User'), 'field' => 'u.name'),
626
    array('data' => t('Operations')),
636
  );
64
656
  $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";
666
  $tablesort = tablesort_sql($header);
676
  if (!empty($filter['where'])) {
680
    $result = pager_query($sql . " WHERE " . $filter['where'] . $tablesort,
50, 0, NULL, $filter['args']);
690
  }
70
  else {
716
    $result = pager_query($sql . $tablesort, 50);
72
  }
73
746
  while ($dblog = db_fetch_object($result)) {
756
    $rows[] = array('data' =>
76
      array(
77
        // Cells
786
        $icons[$dblog->severity],
796
        t($dblog->type),
806
        format_date($dblog->timestamp, 'small'),
816
        l(truncate_utf8(_dblog_format_message($dblog), 56, TRUE, TRUE),
'admin/reports/event/' . $dblog->wid, array('html' => TRUE)),
826
        theme('username', $dblog),
836
        $dblog->link,
846
      ),
85
      // Attributes for tr
866
      'class' => "dblog-" . preg_replace('/[^a-z]/i', '-', $dblog->type) .
' ' . $classes[$dblog->severity]
876
    );
886
  }
89
906
  if (!$rows) {
910
    $rows[] = array(array('data' => t('No log messages available.'),
'colspan' => 6));
920
  }
93
946
  $output .= theme('table', $header, $rows, array('id' => 'admin-dblog'));
956
  $output .= theme('pager', NULL, 50, 0);
96
976
  return $output;
980
}
99
100
/**
101
 * Menu callback; generic function to display a page of the most frequent
102
 * dblog events of a specified type.
103
 */
10428
function dblog_top($type) {
105
106
  $header = array(
10710
    array('data' => t('Count'), 'field' => 'count', 'sort' => 'desc'),
10810
    array('data' => t('Message'), 'field' => 'message')
10910
  );
110
11110
  $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);
112
11310
  $rows = array();
11410
  while ($dblog = db_fetch_object($result)) {
11510
    $rows[] = array($dblog->count,
truncate_utf8(_dblog_format_message($dblog), 56, TRUE, TRUE));
11610
  }
117
11810
  if (empty($rows)) {
1190
    $rows[] = array(array('data' => t('No log messages available.'),
'colspan' => 2));
1200
  }
121
12210
  $output  = theme('table', $header, $rows);
12310
  $output .= theme('pager', NULL, 30, 0);
124
12510
  return $output;
1260
}
127
128
/**
129
 * Menu callback; displays details about a log message.
130
 */
13128
function dblog_event($id) {
1321
  $severity = watchdog_severity_levels();
1331
  $output = '';
1341
  $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);
1351
  if ($dblog = db_fetch_object($result)) {
136
    $rows = array(
137
      array(
1380
        array('data' => t('Type'), 'header' => TRUE),
1390
        t($dblog->type),
1400
      ),
141
      array(
1420
        array('data' => t('Date'), 'header' => TRUE),
1430
        format_date($dblog->timestamp, 'large'),
1440
      ),
145
      array(
1460
        array('data' => t('User'), 'header' => TRUE),
1470
        theme('username', $dblog),
1480
      ),
149
      array(
1500
        array('data' => t('Location'), 'header' => TRUE),
1510
        l($dblog->location, $dblog->location),
1520
      ),
153
      array(
1540
        array('data' => t('Referrer'), 'header' => TRUE),
1550
        l($dblog->referer, $dblog->referer),
1560
      ),
157
      array(
1580
        array('data' => t('Message'), 'header' => TRUE),
1590
        _dblog_format_message($dblog),
1600
      ),
161
      array(
1620
        array('data' => t('Severity'), 'header' => TRUE),
1630
        $severity[$dblog->severity],
1640
      ),
165
      array(
1660
        array('data' => t('Hostname'), 'header' => TRUE),
1670
        check_plain($dblog->hostname),
1680
      ),
169
      array(
1700
        array('data' => t('Operations'), 'header' => TRUE),
1710
        $dblog->link,
1720
      ),
1730
    );
1740
    $attributes = array('class' => 'dblog-event');
1750
    $output = theme('table', array(), $rows, $attributes);
1760
  }
1771
  return $output;
1780
}
179
180
/**
181
 * Build query for dblog administration filters based on session.
182
 */
18328
function dblog_build_filter_query() {
1846
  if (empty($_SESSION['dblog_overview_filter'])) {
1856
    return;
1860
  }
187
1880
  $filters = dblog_filters();
189
190
  // Build query
1910
  $where = $args = array();
1920
  foreach ($_SESSION['dblog_overview_filter'] as $key => $filter) {
1930
    $filter_where = array();
1940
    foreach ($filter as $value) {
1950
      $filter_where[] = $filters[$key]['where'];
1960
      $args[] = $value;
1970
    }
1980
    if (!empty($filter_where)) {
1990
      $where[] = '(' . implode(' OR ', $filter_where) . ')';
2000
    }
2010
  }
2020
  $where = !empty($where) ? implode(' AND ', $where) : '';
203
204
  return array(
2050
    'where' => $where,
2060
    'args' => $args,
2070
  );
2080
}
209
210
211
/**
212
 * List dblog administration filters that can be applied.
213
 */
21428
function dblog_filters() {
2156
  $filters = array();
216
2176
  foreach (_dblog_get_message_types() as $type) {
2186
    $types[$type] = $type;
2196
  }
220
2216
  if (!empty($types)) {
2226
    $filters['type'] = array(
2236
      'title' => t('Type'),
2246
      'where' => "w.type = '%s'",
2256
      'options' => $types,
226
    );
2276
  }
228
2296
  $filters['severity'] = array(
2306
    'title' => t('Severity'),
2316
    'where' => 'w.severity = %d',
2326
    'options' => watchdog_severity_levels(),
233
  );
234
2356
  return $filters;
2360
}
237
238
/**
239
 * Formats a log message for display.
240
 *
241
 * @param $dblog
242
 *   An object with at least the message and variables properties
243
 */
24428
function _dblog_format_message($dblog) {
245
  // Legacy messages and user specified text
24616
  if ($dblog->variables === 'N;') {
24715
    return $dblog->message;
2480
  }
249
  // Message to translate with injected variables
250
  else {
2516
    return t($dblog->message, unserialize($dblog->variables));
252
  }
2530
}
254
255
256
/**
257
 * Return form for dblog administration filters.
258
 *
259
 * @ingroup forms
260
 * @see dblog_filter_form_submit()
261
 * @see dblog_filter_form_validate()
262
 */
26328
function dblog_filter_form() {
2646
  $session = &$_SESSION['dblog_overview_filter'];
2656
  $session = is_array($session) ? $session : array();
2666
  $filters = dblog_filters();
267
2686
  $form['filters'] = array(
2696
    '#type' => 'fieldset',
2706
    '#title' => t('Filter log messages'),
2716
    '#theme' => 'dblog_filters',
2726
    '#collapsible' => TRUE,
2736
    '#collapsed' => empty($session),
274
  );
2756
  foreach ($filters as $key => $filter) {
2766
    $form['filters']['status'][$key] = array(
2776
      '#title' => $filter['title'],
2786
      '#type' => 'select',
2796
      '#multiple' => TRUE,
2806
      '#size' => 8,
2816
      '#options' => $filter['options'],
282
    );
2836
    if (!empty($session[$key])) {
2840
      $form['filters']['status'][$key]['#default_value'] = $session[$key];
2850
    }
2866
  }
287
2886
  $form['filters']['buttons']['submit'] = array(
2896
    '#type' => 'submit',
2906
    '#value' => t('Filter'),
291
  );
2926
  if (!empty($session)) {
2930
    $form['filters']['buttons']['reset'] = array(
2940
      '#type' => 'submit',
2950
      '#value' => t('Reset')
2960
    );
2970
  }
298
2996
  return $form;
3000
}
301
302
/**
303
 * Validate result from dblog administration filter form.
304
 */
30528
function dblog_filter_form_validate($form, &$form_state) {
3060
  if ($form_state['values']['op'] == t('Filter') &&
empty($form_state['values']['type']) &&
empty($form_state['values']['severity'])) {
3070
    form_set_error('type', t('You must select something to filter by.'));
3080
  }
3090
}
310
311
/**
312
 * Process result from dblog administration filter form.
313
 */
31428
function dblog_filter_form_submit($form, &$form_state) {
3150
  $op = $form_state['values']['op'];
3160
  $filters = dblog_filters();
317
  switch ($op) {
3180
    case t('Filter'):
3190
      foreach ($filters as $name => $filter) {
3200
        if (isset($form_state['values'][$name])) {
3210
          $_SESSION['dblog_overview_filter'][$name] =
$form_state['values'][$name];
3220
        }
3230
      }
3240
      break;
3250
    case t('Reset'):
3260
      $_SESSION['dblog_overview_filter'] = array();
3270
      break;
3280
  }
3290
  return 'admin/reports/dblog';
3300
}
33128