source: trunk/admin/history.php @ 1727

Last change on this file since 1727 was 1727, checked in by plg, 17 years ago

Modification: new data model for history, more compact, more efficient. A
summary table is used as cache for history stats display.

New: a Perl script fill_history.pl was added to simulate a high load on
history table (making the efficiency of the new data model obvious).

Modification: function prepend_append_array_items moved from
include/functions_search.inc.php to include/functions_search.inc.php since
this function is used in new file admin/history.php

Modification: admin/images/*_stats.img.php replaced by a simpler and more
generic admin/images/stats.img.php unique file.

New: a history detail search page was added. Currently, only start and end
dates can be modified, it's just a beginning.

File size: 10.8 KB
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-2005 PhpWebGallery Team - http://phpwebgallery.net |
6// +-----------------------------------------------------------------------+
7// | branch        : BSF (Best So Far)
8// | file          : $RCSfile$
9// | last update   : $Date: 2006-11-29 05:18:11 +0100 (mer, 29 nov 2006) $
10// | last modifier : $Author: rvelices $
11// | revision      : $Revision: 1620 $
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/**
29 * Display filtered history lines
30 */
31
32// echo '<pre>$_POST:
33// '; print_r($_POST); echo '</pre>';
34// echo '<pre>$_GET:
35// '; print_r($_GET); echo '</pre>';
36
37// +-----------------------------------------------------------------------+
38// |                              functions                                |
39// +-----------------------------------------------------------------------+
40
41// +-----------------------------------------------------------------------+
42// |                           initialization                              |
43// +-----------------------------------------------------------------------+
44
45if (!defined('PHPWG_ROOT_PATH'))
46{
47  die('Hacking attempt!');
48}
49
50include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
51
52if (isset($_GET['start']) and is_numeric($_GET['start']))
53{
54  $page['start'] = $_GET['start'];
55}
56else
57{
58  $page['start'] = 0;
59}
60
61// +-----------------------------------------------------------------------+
62// | Check Access and exit when user status is not ok                      |
63// +-----------------------------------------------------------------------+
64
65check_status(ACCESS_ADMINISTRATOR);
66
67// +-----------------------------------------------------------------------+
68// | Build search criteria and redirect to results                         |
69// +-----------------------------------------------------------------------+
70
71$errors = array();
72$search = array();
73
74if (isset($_POST['submit']))
75{
76  // dates
77  if (!empty($_POST['start_year']))
78  {
79    $search['fields']['date-after'] = sprintf(
80      '%d-%02d-%02d',
81      $_POST['start_year'],
82      $_POST['start_month'],
83      $_POST['start_day']
84      );
85  }
86
87  if (!empty($_POST['end_year']))
88  {
89    $search['fields']['date-before'] = sprintf(
90      '%d-%02d-%02d',
91      $_POST['end_year'],
92      $_POST['end_month'],
93      $_POST['end_day']
94      );
95  }
96
97  // echo '<pre>'; print_r($search); echo '</pre>';
98 
99  if (!empty($search))
100  {
101    // register search rules in database, then they will be available on
102    // thumbnails page and picture page.
103    $query ='
104INSERT INTO '.SEARCH_TABLE.'
105  (rules)
106  VALUES
107  (\''.serialize($search).'\')
108;';
109    pwg_query($query);
110
111    $search_id = mysql_insert_id();
112   
113    redirect(
114      PHPWG_ROOT_PATH.'admin.php?page=history&search_id='.$search_id
115      );
116  }
117  else
118  {
119    array_push($errors, $lang['search_one_clause_at_least']);
120  }
121}
122
123// +-----------------------------------------------------------------------+
124// |                             template init                             |
125// +-----------------------------------------------------------------------+
126
127$template->set_filenames(array('history'=>'admin/history.tpl'));
128
129$base_url = PHPWG_ROOT_PATH.'admin.php?page=history';
130
131$template->assign_vars(
132  array(
133    'U_HELP' => PHPWG_ROOT_PATH.'popuphelp.php?page=history',
134
135    'F_ACTION' => PHPWG_ROOT_PATH.'admin.php?page=history'
136    )
137  );
138
139$template->assign_vars(
140  array(
141    'TODAY_DAY'   => date('d', time()),
142    'TODAY_MONTH' => date('m', time()),
143    'TODAY_YEAR'  => date('Y', time()),
144    )
145  );
146
147// +-----------------------------------------------------------------------+
148// |                             history lines                             |
149// +-----------------------------------------------------------------------+
150
151if (isset($_GET['search_id'])
152    and $page['search_id'] = (int)$_GET['search_id'])
153{
154  // what are the lines to display in reality ?
155  $query = '
156SELECT rules
157  FROM '.SEARCH_TABLE.'
158  WHERE id = '.$page['search_id'].'
159;';
160  list($serialized_rules) = mysql_fetch_row(pwg_query($query));
161
162  $page['search'] = unserialize($serialized_rules);
163
164  // echo '<pre>'; print_r($page['search']); echo '</pre>';
165 
166  $clauses = array();
167
168  if (isset($page['search']['fields']['date-after']))
169  {
170    array_push(
171      $clauses,
172      "date >= '".$page['search']['fields']['date-after']."'"
173      );
174  }
175
176  if (isset($page['search']['fields']['date-before']))
177  {
178    array_push(
179      $clauses,
180      "date <= '".$page['search']['fields']['date-before']."'"
181      );
182  }
183
184  $clauses = prepend_append_array_items($clauses, '(', ')');
185
186  $where_separator =
187    implode(
188      "\n    AND ",
189      $clauses
190      );
191 
192  $query = '
193SELECT COUNT(*)
194  FROM '.HISTORY_TABLE.'
195  WHERE '.$where_separator.'
196';
197
198  list($page['nb_lines']) = mysql_fetch_row(pwg_query($query));
199
200  $query = '
201SELECT date, time, user_id, IP, section, category_id, tag_ids, image_id
202  FROM '.HISTORY_TABLE.'
203  WHERE '.$where_separator.'
204  LIMIT '.$page['start'].', '.$conf['nb_logs_page'].'
205;';
206
207  $result = pwg_query($query);
208  $history_lines = array();
209  while ($row = mysql_fetch_array($result))
210  {
211    $user_ids[$row['user_id']] = 1;
212
213    if (isset($row['category_id']))
214    {
215      $category_ids[$row['category_id']] = 1;
216    }
217
218    if (isset($row['image_id']))
219    {
220      $image_ids[$row['image_id']] = 1;
221    }
222
223    array_push(
224      $history_lines,
225      $row
226      );
227  }
228
229  // prepare reference data (users, tags, categories...)
230  if (count($user_ids) > 0)
231  {
232    $query = '
233SELECT '.$conf['user_fields']['id'].' AS id
234     , '.$conf['user_fields']['username'].' AS username
235  FROM '.USERS_TABLE.'
236  WHERE id IN ('.implode(',', array_keys($user_ids)).')
237;';
238    $result = pwg_query($query);
239
240    $username_of = array();
241    while ($row = mysql_fetch_array($result))
242    {
243      $username_of[$row['id']] = $row['username'];
244    }
245  }
246
247  if (count($category_ids) > 0)
248  {
249    $query = '
250SELECT id, uppercats
251  FROM '.CATEGORIES_TABLE.'
252  WHERE id IN ('.implode(',', array_keys($category_ids)).')
253;';
254    $uppercats_of = simple_hash_from_query($query, 'id', 'uppercats');
255
256    $name_of_category = array();
257   
258    foreach ($uppercats_of as $category_id => $uppercats)
259    {
260      $name_of_category[$category_id] = get_cat_display_name_cache(
261        $uppercats
262        );
263    }
264  }
265
266  if (count($image_ids) > 0)
267  {
268    $query = '
269SELECT id, IF(name IS NULL, file, name) AS label
270  FROM '.IMAGES_TABLE.'
271  WHERE id IN ('.implode(',', array_keys($image_ids)).')
272;';
273    $label_of_image = simple_hash_from_query($query, 'id', 'label');
274  }
275 
276  $i = 0;
277
278  foreach ($history_lines as $line)
279  {
280    $template->assign_block_vars(
281      'detail',
282      array(
283        'DATE'      => $line['date'],
284        'TIME'      => $line['time'],
285        'USER'      => isset($username_of[$line['user_id']])
286          ? $username_of[$line['user_id']]
287          : $line['user_id']
288        ,
289        'IP'        => $line['IP'],
290        'IMAGE'     => isset($line['image_id'])
291          ? $label_of_image[$line['image_id']]
292          : $line['image_id'],
293        'SECTION'   => $line['section'],
294        'CATEGORY'  => isset($line['category_id'])
295          ? $name_of_category[$line['category_id']]
296          : '',
297        'TAG'       => $line['tag_ids'],
298        'T_CLASS'   => ($i++ % 2) ? 'row1' : 'row2',
299        )
300      );
301  }
302}
303
304// $groups_string = preg_replace(
305//     '/(\d+)/e',
306//     "\$groups['$1']",
307//     implode(
308//       ', ',
309//       $local_user['groups']
310//       )
311//     );
312
313// +-----------------------------------------------------------------------+
314// |                            navigation bar                             |
315// +-----------------------------------------------------------------------+
316
317if (isset($page['search_id']))
318{
319  $navbar = create_navigation_bar(
320    PHPWG_ROOT_PATH.'admin.php'.get_query_string_diff(array('start')),
321    $page['nb_lines'],
322    $page['start'],
323    $conf['nb_logs_page']
324    );
325
326  $template->assign_block_vars(
327    'navigation',
328    array(
329      'NAVBAR' => $navbar
330      )
331    );
332}
333
334// +-----------------------------------------------------------------------+
335// |                             filter form                               |
336// +-----------------------------------------------------------------------+
337
338$form = array();
339
340if (isset($page['search']))
341{
342  if (isset($page['search']['fields']['date-after']))
343  {
344    $tokens = explode('-', $page['search']['fields']['date-after']);
345   
346    $form['start_year']  = (int)$tokens[0];
347    $form['start_month'] = (int)$tokens[1];
348    $form['start_day']   = (int)$tokens[2];
349  }
350
351  if (isset($page['search']['fields']['date-before']))
352  {
353    $tokens = explode('-', $page['search']['fields']['date-before']);
354   
355     (int)$tokens[0];
356     (int)$tokens[1];
357     (int)$tokens[2];
358  }
359}
360else
361{
362  // by default, at page load, we want the selected date to be the current
363  // date
364  $form['start_year']  = $form['end_year']  = date('Y');
365  $form['start_month'] = $form['end_month'] = date('n');
366  $form['start_day']   = $form['end_day']   = date('j');
367}
368
369// start date
370get_day_list('start_day', @$form['start_day']);
371get_month_list('start_month', @$form['start_month']);
372// end date
373get_day_list('end_day', @$form['end_day']);
374get_month_list('end_month', @$form['end_month']);
375
376$template->assign_vars(
377  array(
378    'START_YEAR' => @$form['start_year'],
379    'END_YEAR'   => @$form['end_year'],
380    )
381  );
382 
383// +-----------------------------------------------------------------------+
384// |                           html code display                           |
385// +-----------------------------------------------------------------------+
386
387$template->assign_var_from_handle('ADMIN_CONTENT', 'history');
388?>
Note: See TracBrowser for help on using the repository browser.