00001 <?php
00002
00003
00015 function dblog_admin_settings() {
00016 $form['dblog_row_limit'] = array(
00017 '#type' => 'select',
00018 '#title' => t('Discard log entries above the following row limit'),
00019 '#default_value' => variable_get('dblog_row_limit', 1000),
00020 '#options' => drupal_map_assoc(array(100, 1000, 10000, 100000, 1000000)),
00021 '#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')))
00022 );
00023
00024 return system_settings_form($form);
00025 }
00026
00030 function dblog_overview() {
00031 $filter = dblog_build_filter_query();
00032 $rows = array();
00033 $icons = array(
00034 WATCHDOG_NOTICE => '',
00035 WATCHDOG_WARNING => theme('image', 'misc/watchdog-warning.png', t('warning'), t('warning')),
00036 WATCHDOG_ERROR => theme('image', 'misc/watchdog-error.png', t('error'), t('error')),
00037 );
00038 $classes = array(
00039 WATCHDOG_NOTICE => 'dblog-notice',
00040 WATCHDOG_WARNING => 'dblog-warning',
00041 WATCHDOG_ERROR => 'dblog-error',
00042 );
00043
00044 $output = drupal_get_form('dblog_filter_form');
00045
00046 $header = array(
00047 ' ',
00048 array('data' => t('Type'), 'field' => 'w.type'),
00049 array('data' => t('Date'), 'field' => 'w.wid', 'sort' => 'desc'),
00050 t('Message'),
00051 array('data' => t('User'), 'field' => 'u.name'),
00052 array('data' => t('Operations')),
00053 );
00054
00055 $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";
00056 $tablesort = tablesort_sql($header);
00057 if (!empty($filter['where'])) {
00058 $result = pager_query($sql . " WHERE " . $filter['where'] . $tablesort, 50, 0, NULL, $filter['args']);
00059 }
00060 else {
00061 $result = pager_query($sql . $tablesort, 50);
00062 }
00063
00064 while ($dblog = db_fetch_object($result)) {
00065 $rows[] = array('data' =>
00066 array(
00067
00068 $icons[$dblog->severity],
00069 t($dblog->type),
00070 format_date($dblog->timestamp, 'small'),
00071 l(truncate_utf8(_dblog_format_message($dblog), 56, TRUE, TRUE), 'admin/reports/event/' . $dblog->wid, array('html' => TRUE)),
00072 theme('username', $dblog),
00073 $dblog->link,
00074 ),
00075
00076 'class' => "dblog-" . preg_replace('/[^a-z]/i', '-', $dblog->type) . ' ' . $classes[$dblog->severity]
00077 );
00078 }
00079
00080 if (!$rows) {
00081 $rows[] = array(array('data' => t('No log messages available.'), 'colspan' => 6));
00082 }
00083
00084 $output .= theme('table', $header, $rows, array('id' => 'admin-dblog'));
00085 $output .= theme('pager', NULL, 50, 0);
00086
00087 return $output;
00088 }
00089
00094 function dblog_top($type) {
00095
00096 $header = array(
00097 array('data' => t('Count'), 'field' => 'count', 'sort' => 'desc'),
00098 array('data' => t('Message'), 'field' => 'message')
00099 );
00100
00101 $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);
00102
00103 $rows = array();
00104 while ($dblog = db_fetch_object($result)) {
00105 $rows[] = array($dblog->count, truncate_utf8(_dblog_format_message($dblog), 56, TRUE, TRUE));
00106 }
00107
00108 if (empty($rows)) {
00109 $rows[] = array(array('data' => t('No log messages available.'), 'colspan' => 2));
00110 }
00111
00112 $output = theme('table', $header, $rows);
00113 $output .= theme('pager', NULL, 30, 0);
00114
00115 return $output;
00116 }
00117
00121 function dblog_event($id) {
00122 $severity = watchdog_severity_levels();
00123 $output = '';
00124 $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);
00125 if ($dblog = db_fetch_object($result)) {
00126 $rows = array(
00127 array(
00128 array('data' => t('Type'), 'header' => TRUE),
00129 t($dblog->type),
00130 ),
00131 array(
00132 array('data' => t('Date'), 'header' => TRUE),
00133 format_date($dblog->timestamp, 'large'),
00134 ),
00135 array(
00136 array('data' => t('User'), 'header' => TRUE),
00137 theme('username', $dblog),
00138 ),
00139 array(
00140 array('data' => t('Location'), 'header' => TRUE),
00141 l($dblog->location, $dblog->location),
00142 ),
00143 array(
00144 array('data' => t('Referrer'), 'header' => TRUE),
00145 l($dblog->referer, $dblog->referer),
00146 ),
00147 array(
00148 array('data' => t('Message'), 'header' => TRUE),
00149 _dblog_format_message($dblog),
00150 ),
00151 array(
00152 array('data' => t('Severity'), 'header' => TRUE),
00153 $severity[$dblog->severity],
00154 ),
00155 array(
00156 array('data' => t('Hostname'), 'header' => TRUE),
00157 check_plain($dblog->hostname),
00158 ),
00159 array(
00160 array('data' => t('Operations'), 'header' => TRUE),
00161 $dblog->link,
00162 ),
00163 );
00164 $attributes = array('class' => 'dblog-event');
00165 $output = theme('table', array(), $rows, $attributes);
00166 }
00167 return $output;
00168 }
00169
00173 function dblog_build_filter_query() {
00174 if (empty($_SESSION['dblog_overview_filter'])) {
00175 return;
00176 }
00177
00178 $filters = dblog_filters();
00179
00180
00181 $where = $args = array();
00182 foreach ($_SESSION['dblog_overview_filter'] as $key => $filter) {
00183 $filter_where = array();
00184 foreach ($filter as $value) {
00185 $filter_where[] = $filters[$key]['where'];
00186 $args[] = $value;
00187 }
00188 if (!empty($filter_where)) {
00189 $where[] = '(' . implode(' OR ', $filter_where) . ')';
00190 }
00191 }
00192 $where = !empty($where) ? implode(' AND ', $where) : '';
00193
00194 return array(
00195 'where' => $where,
00196 'args' => $args,
00197 );
00198 }
00199
00200
00204 function dblog_filters() {
00205 $filters = array();
00206
00207 foreach (_dblog_get_message_types() as $type) {
00208 $types[$type] = $type;
00209 }
00210
00211 if (!empty($types)) {
00212 $filters['type'] = array(
00213 'title' => t('Type'),
00214 'where' => "w.type = '%s'",
00215 'options' => $types,
00216 );
00217 }
00218
00219 $filters['severity'] = array(
00220 'title' => t('Severity'),
00221 'where' => 'w.severity = %d',
00222 'options' => watchdog_severity_levels(),
00223 );
00224
00225 return $filters;
00226 }
00227
00234 function _dblog_format_message($dblog) {
00235
00236 if ($dblog->variables === 'N;') {
00237 return $dblog->message;
00238 }
00239
00240 else {
00241 return t($dblog->message, unserialize($dblog->variables));
00242 }
00243 }
00244
00245
00253 function dblog_filter_form() {
00254 $session = &$_SESSION['dblog_overview_filter'];
00255 $session = is_array($session) ? $session : array();
00256 $filters = dblog_filters();
00257
00258 $form['filters'] = array(
00259 '#type' => 'fieldset',
00260 '#title' => t('Filter log messages'),
00261 '#theme' => 'dblog_filters',
00262 '#collapsible' => TRUE,
00263 '#collapsed' => empty($session),
00264 );
00265 foreach ($filters as $key => $filter) {
00266 $form['filters']['status'][$key] = array(
00267 '#title' => $filter['title'],
00268 '#type' => 'select',
00269 '#multiple' => TRUE,
00270 '#size' => 8,
00271 '#options' => $filter['options'],
00272 );
00273 if (!empty($session[$key])) {
00274 $form['filters']['status'][$key]['#default_value'] = $session[$key];
00275 }
00276 }
00277
00278 $form['filters']['buttons']['submit'] = array(
00279 '#type' => 'submit',
00280 '#value' => t('Filter'),
00281 );
00282 if (!empty($session)) {
00283 $form['filters']['buttons']['reset'] = array(
00284 '#type' => 'submit',
00285 '#value' => t('Reset')
00286 );
00287 }
00288
00289 return $form;
00290 }
00291
00295 function dblog_filter_form_validate($form, &$form_state) {
00296 if ($form_state['values']['op'] == t('Filter') && empty($form_state['values']['type']) && empty($form_state['values']['severity'])) {
00297 form_set_error('type', t('You must select something to filter by.'));
00298 }
00299 }
00300
00304 function dblog_filter_form_submit($form, &$form_state) {
00305 $op = $form_state['values']['op'];
00306 $filters = dblog_filters();
00307 switch ($op) {
00308 case t('Filter'):
00309 foreach ($filters as $name => $filter) {
00310 if (isset($form_state['values'][$name])) {
00311 $_SESSION['dblog_overview_filter'][$name] = $form_state['values'][$name];
00312 }
00313 }
00314 break;
00315 case t('Reset'):
00316 $_SESSION['dblog_overview_filter'] = array();
00317 break;
00318 }
00319 return 'admin/reports/dblog';
00320 }