Projet

Général

Profil

Paste
Télécharger (11,5 ko) Statistiques
| Branche: | Révision:

root / drupal7 / modules / dblog / dblog.admin.inc @ db2d93dd

1
<?php
2

    
3
/**
4
 * @file
5
 * Administrative page callbacks for the Database Logging module.
6
 */
7

    
8
/**
9
 * Page callback: Displays a listing of database log messages.
10
 *
11
 * Messages are truncated at 56 chars. Full-length messages can be viewed on the
12
 * message details page.
13
 *
14
 * @see dblog_clear_log_form()
15
 * @see dblog_event()
16
 * @see dblog_filter_form()
17
 * @see dblog_menu()
18
 *
19
 * @ingroup logging_severity_levels
20
 */
21
function dblog_overview() {
22
  $filter = dblog_build_filter_query();
23
  $rows = array();
24
  $classes = array(
25
    WATCHDOG_DEBUG     => 'dblog-debug',
26
    WATCHDOG_INFO      => 'dblog-info',
27
    WATCHDOG_NOTICE    => 'dblog-notice',
28
    WATCHDOG_WARNING   => 'dblog-warning',
29
    WATCHDOG_ERROR     => 'dblog-error',
30
    WATCHDOG_CRITICAL  => 'dblog-critical',
31
    WATCHDOG_ALERT     => 'dblog-alert',
32
    WATCHDOG_EMERGENCY => 'dblog-emerg',
33
  );
34

    
35
  $build['dblog_filter_form'] = drupal_get_form('dblog_filter_form');
36
  $build['dblog_clear_log_form'] = drupal_get_form('dblog_clear_log_form');
37

    
38
  $header = array(
39
    '', // Icon column.
40
    array('data' => t('Type'), 'field' => 'w.type'),
41
    array('data' => t('Date'), 'field' => 'w.wid', 'sort' => 'desc'),
42
    t('Message'),
43
    array('data' => t('User'), 'field' => 'u.name'),
44
    array('data' => t('Operations')),
45
  );
46

    
47
  $query = db_select('watchdog', 'w')->extend('PagerDefault')->extend('TableSort');
48
  $query->leftJoin('users', 'u', 'w.uid = u.uid');
49
  $query
50
    ->fields('w', array('wid', 'uid', 'severity', 'type', 'timestamp', 'message', 'variables', 'link'))
51
    ->addField('u', 'name');
52
  if (!empty($filter['where'])) {
53
    $query->where($filter['where'], $filter['args']);
54
  }
55
  $result = $query
56
    ->limit(50)
57
    ->orderByHeader($header)
58
    ->execute();
59

    
60
  foreach ($result as $dblog) {
61
    $rows[] = array('data' =>
62
      array(
63
        // Cells
64
        array('class' => 'icon'),
65
        t($dblog->type),
66
        format_date($dblog->timestamp, 'short'),
67
        theme('dblog_message', array('event' => $dblog, 'link' => TRUE)),
68
        theme('username', array('account' => $dblog)),
69
        filter_xss($dblog->link),
70
      ),
71
      // Attributes for tr
72
      'class' => array(drupal_html_class('dblog-' . $dblog->type), $classes[$dblog->severity]),
73
    );
74
  }
75

    
76
  $build['dblog_table'] = array(
77
    '#theme' => 'table',
78
    '#header' => $header,
79
    '#rows' => $rows,
80
    '#attributes' => array('id' => 'admin-dblog'),
81
    '#empty' => t('No log messages available.'),
82
  );
83
  $build['dblog_pager'] = array('#theme' => 'pager');
84

    
85
  return $build;
86
}
87

    
88
/**
89
 * Page callback: Shows the most frequent log messages of a given event type.
90
 *
91
 * Messages are not truncated on this page because events detailed herein do not
92
 * have links to a detailed view.
93
 *
94
 * @param string $type
95
 *   Type of database log events to display (e.g., 'search').
96
 *
97
 * @return array
98
 *   A build array in the format expected by drupal_render().
99
 *
100
 * @see dblog_menu()
101
 */
102
function dblog_top($type) {
103

    
104
  $header = array(
105
    array('data' => t('Count'), 'field' => 'count', 'sort' => 'desc'),
106
    array('data' => t('Message'), 'field' => 'message')
107
  );
108
  $count_query = db_select('watchdog');
109
  $count_query->addExpression('COUNT(DISTINCT(message))');
110
  $count_query->condition('type', $type);
111

    
112
  $query = db_select('watchdog', 'w')->extend('PagerDefault')->extend('TableSort');
113
  $query->addExpression('COUNT(wid)', 'count');
114
  $query = $query
115
    ->fields('w', array('message', 'variables'))
116
    ->condition('w.type', $type)
117
    ->groupBy('message')
118
    ->groupBy('variables')
119
    ->limit(30)
120
    ->orderByHeader($header);
121
  $query->setCountQuery($count_query);
122
  $result = $query->execute();
123

    
124
  $rows = array();
125
  foreach ($result as $dblog) {
126
    $rows[] = array($dblog->count, theme('dblog_message', array('event' => $dblog)));
127
  }
128

    
129
  $build['dblog_top_table']  = array(
130
    '#theme' => 'table',
131
    '#header' => $header,
132
    '#rows' => $rows,
133
    '#empty' => t('No log messages available.'),
134
  );
135
  $build['dblog_top_pager'] = array('#theme' => 'pager');
136

    
137
  return $build;
138
}
139

    
140
/**
141
 * Page callback: Displays details about a specific database log message.
142
 *
143
 * @param int $id
144
 *   Unique ID of the database log message.
145
 *
146
 * @return array|string
147
 *   If the ID is located in the Database Logging table, a build array in the
148
 *   format expected by drupal_render(); otherwise, an empty string.
149
 *
150
 * @see dblog_menu()
151
 */
