source: trunk/admin/stats.php @ 26010

Last change on this file since 26010 was 25018, checked in by mistic100, 11 years ago

remove all array_push (50% slower than []) + some changes missing for feature:2978

  • Property svn:eol-style set to LF
File size: 11.2 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  {
[25018]100    $output[] = $row;
[1727]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];
[25018]229      $updates[] = $row;
[2333]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
[25018]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    );
[766]246}
247
[1727]248if (count($updates) > 0)
249{
250  mass_updates(
251    HISTORY_SUMMARY_TABLE,
252    array(
[2333]253      'primary' => array('year','month','day','hour'),
[1727]254      'update'  => array('nb_pages'),
255      ),
256    $updates
257    );
258}
[894]259
[1727]260if (count($inserts) > 0)
[894]261{
[1727]262  mass_inserts(
263    HISTORY_SUMMARY_TABLE,
264    array_keys($inserts[0]),
265    $inserts
266    );
[894]267}
[1727]268
269if ($max_id != 0)
[894]270{
[1727]271  $query = '
272UPDATE '.HISTORY_TABLE.'
273  SET summarized = \'true\'
274  WHERE summarized = \'false\'
275    AND id <= '.$max_id.'
276;';
277  pwg_query($query);
[894]278}
[1727]279
280// +-----------------------------------------------------------------------+
281// | Page parameters check                                                 |
282// +-----------------------------------------------------------------------+
283
284foreach (array('day', 'month', 'year') as $key)
[894]285{
[1727]286  if (isset($_GET[$key]))
287  {
288    $page[$key] = (int)$_GET[$key];
289  }
[894]290}
291
[1727]292if (isset($page['day']))
293{
294  if (!isset($page['month']))
295  {
296    die('month is missing in URL');
297  }
298}
[894]299
[1727]300if (isset($page['month']))
301{
302  if (!isset($page['year']))
303  {
304    die('year is missing in URL');
305  }
306}
[527]307
[1727]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
[25018]323$title_parts[] = '<a href="'.$url.'">'.l10n('Overall').'</a>';
[1727]324
325$period_label = l10n('Year');
326
327if (isset($page['year']))
328{
329  $url.= '&amp;year='.$page['year'];
330
[25018]331  $title_parts[] = '<a href="'.$url.'">'.$page['year'].'</a>';
[1727]332
333  $period_label = l10n('Month');
334}
335
336if (isset($page['month']))
337{
338  $url.= '&amp;month='.$page['month'];
339
[25018]340  $title_parts[] = '<a href="'.$url.'">'.$lang['month'][$page['month']].'</a>';
[1727]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']);
[2108]350
[1727]351  $day_title = sprintf(
352    '%u (%s)',
353    $page['day'],
354    $lang['day'][date('w', $time)]
355    );
[2108]356
[25018]357  $title_parts[] = '<a href="'.$url.'">'.$day_title.'</a>';
[527]358
[1727]359  $period_label = l10n('Hour');
360}
[766]361
[2530]362$template->set_filename('stats', 'stats.tpl');
[1727]363
[1881]364// TabSheet initialization
365history_tabsheet();
366
[2245]367$base_url = get_root_url().'admin.php?page=history';
[2184]368
[2245]369$template->assign(
[1727]370  array(
371    'L_STAT_TITLE' => implode($conf['level_separator'], $title_parts),
372    'PERIOD_LABEL' => $period_label,
[5920]373    'U_HELP' => get_root_url().'admin/popuphelp.php?page=history',
[2245]374    'F_ACTION' => $base_url,
[1727]375    )
376  );
377
378// +-----------------------------------------------------------------------+
379// | Display statistic rows                                                |
380// +-----------------------------------------------------------------------+
381
[1729]382$max_width = 400;
[1727]383
[1729]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;
[1727]413foreach ($summary_lines as $line)
[566]414{
[1729]415  if ($line['nb_pages'] > $max_pages)
416  {
417    $max_pages = $line['nb_pages'];
418  }
[1727]419
[1729]420  $datas[ $line[$key] ] = $line['nb_pages'];
421}
422
[1733]423if (!isset($min_x) and !isset($max_x) and count($datas) > 0)
[1729]424{
425  $min_x = min(array_keys($datas));
426  $max_x = max(array_keys($datas));
427}
428
[1733]429if (count($datas) > 0)
[1729]430{
[1733]431  for ($i = $min_x; $i <= $max_x; $i++)
[766]432  {
[1733]433    if (!isset($datas[$i]))
434    {
435      $datas[$i] = 0;
436    }
[2108]437
[1733]438    $url = null;
[2108]439
[1733]440    if (isset($page['day']))
441    {
[1782]442      $value = sprintf('%02u', $i);
[1733]443    }
444    else if (isset($page['month']))
445    {
446      $url =
[2245]447        get_root_url().'admin.php'
[1733]448        .'?page=stats'
449        .'&amp;year='.$page['year']
450        .'&amp;month='.$page['month']
451        .'&amp;day='.$i
452        ;
[2108]453
[1733]454      $time = mktime(12, 0, 0, $page['month'], $i, $page['year']);
[2108]455
[1733]456      $value = $i.' ('.$lang['day'][date('w', $time)].')';
457    }
458    else if (isset($page['year']))
459    {
460      $url =
[2245]461        get_root_url().'admin.php'
[1733]462        .'?page=stats'
463        .'&amp;year='.$page['year']
464        .'&amp;month='.$i
465        ;
[2108]466
[1733]467      $value = $lang['month'][$i];
468    }
469    else
470    {
471      // at least the year is defined
472      $url =
[2245]473        get_root_url().'admin.php'
[1733]474        .'?page=stats'
475        .'&amp;year='.$i
476        ;
[2108]477
[1733]478      $value = $i;
479    }
[2108]480
[1733]481    if ($datas[$i] != 0 and isset($url))
482    {
483      $value = '<a href="'.$url.'">'.$value.'</a>';
484    }
[2108]485
[2245]486    $template->append(
487      'statrows',
[1733]488      array(
489        'VALUE' => $value,
490        'PAGES' => $datas[$i],
491        'WIDTH' => ceil(($datas[$i] * $max_width) / $max_pages ),
492        )
493      );
[766]494  }
[566]495}
[894]496
[1727]497// +-----------------------------------------------------------------------+
498// | Sending html code                                                     |
499// +-----------------------------------------------------------------------+
[894]500
[527]501$template->assign_var_from_handle('ADMIN_CONTENT', 'stats');
[2108]502?>
Note: See TracBrowser for help on using the repository browser.