source: trunk/admin/update.php @ 642

Last change on this file since 642 was 642, checked in by plg, 19 years ago
  • in admin menu, status option for categories is not "permissions" but "private or public" choice = different language item
  • get_cat_display_name changed : use $conflevel_separator to unify presentation
  • default values for category properties commentable, uploadable, status and visible (set in include/config.inc.php) used for category creation (admin/update, admin/remote_site, admin/cat_list)
  • use mass_inserts in admin/update for inserting new categories
  • only one query for counting the number of sub categories in admin/cat_list
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 19.9 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-2004 PhpWebGallery Team - http://phpwebgallery.net |
6// +-----------------------------------------------------------------------+
7// | branch        : BSF (Best So Far)
8// | file          : $RCSfile$
9// | last update   : $Date: 2004-12-12 21:06:39 +0000 (Sun, 12 Dec 2004) $
10// | last modifier : $Author: plg $
11// | revision      : $Revision: 642 $
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
28if( !defined("PHPWG_ROOT_PATH") )
29{
30  die ("Hacking attempt!");
31}
32include_once( PHPWG_ROOT_PATH.'admin/include/isadmin.inc.php');
33
34define('CURRENT_DATE', date('Y-m-d'));
35// +-----------------------------------------------------------------------+
36// |                              functions                                |
37// +-----------------------------------------------------------------------+
38
39function insert_local_category($id_uppercat)
40{
41  global $conf, $page, $user, $lang, $counts;
42 
43  $uppercats = '';
44  $output = '';
45
46  // 0. retrieving informations on the category to display
47  $cat_directory = PHPWG_ROOT_PATH.'galleries';
48  if (is_numeric($id_uppercat))
49  {
50    $query = '
51SELECT id,name,uppercats,dir,visible,status
52  FROM '.CATEGORIES_TABLE.'
53  WHERE id = '.$id_uppercat.'
54;';
55    $row = mysql_fetch_array(pwg_query($query));
56    $parent = array('id' => $row['id'],
57                    'name' => $row['name'],
58                    'dir' => $row['dir'],
59                    'uppercats' => $row['uppercats'],
60                    'visible' => $row['visible'],
61                    'status' => $row['status']);
62
63    $upper_array = explode( ',', $parent['uppercats']);
64
65    $local_dir = '';
66
67    $database_dirs = array();
68    $query = '
69SELECT id,dir
70  FROM '.CATEGORIES_TABLE.'
71  WHERE id IN ('.$parent['uppercats'].')
72;';
73    $result = pwg_query($query);
74    while ($row = mysql_fetch_array($result))
75    {
76      $database_dirs[$row['id']] = $row['dir'];
77    }
78    foreach ($upper_array as $id)
79    {
80      $local_dir.= $database_dirs[$id].'/';
81    }
82
83    $cat_directory.= '/'.$local_dir;
84
85    // 1. display the category name to update
86    $output = '<ul class="menu">';
87    $output.= '<li><strong>'.$parent['name'].'</strong>';
88    $output.= ' [ '.$parent['dir'].' ]';
89    $output.= '</li>';
90
91    // 2. we search pictures of the category only if the update is for all
92    //    or a cat_id is specified
93    if ($_POST['sync'] == 'files')
94    {
95      $output.= insert_local_element($cat_directory, $id_uppercat);
96    }
97  }
98
99  $fs_subdirs = get_category_directories($cat_directory);
100
101  $sub_category_dirs = array();
102  $query = '
103SELECT id,dir
104  FROM '.CATEGORIES_TABLE.'
105  WHERE site_id = 1
106';
107  if (!is_numeric($id_uppercat))
108  {
109    $query.= ' AND id_uppercat IS NULL';
110  }
111  else
112  {
113    $query.= ' AND id_uppercat = '.$id_uppercat;
114  }
115  $query.= '
116    AND dir IS NOT NULL'; // virtual categories not taken
117  $query.= '
118;';
119  $result = pwg_query($query);
120  while ($row = mysql_fetch_array($result))
121  {
122    $sub_category_dirs[$row['id']] = $row['dir'];
123  }
124 
125  // 3. we have to remove the categories of the database not present anymore
126  $to_delete_categories = array();
127  foreach ($sub_category_dirs as $id => $dir)
128  {
129    if (!in_array($dir, $fs_subdirs))
130    {
131      array_push($to_delete_categories,$id);
132    }
133  }
134  if (count($to_delete_categories) > 0)
135  {
136    delete_categories($to_delete_categories);
137  }
138
139  // array of new categories to insert
140  $inserts = array();
141
142  // calculate default value at category creation
143  $create_values = array();
144  if (isset($parent))
145  {
146    // at creation, must a category be visible or not ? Warning : if
147    // the parent category is invisible, the category is automatically
148    // create invisible. (invisible = locked)
149    if ('false' == $parent['visible'])
150    {
151      $create_values{'visible'} = 'false';
152    }
153    else
154    {
155      $create_values{'visible'} = $conf['newcat_default_visible'];
156    }
157    // at creation, must a category be public or private ? Warning :
158    // if the parent category is private, the category is
159    // automatically create private.
160    if ('private' == $parent['status'])
161    {
162      $create_values{'status'} = 'private';
163    }
164    else
165    {
166      $create_values{'status'} = $conf['newcat_default_status'];
167    }
168  }
169  else
170  {
171    $create_values{'visible'} = $conf['newcat_default_visible'];
172    $create_values{'status'} = $conf['newcat_default_status'];
173  }
174 
175  foreach ($fs_subdirs as $fs_subdir)
176  {
177    // 5. Is the category already existing ? we create a subcat if not
178    //    existing
179    $category_id = array_search($fs_subdir, $sub_category_dirs);
180    if (!is_numeric($category_id))
181    {
182      $insert = array();
183     
184      if (preg_match('/^[a-zA-Z0-9-_.]+$/', $fs_subdir))
185      {
186        $name = str_replace('_', ' ', $fs_subdir);
187
188        $insert{'dir'} = $fs_subdir;
189        $insert{'name'} = $name;
190        $insert{'site_id'} = 1;
191        $insert{'uppercats'} = 'undef';
192        $insert{'commentable'} = $conf['newcat_default_commentable'];
193        $insert{'uploadable'} = $conf['newcat_default_uploadable'];
194        $insert{'status'} = $create_values{'status'};
195        $insert{'visible'} = $create_values{'visible'};
196
197        if (isset($parent))
198        {
199          $insert{'id_uppercat'} = $parent['id'];
200        }
201       
202        array_push($inserts, $insert);
203      }
204      else
205      {
206        $output.= '<span class="update_category_error">"'.$fs_subdir.'" : ';
207        $output.= $lang['update_wrong_dirname'].'</span><br />';
208      }
209    }
210  }
211
212  // we have to create the category
213  if (count($inserts) > 0)
214  {
215    $dbfields = array(
216      'dir','name','site_id','id_uppercat','uppercats','commentable',
217      'uploadable','visible','status'
218      );
219    mass_inserts(CATEGORIES_TABLE, $dbfields, $inserts);
220   
221    $counts['new_categories']+= count($inserts);
222    // updating uppercats field
223    $query = '
224UPDATE '.CATEGORIES_TABLE;
225    if (isset($parent))
226    {
227      $query.= "
228  SET uppercats = CONCAT('".$parent['uppercats']."',',',id)
229  WHERE id_uppercat = ".$parent['id'];
230    }
231    else
232    {
233      $query.= '
234  SET uppercats = id
235  WHERE id_uppercat IS NULL';
236    }
237    $query.= '
238;';
239    pwg_query($query);
240  }
241
242  // Recursive call on the sub-categories (not virtual ones)
243  if (!isset($_POST['cat'])
244      or (isset($_POST['subcats-included'])
245          and $_POST['subcats-included'] == 1))
246  {
247    $query = '
248SELECT id
249  FROM '.CATEGORIES_TABLE.'
250  WHERE site_id = 1
251';
252    if (!is_numeric($id_uppercat))
253    {
254      $query.= '    AND id_uppercat IS NULL';
255    }
256    else
257    {
258      $query.= '    AND id_uppercat = '.$id_uppercat;
259    }
260    $query.= '
261    AND dir IS NOT NULL'; // virtual categories not taken
262    $query.= '
263;';
264    $result = pwg_query($query);
265    while ($row = mysql_fetch_array($result))
266    {
267      $output.= insert_local_category($row['id']);
268    }
269  }
270
271  if (is_numeric($id_uppercat))
272  {
273    $output.= '</ul>';
274  }
275  return $output;
276}
277
278function insert_local_element($dir, $category_id)
279{
280  global $lang,$conf,$counts;
281
282  $output = '';
283
284  // fs means FileSystem : $fs_files contains files in the filesystem found
285  // in $dir that can be managed by PhpWebGallery (see get_pwg_files
286  // function), $fs_thumbnails contains thumbnails, $fs_representatives
287  // contains potentially representative pictures for non picture files
288  $fs_files = get_pwg_files($dir);
289  $fs_thumbnails = get_thumb_files($dir);
290  $fs_representatives = get_representative_files($dir);
291
292  // element deletion
293  $to_delete_elements = array();
294  // deletion of element if the correspond file doesn't exist anymore
295  $query = '
296SELECT id,file
297  FROM '.IMAGES_TABLE.'
298  WHERE storage_category_id = '.$category_id.'
299;';
300  $result = pwg_query($query);
301  while ($row = mysql_fetch_array($result))
302  {
303    if (!in_array($row['file'], $fs_files))
304    {
305      $output.= $row['file'];
306      $output.= ' <span style="font-weight:bold;">';
307      $output.= $lang['update_disappeared'].'</span><br />';
308      array_push($to_delete_elements, $row['id']);
309    }
310  }
311  // in picture case, we also delete the element if the thumbnail doesn't
312  // existe anymore
313  $query = '
314SELECT id,file,tn_ext
315  FROM '.IMAGES_TABLE.'
316  WHERE storage_category_id = '.$category_id.'
317    AND ('.implode(' OR ',
318                   array_map(
319                     create_function('$s', 'return "file LIKE \'%".$s."\'";')
320                     , $conf['picture_ext'])).')
321;';
322  $result = pwg_query($query);
323  while ($row = mysql_fetch_array($result))
324  {
325    $thumbnail = $conf['prefix_thumbnail'];
326    $thumbnail.= get_filename_wo_extension($row['file']);
327    $thumbnail.= '.'.$row['tn_ext'];
328    if (!in_array($thumbnail, $fs_thumbnails))
329    {
330      $output.= $row['file'];
331      $output.= ' : <span style="font-weight:bold;">';
332      $output.= $lang['update_disappeared_tn'].'</span><br />';
333      array_push($to_delete_elements, $row['id']);
334    }
335  }
336
337  $to_delete_elements = array_unique($to_delete_elements);
338  if (count($to_delete_elements) > 0)
339  {
340    delete_elements($to_delete_elements);
341  }
342 
343  $registered_elements = array();
344  $query = '
345SELECT file FROM '.IMAGES_TABLE.'
346   WHERE storage_category_id = '.$category_id.'
347;';
348  $result = pwg_query($query);
349  while ($row = mysql_fetch_array($result))
350  {
351    array_push($registered_elements, $row['file']);
352  }
353
354  // unvalidated pictures are picture uploaded by users, but not validated
355  // by an admin (so not registered truly visible yet)
356  $unvalidated_pictures  = array();
357 
358  $query = '
359SELECT file
360  FROM '.WAITING_TABLE.'
361  WHERE storage_category_id = '.$category_id.'
362    AND validated = \'false\'
363;';
364  $result = pwg_query($query);
365  while ($row = mysql_fetch_array($result))
366  {
367    array_push($unvalidated_pictures, $row['file']);
368  }
369
370  // we only search among the picture present in the filesystem and not
371  // present in the database yet. If we know that this picture is known as
372  // an uploaded one but not validated, it's not tested neither
373  $unregistered_elements = array_diff($fs_files
374                                      ,$registered_elements
375                                      ,$unvalidated_pictures);
376
377  $inserts = array();
378 
379  foreach ($unregistered_elements as $unregistered_element)
380  {
381    if (preg_match('/^[a-zA-Z0-9-_.]+$/', $unregistered_element))
382    {
383      $file_wo_ext = get_filename_wo_extension($unregistered_element);
384      $tn_ext = '';
385      foreach ($conf['picture_ext'] as $ext)
386      {
387        $test = $conf['prefix_thumbnail'].$file_wo_ext.'.'.$ext;
388        if (!in_array($test, $fs_thumbnails))
389        {
390          continue;
391        }
392        else
393        {
394          $tn_ext = $ext;
395          break;
396        }
397      }
398
399      // 2 cases : the element is a picture or not. Indeed, for a picture
400      // thumbnail is mandatory and for non picture element, thumbnail and
401      // representative is optionnal
402      if (in_array(get_extension($unregistered_element), $conf['picture_ext']))
403      {
404        // if we found a thumnbnail corresponding to our picture...
405        if ($tn_ext != '')
406        {
407          $insert = array();
408          $insert['file'] = $unregistered_element;
409          $insert['storage_category_id'] = $category_id;
410          $insert['date_available'] = CURRENT_DATE;
411          $insert['tn_ext'] = $tn_ext;
412          $insert['path'] = $dir.$unregistered_element;
413
414          $counts['new_elements']++;
415          array_push($inserts, $insert);
416        }
417        else
418        {
419          $output.= '<span class="update_error_element">';
420          $output.= $lang['update_missing_tn'].' : '.$unregistered_element;
421          $output.= ' (<span style="font-weight:bold;">';
422          $output.= $conf['prefix_thumbnail'];
423          $output.= get_filename_wo_extension($unregistered_element);
424          $output.= '.XXX</span>';
425          $output.= ', XXX = ';
426          $output.= implode(', ', $conf['picture_ext']);
427          $output.= ')</span><br />';
428        }
429      }
430      else
431      {
432        $representative_ext = '';
433        foreach ($conf['picture_ext'] as $ext)
434        {
435          $candidate = $file_wo_ext.'.'.$ext;
436          if (!in_array($candidate, $fs_representatives))
437          {
438            continue;
439          }
440          else
441          {
442            $representative_ext = $ext;
443            break;
444          }
445        }
446
447        $insert = array();
448        $insert['file'] = $unregistered_element;
449        $insert['path'] = $dir.$unregistered_element;
450        $insert['storage_category_id'] = $category_id;
451        $insert['date_available'] = CURRENT_DATE;
452        if ( $tn_ext != '' )
453        {
454          $insert['tn_ext'] = $tn_ext;
455        }
456        if ( $representative_ext != '' )
457        {
458          $insert['representative_ext'] = $representative_ext;
459        }
460
461        $counts['new_elements']++;
462        array_push($inserts, $insert);
463      }
464    }
465    else
466    {
467      $output.= '<span class="update_error_element">"';
468      $output.= $unregistered_element.'" : ';
469      $output.= $lang['update_wrong_dirname'].'</span><br />';
470    }
471  }
472 
473  if (count($inserts) > 0)
474  {
475    // inserts all found pictures
476    $dbfields = array(
477      'file','storage_category_id','date_available','tn_ext'
478      ,'representative_ext','path'
479      );
480    mass_inserts(IMAGES_TABLE, $dbfields, $inserts);
481
482    // what are the ids of the pictures in the $category_id ?
483    $ids = array();
484
485    $query = '
486SELECT id
487  FROM '.IMAGES_TABLE.'
488  WHERE storage_category_id = '.$category_id.'
489;';
490    $result = pwg_query($query);
491    while ($row = mysql_fetch_array($result))
492    {
493      array_push($ids, $row['id']);
494    }
495
496    // recreation of the links between this storage category pictures and
497    // its storage category
498    $query = '
499DELETE FROM '.IMAGE_CATEGORY_TABLE.'
500  WHERE category_id = '.$category_id.'
501    AND image_id IN ('.implode(',', $ids).')
502;';
503    pwg_query($query);
504
505    foreach ($ids as $num => $image_id)
506    {
507      $ids[$num] =  '('.$category_id.','.$image_id.')';
508    }
509    $query = '
510INSERT INTO '.IMAGE_CATEGORY_TABLE.'
511  (category_id,image_id) VALUES
512  '.implode(',', $ids).'
513;';
514    pwg_query($query);
515
516    set_random_representant(array($category_id));
517  }
518  return $output;
519}
520// +-----------------------------------------------------------------------+
521// |                        template initialization                        |
522// +-----------------------------------------------------------------------+
523$template->set_filenames(array('update'=>'admin/update.tpl'));
524
525$base_url = PHPWG_ROOT_PATH.'admin.php?page=update';
526
527$template->assign_vars(
528  array(
529    'L_SUBMIT'=>$lang['submit'],
530    'L_UPDATE_TITLE'=>$lang['update_default_title'],
531    'L_UPDATE_SYNC_FILES'=>$lang['update_sync_files'],
532    'L_UPDATE_SYNC_DIRS'=>$lang['update_sync_dirs'],
533    'L_UPDATE_SYNC_ALL'=>$lang['update_sync_all'],
534    'L_UPDATE_SYNC_METADATA'=>$lang['update_sync_metadata'],
535    'L_UPDATE_SYNC_METADATA_NEW'=>$lang['update_sync_metadata_new'],
536    'L_UPDATE_SYNC_METADATA_ALL'=>$lang['update_sync_metadata_all'],
537    'L_UPDATE_CATS_SUBSET'=>$lang['update_cats_subset'],
538    'L_RESULT_UPDATE'=>$lang['update_part_research'],
539    'L_NB_NEW_ELEMENTS'=>$lang['update_nb_new_elements'],
540    'L_NB_NEW_CATEGORIES'=>$lang['update_nb_new_categories'],
541    'L_NB_DEL_ELEMENTS'=>$lang['update_nb_del_elements'],
542    'L_NB_DEL_CATEGORIES'=>$lang['update_nb_del_categories'],
543    'L_SEARCH_SUBCATS_INCLUDED'=>$lang['search_subcats_included'],
544   
545    'U_SYNC_DIRS'=>add_session_id($base_url.'&amp;update=dirs'),
546    'U_SYNC_ALL'=>add_session_id($base_url.'&amp;update=all'),
547    'U_SYNC_METADATA_NEW'=>add_session_id($base_url.'&amp;metadata=all:new'),
548    'U_SYNC_METADATA_ALL'=>add_session_id($base_url.'&amp;metadata=all')
549    ));
550// +-----------------------------------------------------------------------+
551// |                        introduction : choices                         |
552// +-----------------------------------------------------------------------+
553if (!isset($_POST['submit']))
554{
555  $template->assign_block_vars('introduction', array());
556
557  $query = '
558SELECT id,name,uppercats,global_rank
559  FROM '.CATEGORIES_TABLE.'
560  WHERE site_id = 1
561;';
562  display_select_cat_wrapper($query,
563                             array(),
564                             'introduction.category_option',
565                             false);
566}
567// +-----------------------------------------------------------------------+
568// |                          synchronize files                            |
569// +-----------------------------------------------------------------------+
570else if (isset($_POST['submit'])
571         and ($_POST['sync'] == 'dirs' or $_POST['sync'] == 'files'))
572{
573  $counts = array(
574    'new_elements' => 0,
575    'new_categories' => 0,
576    'del_elements' => 0,
577    'del_categories' => 0
578    );
579
580  if (isset($_POST['cat']))
581  {
582    $opts['category_id'] = $_POST['cat'];
583  }
584  else
585  {
586    $opts['category_id'] = 'NULL';
587  }
588 
589  $start = get_moment();
590  $categories = insert_local_category($opts['category_id']);
591  echo get_elapsed_time($start,get_moment()).' for scanning directories<br />';
592 
593  $template->assign_block_vars(
594    'update',
595    array(
596      'CATEGORIES'=>$categories,
597      'NB_NEW_CATEGORIES'=>$counts['new_categories'],
598      'NB_DEL_CATEGORIES'=>$counts['del_categories'],
599      'NB_NEW_ELEMENTS'=>$counts['new_elements'],
600      'NB_DEL_ELEMENTS'=>$counts['del_elements']
601      ));
602  $start = get_moment();
603  update_category('all');
604  echo get_elapsed_time($start,get_moment()).' for update_category(all)<br />';
605  $start = get_moment();
606  ordering();
607  update_global_rank();
608  echo get_elapsed_time($start, get_moment()).' for ordering categories<br />';
609}
610// +-----------------------------------------------------------------------+
611// |                          synchronize metadata                         |
612// +-----------------------------------------------------------------------+
613else if (isset($_POST['submit']) and preg_match('/^metadata/', $_POST['sync']))
614{
615  // sync only never synchronized files ?
616  if ($_POST['sync'] == 'metadata_new')
617  {
618    $opts['only_new'] = true;
619  }
620  else
621  {
622    $opts['only_new'] = false;
623  }
624  $opts['category_id'] = '';
625  $opts['recursive'] = true;
626 
627  if (isset($_POST['cat']))
628  {
629    $opts['category_id'] = $_POST['cat'];
630    // recursive ?
631    if (!isset($_POST['subcats-included']) or $_POST['subcats-included'] != 1)
632    {
633      $opts['recursive'] = false;
634    }
635  }
636  $start = get_moment();
637  $files = get_filelist($opts['category_id'],
638                        $opts['recursive'],
639                        $opts['only_new']);
640  echo get_elapsed_time($start, get_moment()).' for get_filelist<br />';
641 
642  $start = get_moment();
643  update_metadata($files);
644  echo get_elapsed_time($start, get_moment()).' for metadata update<br />';
645}
646// +-----------------------------------------------------------------------+
647// |                          sending html code                            |
648// +-----------------------------------------------------------------------+
649$template->assign_var_from_handle('ADMIN_CONTENT', 'update');
650?>
Note: See TracBrowser for help on using the repository browser.