152
function dblog_event($id) {
153
  $severity = watchdog_severity_levels();
154
  $result = db_query('SELECT w.*, u.name, u.uid FROM {watchdog} w INNER JOIN {users} u ON w.uid = u.uid WHERE w.wid = :id', array(':id' => $id))->fetchObject();
155
  if ($dblog = $result) {
156
    $rows = array(
157
      array(
158
        array('data' => t('Type'), 'header' => TRUE),
159
        t($dblog->type),
160
      ),
161
      array(
162
        array('data' => t('Date'), 'header' => TRUE),
163
        format_date($dblog->timestamp, 'long'),
164
      ),
165
      array(
166
        array('data' => t('User'), 'header' => TRUE),
167
        theme('username', array('account' => $dblog)),
168
      ),
169
      array(
170
        array('data' => t('Location'), 'header' => TRUE),
171
        l($dblog->location, $dblog->location),
172
      ),
173
      array(
174
        array('data' => t('Referrer'), 'header' => TRUE),
175
        l($dblog->referer, $dblog->referer),
176
      ),
177
      array(
178
        array('data' => t('Message'), 'header' => TRUE),
179
        theme('dblog_message', array('event' => $dblog)),
180
      ),
181
      array(
182
        array('data' => t('Severity'), 'header' => TRUE),
183
        $severity[$dblog->severity],
184
      ),
185
      array(
186
        array('data' => t('Hostname'), 'header' => TRUE),
187
        check_plain($dblog->hostname),
188
      ),
189
      array(
190
        array('data' => t('Operations'), 'header' => TRUE),
191
        $dblog->link,
192
      ),
193
    );
194
    $build['dblog_table'] = array(
195
      '#theme' => 'table',
196
      '#rows' => $rows,
197
      '#attributes' => array('class' => array('dblog-event')),
198
    );
199
    return $build;
200
  }
201
  else {
202
    return '';
203
  }
204
}
205

    
206
/**
207
 * Builds a query for database log administration filters based on session.
208
 *
209
 * @return array
210
 *   An associative array with keys 'where' and 'args'.
211
 */
212
function dblog_build_filter_query() {
213
  if (empty($_SESSION['dblog_overview_filter'])) {
214
    return;
215
  }
216

    
217
  $filters = dblog_filters();
218

    
219
  // Build query
220
  $where = $args = array();
221
  foreach ($_SESSION['dblog_overview_filter'] as $key => $filter) {
222
    $filter_where = array();
223
    foreach ($filter as $value) {
224
      $filter_where[] = $filters[$key]['where'];
225
      $args[] = $value;
226
    }
227
    if (!empty($filter_where)) {
228
      $where[] = '(' . implode(' OR ', $filter_where) . ')';
229
    }
230
  }
231
  $where = !empty($where) ? implode(' AND ', $where) : '';
232

    
233
  return array(
234
    'where' => $where,
235
    'args' => $args,
236
  );
237
}
238

    
239
/**
240
 * Creates a list of database log administration filters that can be applied.
241
 *
242
 * @return array
243
 *   Associative array of filters. The top-level keys are used as the form
244
 *   element names for the filters, and the values are arrays with the following
245
 *   elements:
246
 *   - title: Title of the filter.
247
 *   - where: The filter condition.
248
 *   - options: Array of options for the select list for the filter.
249
 */
250
function dblog_filters() {
251
  $filters = array();
252

    
253
  foreach (_dblog_get_message_types() as $type) {
254
    $types[$type] = t($type);
255
  }
256

    
257
  if (!empty($types)) {
258
    $filters['type'] = array(
259
      'title' => t('Type'),
260
      'where' => "w.type = ?",
261
      'options' => $types,
262
    );
263
  }
264

    
265
  $filters['severity'] = array(
266
    'title' => t('Severity'),
267
    'where' => 'w.severity = ?',
268
    'options' => watchdog_severity_levels(),
269
  );
270

    
271
  return $filters;
272
}
273

    
274
/**
275
 * Returns HTML for a log message.
276
 *
277
 * @param array $variables
278
 *   An associative array containing:
279
 *   - event: An object with at least the message and variables properties.
280
 *   - link: (optional) Format message as link, event->wid is required.
281
 *
282
 * @ingroup themeable
283
 */
