source: branches/2.6/admin/stats.php @ 26901

Last change on this file since 26901 was 26461, checked in by mistic100, 10 years ago

Update headers to 2014. Happy new year!!

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