source: trunk/admin/stats.php @ 2245

Last change on this file since 2245 was 2245, checked in by rvelices, 16 years ago
  • history, stats and redirect go smarty
  • lang correction
  • small change in calling check_server_plugins (use by ref param instead of global)
  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 11.5 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | PhpWebGallery - a PHP based picture gallery                           |
4// | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net |
5// | Copyright (C) 2003-2007 PhpWebGallery Team - http://phpwebgallery.net |
6// +-----------------------------------------------------------------------+
7// | file          : $Id: stats.php 2245 2008-03-03 12:50:54Z rvelices $
8// | last update   : $Date: 2008-03-03 12:50:54 +0000 (Mon, 03 Mar 2008) $
9// | last modifier : $Author: rvelices $
10// | revision      : $Revision: 2245 $
11// +-----------------------------------------------------------------------+
12// | This program is free software; you can redistribute it and/or modify  |
13// | it under the terms of the GNU General Public License as published by  |
14// | the Free Software Foundation                                          |
15// |                                                                       |
16// | This program is distributed in the hope that it will be useful, but   |
17// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
18// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
19// | General Public License for more details.                              |
20// |                                                                       |
21// | You should have received a copy of the GNU General Public License     |
22// | along with this program; if not, write to the Free Software           |
23// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
24// | USA.                                                                  |
25// +-----------------------------------------------------------------------+
26
27if (!defined("PHPWG_ROOT_PATH"))
28{
29  die ("Hacking attempt!");
30}
31
32include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
33include_once(PHPWG_ROOT_PATH.'admin/include/functions_history.inc.php');
34
35// +-----------------------------------------------------------------------+
36// | Functions                                                             |
37// +-----------------------------------------------------------------------+
38
39function get_summary($year = null, $month = null, $day = null)
40{
41  $query = '
42SELECT
43    year,
44    month,
45    day,
46    hour,
47    nb_pages
48  FROM '.HISTORY_SUMMARY_TABLE;
49
50  if (isset($day))
51  {
52    $query.= '
53  WHERE year = '.$year.'
54    AND month = '.$month.'
55    AND day = '.$day.'
56    AND hour IS NOT NULL
57  ORDER BY
58    year ASC,
59    month ASC,
60    day ASC,
61    hour ASC
62;';
63  }
64  elseif (isset($month))
65  {
66    $query.= '
67  WHERE year = '.$year.'
68    AND month = '.$month.'
69    AND day IS NOT NULL
70    AND hour IS NULL
71  ORDER BY
72    year ASC,
73    month ASC,
74    day ASC
75;';
76  }
77  elseif (isset($year))
78  {
79    $query.= '
80  WHERE year = '.$year.'
81    AND month IS NOT NULL
82    AND day IS NULL
83  ORDER BY
84    year ASC,
85    month ASC
86;';
87  }
88  else
89  {
90    $query.= '
91  WHERE year IS NOT NULL
92    AND month IS NULL
93  ORDER BY
94    year ASC
95;';
96  }
97
98  $result = pwg_query($query);
99
100  $output = array();
101  while ($row = mysql_fetch_array($result))
102  {
103    array_push($output, $row);
104  }
105
106  return $output;
107}
108
109// +-----------------------------------------------------------------------+
110// | Check Access and exit when user status is not ok                      |
111// +-----------------------------------------------------------------------+
112
113check_status(ACCESS_ADMINISTRATOR);
114
115// +-----------------------------------------------------------------------+
116// | Refresh summary from details                                          |
117// +-----------------------------------------------------------------------+
118
119$query = '
120SELECT
121    year,
122    month,
123    day,
124    hour,
125    max(id) AS max_id,
126    COUNT(*) AS nb_pages
127  FROM '.HISTORY_TABLE.'
128  WHERE summarized = \'false\'
129  GROUP BY
130    year ASC,
131    month ASC,
132    day ASC,
133    hour ASC
134;';
135$result = pwg_query($query);
136
137$need_update = array();
138
139$max_id = 0;
140$is_first = true;
141$first_time_key = null;
142
143while ($row = mysql_fetch_array($result))
144{
145  $time_keys = array(
146    sprintf(
147      '%4u',
148      $row['year']
149      ),
150    sprintf(
151      '%4u.%02u',
152      $row['year'], $row['month']
153      ),
154    sprintf(
155      '%4u.%02u.%02u',
156      $row['year'], $row['month'], $row['day']
157      ),
158    sprintf(
159      '%4u.%02u.%02u.%02u',
160      $row['year'], $row['month'], $row['day'], $row['hour']
161      ),
162    );
163
164  foreach ($time_keys as $time_key)
165  {
166    if (!isset($need_update[$time_key]))
167    {
168      $need_update[$time_key] = 0;
169    }
170    $need_update[$time_key] += $row['nb_pages'];
171  }
172
173  if ($row['max_id'] > $max_id)
174  {
175    $max_id = $row['max_id'];
176  }
177
178  if ($is_first)
179  {
180    $is_first = false;
181    $first_time_key = $time_keys[3];
182  }
183}
184
185// Only the oldest time_key might be already summarized, so we have to
186// update the 4 corresponding lines instead of simply inserting them.
187//
188// For example, if the oldest unsummarized is 2005.08.25.21, the 4 lines
189// that can be updated are:
190//
191// +---------------+----------+
192// | id            | nb_pages |
193// +---------------+----------+
194// | 2005          |   241109 |
195// | 2005.08       |    20133 |
196// | 2005.08.25    |      620 |
197// | 2005.08.25.21 |      151 |
198// +---------------+----------+
199
200$existing_time_keys = array();
201
202if (isset($first_time_key))
203{
204  list($year, $month, $day, $hour) = explode('.', $first_time_key);
205
206  $time_keys = array(
207    sprintf('%4u',                $year),
208    sprintf('%4u.%02u',           $year, $month),
209    sprintf('%4u.%02u.%02u',      $year, $month, $day),
210    sprintf('%4u.%02u.%02u.%02u', $year, $month, $day, $hour),
211    );
212
213  $query = '
214SELECT
215    id,
216    nb_pages
217  FROM '.HISTORY_SUMMARY_TABLE.'
218  WHERE id IN (\''.implode("', '", $time_keys).'\')
219;';
220  $result = pwg_query($query);
221  while ($row = mysql_fetch_array($result))
222  {
223    $existing_time_keys[ $row['id'] ] = $row['nb_pages'];
224  }
225}
226
227$updates = array();
228$inserts = array();
229
230foreach (array_keys($need_update) as $time_key)
231{
232  $time_tokens = explode('.', $time_key);
233
234  if (isset($existing_time_keys[$time_key]))
235  {
236    array_push(
237      $updates,
238      array(
239        'id'       => $time_key,
240        'nb_pages' => $existing_time_keys[$time_key] + $need_update[$time_key],
241        )
242      );
243  }
244  else
245  {
246    array_push(
247      $inserts,
248      array(
249        'id'       => $time_key,
250        'year'     => $time_tokens[0],
251        'month'    => @$time_tokens[1],
252        'day'      => @$time_tokens[2],
253        'hour'     => @$time_tokens[3],
254        'nb_pages' => $need_update[$time_key],
255        )
256      );
257  }
258}
259
260if (count($updates) > 0)
261{
262  mass_updates(
263    HISTORY_SUMMARY_TABLE,
264    array(
265      'primary' => array('id'),
266      'update'  => array('nb_pages'),
267      ),
268    $updates
269    );
270}
271
272if (count($inserts) > 0)
273{
274  mass_inserts(
275    HISTORY_SUMMARY_TABLE,
276    array_keys($inserts[0]),
277    $inserts
278    );
279}
280
281if ($max_id != 0)
282{
283  $query = '
284UPDATE '.HISTORY_TABLE.'
285  SET summarized = \'true\'
286  WHERE summarized = \'false\'
287    AND id <= '.$max_id.'
288;';
289  pwg_query($query);
290}
291
292// +-----------------------------------------------------------------------+
293// | Page parameters check                                                 |
294// +-----------------------------------------------------------------------+
295
296foreach (array('day', 'month', 'year') as $key)
297{
298  if (isset($_GET[$key]))
299  {
300    $page[$key] = (int)$_GET[$key];
301  }
302}
303
304if (isset($page['day']))
305{
306  if (!isset($page['month']))
307  {
308    die('month is missing in URL');
309  }
310}
311
312if (isset($page['month']))
313{
314  if (!isset($page['year']))
315  {
316    die('year is missing in URL');
317  }
318}
319
320$summary_lines = get_summary(
321  @$page['year'],
322  @$page['month'],
323  @$page['day']
324  );
325
326// +-----------------------------------------------------------------------+
327// | Display statistics header                                             |
328// +-----------------------------------------------------------------------+
329
330// page title creation
331$title_parts = array();
332
333$url = PHPWG_ROOT_PATH.'admin.php?page=stats';
334
335array_push(
336  $title_parts,
337  '<a href="'.$url.'">'.l10n('Overall').'</a>'
338  );
339
340$period_label = l10n('Year');
341
342if (isset($page['year']))
343{
344  $url.= '&amp;year='.$page['year'];
345
346  array_push(
347    $title_parts,
348    '<a href="'.$url.'">'.$page['year'].'</a>'
349    );
350
351  $period_label = l10n('Month');
352}
353
354if (isset($page['month']))
355{
356  $url.= '&amp;month='.$page['month'];
357
358  array_push(
359    $title_parts,
360    '<a href="'.$url.'">'.$lang['month'][$page['month']].'</a>'
361    );
362
363  $period_label = l10n('Day');
364}
365
366if (isset($page['day']))
367{
368  $url.= '&amp;day='.$page['day'];
369
370  $time = mktime(12, 0, 0, $page['month'], $page['day'], $page['year']);
371
372  $day_title = sprintf(
373    '%u (%s)',
374    $page['day'],
375    $lang['day'][date('w', $time)]
376    );
377
378  array_push(
379    $title_parts,
380    '<a href="'.$url.'">'.$day_title.'</a>'
381    );
382
383  $period_label = l10n('Hour');
384}
385
386$template->set_filename('stats', 'admin/stats.tpl');
387
388// TabSheet initialization
389history_tabsheet();
390
391$base_url = get_root_url().'admin.php?page=history';
392
393$template->assign(
394  array(
395    'L_STAT_TITLE' => implode($conf['level_separator'], $title_parts),
396    'PERIOD_LABEL' => $period_label,
397    'U_HELP' => get_root_url().'popuphelp.php?page=history',
398    'F_ACTION' => $base_url,
399    )
400  );
401
402// +-----------------------------------------------------------------------+
403// | Display statistic rows                                                |
404// +-----------------------------------------------------------------------+
405
406$max_width = 400;
407
408$datas = array();
409
410if (isset($page['day']))
411{
412  $key = 'hour';
413  $min_x = 0;
414  $max_x = 23;
415}
416elseif (isset($page['month']))
417{
418  $key = 'day';
419  $min_x = 1;
420  $max_x = date(
421    't',
422    mktime(12, 0, 0, $page['month'], 1, $page['year'])
423    );
424}
425elseif (isset($page['year']))
426{
427  $key = 'month';
428  $min_x = 1;
429  $max_x = 12;
430}
431else
432{
433  $key = 'year';
434}
435
436$max_pages = 1;
437foreach ($summary_lines as $line)
438{
439  if ($line['nb_pages'] > $max_pages)
440  {
441    $max_pages = $line['nb_pages'];
442  }
443
444  $datas[ $line[$key] ] = $line['nb_pages'];
445}
446
447if (!isset($min_x) and !isset($max_x) and count($datas) > 0)
448{
449  $min_x = min(array_keys($datas));
450  $max_x = max(array_keys($datas));
451}
452
453if (count($datas) > 0)
454{
455  for ($i = $min_x; $i <= $max_x; $i++)
456  {
457    if (!isset($datas[$i]))
458    {
459      $datas[$i] = 0;
460    }
461
462    $url = null;
463
464    if (isset($page['day']))
465    {
466      $value = sprintf('%02u', $i);
467    }
468    else if (isset($page['month']))
469    {
470      $url =
471        get_root_url().'admin.php'
472        .'?page=stats'
473        .'&amp;year='.$page['year']
474        .'&amp;month='.$page['month']
475        .'&amp;day='.$i
476        ;
477
478      $time = mktime(12, 0, 0, $page['month'], $i, $page['year']);
479
480      $value = $i.' ('.$lang['day'][date('w', $time)].')';
481    }
482    else if (isset($page['year']))
483    {
484      $url =
485        get_root_url().'admin.php'
486        .'?page=stats'
487        .'&amp;year='.$page['year']
488        .'&amp;month='.$i
489        ;
490
491      $value = $lang['month'][$i];
492    }
493    else
494    {
495      // at least the year is defined
496      $url =
497        get_root_url().'admin.php'
498        .'?page=stats'
499        .'&amp;year='.$i
500        ;
501
502      $value = $i;
503    }
504
505    if ($datas[$i] != 0 and isset($url))
506    {
507      $value = '<a href="'.$url.'">'.$value.'</a>';
508    }
509
510    $template->append(
511      'statrows',
512      array(
513        'VALUE' => $value,
514        'PAGES' => $datas[$i],
515        'WIDTH' => ceil(($datas[$i] * $max_width) / $max_pages ),
516        )
517      );
518  }
519}
520
521// +-----------------------------------------------------------------------+
522// | Sending html code                                                     |
523// +-----------------------------------------------------------------------+
524
525$template->assign_var_from_handle('ADMIN_CONTENT', 'stats');
526?>
Note: See TracBrowser for help on using the repository browser.