Projet

Général

Profil

Paste
Télécharger (11,8 ko) Statistiques
| Branche: | Révision:

root / drupal7 / modules / statistics / statistics.admin.inc @ f7a2490e

1
<?php
2

    
3
/**
4
 * @file
5
 * Admin page callbacks for the Statistics module.
6
 */
7

    
8
/**
9
 * Page callback: Displays the "recent hits" page.
10
 *
11
 * This displays the pages with recent hits in a given time interval that
12
 * haven't been flushed yet. The flush interval is set on the statistics
13
 * settings form, but is dependent on cron running.
14
 *
15
 * @return
16
 *   A render array containing information about the most recent hits.
17
 */
18
function statistics_recent_hits() {
19
  $header = array(
20
    array('data' => t('Timestamp'), 'field' => 'a.timestamp', 'sort' => 'desc'),
21
    array('data' => t('Page'), 'field' => 'a.path'),
22
    array('data' => t('User'), 'field' => 'u.name'),
23
    array('data' => t('Operations'))
24
  );
25

    
26
  $query = db_select('accesslog', 'a', array('target' => 'slave'))->extend('PagerDefault')->extend('TableSort');
27
  $query->join('users', 'u', 'a.uid = u.uid');
28
  $query
29
    ->fields('a', array('aid', 'timestamp', 'path', 'title', 'uid'))
30
    ->fields('u', array('name'))
31
    ->limit(30)
32
    ->orderByHeader($header);
33

    
34
  $result = $query->execute();
35
  $rows = array();
36
  foreach ($result as $log) {
37
    $rows[] = array(
38
      array('data' => format_date($log->timestamp, 'short'), 'class' => array('nowrap')),
39
      _statistics_format_item($log->title, $log->path),
40
      theme('username', array('account' => $log)),
41
      l(t('details'), "admin/reports/access/$log->aid"));
42
  }
43

    
44
  $build['statistics_table'] = array(
45
    '#theme' => 'table',
46
    '#header' => $header,
47
    '#rows' => $rows,
48
    '#empty' => t('No statistics available.'),
49
  );
50
  $build['statistics_pager'] = array('#theme' => 'pager');
51
  return $build;
52
}
53

    
54
/**
55
 * Page callback: Displays statistics for the "top pages" (most accesses).
56
 *
57
 * This displays the pages with the most hits (the "top pages") within a given
58
 * time period that haven't been flushed yet. The flush interval is set on the
59
 * statistics settings form, but is dependent on cron running.
60
 *
61
 * @return
62
 *   A render array containing information about the the top pages.
63
 */
64
function statistics_top_pages() {
65
  $header = array(
66
    array('data' => t('Hits'), 'field' => 'hits', 'sort' => 'desc'),
67
    array('data' => t('Page'), 'field' => 'path'),
68
    array('data' => t('Average page generation time'), 'field' => 'average_time'),
69
    array('data' => t('Total page generation time'), 'field' => 'total_time')
70
  );
71

    
72
  $query = db_select('accesslog', 'a', array('target' => 'slave'))->extend('PagerDefault')->extend('TableSort');
73
  $query->addExpression('COUNT(path)', 'hits');
74
  // MAX(title) avoids having empty node titles which otherwise causes
75
  // duplicates in the top pages list.
76
  $query->addExpression('MAX(title)', 'title');
77
  $query->addExpression('AVG(timer)', 'average_time');
78
  $query->addExpression('SUM(timer)', 'total_time');
79

    
80
  $query
81
    ->fields('a', array('path'))
82
    ->groupBy('path')
83
    ->limit(30)
84
    ->orderByHeader($header);
85

    
86
  $count_query = db_select('accesslog', 'a', array('target' => 'slave'));
87
  $count_query->addExpression('COUNT(DISTINCT path)');
88
  $query->setCountQuery($count_query);
89

    
90
  $result = $query->execute();
91
  $rows = array();
92
  foreach ($result as $page) {
93
    $rows[] = array($page->hits, _statistics_format_item($page->title, $page->path), t('%time ms', array('%time' => round($page->average_time))), format_interval(round($page->total_time / 1000)));
94
  }
95

    
96
  drupal_set_title(t('Top pages in the past %interval', array('%interval' => format_interval(variable_get('statistics_flush_accesslog_timer', 259200)))), PASS_THROUGH);
97
  $build['statistics_top_pages_table'] = array(
98
    '#theme' => 'table',
99
    '#header' => $header,
100
    '#rows' => $rows,
101
    '#empty' => t('No statistics available.'),
102
  );
103
  $build['statistics_top_pages_pager'] = array('#theme' => 'pager');
104
  return $build;
105
}
106

    
107
/**
108
 * Page callback: Displays the "top visitors" page.
109
 *
110
 * This displays the pages with the top number of visitors in a given time
111
 * interval that haven't been flushed yet. The flush interval is set on the
112
 * statistics settings form, but is dependent on cron running.
113
 *
114
 * @return
115
 *   A render array containing the top visitors information.
116
 */
