dblog.admin.inc

  1. 7.x drupal-7.x/modules/dblog/dblog.admin.inc
  2. 6.x drupal-6.x/modules/dblog/dblog.admin.inc

Administrative page callbacks for the dblog module.

File

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