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