--- faceted_search_stats.info	2009-10-12 13:53:15.352871000 +0800
+++ faceted_search_stats.new.info	2009-10-12 13:46:08.222326400 +0800
@@ -0,0 +1,8 @@
+; $Id: faceted_search_stats.info,v 1.0 $
+name = Faceted Search Statistics
+description = Provides a "top searches" report page for the faceted search modules
+dependencies[] = faceted_search
+dependencies[] = dblog
+package = Faceted Search
+core = 6.x
+php = 5.1
--- faceted_search_stats.module	2009-10-12 13:53:18.380939000 +0800
+++ faceted_search_stats.new.module	2009-10-12 13:43:18.650850700 +0800
@@ -0,0 +1,119 @@
+<?php
+// $Id: faceted_search_stats.module,v 1.0 $
+
+
+/**
+ * Implementation of hook_menu().
+ */
+function fs_stats_menu() {
+  $items['admin/reports/faceted_search'] = array(
+    'title' => 'Top faceted searches',
+    'description' => 'View most popular faceted search keywords and category terms.',
+    'page callback' => 'fs_stats_top',
+    'page arguments' => array('faceted_search'),
+    'access arguments' => array('access site reports'),
+  );
+  return $items;
+}
+
+
+/**
+ * Customized version of dblog_top function in dblog module
+ * Menu callback; generic function to display a page of the most frequent
+ * dblog events of a specified type.
+ */
+function fs_stats_top($type) {
+  global $fs_stats_users;
+
+  // cache the users table: user module doesn't do it at present
+  $fs_stats_users = array();
+  $result = db_query('SELECT uid,name FROM {users}');
+  while ($row = db_fetch_array($result)) {
+    $fs_stats_users[$row['uid']] = $row['name'];
+  }
+  // add anonymous user's name from site information
+  $fs_stats_users[0] = variable_get('anonymous', 'Anonymous');
+
+  $header = array(
+    array('data' => t('Count'), 'field' => 'count', 'sort' => 'desc'),
+    array('data' => t('Message'), 'field' => 'message')
+  );
+
+  $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);
+
+  $rows = array();
+  while ($dblog = db_fetch_object($result)) {
+    $rows[] = array($dblog->count, fs_stats_format_message($dblog));
+  }
+
+  if (empty($rows)) {
+    $rows[] = array(array('data' => t('No log messages available.'), 'colspan' => 2));
+  }
+
+  $output  = theme('table', $header, $rows);
+  $output .= theme('pager', NULL, 30, 0);
+
+  $fs_stats_users = array();
+  return $output;
+}
+
+
+/**
+ * Customized version of _dblog_format_message function in dblog module
+ * Formats a log message for display.
+ *
+ * @param $dblog
+ *   An object with at least the message and variables properties
+ */
+function fs_stats_format_message($dblog) {
+  // Legacy messages and user specified text
+  if ($dblog->variables === 'N;') {
+    return $dblog->message;
+  }
+  // Message to translate with injected variables
+  else {
+    $msgs = unserialize($dblog->variables);
+    if (isset($msgs['%text'])) {
+      $msgs['%text'] = preg_replace_callback(
+        '/(content_type:(\w+)|taxonomy:(\d+)|author:(\d+))/',
+        create_function(
+          '$matches',
+          'global $fs_stats_users;
+
+           if (!empty($matches[2])) {
+             $ctype = node_get_types("type", $matches[2]);
+             if (!empty($ctype)) {
+               return "content_type:\'" . $ctype->name . "\'";
+             }
+             else {
+               return $matches[0];
+             }
+           }
+           elseif (!empty($matches[3]) && module_exists("taxonomy")) {
+             $term = taxonomy_get_term($matches[3]);
+             if (!empty($term)) {
+               return "taxonomy:\'" . $term->name . "\'";
+             }
+             else {
+               return $matches[0];
+             }
+           }
+           elseif (is_numeric($matches[4])) {
+             $user = $fs_stats_users[$matches[4]];
+             if (!empty($user)) {
+               return "author:\'" . $user . "\'";
+             }
+             else {
+               return $matches[0];
+             }
+           }
+           else {
+             return $matches[0];
+           }
+          '),
+        $msgs['%text']
+      );
+    }
+    return t($dblog->message, $msgs);
+  }
+}