117
function statistics_top_visitors() {
118

    
119
  $header = array(
120
    array('data' => t('Hits'), 'field' => 'hits', 'sort' => 'desc'),
121
    array('data' => t('Visitor'), 'field' => 'u.name'),
122
    array('data' => t('Total page generation time'), 'field' => 'total'),
123
    array('data' => user_access('block IP addresses') ? t('Operations') : '', 'colspan' => 2),
124
  );
125
  $query = db_select('accesslog', 'a', array('target' => 'slave'))->extend('PagerDefault')->extend('TableSort');
126
  $query->leftJoin('blocked_ips', 'bl', 'a.hostname = bl.ip');
127
  $query->leftJoin('users', 'u', 'a.uid = u.uid');
128

    
129
  $query->addExpression('COUNT(a.uid)', 'hits');
130
  $query->addExpression('SUM(a.timer)', 'total');
131
  $query
132
    ->fields('a', array('uid', 'hostname'))
133
    ->fields('u', array('name'))
134
    ->fields('bl', array('iid'))
135
    ->groupBy('a.hostname')
136
    ->groupBy('a.uid')
137
    ->groupBy('u.name')
138
    ->groupBy('bl.iid')
139
    ->limit(30)
140
    ->orderByHeader($header);
141

    
142
  $uniques_query = db_select('accesslog')->distinct();
143
  $uniques_query->fields('accesslog', array('uid', 'hostname'));
144
  $count_query = db_select($uniques_query);
145
  $count_query->addExpression('COUNT(*)');
146
  $query->setCountQuery($count_query);
147

    
148
  $result = $query->execute();
149
  $rows = array();
150
  $destination = drupal_get_destination();
151
  foreach ($result as $account) {
152
    $ban_link = $account->iid ? l(t('unblock IP address'), "admin/config/people/ip-blocking/delete/$account->iid", array('query' => $destination)) : l(t('block IP address'), "admin/config/people/ip-blocking/$account->hostname", array('query' => $destination));
153
    $rows[] = array($account->hits, ($account->uid ? theme('username', array('account' => $account)) : $account->hostname), format_interval(round($account->total / 1000)), (user_access('block IP addresses') && !$account->uid) ? $ban_link : '');
154
  }
155

    
156
  drupal_set_title(t('Top visitors in the past %interval', array('%interval' => format_interval(variable_get('statistics_flush_accesslog_timer', 259200)))), PASS_THROUGH);
157
  $build['statistics_top_visitors_table'] = array(
158
    '#theme' => 'table',
159
    '#header' => $header,
160
    '#rows' => $rows,
161
    '#empty' => t('No statistics available.'),
162
  );
163
  $build['statistics_top_visitors_pager'] = array('#theme' => 'pager');
164
  return $build;
165
}
166

    
167
/**
168
 * Page callback: Displays the "top referrers" in the access logs.
169
 *
170
 * This displays the pages with the top referrers in a given time interval that
171
 * haven't been flushed yet. The flush interval is set on the statistics
172
 * settings form, but is dependent on cron running.
173
 *
174
 * @return
175
 *   A render array containing the top referrers information.
176
 */
177
function statistics_top_referrers() {
178
  drupal_set_title(t('Top referrers in the past %interval', array('%interval' => format_interval(variable_get('statistics_flush_accesslog_timer', 259200)))), PASS_THROUGH);
179

    
180
  $header = array(
181
    array('data' => t('Hits'), 'field' => 'hits', 'sort' => 'desc'),
182
    array('data' => t('Url'), 'field' => 'url'),
183
    array('data' => t('Last visit'), 'field' => 'last'),
184
  );
185
  $query = db_select('accesslog', 'a')->extend('PagerDefault')->extend('TableSort');
186

    
187
  $query->addExpression('COUNT(url)', 'hits');
188
  $query->addExpression('MAX(timestamp)', 'last');
189
  $query
190
    ->fields('a', array('url'))
191
    ->condition('url', '%' . $_SERVER['HTTP_HOST'] . '%', 'NOT LIKE')
192
    ->condition('url', '', '<>')
193
    ->groupBy('url')
194
    ->limit(30)
195
    ->orderByHeader($header);
196

    
197
  $count_query = db_select('accesslog', 'a', array('target' => 'slave'));
198
  $count_query->addExpression('COUNT(DISTINCT url)');
199
  $count_query
200
    ->condition('url', '%' . $_SERVER['HTTP_HOST'] . '%', 'NOT LIKE')
201
    ->condition('url', '', '<>');
202
  $query->setCountQuery($count_query);
203

    
204
  $result = $query->execute();
205
  $rows = array();
206
  foreach ($result as $referrer) {
207
    $rows[] = array($referrer->hits, _statistics_link($referrer->url), t('@time ago', array('@time' => format_interval(REQUEST_TIME - $referrer->last))));
208
  }
209

    
210
  $build['statistics_top_referrers_table'] = array(
211
    '#theme' => 'table',
212
    '#header' => $header,
213
    '#rows' => $rows,
214
    '#empty' => t('No statistics available.'),
215
  );
216
  $build['statistics_top_referrers_pager'] = array('#theme' => 'pager');
217
  return $build;
218
}
219

    
220
/**
221
 * Page callback: Gathers page access statistics suitable for rendering.
222
 *
223
 * @param $aid
224
 *   The unique accesslog ID.
225
 *
226
 * @return
227
 *   A render array containing page access statistics. If information for the
228
 *   page was not found, drupal_not_found() is called.
229
 */
