source: trunk/admin/site_update.php @ 2408

Last change on this file since 2408 was 2344, checked in by vdigital, 16 years ago

Logical merge from 1.7.1 Revison 2193
Bug with no ref (and maybe 663): Currently if the "Search in subcategories" checkbox was unselected, all existing categories and elements were duplicated.
Cause: Wrong comparison between fileserver directory list and already known files in tables.

Resolution: keeping only the selected directory from the file server directory list in such cases,
if that one is still allocated.

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 27.0 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based picture gallery                                  |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008      Piwigo Team                  http://piwigo.org |
6// | Copyright(C) 2003-2008 PhpWebGallery Team    http://phpwebgallery.net |
7// | Copyright(C) 2002-2003 Pierrick LE GALL   http://le-gall.net/pierrick |
8// +-----------------------------------------------------------------------+
9// | This program is free software; you can redistribute it and/or modify  |
10// | it under the terms of the GNU General Public License as published by  |
11// | the Free Software Foundation                                          |
12// |                                                                       |
13// | This program is distributed in the hope that it will be useful, but   |
14// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
15// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
16// | General Public License for more details.                              |
17// |                                                                       |
18// | You should have received a copy of the GNU General Public License     |
19// | along with this program; if not, write to the Free Software           |
20// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
21// | USA.                                                                  |
22// +-----------------------------------------------------------------------+
23
24if (!defined('PHPWG_ROOT_PATH'))
25{
26  die('Hacking attempt!');
27}
28
29include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
30
31// +-----------------------------------------------------------------------+
32// | Check Access and exit when user status is not ok                      |
33// +-----------------------------------------------------------------------+
34check_status(ACCESS_ADMINISTRATOR);
35
36if (!is_numeric($_GET['site']))
37{
38  die ('site param missing or invalid');
39}
40$site_id = $_GET['site'];
41
42$query='
43SELECT galleries_url
44  FROM '.SITES_TABLE.'
45  WHERE id = '.$site_id.'
46;';
47list($site_url) = mysql_fetch_row(pwg_query($query));
48if (!isset($site_url))
49{
50  die('site '.$site_id.' does not exist');
51}
52$site_is_remote = url_is_remote($site_url);
53
54list($dbnow) = mysql_fetch_row(pwg_query('SELECT NOW();'));
55define('CURRENT_DATE', $dbnow);
56
57$error_labels = array(
58  'PWG-UPDATE-1' => array(
59    l10n('update_wrong_dirname_short'),
60    l10n('update_wrong_dirname_info')
61    ),
62  'PWG-UPDATE-2' => array(
63    l10n('update_missing_tn_short'),
64    l10n('update_missing_tn_info').implode(',', $conf['picture_ext'])
65    ),
66  'PWG-ERROR-NO-FS' => array(
67    l10n('update_missing_file_or_dir'),
68    l10n('update_missing_file_or_dir_info')
69    ),
70  'PWG-ERROR-VERSION' => array(
71    l10n('update_err_pwg_version_differs'),
72    l10n('update_err_pwg_version_differs_info')
73    ),
74  'PWG-ERROR-NOLISTING' => array(
75    l10n('update_err_remote_listing_not_found'),
76    l10n('update_err_remote_listing_not_found_info')
77    )
78  );
79$errors = array();
80$infos = array();
81
82if ($site_is_remote)
83{
84  include_once(PHPWG_ROOT_PATH.'admin/site_reader_remote.php');
85  $local_listing = null;
86  if ( isset($_GET['local_listing'])
87      and $_GET['local_listing'] )
88  {
89    $local_listing = PHPWG_ROOT_PATH.'listing.xml';
90  }
91  $site_reader = new RemoteSiteReader($site_url, $local_listing);
92}
93else
94{
95  include_once( PHPWG_ROOT_PATH.'admin/site_reader_local.php');
96  $site_reader = new LocalSiteReader($site_url);
97}
98
99$general_failure = true;
100if (isset($_POST['submit']))
101{
102  if (!isset($conf['flip_picture_ext']))
103  {
104    $conf['flip_picture_ext'] = array_flip($conf['picture_ext']);
105  }
106  if ($site_reader->open())
107  {
108    $general_failure = false;
109  }
110
111  // shall we simulate only
112  if ((isset($_POST['simulate']) and $_POST['simulate'] == 1) or is_adviser())
113  {
114    $simulate = true;
115  }
116  else
117  {
118    $simulate = false;
119  }
120}
121
122// +-----------------------------------------------------------------------+
123// |                      directories / categories                         |
124// +-----------------------------------------------------------------------+
125if (isset($_POST['submit'])
126    and ($_POST['sync'] == 'dirs' or $_POST['sync'] == 'files'))
127{
128  $counts['new_categories'] = 0;
129  $counts['del_categories'] = 0;
130  $counts['del_elements'] = 0;
131  $counts['new_elements'] = 0;
132  $counts['upd_elements'] = 0;
133}
134
135
136if (isset($_POST['submit'])
137    and ($_POST['sync'] == 'dirs' or $_POST['sync'] == 'files')
138    and !$general_failure)
139{
140  $start = get_moment();
141  // which categories to update ?
142  $cat_ids = array();
143
144  $query = '
145SELECT id, uppercats, global_rank, status, visible
146  FROM '.CATEGORIES_TABLE.'
147  WHERE dir IS NOT NULL
148    AND site_id = '.$site_id;
149  if (isset($_POST['cat']) and is_numeric($_POST['cat']))
150  {
151    if (isset($_POST['subcats-included']) and $_POST['subcats-included'] == 1)
152    {
153      $query.= '
154    AND uppercats REGEXP \'(^|,)'.$_POST['cat'].'(,|$)\'
155';
156    }
157    else
158    {
159      $query.= '
160    AND id = '.$_POST['cat'].'
161';
162    }
163  }
164  $query.= '
165;';
166  $result = pwg_query($query);
167
168  $db_categories = array();
169  while ($row = mysql_fetch_array($result))
170  {
171    $db_categories[$row['id']] = $row;
172  }
173
174  // get categort full directories in an array for comparison with file
175  // system directory tree
176  $db_fulldirs = get_fulldirs(array_keys($db_categories));
177
178  // what is the base directory to search file system sub-directories ?
179  if (isset($_POST['cat']) and is_numeric($_POST['cat']))
180  {
181    $basedir = $db_fulldirs[$_POST['cat']];
182  }
183  else
184  {
185    $basedir = preg_replace('#/*$#', '', $site_url);
186  }
187
188  // we need to have fulldirs as keys to make efficient comparison
189  $db_fulldirs = array_flip($db_fulldirs);
190
191  // finding next rank for each id_uppercat. By default, each category id
192  // has 1 for next rank on its sub-categories to create
193  $next_rank['NULL'] = 1;
194
195  $query = '
196SELECT id
197  FROM '.CATEGORIES_TABLE.'
198;';
199  $result = pwg_query($query);
200  while ($row = mysql_fetch_array($result))
201  {
202    $next_rank[$row['id']] = 1;
203  }
204
205  // let's see if some categories already have some sub-categories...
206  $query = '
207SELECT id_uppercat, MAX(rank)+1 AS next_rank
208  FROM '.CATEGORIES_TABLE.'
209  GROUP BY id_uppercat
210;';
211  $result = pwg_query($query);
212  while ($row = mysql_fetch_array($result))
213  {
214    // for the id_uppercat NULL, we write 'NULL' and not the empty string
215    if (!isset($row['id_uppercat']) or $row['id_uppercat'] == '')
216    {
217      $row['id_uppercat'] = 'NULL';
218    }
219    $next_rank[$row['id_uppercat']] = $row['next_rank'];
220  }
221
222  // next category id available
223  $query = '
224SELECT IF(MAX(id)+1 IS NULL, 1, MAX(id)+1) AS next_id
225  FROM '.CATEGORIES_TABLE.'
226;';
227  list($next_id) = mysql_fetch_array(pwg_query($query));
228
229  // retrieve sub-directories fulldirs from the site reader
230  $fs_fulldirs = $site_reader->get_full_directories($basedir);
231
232  // get_full_directories doesn't include the base directory, so if it's a
233  // category directory, we need to include it in our array
234  if (isset($_POST['cat']))
235  {
236    array_push($fs_fulldirs, $basedir);
237  }
238  // If $_POST['subcats-included'] != 1 ("Search in subcategories" is unchecked)
239  // $db_fulldirs doesn't include any subdirectories and $fs_fulldirs does
240  // So $fs_fulldirs will be limited to the selected basedir
241  // (if that one is in $fs_fulldirs)
242  if (!isset($_POST['subcats-included']) or $_POST['subcats-included'] != 1)
243  {
244    $fs_fulldirs = array_intersect($fs_fulldirs, array_keys($db_fulldirs));
245  }
246  $inserts = array();
247  // new categories are the directories not present yet in the database
248  foreach (array_diff($fs_fulldirs, array_keys($db_fulldirs)) as $fulldir)
249  {
250    $dir = basename($fulldir);
251    if (preg_match('/^[a-zA-Z0-9-_.]+$/', $dir))
252    {
253      $insert = array(
254        'id'          => $next_id++,
255        'dir'         => $dir,
256        'name'        => str_replace('_', ' ', $dir),
257        'site_id'     => $site_id,
258        'commentable' =>
259          boolean_to_string($conf['newcat_default_commentable']),
260        'uploadable'  => $site_is_remote
261          ? 'false'
262          : boolean_to_string($conf['newcat_default_uploadable']),
263        'status'      => $conf{'newcat_default_status'},
264        'visible'     => boolean_to_string($conf{'newcat_default_visible'}),
265        );
266
267      if (isset($db_fulldirs[dirname($fulldir)]))
268      {
269        $parent = $db_fulldirs[dirname($fulldir)];
270
271        $insert{'id_uppercat'} = $parent;
272        $insert{'uppercats'} =
273          $db_categories[$parent]['uppercats'].','.$insert{'id'};
274        $insert{'rank'} = $next_rank[$parent]++;
275        $insert{'global_rank'} =
276          $db_categories[$parent]['global_rank'].'.'.$insert{'rank'};
277        if ('private' == $db_categories[$parent]['status'])
278        {
279          $insert{'status'} = 'private';
280        }
281        if ('false' == $db_categories[$parent]['visible'])
282        {
283          $insert{'visible'} = 'false';
284        }
285      }
286      else
287      {
288        $insert{'uppercats'} = $insert{'id'};
289        $insert{'rank'} = $next_rank['NULL']++;
290        $insert{'global_rank'} = $insert{'rank'};
291      }
292
293      array_push($inserts, $insert);
294      array_push(
295        $infos,
296        array(
297          'path' => $fulldir,
298          'info' => l10n('update_research_added')
299          )
300        );
301
302      // add the new category to $db_categories and $db_fulldirs array
303      $db_categories[$insert{'id'}] =
304        array(
305          'id' => $insert{'id'},
306          'status' => $insert{'status'},
307          'visible' => $insert{'visible'},
308          'uppercats' => $insert{'uppercats'},
309          'global_rank' => $insert{'global_rank'}
310          );
311      $db_fulldirs[$fulldir] = $insert{'id'};
312      $next_rank[$insert{'id'}] = 1;
313    }
314    else
315    {
316      array_push(
317        $errors,
318        array(
319          'path' => $fulldir,
320          'type' => 'PWG-UPDATE-1'
321          )
322        );
323    }
324  }
325
326  if (count($inserts) > 0)
327  {
328    if (!$simulate)
329    {
330      $dbfields = array(
331        'id','dir','name','site_id','id_uppercat','uppercats','commentable',
332        'uploadable','visible','status','rank','global_rank'
333        );
334      mass_inserts(CATEGORIES_TABLE, $dbfields, $inserts);
335    }
336
337    $counts['new_categories'] = count($inserts);
338  }
339
340  // to delete categories
341  $to_delete = array();
342  foreach (array_diff(array_keys($db_fulldirs), $fs_fulldirs) as $fulldir)
343  {
344    array_push($to_delete, $db_fulldirs[$fulldir]);
345    unset($db_fulldirs[$fulldir]);
346    array_push($infos, array('path' => $fulldir,
347                             'info' => l10n('update_research_deleted')));
348  }
349  if (count($to_delete) > 0)
350  {
351    if (!$simulate)
352    {
353      delete_categories($to_delete);
354    }
355    $counts['del_categories'] = count($to_delete);
356  }
357
358  $template->append('footer_elements', '<!-- scanning dirs : '
359    . get_elapsed_time($start, get_moment())
360    . ' -->' );
361}
362// +-----------------------------------------------------------------------+
363// |                           files / elements                            |
364// +-----------------------------------------------------------------------+
365if (isset($_POST['submit']) and $_POST['sync'] == 'files'
366      and !$general_failure)
367{
368  $start_files = get_moment();
369  $start= $start_files;
370
371  $fs = $site_reader->get_elements($basedir);
372  $template->append('footer_elements', '<!-- get_elements: '
373    . get_elapsed_time($start, get_moment())
374    . ' -->' );
375
376  $cat_ids = array_diff(array_keys($db_categories), $to_delete);
377
378  $db_elements = array();
379  $db_unvalidated = array();
380
381  if (count($cat_ids) > 0)
382  {
383    $query = '
384SELECT id, path
385  FROM '.IMAGES_TABLE.'
386  WHERE storage_category_id IN ('
387      .wordwrap(
388        implode(', ', $cat_ids),
389        80,
390        "\n"
391        ).')
392;';
393    $result = pwg_query($query);
394    while ($row = mysql_fetch_array($result))
395    {
396      $db_elements[$row['id']] = $row['path'];
397    }
398
399    // searching the unvalidated waiting elements (they must not be taken into
400    // account)
401    $query = '
402SELECT file,storage_category_id
403  FROM '.WAITING_TABLE.'
404  WHERE storage_category_id IN (
405'.wordwrap(implode(', ', $cat_ids), 80, "\n").')
406    AND validated = \'false\'
407;';
408    $result = pwg_query($query);
409    while ($row = mysql_fetch_array($result))
410    {
411      array_push(
412        $db_unvalidated,
413        array_search(
414          $row['storage_category_id'],
415          $db_fulldirs)
416        .'/'.$row['file']
417        );
418    }
419  }
420
421  // next element id available
422  $query = '
423SELECT IF(MAX(id)+1 IS NULL, 1, MAX(id)+1) AS next_element_id
424  FROM '.IMAGES_TABLE.'
425;';
426  list($next_element_id) = mysql_fetch_array(pwg_query($query));
427
428  $start = get_moment();
429
430  $inserts = array();
431  $insert_links = array();
432
433  foreach (array_diff(array_keys($fs), $db_elements, $db_unvalidated) as $path)
434  {
435    $insert = array();
436    // storage category must exist
437    $dirname = dirname($path);
438    if (!isset($db_fulldirs[$dirname]))
439    {
440      continue;
441    }
442    $filename = basename($path);
443    if (!preg_match('/^[a-zA-Z0-9-_.]+$/', $filename))
444    {
445      array_push(
446        $errors,
447        array(
448          'path' => $path,
449          'type' => 'PWG-UPDATE-1'
450          )
451        );
452
453      continue;
454    }
455
456    if ( isset( $conf['flip_picture_ext'][get_extension($filename)] )
457          and !isset($fs[$path]['tn_ext']) )
458    { // For a picture thumbnail is mandatory and for non picture element,
459      // thumbnail and representative are optionnal
460      array_push(
461        $errors,
462        array(
463          'path' => $path,
464          'type' => 'PWG-UPDATE-2'
465          )
466        );
467    }
468    else
469    {
470      $insert = array(
471        'id'             => $next_element_id++,
472        'file'           => $filename,
473        'date_available' => CURRENT_DATE,
474        'path'           => $path,
475        'tn_ext'         => isset($fs[$path]['tn_ext'])
476          ? $fs[$path]['tn_ext']
477          : null,
478        'storage_category_id' => $db_fulldirs[$dirname],
479        );
480
481      if ( $_POST['privacy_level']!=0 )
482      {
483        $insert['level'] = $_POST['privacy_level'];
484      }
485
486      array_push(
487        $inserts,
488        $insert
489        );
490
491      array_push(
492        $insert_links,
493        array(
494          'image_id'    => $insert{'id'},
495          'category_id' => $insert['storage_category_id'],
496          )
497        );
498
499      array_push(
500        $infos,
501        array(
502          'path' => $insert{'path'},
503          'info' => l10n('update_research_added')
504          )
505        );
506
507      $caddiables[] = $insert['id'];
508    }
509  }
510
511  if (count($inserts) > 0)
512  {
513    if (!$simulate)
514    {
515      // inserts all new elements
516      mass_inserts(
517        IMAGES_TABLE,
518        array_keys($inserts[0]),
519        $inserts
520        );
521
522      // inserts all links between new elements and their storage category
523      mass_inserts(
524        IMAGE_CATEGORY_TABLE,
525        array_keys($insert_links[0]),
526        $insert_links
527        );
528
529      // add new elements to caddie
530      if (isset($_POST['add_to_caddie']) and $_POST['add_to_caddie'] == 1)
531      {
532        fill_caddie($caddiables);
533      }
534    }
535    $counts['new_elements'] = count($inserts);
536  }
537
538  // delete elements that are in database but not in the filesystem
539  $to_delete_elements = array();
540  foreach (array_diff($db_elements, array_keys($fs)) as $path)
541  {
542    array_push($to_delete_elements, array_search($path, $db_elements));
543    array_push($infos, array('path' => $path,
544                             'info' => l10n('update_research_deleted')));
545  }
546  if (count($to_delete_elements) > 0)
547  {
548    if (!$simulate)
549    {
550      delete_elements($to_delete_elements);
551    }
552    $counts['del_elements'] = count($to_delete_elements);
553  }
554
555  $template->append('footer_elements', '<!-- scanning files : '
556    . get_elapsed_time($start_files, get_moment())
557    . ' -->' );
558
559  // retrieving informations given by uploaders
560  if (!$simulate and count($cat_ids) > 0)
561  {
562    $query = '
563SELECT id,file,storage_category_id,infos
564  FROM '.WAITING_TABLE.'
565  WHERE storage_category_id IN (
566'.wordwrap(implode(', ', $cat_ids), 80, "\n").')
567    AND validated = \'true\'
568;';
569    $result = pwg_query($query);
570
571    $datas = array();
572    $fields =
573      array(
574        'primary' => array('id'),
575        'update'  => array('date_creation', 'author', 'name', 'comment')
576        );
577
578    $waiting_to_delete = array();
579
580    while ($row = mysql_fetch_array($result))
581    {
582      $data = array();
583
584      $query = '
585SELECT id
586  FROM '.IMAGES_TABLE.'
587  WHERE storage_category_id = '.$row['storage_category_id'].'
588    AND file = \''.$row['file'].'\'
589;';
590      list($data['id']) = mysql_fetch_array(pwg_query($query));
591
592      foreach ($fields['update'] as $field)
593      {
594        $data[$field] = addslashes( getAttribute($row['infos'], $field) );
595      }
596
597      array_push($datas, $data);
598      array_push($waiting_to_delete, $row['id']);
599    }
600
601    if (count($datas) > 0)
602    {
603      mass_updates(IMAGES_TABLE, $fields, $datas);
604
605      // delete now useless waiting elements
606      $query = '
607DELETE
608  FROM '.WAITING_TABLE.'
609  WHERE id IN ('.implode(',', $waiting_to_delete).')
610;';
611      pwg_query($query);
612    }
613  }
614}
615
616// +-----------------------------------------------------------------------+
617// |                          synchronize files                            |
618// +-----------------------------------------------------------------------+
619if (isset($_POST['submit'])
620    and ($_POST['sync'] == 'dirs' or $_POST['sync'] == 'files')
621    and !$general_failure )
622{
623  if (!$simulate)
624  {
625    $start = get_moment();
626    update_category('all');
627    $template->append('footer_elements', '<!-- update_category(all) : '
628      . get_elapsed_time($start,get_moment())
629      . ' -->' );
630    $start = get_moment();
631    update_global_rank();
632    $template->append('footer_elements', '<!-- ordering categories : '
633      . get_elapsed_time($start, get_moment())
634      . ' -->');
635  }
636
637  if ($_POST['sync'] == 'files')
638  {
639    $start = get_moment();
640    $opts['category_id'] = '';
641    $opts['recursive'] = true;
642    if (isset($_POST['cat']))
643    {
644      $opts['category_id'] = $_POST['cat'];
645      if (!isset($_POST['subcats-included']) or $_POST['subcats-included'] != 1)
646      {
647        $opts['recursive'] = false;
648      }
649    }
650    $files = get_filelist($opts['category_id'], $site_id,
651                          $opts['recursive'],
652                          false);
653    $template->append('footer_elements', '<!-- get_filelist : '
654      . get_elapsed_time($start, get_moment())
655      . ' -->');
656    $start = get_moment();
657
658    $datas = array();
659    foreach ( $files as $id=>$file )
660    {
661      $data = $site_reader->get_element_update_attributes($file);
662      if ( !is_array($data) )
663      {
664        continue;
665      }
666      $extension = get_extension($file);
667      if ( isset($conf['flip_picture_ext'][$extension]) )
668      {
669        if ( !isset($data['tn_ext']) )
670        {
671          array_push(
672            $errors,
673            array(
674              'path' => $file,
675              'type' => 'PWG-UPDATE-2'
676              )
677            );
678          continue;
679        }
680      }
681
682      $data['id']=$id;
683      array_push($datas, $data);
684    } // end foreach file
685
686    $counts['upd_elements'] = count($datas);
687    if (!$simulate and count($datas)>0 )
688    {
689      mass_updates(
690        IMAGES_TABLE,
691        // fields
692        array(
693          'primary' => array('id'),
694          'update'  => $site_reader->get_update_attributes(),
695          ),
696        $datas
697        );
698    }
699    $template->append('footer_elements', '<!-- update files : '
700      . get_elapsed_time($start,get_moment())
701      . ' -->');
702  }// end if sync files
703}
704
705// +-----------------------------------------------------------------------+
706// |                          synchronize files                            |
707// +-----------------------------------------------------------------------+
708if (isset($_POST['submit'])
709    and ($_POST['sync'] == 'dirs' or $_POST['sync'] == 'files'))
710{
711  $template->assign(
712    'update_result',
713    array(
714      'NB_NEW_CATEGORIES'=>$counts['new_categories'],
715      'NB_DEL_CATEGORIES'=>$counts['del_categories'],
716      'NB_NEW_ELEMENTS'=>$counts['new_elements'],
717      'NB_DEL_ELEMENTS'=>$counts['del_elements'],
718      'NB_UPD_ELEMENTS'=>$counts['upd_elements'],
719      'NB_ERRORS'=>count($errors),
720      ));
721}
722
723// +-----------------------------------------------------------------------+
724// |                          synchronize metadata                         |
725// +-----------------------------------------------------------------------+
726if (isset($_POST['submit']) and preg_match('/^metadata/', $_POST['sync'])
727         and !$general_failure)
728{
729  // sync only never synchronized files ?
730  if ($_POST['sync'] == 'metadata_new')
731  {
732    $opts['only_new'] = true;
733  }
734  else
735  {
736    $opts['only_new'] = false;
737  }
738  $opts['category_id'] = '';
739  $opts['recursive'] = true;
740
741  if (isset($_POST['cat']))
742  {
743    $opts['category_id'] = $_POST['cat'];
744    // recursive ?
745    if (!isset($_POST['subcats-included']) or $_POST['subcats-included'] != 1)
746    {
747      $opts['recursive'] = false;
748    }
749  }
750  $start = get_moment();
751  $files = get_filelist($opts['category_id'], $site_id,
752                        $opts['recursive'],
753                        $opts['only_new']);
754
755  $template->append('footer_elements', '<!-- get_filelist : '
756    . get_elapsed_time($start, get_moment())
757    . ' -->');
758
759  $start = get_moment();
760  $datas = array();
761  $tags_of = array();
762
763  $has_high_images = array();
764
765  $image_ids = array();
766  foreach ($files as $id => $file)
767  {
768    array_push($image_ids, $id);
769  }
770
771  if (count($image_ids) > 0)
772  {
773    $query = '
774SELECT id
775  FROM '.IMAGES_TABLE.'
776  WHERE has_high = \'true\'
777    AND id IN (
778'.wordwrap(implode(', ', $image_ids), 80, "\n").'
779)
780;';
781
782    $result = pwg_query($query);
783    while ($row = mysql_fetch_array($result))
784    {
785      array_push($has_high_images, $row['id']);
786    }
787  }
788
789  foreach ( $files as $id=>$file )
790  {
791    $data = $site_reader->get_element_metadata(
792      $file,
793      in_array($id, $has_high_images)
794      );
795
796    if ( is_array($data) )
797    {
798      $data['date_metadata_update'] = CURRENT_DATE;
799      $data['id']=$id;
800      array_push($datas, $data);
801
802      foreach (array('keywords', 'tags') as $key)
803      {
804        if (isset($data[$key]))
805        {
806          if (!isset($tags_of[$id]))
807          {
808            $tags_of[$id] = array();
809          }
810
811          foreach (explode(',', $data[$key]) as $tag_name)
812          {
813            array_push(
814              $tags_of[$id],
815              tag_id_from_tag_name($tag_name)
816              );
817          }
818        }
819      }
820    }
821    else
822    {
823      array_push($errors, array('path' => $file, 'type' => 'PWG-ERROR-NO-FS'));
824    }
825  }
826
827  if (!$simulate)
828  {
829    if (count($datas) > 0)
830    {
831      mass_updates(
832        IMAGES_TABLE,
833        // fields
834        array(
835          'primary' => array('id'),
836          'update'  => array_unique(
837            array_merge(
838              array_diff(
839                $site_reader->get_metadata_attributes(),
840                // keywords and tags fields are managed separately
841                array('keywords', 'tags')
842                ),
843              array('date_metadata_update'))
844            )
845          ),
846        $datas
847        );
848    }
849    set_tags_of($tags_of);
850  }
851
852  $template->append('footer_elements', '<!-- metadata update : '
853    . get_elapsed_time($start, get_moment())
854    . ' -->');
855
856  $template->assign(
857    'metadata_result',
858    array(
859      'NB_ELEMENTS_DONE' => count($datas),
860      'NB_ELEMENTS_CANDIDATES' => count($files),
861      'NB_ERRORS' => count($errors),
862      ));
863}
864
865// +-----------------------------------------------------------------------+
866// |                        template initialization                        |
867// +-----------------------------------------------------------------------+
868$template->set_filenames(array('update'=>'admin/site_update.tpl'));
869$result_title = '';
870if (isset($simulate) and $simulate)
871{
872  $result_title.= l10n('update_simulation_title').' ';
873}
874
875// used_metadata string is displayed to inform admin which metadata will be
876// used from files for synchronization
877$used_metadata = implode( ', ', $site_reader->get_metadata_attributes());
878if ($site_is_remote and !isset($_POST['submit']) )
879{
880  $used_metadata.= ' + ...';
881}
882
883$template->assign(
884  array(
885    'SITE_URL'=>$site_url,
886    'U_SITE_MANAGER'=> get_root_url().'admin.php?page=site_manager',
887    'L_RESULT_UPDATE'=>$result_title.l10n('update_part_research'),
888    'L_RESULT_METADATA'=>$result_title.l10n('update_result_metadata'),
889    'METADATA_LIST' => $used_metadata,
890    'U_HELP' => get_root_url().'popuphelp.php?page=synchronize',
891    ));
892
893// +-----------------------------------------------------------------------+
894// |                        introduction : choices                         |
895// +-----------------------------------------------------------------------+
896if (!isset($_POST['submit']) or (isset($simulate) and $simulate))
897{
898  if (isset($simulate) and $simulate)
899  {
900    $tpl_introduction = array(
901        'sync'  => $_POST['sync'],
902        'display_info' => isset($_POST['display_info']) and $_POST['display_info']==1,
903        'add_to_caddie' => isset($_POST['add_to_caddie']) and $_POST['add_to_caddie']==1,
904        'subcats_included' => isset($_POST['subcats-included']) and $_POST['subcats-included']==1,
905        'privacy_level_selected' => (int)@$_POST['privacy_level'],
906      );
907
908    if (isset($_POST['cat']) and is_numeric($_POST['cat']))
909    {
910      $cat_selected = array($_POST['cat']);
911    }
912    else
913    {
914      $cat_selected = array();
915    }
916  }
917  else
918  {
919    $tpl_introduction = array(
920        'sync'  => 'dirs',
921        'display_info' => false,
922        'add_to_caddie' => false,
923        'subcats_included' => true,
924        'privacy_level_selected' => 0,
925      );
926
927    $cat_selected = array();
928  }
929
930  $tpl_introduction['privacy_level_options']=array();
931  foreach ($conf['available_permission_levels'] as $level)
932  {
933    $tpl_introduction['privacy_level_options'][$level] = l10n( sprintf('Level %d', $level) );
934  }
935
936  $template->assign('introduction', $tpl_introduction);
937
938  $query = '
939SELECT id,name,uppercats,global_rank
940  FROM '.CATEGORIES_TABLE.'
941  WHERE site_id = '.$site_id.'
942;';
943  display_select_cat_wrapper($query,
944                             $cat_selected,
945                             'category_options',
946                             false);
947}
948
949if (count($errors) > 0)
950{
951  foreach ($errors as $error)
952  {
953    $template->append(
954      'sync_errors',
955      array(
956        'ELEMENT' => $error['path'],
957        'LABEL' => $error['type'].' ('.$error_labels[$error['type']][0].')'
958        ));
959  }
960
961  foreach ($error_labels as $error_type=>$error_description)
962  {
963    $template->append(
964      'sync_error_captions',
965      array(
966        'TYPE' => $error_type,
967        'LABEL' => $error_description[1]
968        ));
969  }
970}
971
972if (count($infos) > 0
973    and isset($_POST['display_info'])
974    and $_POST['display_info'] == 1)
975{
976  foreach ($infos as $info)
977  {
978    $template->append(
979      'sync_infos',
980      array(
981        'ELEMENT' => $info['path'],
982        'LABEL' => $info['info']
983        ));
984  }
985}
986
987// +-----------------------------------------------------------------------+
988// |                          sending html code                            |
989// +-----------------------------------------------------------------------+
990$template->assign_var_from_handle('ADMIN_CONTENT', 'update');
991?>
Note: See TracBrowser for help on using the repository browser.