source: trunk/admin/stats.php @ 1782

Last change on this file since 1782 was 1782, checked in by rvelices, 17 years ago

history: removed some php warnings and reduced some annoying language warnings

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.3 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 1782 2007-02-06 01:10:59Z rvelices $
9// | last update   : $Date: 2007-02-06 01:10:59 +0000 (Tue, 06 Feb 2007) $
10// | last modifier : $Author: rvelices $
11// | revision      : $Revision: 1782 $
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');
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_filenames(array('stats'=>'admin/stats.tpl'));
387
388$template->assign_vars(
389  array(
390    'L_STAT_TITLE' => implode($conf['level_separator'], $title_parts),
391    'PERIOD_LABEL' => $period_label,
392    )
393  );
394
395// +-----------------------------------------------------------------------+
396// | Display statistic rows                                                |
397// +-----------------------------------------------------------------------+
398
399$max_width = 400;
400
401$datas = array();
402
403if (isset($page['day']))
404{
405  $key = 'hour';
406  $min_x = 0;
407  $max_x = 23;
408}
409elseif (isset($page['month']))
410{
411  $key = 'day';
412  $min_x = 1;
413  $max_x = date(
414    't',
415    mktime(12, 0, 0, $page['month'], 1, $page['year'])
416    );
417}
418elseif (isset($page['year']))
419{
420  $key = 'month';
421  $min_x = 1;
422  $max_x = 12;
423}
424else
425{
426  $key = 'year';
427}
428
429$max_pages = 1;
430foreach ($summary_lines as $line)
431{
432  if ($line['nb_pages'] > $max_pages)
433  {
434    $max_pages = $line['nb_pages'];
435  }
436
437  $datas[ $line[$key] ] = $line['nb_pages'];
438}
439
440if (!isset($min_x) and !isset($max_x) and count($datas) > 0)
441{
442  $min_x = min(array_keys($datas));
443  $max_x = max(array_keys($datas));
444}
445
446if (count($datas) > 0)
447{
448  for ($i = $min_x; $i <= $max_x; $i++)
449  {
450    if (!isset($datas[$i]))
451    {
452      $datas[$i] = 0;
453    }
454 
455    $url = null;
456 
457    if (isset($page['day']))
458    {
459      $value = sprintf('%02u', $i);
460    }
461    else if (isset($page['month']))
462    {
463      $url =
464        PHPWG_ROOT_PATH.'admin.php'
465        .'?page=stats'
466        .'&amp;year='.$page['year']
467        .'&amp;month='.$page['month']
468        .'&amp;day='.$i
469        ;
470 
471      $time = mktime(12, 0, 0, $page['month'], $i, $page['year']);
472     
473      $value = $i.' ('.$lang['day'][date('w', $time)].')';
474    }
475    else if (isset($page['year']))
476    {
477      $url =
478        PHPWG_ROOT_PATH.'admin.php'
479        .'?page=stats'
480        .'&amp;year='.$page['year']
481        .'&amp;month='.$i
482        ;
483     
484      $value = $lang['month'][$i];
485    }
486    else
487    {
488      // at least the year is defined
489      $url =
490        PHPWG_ROOT_PATH.'admin.php'
491        .'?page=stats'
492        .'&amp;year='.$i
493        ;
494     
495      $value = $i;
496    }
497 
498    if ($datas[$i] != 0 and isset($url))
499    {
500      $value = '<a href="'.$url.'">'.$value.'</a>';
501    }
502   
503    $template->assign_block_vars(
504      'statrow',
505      array(
506        'VALUE' => $value,
507        'PAGES' => $datas[$i],
508        'WIDTH' => ceil(($datas[$i] * $max_width) / $max_pages ),
509        )
510      );
511  }
512}
513
514// +-----------------------------------------------------------------------+
515// | Sending html code                                                     |
516// +-----------------------------------------------------------------------+
517
518$template->assign_var_from_handle('ADMIN_CONTENT', 'stats');
519?>
520
Note: See TracBrowser for help on using the repository browser.