230
function statistics_access_log($aid) {
231
  $access = db_query('SELECT a.*, u.name FROM {accesslog} a LEFT JOIN {users} u ON a.uid = u.uid WHERE aid = :aid', array(':aid' => $aid))->fetch();
232
  if ($access) {
233
    $rows[] = array(
234
      array('data' => t('URL'), 'header' => TRUE),
235
      l(url($access->path, array('absolute' => TRUE)), $access->path)
236
    );
237
    // It is safe to avoid filtering $access->title through check_plain because
238
    // it comes from drupal_get_title().
239
    $rows[] = array(
240
      array('data' => t('Title'), 'header' => TRUE),
241
      $access->title
242
    );
243
    $rows[] = array(
244
      array('data' => t('Referrer'), 'header' => TRUE),
245
      ($access->url ? l($access->url, $access->url) : '')
246
    );
247
    $rows[] = array(
248
      array('data' => t('Date'), 'header' => TRUE),
249
      format_date($access->timestamp, 'long')
250
    );
251
    $rows[] = array(
252
      array('data' => t('User'), 'header' => TRUE),
253
      theme('username', array('account' => $access))
254
    );
255
    $rows[] = array(
256
      array('data' => t('Hostname'), 'header' => TRUE),
257
      check_plain($access->hostname)
258
    );
259

    
260
    $build['statistics_table'] = array(
261
      '#theme' => 'table',
262
      '#rows' => $rows,
263
    );
264
    return $build;
265
  }
266
  return MENU_NOT_FOUND;
267
}
268

    
269
/**
270
 * Form constructor for the statistics administration form.
271
 *
272
 * @ingroup forms
273
 * @see system_settings_form()
274
 */
275
function statistics_settings_form() {
276
  // Access log settings.
277
  $form['access'] = array(
278
    '#type' => 'fieldset',
279
    '#title' => t('Access log settings'),
280
  );
281
  $form['access']['statistics_enable_access_log'] = array(
282
    '#type' => 'checkbox',
283
    '#title' => t('Enable access log'),
284
    '#default_value' => variable_get('statistics_enable_access_log', 0),
285
    '#description' => t('Log each page access. Required for referrer statistics.'),
286
  );
287
  $form['access']['statistics_flush_accesslog_timer'] = array(
288
    '#type' => 'select',
289
    '#title' => t('Discard access logs older than'),
290
    '#default_value' => variable_get('statistics_flush_accesslog_timer', 259200),
291
    '#options' => array(0 => t('Never')) + drupal_map_assoc(array(3600, 10800, 21600, 32400, 43200, 86400, 172800, 259200, 604800, 1209600, 2419200, 4838400, 9676800), 'format_interval'),
292
    '#description' => t('Older access log entries (including referrer statistics) will be automatically discarded. (Requires a correctly configured <a href="@cron">cron maintenance task</a>.)', array('@cron' => url('admin/reports/status'))),
293
  );
294

    
295
  // Content counter settings.
296
  $form['content'] = array(
297
    '#type' => 'fieldset',
298
    '#title' => t('Content viewing counter settings'),
299
  );
300
  $form['content']['statistics_count_content_views'] = array(
301
    '#type' => 'checkbox',
302
    '#title' => t('Count content views'),
303
    '#default_value' => variable_get('statistics_count_content_views', 0),
304
    '#description' => t('Increment a counter each time content is viewed.'),
305
  );
306
  $form['content']['statistics_count_content_views_ajax'] = array(
307
    '#type' => 'checkbox',
308
    '#title' => t('Use Ajax to increment the counter'),
309
    '#default_value' => variable_get('statistics_count_content_views_ajax', 0),
310
    '#description' => t('Perform the count asynchronously after page load rather than during page generation.'),
311
    '#states' => array(
312
      'disabled' => array(
313
        ':input[name="statistics_count_content_views"]' => array('checked' => FALSE),
314
      ),
315
    ),
316
  );
317

    
318
  return system_settings_form($form);
319
}