source: trunk/admin/stats.php @ 21984

Last change on this file since 21984 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
RevLine 
[24]1<?php
[362]2// +-----------------------------------------------------------------------+
[8728]3// | Piwigo - a PHP based photo gallery                                    |
[2297]4// +-----------------------------------------------------------------------+
[19703]5// | Copyright(C) 2008-2013 Piwigo Team                  http://piwigo.org |
[2297]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// +-----------------------------------------------------------------------+
[1727]23
24if (!defined("PHPWG_ROOT_PATH"))
[527]25{
[1727]26  die ("Hacking attempt!");
[527]27}
28
[1072]29include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
[1881]30include_once(PHPWG_ROOT_PATH.'admin/include/functions_history.inc.php');
[1072]31
32// +-----------------------------------------------------------------------+
[1727]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;
[2108]46
[1727]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
[2108]83;';
[1727]84  }
85  else
86  {
87    $query.= '
88  WHERE year IS NOT NULL
89    AND month IS NULL
90  ORDER BY
91    year ASC
[2108]92;';
[1727]93  }
94
95  $result = pwg_query($query);
96
97  $output = array();
[4325]98  while ($row = pwg_db_fetch_assoc($result))
[1727]99  {
100    array_push($output, $row);
101  }
102
103  return $output;
104}
105
106// +-----------------------------------------------------------------------+
[1072]107// | Check Access and exit when user status is not ok                      |
108// +-----------------------------------------------------------------------+
[1727]109
[1072]110check_status(ACCESS_ADMINISTRATOR);
111
[1727]112// +-----------------------------------------------------------------------+
113// | Refresh summary from details                                          |
114// +-----------------------------------------------------------------------+
[766]115
[1727]116$query = '
117SELECT
[2333]118    date,
[6578]119    '.pwg_db_get_hour('time').' AS hour,
[2333]120    MAX(id) AS max_id,
[1727]121    COUNT(*) AS nb_pages
122  FROM '.HISTORY_TABLE.'
123  WHERE summarized = \'false\'
124  GROUP BY
[6463]125    date,
126    hour
127  ORDER BY
[2333]128    date ASC,
[6463]129    hour ASC
[1727]130;';
131$result = pwg_query($query);
[894]132
[1727]133$need_update = array();
134
135$max_id = 0;
136$is_first = true;
137$first_time_key = null;
138
[4325]139while ($row = pwg_db_fetch_assoc($result))
[766]140{
[1727]141  $time_keys = array(
[2333]142    substr($row['date'], 0, 4), //yyyy
143    substr($row['date'], 0, 7), //yyyy-mm
144    substr($row['date'], 0, 10),//yyyy-mm-dd
[1727]145    sprintf(
[2333]146      '%s-%02u',
147      $row['date'], $row['hour']
[1727]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  }
[894]170}
[1727]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 |
[2333]182// | 2005-08       |    20133 |
183// | 2005-08-25    |      620 |
184// | 2005-08-25-21 |      151 |
[1727]185// +---------------+----------+
186
187
[2333]188$updates = array();
189$inserts = array();
190
[1727]191if (isset($first_time_key))
[894]192{
[2333]193  list($year, $month, $day, $hour) = explode('-', $first_time_key);
[1727]194
195  $query = '
[2333]196SELECT *
[1727]197  FROM '.HISTORY_SUMMARY_TABLE.'
[2333]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    )
[1727]208;';
209  $result = pwg_query($query);
[4325]210  while ($row = pwg_db_fetch_assoc($result))
[1727]211  {
[2333]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    }
[1727]232  }
[766]233}
[1727]234
[2333]235foreach ($need_update as $time_key => $nb_pages)
[766]236{
[2333]237  $time_tokens = explode('-', $time_key);
[1727]238
[2333]239  array_push(
[1727]240      $inserts,
241      array(
242        'year'     => $time_tokens[0],
243        'month'    => @$time_tokens[1],
244        'day'      => @$time_tokens[2],
245        'hour'     => @$time_tokens[3],
[2333]246        'nb_pages' => $nb_pages,
[1727]247        )
248      );
[766]249}
250
[1727]251if (count($updates) > 0)
252{
253  mass_updates(
254    HISTORY_SUMMARY_TABLE,
255    array(
[2333]256      'primary' => array('year','month','day','hour'),
[1727]257      'update'  => array('nb_pages'),
258      ),
259    $updates
260    );
261}
[894]262
[1727]263if (count($inserts) > 0)
[894]264{
[1727]265  mass_inserts(
266    HISTORY_SUMMARY_TABLE,
267    array_keys($inserts[0]),
268    $inserts
269    );
[894]270}
[1727]271
272if ($max_id != 0)
[894]273{
[1727]274  $query = '
275UPDATE '.HISTORY_TABLE.'
276  SET summarized = \'true\'
277  WHERE summarized = \'false\'
278    AND id <= '.$max_id.'
279;';
280  pwg_query($query);
[894]281}
[1727]282
283// +-----------------------------------------------------------------------+
284// | Page parameters check                                                 |
285// +-----------------------------------------------------------------------+
286
287foreach (array('day', 'month', 'year') as $key)
[894]288{
[1727]289  if (isset($_GET[$key]))
290  {
291    $page[$key] = (int)$_GET[$key];
292  }
[894]293}
294
[1727]295if (isset($page['day']))
296{
297  if (!isset($page['month']))
298  {
299    die('month is missing in URL');
300  }
301}
[894]302
[1727]303if (isset($page['month']))
304{
305  if (!isset($page['year']))
306  {
307    die('year is missing in URL');
308  }
309}
[527]310
[1727]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,
[1782]339    '<a href="'.$url.'">'.$page['year'].'</a>'
[1727]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']);
[2108]362
[1727]363  $day_title = sprintf(
364    '%u (%s)',
365    $page['day'],
366    $lang['day'][date('w', $time)]
367    );
[2108]368
[1727]369  array_push(
370    $title_parts,
371    '<a href="'.$url.'">'.$day_title.'</a>'
372    );
[527]373
[1727]374  $period_label = l10n('Hour');
375}
[766]376
[2530]377$template->set_filename('stats', 'stats.tpl');
[1727]378
[1881]379// TabSheet initialization
380history_tabsheet();
381
[2245]382$base_url = get_root_url().'admin.php?page=history';
[2184]383
[2245]384$template->assign(
[1727]385  array(
386    'L_STAT_TITLE' => implode($conf['level_separator'], $title_parts),
387    'PERIOD_LABEL' => $period_label,
[5920]388    'U_HELP' => get_root_url().'admin/popuphelp.php?page=history',
[2245]389    'F_ACTION' => $base_url,
[1727]390    )
391  );
392
393// +-----------------------------------------------------------------------+
394// | Display statistic rows                                                |
395// +-----------------------------------------------------------------------+
396
[1729]397$max_width = 400;
[1727]398
[1729]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;
[1727]428foreach ($summary_lines as $line)
[566]429{
[1729]430  if ($line['nb_pages'] > $max_pages)
431  {
432    $max_pages = $line['nb_pages'];
433  }
[1727]434
[1729]435  $datas[ $line[$key] ] = $line['nb_pages'];
436}
437
[1733]438if (!isset($min_x) and !isset($max_x) and count($datas) > 0)
[1729]439{
440  $min_x = min(array_keys($datas));
441  $max_x = max(array_keys($datas));
442}
443
[1733]444if (count($datas) > 0)
[1729]445{
[1733]446  for ($i = $min_x; $i <= $max_x; $i++)
[766]447  {
[1733]448    if (!isset($datas[$i]))
449    {
450      $datas[$i] = 0;
451    }
[2108]452
[1733]453    $url = null;
[2108]454
[1733]455    if (isset($page['day']))
456    {
[1782]457      $value = sprintf('%02u', $i);
[1733]458    }
459    else if (isset($page['month']))
460    {
461      $url =
[2245]462        get_root_url().'admin.php'
[1733]463        .'?page=stats'
464        .'&amp;year='.$page['year']
465        .'&amp;month='.$page['month']
466        .'&amp;day='.$i
467        ;
[2108]468
[1733]469      $time = mktime(12, 0, 0, $page['month'], $i, $page['year']);
[2108]470
[1733]471      $value = $i.' ('.$lang['day'][date('w', $time)].')';
472    }
473    else if (isset($page['year']))
474    {
475      $url =
[2245]476        get_root_url().'admin.php'
[1733]477        .'?page=stats'
478        .'&amp;year='.$page['year']
479        .'&amp;month='.$i
480        ;
[2108]481
[1733]482      $value = $lang['month'][$i];
483    }
484    else
485    {
486      // at least the year is defined
487      $url =
[2245]488        get_root_url().'admin.php'
[1733]489        .'?page=stats'
490        .'&amp;year='.$i
491        ;
[2108]492
[1733]493      $value = $i;
494    }
[2108]495
[1733]496    if ($datas[$i] != 0 and isset($url))
497    {
498      $value = '<a href="'.$url.'">'.$value.'</a>';
499    }
[2108]500
[2245]501    $template->append(
502      'statrows',
[1733]503      array(
504        'VALUE' => $value,
505        'PAGES' => $datas[$i],
506        'WIDTH' => ceil(($datas[$i] * $max_width) / $max_pages ),
507        )
508      );
[766]509  }
[566]510}
[894]511
[1727]512// +-----------------------------------------------------------------------+
513// | Sending html code                                                     |
514// +-----------------------------------------------------------------------+
[894]515
[527]516$template->assign_var_from_handle('ADMIN_CONTENT', 'stats');
[2108]517?>
Note: See TracBrowser for help on using the repository browser.