| 1 |
<?php
|
| 2 |
// $Id: search_engine_referers.module,v 1.12 2009/04/15 20:26:08 soxofaan Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* Implementation of hook_menu()
|
| 6 |
*/
|
| 7 |
function search_engine_referers_menu() {
|
| 8 |
$items = array();
|
| 9 |
$items['admin/reports/search_engine_referers'] = array(
|
| 10 |
'title' => 'Search engine referers',
|
| 11 |
'description' => 'Extract search queries from search engine referers in the access log.',
|
| 12 |
'page callback' => 'search_engine_referers_overview',
|
| 13 |
'access arguments' => array('view search engine referers'),
|
| 14 |
);
|
| 15 |
return $items;
|
| 16 |
}
|
| 17 |
|
| 18 |
/**
|
| 19 |
* Implementation of hook_perm()
|
| 20 |
*/
|
| 21 |
function search_engine_referers_perm() {
|
| 22 |
return array('view search engine referers');
|
| 23 |
}
|
| 24 |
|
| 25 |
/**
|
| 26 |
* Callback function for overview page
|
| 27 |
*/
|
| 28 |
function search_engine_referers_overview() {
|
| 29 |
// check if access logging is enabled
|
| 30 |
if (!variable_get('statistics_enable_access_log', 0)) {
|
| 31 |
drupal_set_message(t('The access log is not enabled. The listing of search engine referers will be empty or outdated. <a href="!accesslog">Enable and configure the access log</a>.', array('!accesslog' => url('admin/reports/settings'))), 'warning');
|
| 32 |
}
|
| 33 |
|
| 34 |
$output = '';
|
| 35 |
|
| 36 |
// search engine patterns and properties
|
| 37 |
$engines = array(
|
| 38 |
'Google' => array(
|
| 39 |
'host_pattern_sql' => 'http://www.google.%',
|
| 40 |
'host_pattern_preg' => '!google(\\.[a-z]+)+$!',
|
| 41 |
'path_pattern' => '!^/(search|url)!',
|
| 42 |
'query_variable' => 'q',
|
| 43 |
),
|
| 44 |
'Yahoo!' => array(
|
| 45 |
'host_pattern_sql' => 'http://%search.yahoo.com/%',
|
| 46 |
'host_pattern_preg' => '!([a-z]+\\.)*search.yahoo.com!',
|
| 47 |
'path_pattern' => '!^/search!',
|
| 48 |
'query_variable' => 'p',
|
| 49 |
),
|
| 50 |
'Live search' => array(
|
| 51 |
'host_pattern_sql' => 'http://search.%.com/results.aspx%',
|
| 52 |
'host_pattern_preg' => '!search\\.(live|msn)\\.com!',
|
| 53 |
'path_pattern' => '!^/results.aspx!',
|
| 54 |
'query_variable' => 'q',
|
| 55 |
),
|
| 56 |
'Yandex' => array(
|
| 57 |
'host_pattern_sql' => 'http://%yandex.ru/yandsearch%',
|
| 58 |
'host_pattern_preg' => '!yandex.ru!',
|
| 59 |
'path_pattern' => '!^/yandsearch!',
|
| 60 |
'query_variable' => 'text',
|
| 61 |
),
|
| 62 |
'Rambler' => array(
|
| 63 |
'host_pattern_sql' => 'http://www.rambler.ru/srch%',
|
| 64 |
'host_pattern_preg' => '!rambler.ru!',
|
| 65 |
'path_pattern' => '!^/srch!',
|
| 66 |
'query_variable' => 'words',
|
| 67 |
),
|
| 68 |
);
|
| 69 |
$num = intval(variable_get('search_engine_referers_db_limit', 10));
|
| 70 |
|
| 71 |
// build the table
|
| 72 |
$header = array(t('Date'), t('Page'), t('Search engine'), t('Query'));
|
| 73 |
$rows = array();
|
| 74 |
|
| 75 |
// iterate over the engines
|
| 76 |
foreach ($engines as $engine_name => $engine) {
|
| 77 |
$result = db_query_range("SELECT aid, timestamp, url, path FROM {accesslog} WHERE LOWER(url) LIKE LOWER('%s') ORDER BY timestamp DESC", $engine['host_pattern_sql'], 0, $num);
|
| 78 |
while ($r = db_fetch_object($result)) {
|
| 79 |
$url_data = parse_url($r->url);
|
| 80 |
if (preg_match($engine['host_pattern_preg'], $url_data['host']) && preg_match($engine['path_pattern'], $url_data['path'])) {
|
| 81 |
parse_str($url_data['query'], $query_data);
|
| 82 |
# Create link to referer page
|
| 83 |
$url = l($query_data[$engine['query_variable']], $r->url);
|
| 84 |
# Fix empty links, which can happen if query_variable isn't found or in
|
| 85 |
# case of non UTF encoding of the query.
|
| 86 |
if (substr($url, -5) == '></a>') {
|
| 87 |
$url = l(t('[n/a]'), $r->url);
|
| 88 |
}
|
| 89 |
# Page
|
| 90 |
$title = drupal_get_path_alias($r->path);
|
| 91 |
$title = truncate_utf8($title, 30, FALSE, TRUE);
|
| 92 |
# Build row.
|
| 93 |
$rows[$r->aid] = array(format_date($r->timestamp, 'small'), l($title, $r->path), $url_data['host'], $url);
|
| 94 |
}
|
| 95 |
}
|
| 96 |
}
|
| 97 |
// Sort rows by date (access id actually)
|
| 98 |
krsort($rows);
|
| 99 |
|
| 100 |
if (empty($rows)) {
|
| 101 |
$rows[] = array(array('data' => t('No search engine referers found.'), 'colspan' => 4));
|
| 102 |
}
|
| 103 |
|
| 104 |
// theme the table
|
| 105 |
$output .= theme('table', $header, $rows);
|
| 106 |
|
| 107 |
// add settings form
|
| 108 |
$output .= drupal_get_form('search_engine_referers_settings');
|
| 109 |
|
| 110 |
return $output;
|
| 111 |
}
|
| 112 |
|
| 113 |
/**
|
| 114 |
* Form generating function for search_engine_referers settings
|
| 115 |
*/
|
| 116 |
function search_engine_referers_settings() {
|
| 117 |
$form = array();
|
| 118 |
$form['search_engine_referers_db_limit'] = array(
|
| 119 |
'#type' => 'select',
|
| 120 |
'#title' => t('Maximum number of entries per search engine to display'),
|
| 121 |
'#options' => array(5 => 5, 10 => 10, 15 => 15, 20 => 20, 30 => 30, 50 => 50),
|
| 122 |
'#default_value' => variable_get('search_engine_referers_db_limit', 10),
|
| 123 |
);
|
| 124 |
return system_settings_form($form);
|
| 125 |
}
|