source: trunk/admin/stats.php @ 6276

Last change on this file since 6276 was 5920, checked in by nikrou, 14 years ago

Bug 1617 fixed : help page is displayed in current theme in public or admin pages

  • Property svn:eol-style set to LF
File size: 11.3 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based picture gallery                                  |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2010 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    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 ASC,
126    HOUR(time) ASC
127;';
128$result = pwg_query($query);
129
130$need_update = array();
131
132$max_id = 0;
133$is_first = true;
134$first_time_key = null;
135
136while ($row = pwg_db_fetch_assoc($result))
137{
138  $time_keys = array(
139    substr($row['date'], 0, 4), //yyyy
140    substr($row['date'], 0, 7), //yyyy-mm
141    substr($row['date'], 0, 10),//yyyy-mm-dd
142    sprintf(
143      '%s-%02u',
144      $row['date'], $row['hour']
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  }
167}
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 |
179// | 2005-08       |    20133 |
180// | 2005-08-25    |      620 |
181// | 2005-08-25-21 |      151 |
182// +---------------+----------+
183
184
185$updates = array();
186$inserts = array();
187
188if (isset($first_time_key))
189{
190  list($year, $month, $day, $hour) = explode('-', $first_time_key);
191
192  $query = '
193SELECT *
194  FROM '.HISTORY_SUMMARY_TABLE.'
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    )
205;';
206  $result = pwg_query($query);
207  while ($row = pwg_db_fetch_assoc($result))
208  {
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    }
229  }
230}
231
232foreach ($need_update as $time_key => $nb_pages)
233{
234  $time_tokens = explode('-', $time_key);
235
236  array_push(
237      $inserts,
238      array(
239        'year'     => $time_tokens[0],
240        'month'    => @$time_tokens[1],
241        'day'      => @$time_tokens[2],
242        'hour'     => @$time_tokens[3],
243        'nb_pages' => $nb_pages,
244        )
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
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,
336    '<a href="'.$url.'">'.$page['year'].'</a>'
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']);
359
360  $day_title = sprintf(
361    '%u (%s)',
362    $page['day'],
363    $lang['day'][date('w', $time)]
364    );
365
366  array_push(
367    $title_parts,
368    '<a href="'.$url.'">'.$day_title.'</a>'
369    );
370
371  $period_label = l10n('Hour');
372}
373
374$template->set_filename('stats', 'stats.tpl');
375
376// TabSheet initialization
377history_tabsheet();
378
379$base_url = get_root_url().'admin.php?page=history';
380
381$template->assign(
382  array(
383    'L_STAT_TITLE' => implode($conf['level_separator'], $title_parts),
384    'PERIOD_LABEL' => $period_label,
385    'U_HELP' => get_root_url().'admin/popuphelp.php?page=history',
386    'F_ACTION' => $base_url,
387    )
388  );
389
390// +-----------------------------------------------------------------------+
391// | Display statistic rows                                                |
392// +-----------------------------------------------------------------------+
393
394$max_width = 400;
395
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;
425foreach ($summary_lines as $line)
426{
427  if ($line['nb_pages'] > $max_pages)
428  {
429    $max_pages = $line['nb_pages'];
430  }
431
432  $datas[ $line[$key] ] = $line['nb_pages'];
433}
434
435if (!isset($min_x) and !isset($max_x) and count($datas) > 0)
436{
437  $min_x = min(array_keys($datas));
438  $max_x = max(array_keys($datas));
439}
440
441if (count($datas) > 0)
442{
443  for ($i = $min_x; $i <= $max_x; $i++)
444  {
445    if (!isset($datas[$i]))
446    {
447      $datas[$i] = 0;
448    }
449
450    $url = null;
451
452    if (isset($page['day']))
453    {
454      $value = sprintf('%02u', $i);
455    }
456    else if (isset($page['month']))
457    {
458      $url =
459        get_root_url().'admin.php'
460        .'?page=stats'
461        .'&amp;year='.$page['year']
462        .'&amp;month='.$page['month']
463        .'&amp;day='.$i
464        ;
465
466      $time = mktime(12, 0, 0, $page['month'], $i, $page['year']);
467
468      $value = $i.' ('.$lang['day'][date('w', $time)].')';
469    }
470    else if (isset($page['year']))
471    {
472      $url =
473        get_root_url().'admin.php'
474        .'?page=stats'
475        .'&amp;year='.$page['year']
476        .'&amp;month='.$i
477        ;
478
479      $value = $lang['month'][$i];
480    }
481    else
482    {
483      // at least the year is defined
484      $url =
485        get_root_url().'admin.php'
486        .'?page=stats'
487        .'&amp;year='.$i
488        ;
489
490      $value = $i;
491    }
492
493    if ($datas[$i] != 0 and isset($url))
494    {
495      $value = '<a href="'.$url.'">'.$value.'</a>';
496    }
497
498    $template->append(
499      'statrows',
500      array(
501        'VALUE' => $value,
502        'PAGES' => $datas[$i],
503        'WIDTH' => ceil(($datas[$i] * $max_width) / $max_pages ),
504        )
505      );
506  }
507}
508
509// +-----------------------------------------------------------------------+
510// | Sending html code                                                     |
511// +-----------------------------------------------------------------------+
512
513$template->assign_var_from_handle('ADMIN_CONTENT', 'stats');
514?>
Note: See TracBrowser for help on using the repository browser.