source: trunk/admin/stats.php @ 19703

Last change on this file since 19703 was 19703, checked in by plg, 11 years ago

update Piwigo headers to 2013 (the end of the world didn't occur as expected on r12922)

  • Property svn:eol-style set to LF
File size: 11.4 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based photo gallery                                    |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2013 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    array_push($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      array_push($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  array_push(
240      $inserts,
241      array(
242        'year'     => $time_tokens[0],
243        'month'    => @$time_tokens[1],
244        'day'      => @$time_tokens[2],
245        'hour'     => @$time_tokens[3],
246        'nb_pages' => $nb_pages,
247        )
248      );
249}
250
251if (count($updates) > 0)
252{
253  mass_updates(
254    HISTORY_SUMMARY_TABLE,
255    array(
256      'primary' => array('year','month','day','hour'),
257      'update'  => array('nb_pages'),
258      ),
259    $updates
260    );
261}
262
263if (count($inserts) > 0)
264{
265  mass_inserts(
266    HISTORY_SUMMARY_TABLE,
267    array_keys($inserts[0]),
268    $inserts
269    );
270}
271
272if ($max_id != 0)
273{
274  $query = '
275UPDATE '.HISTORY_TABLE.'
276  SET summarized = \'true\'
277  WHERE summarized = \'false\'
278    AND id <= '.$max_id.'
279;';
280  pwg_query($query);
281}
282
283// +-----------------------------------------------------------------------+
284// | Page parameters check                                                 |
285// +-----------------------------------------------------------------------+
286
287foreach (array('day', 'month', 'year') as $key)
288{
289  if (isset($_GET[$key]))
290  {
291    $page[$key] = (int)$_GET[$key];
292  }
293}
294
295if (isset($page['day']))
296{
297  if (!isset($page['month']))
298  {
299    die('month is missing in URL');
300  }
301}
302
303if (isset($page['month']))
304{
305  if (!isset($page['year']))
306  {
307    die('year is missing in URL');
308  }
309}
310
311$summary_lines = get_summary(
312  @$page['year'],
313  @$page['month'],
314  @$page['day']
315  );
316
317// +-----------------------------------------------------------------------+
318// | Display statistics header                                             |
319// +-----------------------------------------------------------------------+
320
321// page title creation
322$title_parts = array();
323
324$url = PHPWG_ROOT_PATH.'admin.php?page=stats';
325
326array_push(
327  $title_parts,
328  '<a href="'.$url.'">'.l10n('Overall').'</a>'
329  );
330
331$period_label = l10n('Year');
332
333if (isset($page['year']))
334{
335  $url.= '&amp;year='.$page['year'];
336
337  array_push(
338    $title_parts,
339    '<a href="'.$url.'">'.$page['year'].'</a>'
340    );
341
342  $period_label = l10n('Month');
343}
344
345if (isset($page['month']))
346{
347  $url.= '&amp;month='.$page['month'];
348
349  array_push(
350    $title_parts,
351    '<a href="'.$url.'">'.$lang['month'][$page['month']].'</a>'
352    );
353
354  $period_label = l10n('Day');
355}
356
357if (isset($page['day']))
358{
359  $url.= '&amp;day='.$page['day'];
360
361  $time = mktime(12, 0, 0, $page['month'], $page['day'], $page['year']);
362
363  $day_title = sprintf(
364    '%u (%s)',
365    $page['day'],
366    $lang['day'][date('w', $time)]
367    );
368
369  array_push(
370    $title_parts,
371    '<a href="'.$url.'">'.$day_title.'</a>'
372    );
373
374  $period_label = l10n('Hour');
375}
376
377$template->set_filename('stats', 'stats.tpl');
378
379// TabSheet initialization
380history_tabsheet();
381
382$base_url = get_root_url().'admin.php?page=history';
383
384$template->assign(
385  array(
386    'L_STAT_TITLE' => implode($conf['level_separator'], $title_parts),
387    'PERIOD_LABEL' => $period_label,
388    'U_HELP' => get_root_url().'admin/popuphelp.php?page=history',
389    'F_ACTION' => $base_url,
390    )
391  );
392
393// +-----------------------------------------------------------------------+
394// | Display statistic rows                                                |
395// +-----------------------------------------------------------------------+
396
397$max_width = 400;
398
399$datas = array();
400
401if (isset($page['day']))
402{
403  $key = 'hour';
404  $min_x = 0;
405  $max_x = 23;
406}
407elseif (isset($page['month']))
408{
409  $key = 'day';
410  $min_x = 1;
411  $max_x = date(
412    't',
413    mktime(12, 0, 0, $page['month'], 1, $page['year'])
414    );
415}
416elseif (isset($page['year']))
417{
418  $key = 'month';
419  $min_x = 1;
420  $max_x = 12;
421}
422else
423{
424  $key = 'year';
425}
426
427$max_pages = 1;
428foreach ($summary_lines as $line)
429{
430  if ($line['nb_pages'] > $max_pages)
431  {
432    $max_pages = $line['nb_pages'];
433  }
434
435  $datas[ $line[$key] ] = $line['nb_pages'];
436}
437
438if (!isset($min_x) and !isset($max_x) and count($datas) > 0)
439{
440  $min_x = min(array_keys($datas));
441  $max_x = max(array_keys($datas));
442}
443
444if (count($datas) > 0)
445{
446  for ($i = $min_x; $i <= $max_x; $i++)
447  {
448    if (!isset($datas[$i]))
449    {
450      $datas[$i] = 0;
451    }
452
453    $url = null;
454
455    if (isset($page['day']))
456    {
457      $value = sprintf('%02u', $i);
458    }
459    else if (isset($page['month']))
460    {
461      $url =
462        get_root_url().'admin.php'
463        .'?page=stats'
464        .'&amp;year='.$page['year']
465        .'&amp;month='.$page['month']
466        .'&amp;day='.$i
467        ;
468
469      $time = mktime(12, 0, 0, $page['month'], $i, $page['year']);
470
471      $value = $i.' ('.$lang['day'][date('w', $time)].')';
472    }
473    else if (isset($page['year']))
474    {
475      $url =
476        get_root_url().'admin.php'
477        .'?page=stats'
478        .'&amp;year='.$page['year']
479        .'&amp;month='.$i
480        ;
481
482      $value = $lang['month'][$i];
483    }
484    else
485    {
486      // at least the year is defined
487      $url =
488        get_root_url().'admin.php'
489        .'?page=stats'
490        .'&amp;year='.$i
491        ;
492
493      $value = $i;
494    }
495
496    if ($datas[$i] != 0 and isset($url))
497    {
498      $value = '<a href="'.$url.'">'.$value.'</a>';
499    }
500
501    $template->append(
502      'statrows',
503      array(
504        'VALUE' => $value,
505        'PAGES' => $datas[$i],
506        'WIDTH' => ceil(($datas[$i] * $max_width) / $max_pages ),
507        )
508      );
509  }
510}
511
512// +-----------------------------------------------------------------------+
513// | Sending html code                                                     |
514// +-----------------------------------------------------------------------+
515
516$template->assign_var_from_handle('ADMIN_CONTENT', 'stats');
517?>
Note: See TracBrowser for help on using the repository browser.