source: trunk/admin/stats.php @ 2299

Last change on this file since 2299 was 2299, checked in by plg, 16 years ago

Bug fixed: as rvelices notified me by email, my header replacement script was
bugged (r2297 was repeating new and old header).

By the way, I've also removed the replacement keywords. We were using them
because it was a common usage with CVS but it is advised not to use them with
Subversion. Personnaly, it is a problem when I search differences between 2
Piwigo installations outside Subversion.

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 11.5 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based picture gallery                                  |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008      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 = mysql_fetch_array($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    year,
119    month,
120    day,
121    hour,
122    max(id) AS max_id,
123    COUNT(*) AS nb_pages
124  FROM '.HISTORY_TABLE.'
125  WHERE summarized = \'false\'
126  GROUP BY
127    year ASC,
128    month ASC,
129    day ASC,
130    hour ASC
131;';
132$result = pwg_query($query);
133
134$need_update = array();
135
136$max_id = 0;
137$is_first = true;
138$first_time_key = null;
139
140while ($row = mysql_fetch_array($result))
141{
142  $time_keys = array(
143    sprintf(
144      '%4u',
145      $row['year']
146      ),
147    sprintf(
148      '%4u.%02u',
149      $row['year'], $row['month']
150      ),
151    sprintf(
152      '%4u.%02u.%02u',
153      $row['year'], $row['month'], $row['day']
154      ),
155    sprintf(
156      '%4u.%02u.%02u.%02u',
157      $row['year'], $row['month'], $row['day'], $row['hour']
158      ),
159    );
160
161  foreach ($time_keys as $time_key)
162  {
163    if (!isset($need_update[$time_key]))
164    {
165      $need_update[$time_key] = 0;
166    }
167    $need_update[$time_key] += $row['nb_pages'];
168  }
169
170  if ($row['max_id'] > $max_id)
171  {
172    $max_id = $row['max_id'];
173  }
174
175  if ($is_first)
176  {
177    $is_first = false;
178    $first_time_key = $time_keys[3];
179  }
180}
181
182// Only the oldest time_key might be already summarized, so we have to
183// update the 4 corresponding lines instead of simply inserting them.
184//
185// For example, if the oldest unsummarized is 2005.08.25.21, the 4 lines
186// that can be updated are:
187//
188// +---------------+----------+
189// | id            | nb_pages |
190// +---------------+----------+
191// | 2005          |   241109 |
192// | 2005.08       |    20133 |
193// | 2005.08.25    |      620 |
194// | 2005.08.25.21 |      151 |
195// +---------------+----------+
196
197$existing_time_keys = array();
198
199if (isset($first_time_key))
200{
201  list($year, $month, $day, $hour) = explode('.', $first_time_key);
202
203  $time_keys = array(
204    sprintf('%4u',                $year),
205    sprintf('%4u.%02u',           $year, $month),
206    sprintf('%4u.%02u.%02u',      $year, $month, $day),
207    sprintf('%4u.%02u.%02u.%02u', $year, $month, $day, $hour),
208    );
209
210  $query = '
211SELECT
212    id,
213    nb_pages
214  FROM '.HISTORY_SUMMARY_TABLE.'
215  WHERE id IN (\''.implode("', '", $time_keys).'\')
216;';
217  $result = pwg_query($query);
218  while ($row = mysql_fetch_array($result))
219  {
220    $existing_time_keys[ $row['id'] ] = $row['nb_pages'];
221  }
222}
223
224$updates = array();
225$inserts = array();
226
227foreach (array_keys($need_update) as $time_key)
228{
229  $time_tokens = explode('.', $time_key);
230
231  if (isset($existing_time_keys[$time_key]))
232  {
233    array_push(
234      $updates,
235      array(
236        'id'       => $time_key,
237        'nb_pages' => $existing_time_keys[$time_key] + $need_update[$time_key],
238        )
239      );
240  }
241  else
242  {
243    array_push(
244      $inserts,
245      array(
246        'id'       => $time_key,
247        'year'     => $time_tokens[0],
248        'month'    => @$time_tokens[1],
249        'day'      => @$time_tokens[2],
250        'hour'     => @$time_tokens[3],
251        'nb_pages' => $need_update[$time_key],
252        )
253      );
254  }
255}
256
257if (count($updates) > 0)
258{
259  mass_updates(
260    HISTORY_SUMMARY_TABLE,
261    array(
262      'primary' => array('id'),
263      'update'  => array('nb_pages'),
264      ),
265    $updates
266    );
267}
268
269if (count($inserts) > 0)
270{
271  mass_inserts(
272    HISTORY_SUMMARY_TABLE,
273    array_keys($inserts[0]),
274    $inserts
275    );
276}
277
278if ($max_id != 0)
279{
280  $query = '
281UPDATE '.HISTORY_TABLE.'
282  SET summarized = \'true\'
283  WHERE summarized = \'false\'
284    AND id <= '.$max_id.'
285;';
286  pwg_query($query);
287}
288
289// +-----------------------------------------------------------------------+
290// | Page parameters check                                                 |
291// +-----------------------------------------------------------------------+
292
293foreach (array('day', 'month', 'year') as $key)
294{
295  if (isset($_GET[$key]))
296  {
297    $page[$key] = (int)$_GET[$key];
298  }
299}
300
301if (isset($page['day']))
302{
303  if (!isset($page['month']))
304  {
305    die('month is missing in URL');
306  }
307}
308
309if (isset($page['month']))
310{
311  if (!isset($page['year']))
312  {
313    die('year is missing in URL');
314  }
315}
316
317$summary_lines = get_summary(
318  @$page['year'],
319  @$page['month'],
320  @$page['day']
321  );
322
323// +-----------------------------------------------------------------------+
324// | Display statistics header                                             |
325// +-----------------------------------------------------------------------+
326
327// page title creation
328$title_parts = array();
329
330$url = PHPWG_ROOT_PATH.'admin.php?page=stats';
331
332array_push(
333  $title_parts,
334  '<a href="'.$url.'">'.l10n('Overall').'</a>'
335  );
336
337$period_label = l10n('Year');
338
339if (isset($page['year']))
340{
341  $url.= '&amp;year='.$page['year'];
342
343  array_push(
344    $title_parts,
345    '<a href="'.$url.'">'.$page['year'].'</a>'
346    );
347
348  $period_label = l10n('Month');
349}
350
351if (isset($page['month']))
352{
353  $url.= '&amp;month='.$page['month'];
354
355  array_push(
356    $title_parts,
357    '<a href="'.$url.'">'.$lang['month'][$page['month']].'</a>'
358    );
359
360  $period_label = l10n('Day');
361}
362
363if (isset($page['day']))
364{
365  $url.= '&amp;day='.$page['day'];
366
367  $time = mktime(12, 0, 0, $page['month'], $page['day'], $page['year']);
368
369  $day_title = sprintf(
370    '%u (%s)',
371    $page['day'],
372    $lang['day'][date('w', $time)]
373    );
374
375  array_push(
376    $title_parts,
377    '<a href="'.$url.'">'.$day_title.'</a>'
378    );
379
380  $period_label = l10n('Hour');
381}
382
383$template->set_filename('stats', 'admin/stats.tpl');
384
385// TabSheet initialization
386history_tabsheet();
387
388$base_url = get_root_url().'admin.php?page=history';
389
390$template->assign(
391  array(
392    'L_STAT_TITLE' => implode($conf['level_separator'], $title_parts),
393    'PERIOD_LABEL' => $period_label,
394    'U_HELP' => get_root_url().'popuphelp.php?page=history',
395    'F_ACTION' => $base_url,
396    )
397  );
398
399// +-----------------------------------------------------------------------+
400// | Display statistic rows                                                |
401// +-----------------------------------------------------------------------+
402
403$max_width = 400;
404
405$datas = array();
406
407if (isset($page['day']))
408{
409  $key = 'hour';
410  $min_x = 0;
411  $max_x = 23;
412}
413elseif (isset($page['month']))
414{
415  $key = 'day';
416  $min_x = 1;
417  $max_x = date(
418    't',
419    mktime(12, 0, 0, $page['month'], 1, $page['year'])
420    );
421}
422elseif (isset($page['year']))
423{
424  $key = 'month';
425  $min_x = 1;
426  $max_x = 12;
427}
428else
429{
430  $key = 'year';
431}
432
433$max_pages = 1;
434foreach ($summary_lines as $line)
435{
436  if ($line['nb_pages'] > $max_pages)
437  {
438    $max_pages = $line['nb_pages'];
439  }
440
441  $datas[ $line[$key] ] = $line['nb_pages'];
442}
443
444if (!isset($min_x) and !isset($max_x) and count($datas) > 0)
445{
446  $min_x = min(array_keys($datas));
447  $max_x = max(array_keys($datas));
448}
449
450if (count($datas) > 0)
451{
452  for ($i = $min_x; $i <= $max_x; $i++)
453  {
454    if (!isset($datas[$i]))
455    {
456      $datas[$i] = 0;
457    }
458
459    $url = null;
460
461    if (isset($page['day']))
462    {
463      $value = sprintf('%02u', $i);
464    }
465    else if (isset($page['month']))
466    {
467      $url =
468        get_root_url().'admin.php'
469        .'?page=stats'
470        .'&amp;year='.$page['year']
471        .'&amp;month='.$page['month']
472        .'&amp;day='.$i
473        ;
474
475      $time = mktime(12, 0, 0, $page['month'], $i, $page['year']);
476
477      $value = $i.' ('.$lang['day'][date('w', $time)].')';
478    }
479    else if (isset($page['year']))
480    {
481      $url =
482        get_root_url().'admin.php'
483        .'?page=stats'
484        .'&amp;year='.$page['year']
485        .'&amp;month='.$i
486        ;
487
488      $value = $lang['month'][$i];
489    }
490    else
491    {
492      // at least the year is defined
493      $url =
494        get_root_url().'admin.php'
495        .'?page=stats'
496        .'&amp;year='.$i
497        ;
498
499      $value = $i;
500    }
501
502    if ($datas[$i] != 0 and isset($url))
503    {
504      $value = '<a href="'.$url.'">'.$value.'</a>';
505    }
506
507    $template->append(
508      'statrows',
509      array(
510        'VALUE' => $value,
511        'PAGES' => $datas[$i],
512        'WIDTH' => ceil(($datas[$i] * $max_width) / $max_pages ),
513        )
514      );
515  }
516}
517
518// +-----------------------------------------------------------------------+
519// | Sending html code                                                     |
520// +-----------------------------------------------------------------------+
521
522$template->assign_var_from_handle('ADMIN_CONTENT', 'stats');
523?>
Note: See TracBrowser for help on using the repository browser.