source: trunk/admin/update.php @ 496

Last change on this file since 496 was 494, checked in by z0rglub, 20 years ago

very small refactoring

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