284
function theme_dblog_message($variables) {
285
  $output = '';
286
  $event = $variables['event'];
287
  // Check for required properties.
288
  if (isset($event->message) && isset($event->variables)) {
289
    // Messages without variables or user specified text.
290
    if ($event->variables === 'N;') {
291
      $output = $event->message;
292
    }
293
    // Message to translate with injected variables.
294
    else {
295
      $output = t($event->message, unserialize($event->variables));
296
    }
297
    if ($variables['link'] && isset($event->wid)) {
298
      // Truncate message to 56 chars.
299
      $output = truncate_utf8(filter_xss($output, array()), 56, TRUE, TRUE);
300
      $output = l($output, 'admin/reports/event/' . $event->wid, array('html' => TRUE));
301
    }
302
  }
303
  return $output;
304
}
305

    
306
/**
307
 * Form constructor for the database logging filter form.
308
 *
309
 * @see dblog_filter_form_validate()
310
 * @see dblog_filter_form_submit()
311
 * @see dblog_overview()
312
 *
313
 * @ingroup forms
314
 */
315
function dblog_filter_form($form) {
316
  $filters = dblog_filters();
317

    
318
  $form['filters'] = array(
319
    '#type' => 'fieldset',
320
    '#title' => t('Filter log messages'),
321
    '#collapsible' => TRUE,
322
    '#collapsed' => empty($_SESSION['dblog_overview_filter']),
323
  );
324
  foreach ($filters as $key => $filter) {
325
    $form['filters']['status'][$key] = array(
326
      '#title' => $filter['title'],
327
      '#type' => 'select',
328
      '#multiple' => TRUE,
329
      '#size' => 8,
330
      '#options' => $filter['options'],
331
    );
332
    if (!empty($_SESSION['dblog_overview_filter'][$key])) {
333
      $form['filters']['status'][$key]['#default_value'] = $_SESSION['dblog_overview_filter'][$key];
334
    }
335
  }
336

    
337
  $form['filters']['actions'] = array(
338
    '#type' => 'actions',
339
    '#attributes' => array('class' => array('container-inline')),
340
  );
341
  $form['filters']['actions']['submit'] = array(
342
    '#type' => 'submit',
343
    '#value' => t('Filter'),
344
  );
345
  if (!empty($_SESSION['dblog_overview_filter'])) {
346
    $form['filters']['actions']['reset'] = array(
347
      '#type' => 'submit',
348
      '#value' => t('Reset')
349
    );
350
  }
351
  return $form;
352
}
353

    
354
/**
355
 * Form validation handler for dblog_filter_form().
356
 *
357
 * @see dblog_filter_form_submit()
358
 */
359
function dblog_filter_form_validate($form, &$form_state) {
360
  if ($form_state['values']['op'] == t('Filter') && empty($form_state['values']['type']) && empty($form_state['values']['severity'])) {
361
    form_set_error('type', t('You must select something to filter by.'));
362
  }
363
}
364

    
365
/**
366
 * Form submission handler for dblog_filter_form().
367
 *
368
 * @see dblog_filter_form_validate()
369
 */
370
function dblog_filter_form_submit($form, &$form_state) {
371
  $op = $form_state['values']['op'];
372
  $filters = dblog_filters();
373
  switch ($op) {
374
    case t('Filter'):
375
      foreach ($filters as $name => $filter) {
376
        if (isset($form_state['values'][$name])) {
377
          $_SESSION['dblog_overview_filter'][$name] = $form_state['values'][$name];
378
        }
379
      }
380
      break;
381
    case t('Reset'):
382
      $_SESSION['dblog_overview_filter'] = array();
383
      break;
384
  }
385
  return 'admin/reports/dblog';
386
}
387

    
388
/**
389
 * Form constructor for the form that clears out the log.
390
 *
391
 * @see dblog_clear_log_submit()
392
 * @ingroup forms
393
 */
394
function dblog_clear_log_form($form) {
395
  $form['dblog_clear'] = array(
396
    '#type' => 'fieldset',
397
    '#title' => t('Clear log messages'),
398
    '#description' => t('This will permanently remove the log messages from the database.'),
399
    '#collapsible' => TRUE,
400
    '#collapsed' => TRUE,
401
  );
402
  $form['dblog_clear']['clear'] = array(
403
    '#type' => 'submit',
404
    '#value' => t('Clear log messages'),
405
    '#submit' => array('dblog_clear_log_submit'),
406
  );
407

    
408
  return $form;
409
}
410

    
411
/**
412
 * Form submission handler for dblog_clear_log_form().
413
 */
414
function dblog_clear_log_submit() {
415
  $_SESSION['dblog_overview_filter'] = array();
416
  db_delete('watchdog')->execute();
417
  drupal_set_message(t('Database log cleared.'));
418
}