source: branches/2.1/admin/stats.php @ 6364

Last change on this file since 6364 was 6364, checked in by plg, 14 years ago

remove all svn:mergeinfo properties

  • Property svn:eol-style set to LF
File size: 11.3 KB
RevLine 
[24]1<?php
[362]2// +-----------------------------------------------------------------------+
[2297]3// | Piwigo - a PHP based picture gallery                                  |
4// +-----------------------------------------------------------------------+
[5196]5// | Copyright(C) 2008-2010 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,
119    HOUR(time) AS hour,
120    MAX(id) AS max_id,
[1727]121    COUNT(*) AS nb_pages
122  FROM '.HISTORY_TABLE.'
123  WHERE summarized = \'false\'
124  GROUP BY
[2333]125    date ASC,
126    HOUR(time) ASC
[1727]127;';
128$result = pwg_query($query);
[894]129
[1727]130$need_update = array();
131
132$max_id = 0;
133$is_first = true;
134$first_time_key = null;
135
[4325]136while ($row = pwg_db_fetch_assoc($result))
[766]137{
[1727]138  $time_keys = array(
[2333]139    substr($row['date'], 0, 4), //yyyy
140    substr($row['date'], 0, 7), //yyyy-mm
141    substr($row['date'], 0, 10),//yyyy-mm-dd
[1727]142    sprintf(
[2333]143      '%s-%02u',
144      $row['date'], $row['hour']
[1727]145      ),
146    );
147
148  foreach ($time_keys as $time_key)
149  {
150    if (!isset($need_update[$time_key]))
151    {
152      $need_update[$time_key] = 0;
153    }
154    $need_update[$time_key] += $row['nb_pages'];
155  }
156
157  if ($row['max_id'] > $max_id)
158  {
159    $max_id = $row['max_id'];
160  }
161
162  if ($is_first)
163  {
164    $is_first = false;
165    $first_time_key = $time_keys[3];
166  }
[894]167}
[1727]168
169// Only the oldest time_key might be already summarized, so we have to
170// update the 4 corresponding lines instead of simply inserting them.
171//
172// For example, if the oldest unsummarized is 2005.08.25.21, the 4 lines
173// that can be updated are:
174//
175// +---------------+----------+
176// | id            | nb_pages |
177// +---------------+----------+
178// | 2005          |   241109 |
[2333]179// | 2005-08       |    20133 |
180// | 2005-08-25    |      620 |
181// | 2005-08-25-21 |      151 |
[1727]182// +---------------+----------+
183
184
[2333]185$updates = array();
186$inserts = array();
187
[1727]188if (isset($first_time_key))
[894]189{
[2333]190  list($year, $month, $day, $hour) = explode('-', $first_time_key);
[1727]191
192  $query = '
[2333]193SELECT *
[1727]194  FROM '.HISTORY_SUMMARY_TABLE.'
[2333]195  WHERE year='.$year.'
196    AND ( month IS NULL
197      OR ( month='.$month.'
198        AND ( day is NULL
199          OR (day='.$day.'
200            AND (hour IS NULL OR hour='.$hour.')
201          )
202        )
203      )
204    )
[1727]205;';
206  $result = pwg_query($query);
[4325]207  while ($row = pwg_db_fetch_assoc($result))
[1727]208  {
[2333]209    $key = sprintf('%4u', $row['year']);
210    if ( isset($row['month']) )
211    {
212      $key .= sprintf('-%02u', $row['month']);
213      if ( isset($row['day']) )
214      {
215        $key .= sprintf('-%02u', $row['day']);
216        if ( isset($row['hour']) )
217        {
218          $key .= sprintf('-%02u', $row['hour']);
219        }
220      }
221    }
222
223    if (isset($need_update[$key]))
224    {
225      $row['nb_pages'] += $need_update[$key];
226      array_push($updates, $row);
227      unset($need_update[$key]);
228    }
[1727]229  }
[766]230}
[1727]231
[2333]232foreach ($need_update as $time_key => $nb_pages)
[766]233{
[2333]234  $time_tokens = explode('-', $time_key);
[1727]235
[2333]236  array_push(
[1727]237      $inserts,
238      array(
239        'year'     => $time_tokens[0],
240        'month'    => @$time_tokens[1],
241        'day'      => @$time_tokens[2],
242        'hour'     => @$time_tokens[3],
[2333]243        'nb_pages' => $nb_pages,
[1727]244        )
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
323array_push(
324  $title_parts,
325  '<a href="'.$url.'">'.l10n('Overall').'</a>'
326  );
327
328$period_label = l10n('Year');
329
330if (isset($page['year']))
331{
332  $url.= '&amp;year='.$page['year'];
333
334  array_push(
335    $title_parts,
[1782]336    '<a href="'.$url.'">'.$page['year'].'</a>'
[1727]337    );
338
339  $period_label = l10n('Month');
340}
341
342if (isset($page['month']))
343{
344  $url.= '&amp;month='.$page['month'];
345
346  array_push(
347    $title_parts,
348    '<a href="'.$url.'">'.$lang['month'][$page['month']].'</a>'
349    );
350
351  $period_label = l10n('Day');
352}
353
354if (isset($page['day']))
355{
356  $url.= '&amp;day='.$page['day'];
357
358  $time = mktime(12, 0, 0, $page['month'], $page['day'], $page['year']);
[2108]359
[1727]360  $day_title = sprintf(
361    '%u (%s)',
362    $page['day'],
363    $lang['day'][date('w', $time)]
364    );
[2108]365
[1727]366  array_push(
367    $title_parts,
368    '<a href="'.$url.'">'.$day_title.'</a>'
369    );
[527]370
[1727]371  $period_label = l10n('Hour');
372}
[766]373
[2530]374$template->set_filename('stats', 'stats.tpl');
[1727]375
[1881]376// TabSheet initialization
377history_tabsheet();
378
[2245]379$base_url = get_root_url().'admin.php?page=history';
[2184]380
[2245]381$template->assign(
[1727]382  array(
383    'L_STAT_TITLE' => implode($conf['level_separator'], $title_parts),
384    'PERIOD_LABEL' => $period_label,
[5920]385    'U_HELP' => get_root_url().'admin/popuphelp.php?page=history',
[2245]386    'F_ACTION' => $base_url,
[1727]387    )
388  );
389
390// +-----------------------------------------------------------------------+
391// | Display statistic rows                                                |
392// +-----------------------------------------------------------------------+
393
[1729]394$max_width = 400;
[1727]395
[1729]396$datas = array();
397
398if (isset($page['day']))
399{
400  $key = 'hour';
401  $min_x = 0;
402  $max_x = 23;
403}
404elseif (isset($page['month']))
405{
406  $key = 'day';
407  $min_x = 1;
408  $max_x = date(
409    't',
410    mktime(12, 0, 0, $page['month'], 1, $page['year'])
411    );
412}
413elseif (isset($page['year']))
414{
415  $key = 'month';
416  $min_x = 1;
417  $max_x = 12;
418}
419else
420{
421  $key = 'year';
422}
423
424$max_pages = 1;
[1727]425foreach ($summary_lines as $line)
[566]426{
[1729]427  if ($line['nb_pages'] > $max_pages)
428  {
429    $max_pages = $line['nb_pages'];
430  }
[1727]431
[1729]432  $datas[ $line[$key] ] = $line['nb_pages'];
433}
434
[1733]435if (!isset($min_x) and !isset($max_x) and count($datas) > 0)
[1729]436{
437  $min_x = min(array_keys($datas));
438  $max_x = max(array_keys($datas));
439}
440
[1733]441if (count($datas) > 0)
[1729]442{
[1733]443  for ($i = $min_x; $i <= $max_x; $i++)
[766]444  {
[1733]445    if (!isset($datas[$i]))
446    {
447      $datas[$i] = 0;
448    }
[2108]449
[1733]450    $url = null;
[2108]451
[1733]452    if (isset($page['day']))
453    {
[1782]454      $value = sprintf('%02u', $i);
[1733]455    }
456    else if (isset($page['month']))
457    {
458      $url =
[2245]459        get_root_url().'admin.php'
[1733]460        .'?page=stats'
461        .'&amp;year='.$page['year']
462        .'&amp;month='.$page['month']
463        .'&amp;day='.$i
464        ;
[2108]465
[1733]466      $time = mktime(12, 0, 0, $page['month'], $i, $page['year']);
[2108]467
[1733]468      $value = $i.' ('.$lang['day'][date('w', $time)].')';
469    }
470    else if (isset($page['year']))
471    {
472      $url =
[2245]473        get_root_url().'admin.php'
[1733]474        .'?page=stats'
475        .'&amp;year='.$page['year']
476        .'&amp;month='.$i
477        ;
[2108]478
[1733]479      $value = $lang['month'][$i];
480    }
481    else
482    {
483      // at least the year is defined
484      $url =
[2245]485        get_root_url().'admin.php'
[1733]486        .'?page=stats'
487        .'&amp;year='.$i
488        ;
[2108]489
[1733]490      $value = $i;
491    }
[2108]492
[1733]493    if ($datas[$i] != 0 and isset($url))
494    {
495      $value = '<a href="'.$url.'">'.$value.'</a>';
496    }
[2108]497
[2245]498    $template->append(
499      'statrows',
[1733]500      array(
501        'VALUE' => $value,
502        'PAGES' => $datas[$i],
503        'WIDTH' => ceil(($datas[$i] * $max_width) / $max_pages ),
504        )
505      );
[766]506  }
[566]507}
[894]508
[1727]509// +-----------------------------------------------------------------------+
510// | Sending html code                                                     |
511// +-----------------------------------------------------------------------+
[894]512
[527]513$template->assign_var_from_handle('ADMIN_CONTENT', 'stats');
[2108]514?>
Note: See TracBrowser for help on using the repository browser.