root/trunk/admin/stats.php @ 2108

Revision 2108, 11.4 KB (checked in by rvelices, 6 years ago)

- remove unused css for IE7
- fix admin view plugin (due to a recent change elsewhere)
- remove an echo from admin/stats.php

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | PhpWebGallery - a PHP based picture gallery                           |
4// | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net |
5// | Copyright (C) 2003-2007 PhpWebGallery Team - http://phpwebgallery.net |
6// +-----------------------------------------------------------------------+
7// | file          : $Id$
8// | last update   : $Date$
9// | last modifier : $Author$
10// | revision      : $Revision$
11// +-----------------------------------------------------------------------+
12// | This program is free software; you can redistribute it and/or modify  |
13// | it under the terms of the GNU General Public License as published by  |
14// | the Free Software Foundation                                          |
15// |                                                                       |
16// | This program is distributed in the hope that it will be useful, but   |
17// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
18// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
19// | General Public License for more details.                              |
20// |                                                                       |
21// | You should have received a copy of the GNU General Public License     |
22// | along with this program; if not, write to the Free Software           |
23// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
24// | USA.                                                                  |
25// +-----------------------------------------------------------------------+
26
27if (!defined("PHPWG_ROOT_PATH"))
28{
29  die ("Hacking attempt!");
30}
31
32include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
33include_once(PHPWG_ROOT_PATH.'admin/include/functions_history.inc.php');
34
35// +-----------------------------------------------------------------------+
36// | Functions                                                             |
37// +-----------------------------------------------------------------------+
38
39function get_summary($year = null, $month = null, $day = null)
40{
41  $query = '
42SELECT
43    year,
44    month,
45    day,
46    hour,
47    nb_pages
48  FROM '.HISTORY_SUMMARY_TABLE;
49
50  if (isset($day))
51  {
52    $query.= '
53  WHERE year = '.$year.'
54    AND month = '.$month.'
55    AND day = '.$day.'
56    AND hour IS NOT NULL
57  ORDER BY
58    year ASC,
59    month ASC,
60    day ASC,
61    hour ASC
62;';
63  }
64  elseif (isset($month))
65  {
66    $query.= '
67  WHERE year = '.$year.'
68    AND month = '.$month.'
69    AND day IS NOT NULL
70    AND hour IS NULL
71  ORDER BY
72    year ASC,
73    month ASC,
74    day ASC
75;';
76  }
77  elseif (isset($year))
78  {
79    $query.= '
80  WHERE year = '.$year.'
81    AND month IS NOT NULL
82    AND day IS NULL
83  ORDER BY
84    year ASC,
85    month ASC
86;';
87  }
88  else
89  {
90    $query.= '
91  WHERE year IS NOT NULL
92    AND month IS NULL
93  ORDER BY
94    year ASC
95;';
96  }
97
98  $result = pwg_query($query);
99
100  $output = array();
101  while ($row = mysql_fetch_array($result))
102  {
103    array_push($output, $row);
104  }
105
106  return $output;
107}
108
109// +-----------------------------------------------------------------------+
110// | Check Access and exit when user status is not ok                      |
111// +-----------------------------------------------------------------------+
112
113check_status(ACCESS_ADMINISTRATOR);
114
115// +-----------------------------------------------------------------------+
116// | Refresh summary from details                                          |
117// +-----------------------------------------------------------------------+
118
119$query = '
120SELECT
121    year,
122    month,
123    day,
124    hour,
125    max(id) AS max_id,
126    COUNT(*) AS nb_pages
127  FROM '.HISTORY_TABLE.'
128  WHERE summarized = \'false\'
129  GROUP BY
130    year ASC,
131    month ASC,
132    day ASC,
133    hour ASC
134;';
135$result = pwg_query($query);
136
137$need_update = array();
138
139$max_id = 0;
140$is_first = true;
141$first_time_key = null;
142
143while ($row = mysql_fetch_array($result))
144{
145  $time_keys = array(
146    sprintf(
147      '%4u',
148      $row['year']
149      ),
150    sprintf(
151      '%4u.%02u',
152      $row['year'], $row['month']
153      ),
154    sprintf(
155      '%4u.%02u.%02u',
156      $row['year'], $row['month'], $row['day']
157      ),
158    sprintf(
159      '%4u.%02u.%02u.%02u',
160      $row['year'], $row['month'], $row['day'], $row['hour']
161      ),
162    );
163
164  foreach ($time_keys as $time_key)
165  {
166    if (!isset($need_update[$time_key]))
167    {
168      $need_update[$time_key] = 0;
169    }
170    $need_update[$time_key] += $row['nb_pages'];
171  }
172
173  if ($row['max_id'] > $max_id)
174  {
175    $max_id = $row['max_id'];
176  }
177
178  if ($is_first)
179  {
180    $is_first = false;
181    $first_time_key = $time_keys[3];
182  }
183}
184
185// Only the oldest time_key might be already summarized, so we have to
186// update the 4 corresponding lines instead of simply inserting them.
187//
188// For example, if the oldest unsummarized is 2005.08.25.21, the 4 lines
189// that can be updated are:
190//
191// +---------------+----------+
192// | id            | nb_pages |
193// +---------------+----------+
194// | 2005          |   241109 |
195// | 2005.08       |    20133 |
196// | 2005.08.25    |      620 |
197// | 2005.08.25.21 |      151 |
198// +---------------+----------+
199
200$existing_time_keys = array();
201
202if (isset($first_time_key))
203{
204  list($year, $month, $day, $hour) = explode('.', $first_time_key);
205
206  $time_keys = array(
207    sprintf('%4u',                $year),
208    sprintf('%4u.%02u',           $year, $month),
209    sprintf('%4u.%02u.%02u',      $year, $month, $day),
210    sprintf('%4u.%02u.%02u.%02u', $year, $month, $day, $hour),
211    );
212
213  $query = '
214SELECT
215    id,
216    nb_pages
217  FROM '.HISTORY_SUMMARY_TABLE.'
218  WHERE id IN (\''.implode("', '", $time_keys).'\')
219;';
220  $result = pwg_query($query);
221  while ($row = mysql_fetch_array($result))
222  {
223    $existing_time_keys[ $row['id'] ] = $row['nb_pages'];
224  }
225}
226
227$updates = array();
228$inserts = array();
229
230foreach (array_keys($need_update) as $time_key)
231{
232  $time_tokens = explode('.', $time_key);
233
234  if (isset($existing_time_keys[$time_key]))
235  {
236    array_push(
237      $updates,
238      array(
239        'id'       => $time_key,
240        'nb_pages' => $existing_time_keys[$time_key] + $need_update[$time_key],
241        )
242      );
243  }
244  else
245  {
246    array_push(
247      $inserts,
248      array(
249        'id'       => $time_key,
250        'year'     => $time_tokens[0],
251        'month'    => @$time_tokens[1],
252        'day'      => @$time_tokens[2],
253        'hour'     => @$time_tokens[3],
254        'nb_pages' => $need_update[$time_key],
255        )
256      );
257  }
258}
259
260if (count($updates) > 0)
261{
262  mass_updates(
263    HISTORY_SUMMARY_TABLE,
264    array(
265      'primary' => array('id'),
266      'update'  => array('nb_pages'),
267      ),
268    $updates
269    );
270}
271
272if (count($inserts) > 0)
273{
274  mass_inserts(
275    HISTORY_SUMMARY_TABLE,
276    array_keys($inserts[0]),
277    $inserts
278    );
279}
280
281if ($max_id != 0)
282{
283  $query = '
284UPDATE '.HISTORY_TABLE.'
285  SET summarized = \'true\'
286  WHERE summarized = \'false\'
287    AND id <= '.$max_id.'
288;';
289  pwg_query($query);
290}
291
292// +-----------------------------------------------------------------------+
293// | Page parameters check                                                 |
294// +-----------------------------------------------------------------------+
295
296foreach (array('day', 'month', 'year') as $key)
297{
298  if (isset($_GET[$key]))
299  {
300    $page[$key] = (int)$_GET[$key];
301  }
302}
303
304if (isset($page['day']))
305{
306  if (!isset($page['month']))
307  {
308    die('month is missing in URL');
309  }
310}
311
312if (isset($page['month']))
313{
314  if (!isset($page['year']))
315  {
316    die('year is missing in URL');
317  }
318}
319
320$summary_lines = get_summary(
321  @$page['year'],
322  @$page['month'],
323  @$page['day']
324  );
325
326// +-----------------------------------------------------------------------+
327// | Display statistics header                                             |
328// +-----------------------------------------------------------------------+
329
330// page title creation
331$title_parts = array();
332
333$url = PHPWG_ROOT_PATH.'admin.php?page=stats';
334
335array_push(
336  $title_parts,
337  '<a href="'.$url.'">'.l10n('Overall').'</a>'
338  );
339
340$period_label = l10n('Year');
341
342if (isset($page['year']))
343{
344  $url.= '&amp;year='.$page['year'];
345
346  array_push(
347    $title_parts,
348    '<a href="'.$url.'">'.$page['year'].'</a>'
349    );
350
351  $period_label = l10n('Month');
352}
353
354if (isset($page['month']))
355{
356  $url.= '&amp;month='.$page['month'];
357
358  array_push(
359    $title_parts,
360    '<a href="'.$url.'">'.$lang['month'][$page['month']].'</a>'
361    );
362
363  $period_label = l10n('Day');
364}
365
366if (isset($page['day']))
367{
368  $url.= '&amp;day='.$page['day'];
369
370  $time = mktime(12, 0, 0, $page['month'], $page['day'], $page['year']);
371
372  $day_title = sprintf(
373    '%u (%s)',
374    $page['day'],
375    $lang['day'][date('w', $time)]
376    );
377
378  array_push(
379    $title_parts,
380    '<a href="'.$url.'">'.$day_title.'</a>'
381    );
382
383  $period_label = l10n('Hour');
384}
385
386$template->set_filename('stats', 'admin/stats.tpl');
387
388// TabSheet initialization
389history_tabsheet();
390
391$template->assign_vars(
392  array(
393    'L_STAT_TITLE' => implode($conf['level_separator'], $title_parts),
394    'PERIOD_LABEL' => $period_label,
395    )
396  );
397
398// +-----------------------------------------------------------------------+
399// | Display statistic rows                                                |
400// +-----------------------------------------------------------------------+
401
402$max_width = 400;
403
404$datas = array();
405
406if (isset($page['day']))
407{
408  $key = 'hour';
409  $min_x = 0;
410  $max_x = 23;
411}
412elseif (isset($page['month']))
413{
414  $key = 'day';
415  $min_x = 1;
416  $max_x = date(
417    't',
418    mktime(12, 0, 0, $page['month'], 1, $page['year'])
419    );
420}
421elseif (isset($page['year']))
422{
423  $key = 'month';
424  $min_x = 1;
425  $max_x = 12;
426}
427else
428{
429  $key = 'year';
430}
431
432$max_pages = 1;
433foreach ($summary_lines as $line)
434{
435  if ($line['nb_pages'] > $max_pages)
436  {
437    $max_pages = $line['nb_pages'];
438  }
439
440  $datas[ $line[$key] ] = $line['nb_pages'];
441}
442
443if (!isset($min_x) and !isset($max_x) and count($datas) > 0)
444{
445  $min_x = min(array_keys($datas));
446  $max_x = max(array_keys($datas));
447}
448
449if (count($datas) > 0)
450{
451  for ($i = $min_x; $i <= $max_x; $i++)
452  {
453    if (!isset($datas[$i]))
454    {
455      $datas[$i] = 0;
456    }
457
458    $url = null;
459
460    if (isset($page['day']))
461    {
462      $value = sprintf('%02u', $i);
463    }
464    else if (isset($page['month']))
465    {
466      $url =
467        PHPWG_ROOT_PATH.'admin.php'
468        .'?page=stats'
469        .'&amp;year='.$page['year']
470        .'&amp;month='.$page['month']
471        .'&amp;day='.$i
472        ;
473
474      $time = mktime(12, 0, 0, $page['month'], $i, $page['year']);
475
476      $value = $i.' ('.$lang['day'][date('w', $time)].')';
477    }
478    else if (isset($page['year']))
479    {
480      $url =
481        PHPWG_ROOT_PATH.'admin.php'
482        .'?page=stats'
483        .'&amp;year='.$page['year']
484        .'&amp;month='.$i
485        ;
486
487      $value = $lang['month'][$i];
488    }
489    else
490    {
491      // at least the year is defined
492      $url =
493        PHPWG_ROOT_PATH.'admin.php'
494        .'?page=stats'
495        .'&amp;year='.$i
496        ;
497
498      $value = $i;
499    }
500
501    if ($datas[$i] != 0 and isset($url))
502    {
503      $value = '<a href="'.$url.'">'.$value.'</a>';
504    }
505
506    $template->assign_block_vars(
507      'statrow',
508      array(
509        'VALUE' => $value,
510        'PAGES' => $datas[$i],
511        'WIDTH' => ceil(($datas[$i] * $max_width) / $max_pages ),
512        )
513      );
514  }
515}
516
517// +-----------------------------------------------------------------------+
518// | Sending html code                                                     |
519// +-----------------------------------------------------------------------+
520
521$template->assign_var_from_handle('ADMIN_CONTENT', 'stats');
522?>
Note: See TracBrowser for help on using the browser.