source: trunk/admin/stats.php @ 1900

Last change on this file since 1900 was 1900, checked in by rub, 17 years ago

Apply property svn:eol-style Value: LF

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