source: trunk/admin/update.php @ 638

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