Index: modules/statistics/statistics.test =================================================================== RCS file: /cvs/drupal/drupal/modules/statistics/statistics.test,v retrieving revision 1.6 diff -u -r1.6 statistics.test --- modules/statistics/statistics.test 30 Dec 2008 16:43:18 -0000 1.6 +++ modules/statistics/statistics.test 27 Jan 2009 20:29:25 -0000 @@ -59,3 +59,48 @@ $this->assertRaw(t('The IP address %ip was deleted.', array('%ip' => $test_ip_address)), t('IP address deleted.')); } } + +class StatisticsAdminTestCase extends DrupalWebTestCase { + function getInfo() { + return array( + 'name' => t('Statistics administration'), + 'description' => t('Test admininstration page functionality.'), + 'group' => t('Statistics') + ); + } + + function setUp() { + parent::setUp('statistics'); + } + + /** + * Add some entries to access/referrer/recent hits/top pages and filter them. + */ + function testStatisticsAdmin() { + // Create some log entries. + // access log + $access_text1 = $this->randomName(); + $access_text2 = $this->randomName(); + db_query("INSERT INTO {accesslog} (title, path, url, hostname, uid, sid, timer, timestamp) values('%s', '%s', '%s', '%s', %d, '%s', %d, %d)", $access_text1, $access_text1, 'http://example.com', '192.168.1.1', '0', '10', '10', REQUEST_TIME); + db_query("INSERT INTO {accesslog} (title, path, url, hostname, uid, sid, timer, timestamp) values('%s', '%s', '%s', '%s', %d, '%s', %d, %d)", $access_text2, $access_text2, 'http://sub.example.com', '192.168.1.2', '0', '10', '10', REQUEST_TIME); + + // Create admin user to filter the logs. + $admin_user = $this->drupalCreateUser(array('access statistics')); + $this->drupalLogin($admin_user); + $this->drupalGet('admin/reports/hits'); + $this->assertText($access_text1, t("Found '%text' on recent hits page", array('%text' => $access_text1))); + $this->assertText($access_text2, t("Found '%text' on recent hits page", array('%text' => $access_text2))); + + // Filter the access log by page. + $edit = array(); + $edit['filter'] = 'page'; + $edit['page'] = $access_text1; + + $this->drupalPost('admin/reports/hits', $edit, t('Filter')); + + // Check if the correct log items show up. + $this->assertNoText($access_text1, t("Found '%text' not on filtered by page on recent hits page", array('%text' => $access_text1))); + $this->assertText($access_text2, t("Found '%text' on filtered by page on recent hits page", array('%text' => $access_text2))); + } + +} Index: modules/statistics/statistics.admin.inc =================================================================== RCS file: /cvs/drupal/drupal/modules/statistics/statistics.admin.inc,v retrieving revision 1.17 diff -u -r1.17 statistics.admin.inc --- modules/statistics/statistics.admin.inc 22 Jan 2009 12:01:24 -0000 1.17 +++ modules/statistics/statistics.admin.inc 27 Jan 2009 20:29:24 -0000 @@ -10,6 +10,9 @@ * Menu callback; presents the "recent hits" page. */ function statistics_recent_hits() { + $filtertype = 'hits'; + $filter = statistics_build_filter_query($filtertype); + $header = array( array('data' => t('Timestamp'), 'field' => 'a.timestamp', 'sort' => 'desc'), array('data' => t('Page'), 'field' => 'a.path'), @@ -17,9 +20,11 @@ array('data' => t('Operations')) ); - $sql = 'SELECT a.aid, a.path, a.title, a.uid, u.name, a.timestamp FROM {accesslog} a LEFT JOIN {users} u ON u.uid = a.uid' . tablesort_sql($header); + $sql = 'SELECT a.aid, a.path, a.title, a.uid, u.name, a.timestamp FROM {accesslog} a LEFT JOIN {users} u ON u.uid = a.uid' . $filter['join'] . ' WHERE 1 ' . $filter['where']; + $sql .= tablesort_sql($header); + $query_count = 'SELECT COUNT(DISTINCT a.aid) FROM {accesslog} a LEFT JOIN {users} u ON u.uid = a.uid' . $filter['join'] . ' WHERE 1 ' . $filter['where']; + $result = pager_query($sql, 30, 0, $query_count, $filter['args']); - $result = pager_query($sql, 30); $rows = array(); while ($log = db_fetch_object($result)) { $rows[] = array( @@ -33,7 +38,8 @@ $rows[] = array(array('data' => t('No statistics available.'), 'colspan' => 4)); } - $output = theme('table', $header, $rows); + $output = drupal_get_form('statistics_filter_form', 'hits'); + $output .= theme('table', $header, $rows); $output .= theme('pager', NULL, 30, 0); return $output; } @@ -42,18 +48,21 @@ * Menu callback; presents the "top pages" page. */ function statistics_top_pages() { - // MAX(title) avoids having empty node titles which otherwise causes duplicates in the top pages list - $sql = "SELECT COUNT(path) AS hits, path, MAX(title) AS title, AVG(timer) AS average_time, SUM(timer) AS total_time FROM {accesslog} GROUP BY path"; - $sql_cnt = "SELECT COUNT(DISTINCT(path)) FROM {accesslog}"; + $filtertype = 'pages'; + $filter = statistics_build_filter_query($filtertype); $header = array( array('data' => t('Hits'), 'field' => 'hits', 'sort' => 'desc'), array('data' => t('Page'), 'field' => 'path'), array('data' => t('Average page generation time'), 'field' => 'average_time'), array('data' => t('Total page generation time'), 'field' => 'total_time') - ); + ) + ; + // MAX(title) avoids having empty node titles which otherwise causes duplicates in the top pages list + $sql = 'SELECT COUNT(path) AS hits, path, MAX(title) AS title, AVG(timer) AS average_time, SUM(timer) AS total_time FROM {accesslog}' . $filter['join'] . ' WHERE 1 ' . $filter['where'] . ' GROUP BY path'; $sql .= tablesort_sql($header); - $result = pager_query($sql, 30, 0, $sql_cnt); + $sql_cnt = 'SELECT COUNT(DISTINCT(path)) FROM {accesslog}' . $filter['join'] . ' WHERE 1 ' . $filter['where']; + $result = pager_query($sql, 30, 0, $sql_cnt, $filter['args']); $rows = array(); while ($page = db_fetch_object($result)) { @@ -65,7 +74,8 @@ } drupal_set_title(t('Top pages in the past %interval', array('%interval' => format_interval(variable_get('statistics_flush_accesslog_timer', 259200)))), PASS_THROUGH); - $output = theme('table', $header, $rows); + $output = drupal_get_form('statistics_filter_form', $filtertype); + $output .= theme('table', $header, $rows); $output .= theme('pager', NULL, 30, 0); return $output; } @@ -74,6 +84,8 @@ * Menu callback; presents the "top visitors" page. */ function statistics_top_visitors() { + $filtertype = 'visitors'; + $filter = statistics_build_filter_query($filtertype); $header = array( array('data' => t('Hits'), 'field' => 'hits', 'sort' => 'desc'), @@ -82,9 +94,11 @@ array('data' => user_access('block IP addresses') ? t('Operations') : '', 'colspan' => 2), ); - $sql = "SELECT COUNT(a.uid) AS hits, a.uid, u.name, a.hostname, SUM(a.timer) AS total, bl.iid FROM {accesslog} a LEFT JOIN {blocked_ips} bl ON a.hostname = bl.ip LEFT JOIN {users} u ON a.uid = u.uid GROUP BY a.hostname, a.uid, u.name, bl.iid" . tablesort_sql($header); - $sql_cnt = "SELECT COUNT(DISTINCT(CONCAT(CAST(uid AS char), hostname))) FROM {accesslog}"; - $result = pager_query($sql, 30, 0, $sql_cnt); + $sql = 'SELECT COUNT(a.uid) AS hits, a.uid, u.name, a.hostname, SUM(a.timer) AS total, bl.iid FROM {accesslog} a LEFT JOIN {blocked_ips} bl ON a.hostname = bl.ip LEFT JOIN {users} u ON a.uid = u.uid'; + $sql .= $filter['join'] . ' WHERE 1 ' . $filter['where'] . 'GROUP BY a.hostname, a.uid, u.name, bl.iid'; + $sql .= tablesort_sql($header); + $sql_cnt = 'SELECT COUNT(DISTINCT(CONCAT(CAST(a.uid AS char), hostname))) FROM {accesslog} a LEFT JOIN {users} u ON u.uid = a.uid' . $filter['join'] . ' WHERE 1 ' . $filter['where']; + $result = pager_query($sql, 30, 0, $sql_cnt, $filter['args']); $rows = array(); while ($account = db_fetch_object($result)) { @@ -98,7 +112,8 @@ } drupal_set_title(t('Top visitors in the past %interval', array('%interval' => format_interval(variable_get('statistics_flush_accesslog_timer', 259200)))), PASS_THROUGH); - $output = theme('table', $header, $rows); + $output = drupal_get_form('statistics_filter_form', $filtertype); + $output .= theme('table', $header, $rows); $output .= theme('pager', NULL, 30, 0); return $output; } @@ -107,9 +122,8 @@ * Menu callback; presents the "referrer" page. */ function statistics_top_referrers() { - $query = "SELECT url, COUNT(url) AS hits, MAX(timestamp) AS last FROM {accesslog} WHERE url NOT LIKE :host AND url <> '' GROUP BY url"; - $query_cnt = "SELECT COUNT(DISTINCT(url)) FROM {accesslog} WHERE url <> '' AND url NOT LIKE :host"; - drupal_set_title(t('Top referrers in the past %interval', array('%interval' => format_interval(variable_get('statistics_flush_accesslog_timer', 259200)))), PASS_THROUGH); + $filtertype = 'referrers'; + $filter = statistics_build_filter_query($filtertype); $header = array( array('data' => t('Hits'), 'field' => 'hits', 'sort' => 'desc'), @@ -117,8 +131,12 @@ array('data' => t('Last visit'), 'field' => 'last'), ); + $query = 'SELECT url, COUNT(url) AS hits, MAX(timestamp) AS last FROM {accesslog}' . $filter['join'] . " WHERE url NOT LIKE '%s' AND url <> ''" . $filter['where'] . ' GROUP BY url'; + $query_cnt = 'SELECT COUNT(DISTINCT(url)) FROM {accesslog}' . $filter['join'] . " WHERE url <> '' AND url NOT LIKE '%s' " . $filter['where']; + drupal_set_title(t('Top referrers in the past %interval', array('%interval' => format_interval(variable_get('statistics_flush_accesslog_timer', 259200)))), PASS_THROUGH); + $query .= tablesort_sql($header); - $result = pager_query($query, 30, 0, $query_cnt, array(':host' => '%'. $_SERVER['HTTP_HOST'] .'%')); + $result = pager_query($query, 30, 0, $query_cnt, array_merge(array('%'. $_SERVER['HTTP_HOST'] .'%'), $filter['args'])); $rows = array(); while ($referrer = db_fetch_object($result)) { @@ -129,7 +147,8 @@ $rows[] = array(array('data' => t('No statistics available.'), 'colspan' => 3)); } - $output = theme('table', $header, $rows); + $output = drupal_get_form('statistics_filter_form', $filtertype); + $output .= theme('table', $header, $rows); $output .= theme('pager', NULL, 30, 0); return $output; } @@ -175,6 +194,111 @@ } /** + * Form builder; Return form for logging filters. + * + * @param $type Type of logging. + * + * @ingroup forms + * @see statistics_filter_form_submit() + */ +function statistics_filter_form($form_state, $type) { + $session = isset($_SESSION['statistics_filter']) ? $_SESSION['statistics_filter'] : array(); + $session = (is_array($session) && isset($session[$type]) ) ? $session[$type] : array(); + $filters = statistics_filters(); + + $i = 0; + $form['filters'] = array( + '#type' => 'fieldset', + '#title' => t('Hide !type where', array('!type' => $filters[$type]['#title'])), + '#theme' => 'statistics_filters', + '#collapsible' => TRUE, + '#collapsed' => FALSE, + ); + + $form['filters']['type'] = array( + '#type' => 'value', + '#value' => $type, + ); + + // get only type specific filters. + $filters = $filters[$type]['#filters']; + + foreach ($session as $filter) { + list($filtertype, $filtervalue) = $filter; + $params = array('%property' => $filters[$filtertype]['title'] , '%value' => $filtervalue); + if ($i++ > 0) { + $form['filters']['current'][] = array('#markup' => t('and where %property is %value', $params)); + } + else { + $form['filters']['current'][] = array('#markup' => t('%property is %value', $params)); + } + } + + foreach ($filters as $key => $filter) { + $names[$key] = $filter['title']; + $form['filters']['status'][$key] = array( + '#type' => 'textfield', + '#size' => 50, + ); + } + + $form['filters']['filter'] = array( + '#type' => 'radios', + '#options' => $names, + ); + + $form['filters']['buttons']['submit'] = array( + '#type' => 'submit', + '#value' => (count($session) ? t('Refine') : t('Filter')), + ); + if (count($session)) { + $form['filters']['buttons']['undo'] = array( + '#type' => 'submit', + '#value' => t('Undo'), + ); + $form['filters']['buttons']['reset'] = array( + '#type' => 'submit', + '#value' => t('Reset'), + ); + } + + drupal_add_js('misc/form.js'); + + return $form; +} + +/** + * Process result from statistics filter form. + */ +function statistics_filter_form_submit($form, &$form_state) { + $op = $form_state['values']['op']; + $type = $form_state['values']['type']; + $filters = statistics_filters(); + // get only type specific filters. + $filters = $filters[$type]['#filters']; + switch ($op) { + case t('Filter'): + case t('Refine'): + if (isset($form_state['values']['filter'])) { + $filter = $form_state['values']['filter']; + if (isset($form_state['values'][$filter])) { + $_SESSION['statistics_filter'][$type][] = array($filter, $form_state['values'][$filter]); + } + } + break; + case t('Undo'): + array_pop($_SESSION['statistics_filter'][$type]); + break; + case t('Reset'): + $_SESSION['statistics_filter'][$type] = array(); + break; + } + + $form_state['redirect'] = 'admin/reports/' . $type; + return; +} + +/** * Form builder; Configure access logging. * * @ingroup forms @@ -213,3 +337,49 @@ return system_settings_form($form, TRUE); } + +/** + * Theme user administration filter form. + * + * @ingroup themeable + */ +function theme_statistics_filter_form(&$form) { + $output = '
'; + $output .= drupal_render($form['filters']); + $output .= '
'; + $output .= drupal_render($form); + return $output; +} + +/** + * Theme statistics filter selector. + * + * @ingroup themeable + */ +function theme_statistics_filters(&$form) { + $output = ''; + + return $output; +} Index: modules/statistics/statistics.module =================================================================== RCS file: /cvs/drupal/drupal/modules/statistics/statistics.module,v retrieving revision 1.296 diff -u -r1.296 statistics.module --- modules/statistics/statistics.module 26 Jan 2009 14:08:43 -0000 1.296 +++ modules/statistics/statistics.module 27 Jan 2009 20:29:25 -0000 @@ -38,6 +38,22 @@ } /** + * Implementation of hook_theme(). + */ +function statistics_theme() { + return array( + 'statistics_filter_form' => array( + 'arguments' => array('form' => NULL), + 'file' => 'statistics.admin.inc', + ), + 'statistics_filters' => array( + 'arguments' => array('form' => NULL), + 'file' => 'statistics.admin.inc', + ), + ); +} + +/** * Implementation of hook_exit(). * * This is where statistics are gathered on page accesses. @@ -324,6 +340,109 @@ } /** + * List statistics filters that can be applied. + */ +function statistics_filters() { + // Regular filters + $filters = array(); + + $filters['hits'] = array( + '#title' => t('recent hits'), + '#filters' => array( + 'page' => array( + 'title' => t('page'), + 'where' => 'a.path NOT LIKE \'%s\'', + 'join' => '', + ), + 'uid' => array( + 'title' => t('userid'), + 'where' => 'a.uid != %d', + 'join' => '', + ), + 'user' => array( + 'title' => t('username'), + 'where' => 'u.name NOT LIKE \'%s\'', + 'join' => '', + ), + ), + ); + $filters['pages'] = array( + '#title' => t('top pages'), + '#filters' => array( + 'path' => array( + 'title' => t('path'), + 'where' => 'path NOT LIKE \'%s\'', + 'join' => '', + ), + 'title' => array( + 'title' => t('title'), + 'where' => 'title NOT LIKE \'%s\'', + 'join' => '', + ), + ), + ); + $filters['referrers'] = array( + '#title' => t('top referrers'), + '#filters' => array( + 'url' => array( + 'title' => t('url'), + 'where' => 'url NOT LIKE \'%s\'', + 'join' => '', + ), + ), + ); + $filters['visitors'] = array( + '#title' => t('top visitors'), + '#filters' => array( + 'path' => array( + 'title' => t('username'), + 'where' => 'u.name NOT LIKE \'%s\'', + 'join' => '', + ), + 'hostname' => array( + 'title' => t('hostname'), + 'where' => 'a.hostname NOT LIKE \'%s\'', + 'join' => '', + ), + ), + ); + + return $filters; +} + +/** + * Build query for statistic filters based on session and statistic type. + * + * @param $type + * Type of filter. + */ +function statistics_build_filter_query($type) { + $filters = statistics_filters(); + // get type specific filters. + $filters = $filters[$type]['#filters']; + + if (!isset($_SESSION['statistics_filter'][$type])) { + return array('where' => '', 'join' => '', 'args' => array()); + } + + // Build query. + $where = $args = $join = array(); + foreach ($_SESSION['statistics_filter'][$type] as $filter) { + list($key, $value) = $filter; + $where[] = $filters[$key]['where']; + $join[] = $filters[$key]['join']; + $args[] = $value; + } + $where = !empty($where) ? ' AND ' . implode(' AND ', $where) : ''; + $join = !empty($join) ? ' ' . implode(' ', array_unique($join)) : ''; + + return array('where' => $where, + 'join' => $join, + 'args' => $args, + ); +} + +/** * It is possible to adjust the width of columns generated by the * statistics module. */ Index: misc/form.js =================================================================== RCS file: /cvs/drupal/drupal/misc/form.js,v retrieving revision 1.2 diff -u -r1.2 form.js --- misc/form.js 29 Oct 2008 10:01:26 -0000 1.2 +++ misc/form.js 27 Jan 2009 20:29:23 -0000 @@ -8,5 +8,10 @@ $('.multiselect input:radio[value="'+ this.id.substr(5) +'"]') .attr('checked', true); }); + $('.multiselect input:not(.multiselectSelector-processed)', context) + .addClass('multiselectSelector-processed').change(function() { + $('.multiselect input:radio[value="'+ this.id.substr(5) +'"]') + .attr('checked', true); + }); } }; Index: .buildpath =================================================================== RCS file: .buildpath diff -N .buildpath --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ .buildpath 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,5 @@ + + + + + Index: .project =================================================================== RCS file: .project diff -N .project --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ .project 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,22 @@ + + + drupal-7 + + + + + + org.eclipse.wst.validation.validationbuilder + + + + + org.eclipse.dltk.core.scriptbuilder + + + + + + org.eclipse.php.core.PHPNature + +