dblog.admin.inc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. <?php
  2. /**
  3. * @file
  4. * Administrative page callbacks for the dblog module.
  5. */
  6. /**
  7. * Menu callback; displays a listing of log messages.
  8. *
  9. * Messages are truncated at 56 chars. Full-length message could be viewed at
  10. * the message details page.
  11. *
  12. * @ingroup logging_severity_levels
  13. */
  14. function dblog_overview() {
  15. $filter = dblog_build_filter_query();
  16. $rows = array();
  17. $classes = array(
  18. WATCHDOG_DEBUG => 'dblog-debug',
  19. WATCHDOG_INFO => 'dblog-info',
  20. WATCHDOG_NOTICE => 'dblog-notice',
  21. WATCHDOG_WARNING => 'dblog-warning',
  22. WATCHDOG_ERROR => 'dblog-error',
  23. WATCHDOG_CRITICAL => 'dblog-critical',
  24. WATCHDOG_ALERT => 'dblog-alert',
  25. WATCHDOG_EMERGENCY => 'dblog-emerg',
  26. );
  27. $build['dblog_filter_form'] = drupal_get_form('dblog_filter_form');
  28. $build['dblog_clear_log_form'] = drupal_get_form('dblog_clear_log_form');
  29. $header = array(
  30. '', // Icon column.
  31. array('data' => t('Type'), 'field' => 'w.type'),
  32. array('data' => t('Date'), 'field' => 'w.wid', 'sort' => 'desc'),
  33. t('Message'),
  34. array('data' => t('User'), 'field' => 'u.name'),
  35. array('data' => t('Operations')),
  36. );
  37. $query = db_select('watchdog', 'w')->extend('PagerDefault')->extend('TableSort');
  38. $query->leftJoin('users', 'u', 'w.uid = u.uid');
  39. $query
  40. ->fields('w', array('wid', 'uid', 'severity', 'type', 'timestamp', 'message', 'variables', 'link'))
  41. ->addField('u', 'name');
  42. if (!empty($filter['where'])) {
  43. $query->where($filter['where'], $filter['args']);
  44. }
  45. $result = $query
  46. ->limit(50)
  47. ->orderByHeader($header)
  48. ->execute();
  49. foreach ($result as $dblog) {
  50. $rows[] = array('data' =>
  51. array(
  52. // Cells
  53. array('class' => 'icon'),
  54. t($dblog->type),
  55. format_date($dblog->timestamp, 'short'),
  56. theme('dblog_message', array('event' => $dblog, 'link' => TRUE)),
  57. theme('username', array('account' => $dblog)),
  58. filter_xss($dblog->link),
  59. ),
  60. // Attributes for tr
  61. 'class' => array(drupal_html_class('dblog-' . $dblog->type), $classes[$dblog->severity]),
  62. );
  63. }
  64. $build['dblog_table'] = array(
  65. '#theme' => 'table',
  66. '#header' => $header,
  67. '#rows' => $rows,
  68. '#attributes' => array('id' => 'admin-dblog'),
  69. '#empty' => t('No log messages available.'),
  70. );
  71. $build['dblog_pager'] = array('#theme' => 'pager');
  72. return $build;
  73. }
  74. /**
  75. * Menu callback; generic function to display a page of the most frequent events.
  76. *
  77. * Messages are not truncated because events from this page have no detail view.
  78. *
  79. * @param $type
  80. * type of dblog events to display.
  81. */
  82. function dblog_top($type) {
  83. $header = array(
  84. array('data' => t('Count'), 'field' => 'count', 'sort' => 'desc'),
  85. array('data' => t('Message'), 'field' => 'message')
  86. );
  87. $count_query = db_select('watchdog');
  88. $count_query->addExpression('COUNT(DISTINCT(message))');
  89. $count_query->condition('type', $type);
  90. $query = db_select('watchdog', 'w')->extend('PagerDefault')->extend('TableSort');
  91. $query->addExpression('COUNT(wid)', 'count');
  92. $query = $query
  93. ->fields('w', array('message', 'variables'))
  94. ->condition('w.type', $type)
  95. ->groupBy('message')
  96. ->groupBy('variables')
  97. ->limit(30)
  98. ->orderByHeader($header);
  99. $query->setCountQuery($count_query);
  100. $result = $query->execute();
  101. $rows = array();
  102. foreach ($result as $dblog) {
  103. $rows[] = array($dblog->count, theme('dblog_message', array('event' => $dblog)));
  104. }
  105. $build['dblog_top_table'] = array(
  106. '#theme' => 'table',
  107. '#header' => $header,
  108. '#rows' => $rows,
  109. '#empty' => t('No log messages available.'),
  110. );
  111. $build['dblog_top_pager'] = array('#theme' => 'pager');
  112. return $build;
  113. }
  114. /**
  115. * Menu callback; displays details about a log message.
  116. */
  117. function dblog_event($id) {
  118. $severity = watchdog_severity_levels();
  119. $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();
  120. if ($dblog = $result) {
  121. $rows = array(
  122. array(
  123. array('data' => t('Type'), 'header' => TRUE),
  124. t($dblog->type),
  125. ),
  126. array(
  127. array('data' => t('Date'), 'header' => TRUE),
  128. format_date($dblog->timestamp, 'long'),
  129. ),
  130. array(
  131. array('data' => t('User'), 'header' => TRUE),
  132. theme('username', array('account' => $dblog)),
  133. ),
  134. array(
  135. array('data' => t('Location'), 'header' => TRUE),
  136. l($dblog->location, $dblog->location),
  137. ),
  138. array(
  139. array('data' => t('Referrer'), 'header' => TRUE),
  140. l($dblog->referer, $dblog->referer),
  141. ),
  142. array(
  143. array('data' => t('Message'), 'header' => TRUE),
  144. theme('dblog_message', array('event' => $dblog)),
  145. ),
  146. array(
  147. array('data' => t('Severity'), 'header' => TRUE),
  148. $severity[$dblog->severity],
  149. ),
  150. array(
  151. array('data' => t('Hostname'), 'header' => TRUE),
  152. check_plain($dblog->hostname),
  153. ),
  154. array(
  155. array('data' => t('Operations'), 'header' => TRUE),
  156. $dblog->link,
  157. ),
  158. );
  159. $build['dblog_table'] = array(
  160. '#theme' => 'table',
  161. '#rows' => $rows,
  162. '#attributes' => array('class' => array('dblog-event')),
  163. );
  164. return $build;
  165. }
  166. else {
  167. return '';
  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. $filters = dblog_filters();
  178. // Build query
  179. $where = $args = array();
  180. foreach ($_SESSION['dblog_overview_filter'] as $key => $filter) {
  181. $filter_where = array();
  182. foreach ($filter as $value) {
  183. $filter_where[] = $filters[$key]['where'];
  184. $args[] = $value;
  185. }
  186. if (!empty($filter_where)) {
  187. $where[] = '(' . implode(' OR ', $filter_where) . ')';
  188. }
  189. }
  190. $where = !empty($where) ? implode(' AND ', $where) : '';
  191. return array(
  192. 'where' => $where,
  193. 'args' => $args,
  194. );
  195. }
  196. /**
  197. * List dblog administration filters that can be applied.
  198. */
  199. function dblog_filters() {
  200. $filters = array();
  201. foreach (_dblog_get_message_types() as $type) {
  202. $types[$type] = t($type);
  203. }
  204. if (!empty($types)) {
  205. $filters['type'] = array(
  206. 'title' => t('Type'),
  207. 'where' => "w.type = ?",
  208. 'options' => $types,
  209. );
  210. }
  211. $filters['severity'] = array(
  212. 'title' => t('Severity'),
  213. 'where' => 'w.severity = ?',
  214. 'options' => watchdog_severity_levels(),
  215. );
  216. return $filters;
  217. }
  218. /**
  219. * Returns HTML for a log message.
  220. *
  221. * @param $variables
  222. * An associative array containing:
  223. * - event: An object with at least the message and variables properties.
  224. * - link: (optional) Format message as link, event->wid is required.
  225. *
  226. * @ingroup themeable
  227. */
  228. function theme_dblog_message($variables) {
  229. $output = '';
  230. $event = $variables['event'];
  231. // Check for required properties.
  232. if (isset($event->message) && isset($event->variables)) {
  233. // Messages without variables or user specified text.
  234. if ($event->variables === 'N;') {
  235. $output = $event->message;
  236. }
  237. // Message to translate with injected variables.
  238. else {
  239. $output = t($event->message, unserialize($event->variables));
  240. }
  241. if ($variables['link'] && isset($event->wid)) {
  242. // Truncate message to 56 chars.
  243. $output = truncate_utf8(filter_xss($output, array()), 56, TRUE, TRUE);
  244. $output = l($output, 'admin/reports/event/' . $event->wid, array('html' => TRUE));
  245. }
  246. }
  247. return $output;
  248. }
  249. /**
  250. * Return form for dblog administration filters.
  251. *
  252. * @ingroup forms
  253. * @see dblog_filter_form_submit()
  254. * @see dblog_filter_form_validate()
  255. */
  256. function dblog_filter_form($form) {
  257. $filters = dblog_filters();
  258. $form['filters'] = array(
  259. '#type' => 'fieldset',
  260. '#title' => t('Filter log messages'),
  261. '#collapsible' => TRUE,
  262. '#collapsed' => empty($_SESSION['dblog_overview_filter']),
  263. );
  264. foreach ($filters as $key => $filter) {
  265. $form['filters']['status'][$key] = array(
  266. '#title' => $filter['title'],
  267. '#type' => 'select',
  268. '#multiple' => TRUE,
  269. '#size' => 8,
  270. '#options' => $filter['options'],
  271. );
  272. if (!empty($_SESSION['dblog_overview_filter'][$key])) {
  273. $form['filters']['status'][$key]['#default_value'] = $_SESSION['dblog_overview_filter'][$key];
  274. }
  275. }
  276. $form['filters']['actions'] = array(
  277. '#type' => 'actions',
  278. '#attributes' => array('class' => array('container-inline')),
  279. );
  280. $form['filters']['actions']['submit'] = array(
  281. '#type' => 'submit',
  282. '#value' => t('Filter'),
  283. );
  284. if (!empty($_SESSION['dblog_overview_filter'])) {
  285. $form['filters']['actions']['reset'] = array(
  286. '#type' => 'submit',
  287. '#value' => t('Reset')
  288. );
  289. }
  290. return $form;
  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. * Process result from dblog administration filter form.
  302. */
  303. function dblog_filter_form_submit($form, &$form_state) {
  304. $op = $form_state['values']['op'];
  305. $filters = dblog_filters();
  306. switch ($op) {
  307. case t('Filter'):
  308. foreach ($filters as $name => $filter) {
  309. if (isset($form_state['values'][$name])) {
  310. $_SESSION['dblog_overview_filter'][$name] = $form_state['values'][$name];
  311. }
  312. }
  313. break;
  314. case t('Reset'):
  315. $_SESSION['dblog_overview_filter'] = array();
  316. break;
  317. }
  318. return 'admin/reports/dblog';
  319. }
  320. /**
  321. * Return form for dblog clear button.
  322. *
  323. * @ingroup forms
  324. * @see dblog_clear_log_submit()
  325. */
  326. function dblog_clear_log_form($form) {
  327. $form['dblog_clear'] = array(
  328. '#type' => 'fieldset',
  329. '#title' => t('Clear log messages'),
  330. '#description' => t('This will permanently remove the log messages from the database.'),
  331. '#collapsible' => TRUE,
  332. '#collapsed' => TRUE,
  333. );
  334. $form['dblog_clear']['clear'] = array(
  335. '#type' => 'submit',
  336. '#value' => t('Clear log messages'),
  337. '#submit' => array('dblog_clear_log_submit'),
  338. );
  339. return $form;
  340. }
  341. /**
  342. * Submit callback: clear database with log messages.
  343. */
  344. function dblog_clear_log_submit() {
  345. $_SESSION['dblog_overview_filter'] = array();
  346. db_delete('watchdog')->execute();
  347. drupal_set_message(t('Database log cleared.'));
  348. }