Projet

Général

Profil

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

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

1 85ad3d82 Assos Assos
<?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 b4adf10d Assos Assos
    ->orderByHeader($header)
141
    ->orderBy('a.hostname');
142 85ad3d82 Assos Assos
143
  $uniques_query = db_select('accesslog')->distinct();
144
  $uniques_query->fields('accesslog', array('uid', 'hostname'));
145
  $count_query = db_select($uniques_query);
146
  $count_query->addExpression('COUNT(*)');
147
  $query->setCountQuery($count_query);
148
149
  $result = $query->execute();
150
  $rows = array();
151
  $destination = drupal_get_destination();
152
  foreach ($result as $account) {
153
    $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));
154
    $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 : '');
155
  }
156
157
  drupal_set_title(t('Top visitors in the past %interval', array('%interval' => format_interval(variable_get('statistics_flush_accesslog_timer', 259200)))), PASS_THROUGH);
158
  $build['statistics_top_visitors_table'] = array(
159
    '#theme' => 'table',
160
    '#header' => $header,
161
    '#rows' => $rows,
162
    '#empty' => t('No statistics available.'),
163
  );
164
  $build['statistics_top_visitors_pager'] = array('#theme' => 'pager');
165
  return $build;
166
}
167
168
/**
169
 * Page callback: Displays the "top referrers" in the access logs.
170
 *
171
 * This displays the pages with the top referrers in a given time interval that
172
 * haven't been flushed yet. The flush interval is set on the statistics
173
 * settings form, but is dependent on cron running.
174
 *
175
 * @return
176
 *   A render array containing the top referrers information.
177
 */
178
function statistics_top_referrers() {
179
  drupal_set_title(t('Top referrers in the past %interval', array('%interval' => format_interval(variable_get('statistics_flush_accesslog_timer', 259200)))), PASS_THROUGH);
180
181
  $header = array(
182
    array('data' => t('Hits'), 'field' => 'hits', 'sort' => 'desc'),
183
    array('data' => t('Url'), 'field' => 'url'),
184
    array('data' => t('Last visit'), 'field' => 'last'),
185
  );
186
  $query = db_select('accesslog', 'a')->extend('PagerDefault')->extend('TableSort');
187
188
  $query->addExpression('COUNT(url)', 'hits');
189
  $query->addExpression('MAX(timestamp)', 'last');
190
  $query
191
    ->fields('a', array('url'))
192
    ->condition('url', '%' . $_SERVER['HTTP_HOST'] . '%', 'NOT LIKE')
193
    ->condition('url', '', '<>')
194
    ->groupBy('url')
195
    ->limit(30)
196
    ->orderByHeader($header);
197
198
  $count_query = db_select('accesslog', 'a', array('target' => 'slave'));
199
  $count_query->addExpression('COUNT(DISTINCT url)');
200
  $count_query
201
    ->condition('url', '%' . $_SERVER['HTTP_HOST'] . '%', 'NOT LIKE')
202
    ->condition('url', '', '<>');
203
  $query->setCountQuery($count_query);
204
205
  $result = $query->execute();
206
  $rows = array();
207
  foreach ($result as $referrer) {
208
    $rows[] = array($referrer->hits, _statistics_link($referrer->url), t('@time ago', array('@time' => format_interval(REQUEST_TIME - $referrer->last))));
209
  }
210
211
  $build['statistics_top_referrers_table'] = array(
212
    '#theme' => 'table',
213
    '#header' => $header,
214
    '#rows' => $rows,
215
    '#empty' => t('No statistics available.'),
216
  );
217
  $build['statistics_top_referrers_pager'] = array('#theme' => 'pager');
218
  return $build;
219
}
220
221
/**
222
 * Page callback: Gathers page access statistics suitable for rendering.
223
 *
224
 * @param $aid
225
 *   The unique accesslog ID.
226
 *
227
 * @return
228
 *   A render array containing page access statistics. If information for the
229
 *   page was not found, drupal_not_found() is called.
230
 */
231
function statistics_access_log($aid) {
232
  $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();
233
  if ($access) {
234
    $rows[] = array(
235
      array('data' => t('URL'), 'header' => TRUE),
236
      l(url($access->path, array('absolute' => TRUE)), $access->path)
237
    );
238
    // It is safe to avoid filtering $access->title through check_plain because
239
    // it comes from drupal_get_title().
240
    $rows[] = array(
241
      array('data' => t('Title'), 'header' => TRUE),
242
      $access->title
243
    );
244
    $rows[] = array(
245
      array('data' => t('Referrer'), 'header' => TRUE),
246
      ($access->url ? l($access->url, $access->url) : '')
247
    );
248
    $rows[] = array(
249
      array('data' => t('Date'), 'header' => TRUE),
250
      format_date($access->timestamp, 'long')
251
    );
252
    $rows[] = array(
253
      array('data' => t('User'), 'header' => TRUE),
254
      theme('username', array('account' => $access))
255
    );
256
    $rows[] = array(
257
      array('data' => t('Hostname'), 'header' => TRUE),
258
      check_plain($access->hostname)
259
    );
260
261
    $build['statistics_table'] = array(
262
      '#theme' => 'table',
263
      '#rows' => $rows,
264
    );
265
    return $build;
266
  }
267
  return MENU_NOT_FOUND;
268
}
269
270
/**
271
 * Form constructor for the statistics administration form.
272
 *
273
 * @ingroup forms
274
 * @see system_settings_form()
275
 */
276
function statistics_settings_form() {
277
  // Access log settings.
278
  $form['access'] = array(
279
    '#type' => 'fieldset',
280
    '#title' => t('Access log settings'),
281
  );
282
  $form['access']['statistics_enable_access_log'] = array(
283
    '#type' => 'checkbox',
284
    '#title' => t('Enable access log'),
285
    '#default_value' => variable_get('statistics_enable_access_log', 0),
286
    '#description' => t('Log each page access. Required for referrer statistics.'),
287
  );
288
  $form['access']['statistics_flush_accesslog_timer'] = array(
289
    '#type' => 'select',
290
    '#title' => t('Discard access logs older than'),
291
    '#default_value' => variable_get('statistics_flush_accesslog_timer', 259200),
292
    '#options' => array(0 => t('Never')) + drupal_map_assoc(array(3600, 10800, 21600, 32400, 43200, 86400, 172800, 259200, 604800, 1209600, 2419200, 4838400, 9676800), 'format_interval'),
293
    '#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'))),
294
  );
295
296
  // Content counter settings.
297
  $form['content'] = array(
298
    '#type' => 'fieldset',
299
    '#title' => t('Content viewing counter settings'),
300
  );
301
  $form['content']['statistics_count_content_views'] = array(
302
    '#type' => 'checkbox',
303
    '#title' => t('Count content views'),
304
    '#default_value' => variable_get('statistics_count_content_views', 0),
305
    '#description' => t('Increment a counter each time content is viewed.'),
306
  );
307
  $form['content']['statistics_count_content_views_ajax'] = array(
308
    '#type' => 'checkbox',
309
    '#title' => t('Use Ajax to increment the counter'),
310
    '#default_value' => variable_get('statistics_count_content_views_ajax', 0),
311
    '#description' => t('Perform the count asynchronously after page load rather than during page generation.'),
312
    '#states' => array(
313
      'disabled' => array(
314
        ':input[name="statistics_count_content_views"]' => array('checked' => FALSE),
315
      ),
316
    ),
317
  );
318
319
  return system_settings_form($form);
320
}