? 645978-dblog-features.patch
Index: mongodb_watchdog.admin.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/mongodb/mongodb_watchdog/mongodb_watchdog.admin.inc,v
retrieving revision 1.1
diff -u -p -r1.1 mongodb_watchdog.admin.inc
--- mongodb_watchdog.admin.inc	29 Nov 2009 22:42:13 -0000	1.1
+++ mongodb_watchdog.admin.inc	29 Nov 2009 23:40:28 -0000
@@ -12,6 +12,30 @@
  *   Use exposed filter like dblog.
  */
 function mongodb_watchdog_overview() {
+  $filter = mongodb_watchdog_build_filter_query();
+  dsm($filter);
+
+  $rows = array();
+  $icons = array(
+    WATCHDOG_DEBUG    => '',
+    WATCHDOG_INFO     => '',
+    WATCHDOG_NOTICE   => '',
+    WATCHDOG_WARNING  => theme('image', array('path' => 'misc/watchdog-warning.png', 'alt' => t('warning'), 'title' => t('warning'))),
+    WATCHDOG_ERROR    => theme('image', array('path' => 'misc/watchdog-error.png', 'alt' => t('error'), 'title' => t('error'))),
+    WATCHDOG_CRITICAL => theme('image', array('path' => 'misc/watchdog-error.png', 'alt' => t('critical'), 'title' => t('critical'))),
+    WATCHDOG_ALERT    => theme('image', array('path' => 'misc/watchdog-error.png', 'alt' => t('alert'), 'title' => t('alert'))),
+    WATCHDOG_EMERG    => theme('image', array('path' => 'misc/watchdog-error.png', 'alt' => t('emergency'), 'title' => t('emergency'))),
+  );
+  $classes = array(
+    WATCHDOG_DEBUG    => 'mongodb_watchdog-debug',
+    WATCHDOG_INFO     => 'mongodb_watchdog-info',
+    WATCHDOG_NOTICE   => 'mongodb_watchdog-notice',
+    WATCHDOG_WARNING  => 'mongodb_watchdog-warning',
+    WATCHDOG_ERROR    => 'mongodb_watchdog-error',
+    WATCHDOG_CRITICAL => 'mongodb_watchdog-critical',
+    WATCHDOG_ALERT    => 'mongodb_watchdog-alert',
+    WATCHDOG_EMERG    => 'mongodb_watchdog-emerg',
+  );
 
   // Load settings.
   $server_name  = variable_get('mongodb_watchdog_connection', 'localhost');
@@ -23,39 +47,234 @@ function mongodb_watchdog_overview() {
     ->selectDB($db_name)
     ->selectCollection($collection_name);
   $cursor = $collection->find();
+
+  $build['mongodb_watchdog_filter_form'] = drupal_get_form('mongodb_watchdog_filter_form');
+  $build['mongodb_watchdog_clear_log_form'] = drupal_get_form('mongodb_watchdog_clear_log_form');
+
+  $header = array(
+    '', // Icon column.
+    array('data' => t('Type')),
+    array('data' => t('Date')),
+    t('Message'),
+    t('User'),
+    t('Operations'),
+  );
+
   $rows = array();
   foreach ($cursor as $id => $value) {
-    $rows[$id] = array(print_r($value, TRUE), print_r(array_keys($value), TRUE));
     $rows[$id] = array(
-      // $id,
-      $value['type'],
+      $icons[$value['severity']],
+      t($value['type']),
       format_date($value['timestamp'], 'short'),
-      isset($value['variables'])
-        ? t($value['message'], $value['variables'])
-        : t($value['message']),
-      $value['severity'],
+      truncate_utf8(_mongodb_watchdog_format_message((object) $value), 56, TRUE, TRUE),
+      theme('username', array('account' => (object) $value['user'])),
       $value['link'],
-      $value['user'],
-      l($value['request_uri'], $value['request_uri']),
-      l($value['referer'], $value['referer']),
-      $value['ip'],
     );
   }
-  $header = array(
-    // t('ID'),
-    array('data' => t('Type')),
-    array('data' => t('Date')),
-    t('Message'),
-    t('Severity'),
-    t('Link'),
-    t('User'),
-    t('URL'),
-    t('Referrer'),
-    t('IP'),
-  );
-  $ret = theme('table', array(
-    'header' => $header,
-    'rows'   => $rows,
-  ));
-  return $ret;
-}
\ No newline at end of file
+
+  $build['mongodb_watchdog_table'] = array(
+    '#theme' => 'table',
+    '#header' => $header,
+    '#rows' => $rows,
+    '#attributes' => array('id' => 'admin-mongodb_watchdog'),
+  );
+  return $build;
+}
+
+/**
+ * Formats a log message for display.
+ *
+ * @param $dblog
+ *   An object with at least the message and variables properties
+ */
+function _mongodb_watchdog_format_message($mongodb_watchdog) {
+  // Legacy messages and user specified text
+  if (!$mongodb_watchdog->variables) {
+    return $mongodb_watchdog->message;
+  }
+  // Message to translate with injected variables
+  else {
+    return t($mongodb_watchdog->message, $mongodb_watchdog->variables);
+  }
+}
+
+function mongodb_watchdog_filter_form($form) {
+  $filters = mongodb_watchdog_filters();
+
+  $form['filters'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Filter log messages'),
+    //'#theme' => 'dblog_filters',
+    '#collapsible' => TRUE,
+    '#collapsed' => empty($session),
+  );
+  foreach ($filters as $key => $filter) {
+    $form['filters']['status'][$key] = array(
+      '#title' => $filter['title'],
+      '#type' => 'select',
+      '#multiple' => TRUE,
+      '#size' => 8,
+      '#options' => $filter['options'],
+    );
+    if (!empty($_SESSION['mongodb_watchdog_overview_filter'][$key])) {
+      $form['filters']['status'][$key]['#default_value'] = $_SESSION['mongodb_watchdog_overview_filter'][$key];
+    }
+  }
+
+  $form['filters']['buttons']['submit'] = array(
+    '#type' => 'submit',
+    '#value' => t('Filter'),
+  );
+  if (!empty($_SESSION['mongodb_watchdog_overview_filter'])) {
+    $form['filters']['buttons']['reset'] = array(
+      '#type' => 'submit',
+      '#value' => t('Reset')
+    );
+  }
+
+  return $form;
+}
+
+/**
+ * Validate result from mongodb_watchdog administration filter form.
+ */
+function mongodb_watchdog_filter_form_validate($form, &$form_state) {
+  if ($form_state['values']['op'] == t('Filter') && empty($form_state['values']['type']) && empty($form_state['values']['severity'])) {
+    form_set_error('type', t('You must select something to filter by.'));
+  }
+}
+
+/**
+ * Process result from mongodb_watchdog administration filter form.
+ */
+function mongodb_watchdog_filter_form_submit($form, &$form_state) {
+  $op = $form_state['values']['op'];
+  $filters = mongodb_watchdog_filters();
+  switch ($op) {
+    case t('Filter'):
+      foreach ($filters as $name => $filter) {
+        if (isset($form_state['values'][$name])) {
+          $_SESSION['mongodb_watchdog_overview_filter'][$name] = $form_state['values'][$name];
+        }
+      }
+      break;
+    case t('Reset'):
+      $_SESSION['mongodb_watchdog_overview_filter'] = array();
+      break;
+  }
+  return 'admin/reports/mongodb';
+}
+
+/*
+ * List mongodb_watchdog administration filters that can be applied.
+ */
+function mongodb_watchdog_filters() {
+  $filters = array();
+
+  foreach (_mongodb_watchdog_get_message_types() as $type) {
+    $types[$type] = $type;
+  }
+
+  if (!empty($types)) {
+    $filters['type'] = array(
+      'title' => t('Type'),
+      'where' => "w.type = ?",
+      'options' => $types,
+    );
+  }
+
+  $filters['severity'] = array(
+    'title' => t('Severity'),
+    'where' => 'w.severity = ?',
+    'options' => watchdog_severity_levels(),
+  );
+
+  return $filters;
+}
+
+function _mongodb_watchdog_get_message_types() {
+
+  $server_name  = variable_get('mongodb_watchdog_connection', 'localhost');
+  $db_name = variable_get('mongodb_watchdog_dbname', 'drupal');
+  $collection_name = variable_get('mongodb_watchdog_collectionname', 'watchdog');
+  $mongo = new Mongo($server_name);
+
+  $db = $mongo
+    ->selectDB($db_name);
+
+  $result = $db->command(array("distinct" => $collection_name, "key" => "type"));
+  return $result['values'];
+}
+
+/**
+ * Return form for mongodb_watchdog clear button.
+ *
+ * @ingroup forms
+ * @see dblog_clear_log_submit()
+ */
+function mongodb_watchdog_clear_log_form($form) {
+  $form['mongodb_watchdog_clear'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Clear log messages'),
+    '#description' => t('This will permanently remove the log messages from the database.'),
+    '#collapsible' => TRUE,
+    '#collapsed' => TRUE,
+  );
+  $form['mongodb_watchdog_clear']['clear'] = array(
+    '#type' => 'submit',
+    '#value' => t('Clear log messages'),
+    '#submit' => array('mongodb_watchdog_clear_log_submit'),
+  );
+
+  return $form;
+}
+
+/**
+ * Submit callback: clear database with log messages.
+ */
+function mongodb_watchdog_clear_log_submit() {
+
+  $server_name  = variable_get('mongodb_watchdog_connection', 'localhost');
+  $db_name = variable_get('mongodb_watchdog_dbname', 'drupal');
+  $collection_name = variable_get('mongodb_watchdog_collectionname', 'watchdog');
+  $mongo = new Mongo($server_name);
+
+  $db = $mongo
+    ->selectDB($db_name);
+
+  $db->dropCollection($collection_name);
+
+  drupal_set_message(t('MongoDB log cleared.'));
+}
+
+/**
+ * TODO
+ *   Port this function to mongodb syntax.
+ */
+function mongodb_watchdog_build_filter_query() {
+  if (empty($_SESSION['mongodb_watchdog_overview_filter'])) {
+    return;
+  }
+
+  $filters = mongodb_watchdog_filters();
+
+  // Build query
+  $where = $args = array();
+  foreach ($_SESSION['mongodb_watchdog_overview_filter'] as $key => $filter) {
+    $filter_where = array();
+    foreach ($filter as $value) {
+      $filter_where[] = $filters[$key]['where'];
+      $args[] = $value;
+    }
+    if (!empty($filter_where)) {
+      $where[] = '(' . implode(' OR ', $filter_where) . ')';
+    }
+  }
+  $where = !empty($where) ? implode(' AND ', $where) : '';
+
+  return array(
+    'where' => $where,
+    'args' => $args,
+  );
+}
+
Index: mongodb_watchdog.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/mongodb/mongodb_watchdog/mongodb_watchdog.module,v
retrieving revision 1.1
diff -u -p -r1.1 mongodb_watchdog.module
--- mongodb_watchdog.module	29 Nov 2009 22:42:13 -0000	1.1
+++ mongodb_watchdog.module	29 Nov 2009 23:40:28 -0000
@@ -56,7 +56,7 @@ function mongodb_watchdog_form_system_lo
   $form['mongodb_watchdog']['mongodb_watchdog_connection'] = array(
     '#title' => t('MongoDB Connection URL'),
     '#type' => 'textfield',
-    '#default_value' => variable_get('mongodb_watchdog_connection', ''),
+    '#default_value' => variable_get('mongodb_watchdog_connection', NULL),
   );
   $form['mongodb_watchdog']['mongodb_watchdog_dbname'] = array(
     '#title' => t('MongoDB Database name'),
@@ -75,7 +75,7 @@ function mongodb_watchdog_form_system_lo
 /**
  * Implement hook_watchdog().
  */
-function mongodb_watchdog_watchdog($log_entry) {
+function mongodb_watchdog_watchdog(array $log_entry) {
   // TODO Figure out how to use a static but also close the session at the end.
   //static $mongo;
 
@@ -88,3 +88,4 @@ function mongodb_watchdog_watchdog($log_
 
   $mongo->close();
 }
+
