source: trunk/admin/site_update.php @ 2306

Last change on this file since 2306 was 2306, checked in by rvelices, 16 years ago
  • merged function ordering() with update_global_rank() and also optimized the queries
  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 26.6 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
239  $inserts = array();
240  // new categories are the directories not present yet in the database
241  foreach (array_diff($fs_fulldirs, array_keys($db_fulldirs)) as $fulldir)
242  {
243    $dir = basename($fulldir);
244    if (preg_match('/^[a-zA-Z0-9-_.]+$/', $dir))
245    {
246      $insert = array(
247        'id'          => $next_id++,
248        'dir'         => $dir,
249        'name'        => str_replace('_', ' ', $dir),
250        'site_id'     => $site_id,
251        'commentable' =>
252          boolean_to_string($conf['newcat_default_commentable']),
253        'uploadable'  => $site_is_remote
254          ? 'false'
255          : boolean_to_string($conf['newcat_default_uploadable']),
256        'status'      => $conf{'newcat_default_status'},
257        'visible'     => boolean_to_string($conf{'newcat_default_visible'}),
258        );
259
260      if (isset($db_fulldirs[dirname($fulldir)]))
261      {
262        $parent = $db_fulldirs[dirname($fulldir)];
263
264        $insert{'id_uppercat'} = $parent;
265        $insert{'uppercats'} =
266          $db_categories[$parent]['uppercats'].','.$insert{'id'};
267        $insert{'rank'} = $next_rank[$parent]++;
268        $insert{'global_rank'} =
269          $db_categories[$parent]['global_rank'].'.'.$insert{'rank'};
270        if ('private' == $db_categories[$parent]['status'])
271        {
272          $insert{'status'} = 'private';
273        }
274        if ('false' == $db_categories[$parent]['visible'])
275        {
276          $insert{'visible'} = 'false';
277        }
278      }
279      else
280      {
281        $insert{'uppercats'} = $insert{'id'};
282        $insert{'rank'} = $next_rank['NULL']++;
283        $insert{'global_rank'} = $insert{'rank'};
284      }
285
286      array_push($inserts, $insert);
287      array_push(
288        $infos,
289        array(
290          'path' => $fulldir,
291          'info' => l10n('update_research_added')
292          )
293        );
294
295      // add the new category to $db_categories and $db_fulldirs array
296      $db_categories[$insert{'id'}] =
297        array(
298          'id' => $insert{'id'},
299          'status' => $insert{'status'},
300          'visible' => $insert{'visible'},
301          'uppercats' => $insert{'uppercats'},
302          'global_rank' => $insert{'global_rank'}
303          );
304      $db_fulldirs[$fulldir] = $insert{'id'};
305      $next_rank[$insert{'id'}] = 1;
306    }
307    else
308    {
309      array_push(
310        $errors,
311        array(
312          'path' => $fulldir,
313          'type' => 'PWG-UPDATE-1'
314          )
315        );
316    }
317  }
318
319  if (count($inserts) > 0)
320  {
321    if (!$simulate)
322    {
323      $dbfields = array(
324        'id','dir','name','site_id','id_uppercat','uppercats','commentable',
325        'uploadable','visible','status','rank','global_rank'
326        );
327      mass_inserts(CATEGORIES_TABLE, $dbfields, $inserts);
328    }
329
330    $counts['new_categories'] = count($inserts);
331  }
332
333  // to delete categories
334  $to_delete = array();
335  foreach (array_diff(array_keys($db_fulldirs), $fs_fulldirs) as $fulldir)
336  {
337    array_push($to_delete, $db_fulldirs[$fulldir]);
338    unset($db_fulldirs[$fulldir]);
339    array_push($infos, array('path' => $fulldir,
340                             'info' => l10n('update_research_deleted')));
341  }
342  if (count($to_delete) > 0)
343  {
344    if (!$simulate)
345    {
346      delete_categories($to_delete);
347    }
348    $counts['del_categories'] = count($to_delete);
349  }
350
351  $template->append('footer_elements', '<!-- scanning dirs : '
352    . get_elapsed_time($start, get_moment())
353    . ' -->' );
354}
355// +-----------------------------------------------------------------------+
356// |                           files / elements                            |
357// +-----------------------------------------------------------------------+
358if (isset($_POST['submit']) and $_POST['sync'] == 'files'
359      and !$general_failure)
360{
361  $start_files = get_moment();
362  $start= $start_files;
363
364  $fs = $site_reader->get_elements($basedir);
365  $template->append('footer_elements', '<!-- get_elements: '
366    . get_elapsed_time($start, get_moment())
367    . ' -->' );
368
369  $cat_ids = array_diff(array_keys($db_categories), $to_delete);
370
371  $db_elements = array();
372  $db_unvalidated = array();
373
374  if (count($cat_ids) > 0)
375  {
376    $query = '
377SELECT id, path
378  FROM '.IMAGES_TABLE.'
379  WHERE storage_category_id IN ('
380      .wordwrap(
381        implode(', ', $cat_ids),
382        80,
383        "\n"
384        ).')
385;';
386    $result = pwg_query($query);
387    while ($row = mysql_fetch_array($result))
388    {
389      $db_elements[$row['id']] = $row['path'];
390    }
391
392    // searching the unvalidated waiting elements (they must not be taken into
393    // account)
394    $query = '
395SELECT file,storage_category_id
396  FROM '.WAITING_TABLE.'
397  WHERE storage_category_id IN (
398'.wordwrap(implode(', ', $cat_ids), 80, "\n").')
399    AND validated = \'false\'
400;';
401    $result = pwg_query($query);
402    while ($row = mysql_fetch_array($result))
403    {
404      array_push(
405        $db_unvalidated,
406        array_search(
407          $row['storage_category_id'],
408          $db_fulldirs)
409        .'/'.$row['file']
410        );
411    }
412  }
413
414  // next element id available
415  $query = '
416SELECT IF(MAX(id)+1 IS NULL, 1, MAX(id)+1) AS next_element_id
417  FROM '.IMAGES_TABLE.'
418;';
419  list($next_element_id) = mysql_fetch_array(pwg_query($query));
420
421  $start = get_moment();
422
423  $inserts = array();
424  $insert_links = array();
425
426  foreach (array_diff(array_keys($fs), $db_elements, $db_unvalidated) as $path)
427  {
428    $insert = array();
429    // storage category must exist
430    $dirname = dirname($path);
431    if (!isset($db_fulldirs[$dirname]))
432    {
433      continue;
434    }
435    $filename = basename($path);
436    if (!preg_match('/^[a-zA-Z0-9-_.]+$/', $filename))
437    {
438      array_push(
439        $errors,
440        array(
441          'path' => $path,
442          'type' => 'PWG-UPDATE-1'
443          )
444        );
445
446      continue;
447    }
448
449    if ( isset( $conf['flip_picture_ext'][get_extension($filename)] )
450          and !isset($fs[$path]['tn_ext']) )
451    { // For a picture thumbnail is mandatory and for non picture element,
452      // thumbnail and representative are optionnal
453      array_push(
454        $errors,
455        array(
456          'path' => $path,
457          'type' => 'PWG-UPDATE-2'
458          )
459        );
460    }
461    else
462    {
463      $insert = array(
464        'id'             => $next_element_id++,
465        'file'           => $filename,
466        'date_available' => CURRENT_DATE,
467        'path'           => $path,
468        'tn_ext'         => isset($fs[$path]['tn_ext'])
469          ? $fs[$path]['tn_ext']
470          : null,
471        'storage_category_id' => $db_fulldirs[$dirname],
472        );
473
474      if ( $_POST['privacy_level']!=0 )
475      {
476        $insert['level'] = $_POST['privacy_level'];
477      }
478
479      array_push(
480        $inserts,
481        $insert
482        );
483
484      array_push(
485        $insert_links,
486        array(
487          'image_id'    => $insert{'id'},
488          'category_id' => $insert['storage_category_id'],
489          )
490        );
491
492      array_push(
493        $infos,
494        array(
495          'path' => $insert{'path'},
496          'info' => l10n('update_research_added')
497          )
498        );
499
500      $caddiables[] = $insert['id'];
501    }
502  }
503
504  if (count($inserts) > 0)
505  {
506    if (!$simulate)
507    {
508      // inserts all new elements
509      mass_inserts(
510        IMAGES_TABLE,
511        array_keys($inserts[0]),
512        $inserts
513        );
514
515      // inserts all links between new elements and their storage category
516      mass_inserts(
517        IMAGE_CATEGORY_TABLE,
518        array_keys($insert_links[0]),
519        $insert_links
520        );
521
522      // add new elements to caddie
523      if (isset($_POST['add_to_caddie']) and $_POST['add_to_caddie'] == 1)
524      {
525        fill_caddie($caddiables);
526      }
527    }
528    $counts['new_elements'] = count($inserts);
529  }
530
531  // delete elements that are in database but not in the filesystem
532  $to_delete_elements = array();
533  foreach (array_diff($db_elements, array_keys($fs)) as $path)
534  {
535    array_push($to_delete_elements, array_search($path, $db_elements));
536    array_push($infos, array('path' => $path,
537                             'info' => l10n('update_research_deleted')));
538  }
539  if (count($to_delete_elements) > 0)
540  {
541    if (!$simulate)
542    {
543      delete_elements($to_delete_elements);
544    }
545    $counts['del_elements'] = count($to_delete_elements);
546  }
547
548  $template->append('footer_elements', '<!-- scanning files : '
549    . get_elapsed_time($start_files, get_moment())
550    . ' -->' );
551
552  // retrieving informations given by uploaders
553  if (!$simulate and count($cat_ids) > 0)
554  {
555    $query = '
556SELECT id,file,storage_category_id,infos
557  FROM '.WAITING_TABLE.'
558  WHERE storage_category_id IN (
559'.wordwrap(implode(', ', $cat_ids), 80, "\n").')
560    AND validated = \'true\'
561;';
562    $result = pwg_query($query);
563
564    $datas = array();
565    $fields =
566      array(
567        'primary' => array('id'),
568        'update'  => array('date_creation', 'author', 'name', 'comment')
569        );
570
571    $waiting_to_delete = array();
572
573    while ($row = mysql_fetch_array($result))
574    {
575      $data = array();
576
577      $query = '
578SELECT id
579  FROM '.IMAGES_TABLE.'
580  WHERE storage_category_id = '.$row['storage_category_id'].'
581    AND file = \''.$row['file'].'\'
582;';
583      list($data['id']) = mysql_fetch_array(pwg_query($query));
584
585      foreach ($fields['update'] as $field)
586      {
587        $data[$field] = addslashes( getAttribute($row['infos'], $field) );
588      }
589
590      array_push($datas, $data);
591      array_push($waiting_to_delete, $row['id']);
592    }
593
594    if (count($datas) > 0)
595    {
596      mass_updates(IMAGES_TABLE, $fields, $datas);
597
598      // delete now useless waiting elements
599      $query = '
600DELETE
601  FROM '.WAITING_TABLE.'
602  WHERE id IN ('.implode(',', $waiting_to_delete).')
603;';
604      pwg_query($query);
605    }
606  }
607}
608
609// +-----------------------------------------------------------------------+
610// |                          synchronize files                            |
611// +-----------------------------------------------------------------------+
612if (isset($_POST['submit'])
613    and ($_POST['sync'] == 'dirs' or $_POST['sync'] == 'files')
614    and !$general_failure )
615{
616  if (!$simulate)
617  {
618    $start = get_moment();
619    update_category('all');
620    $template->append('footer_elements', '<!-- update_category(all) : '
621      . get_elapsed_time($start,get_moment())
622      . ' -->' );
623    $start = get_moment();
624    update_global_rank();
625    $template->append('footer_elements', '<!-- ordering categories : '
626      . get_elapsed_time($start, get_moment())
627      . ' -->');
628  }
629
630  if ($_POST['sync'] == 'files')
631  {
632    $start = get_moment();
633    $opts['category_id'] = '';
634    $opts['recursive'] = true;
635    if (isset($_POST['cat']))
636    {
637      $opts['category_id'] = $_POST['cat'];
638      if (!isset($_POST['subcats-included']) or $_POST['subcats-included'] != 1)
639      {
640        $opts['recursive'] = false;
641      }
642    }
643    $files = get_filelist($opts['category_id'], $site_id,
644                          $opts['recursive'],
645                          false);
646    $template->append('footer_elements', '<!-- get_filelist : '
647      . get_elapsed_time($start, get_moment())
648      . ' -->');
649    $start = get_moment();
650
651    $datas = array();
652    foreach ( $files as $id=>$file )
653    {
654      $data = $site_reader->get_element_update_attributes($file);
655      if ( !is_array($data) )
656      {
657        continue;
658      }
659      $extension = get_extension($file);
660      if ( isset($conf['flip_picture_ext'][$extension]) )
661      {
662        if ( !isset($data['tn_ext']) )
663        {
664          array_push(
665            $errors,
666            array(
667              'path' => $file,
668              'type' => 'PWG-UPDATE-2'
669              )
670            );
671          continue;
672        }
673      }
674
675      $data['id']=$id;
676      array_push($datas, $data);
677    } // end foreach file
678
679    $counts['upd_elements'] = count($datas);
680    if (!$simulate and count($datas)>0 )
681    {
682      mass_updates(
683        IMAGES_TABLE,
684        // fields
685        array(
686          'primary' => array('id'),
687          'update'  => $site_reader->get_update_attributes(),
688          ),
689        $datas
690        );
691    }
692    $template->append('footer_elements', '<!-- update files : '
693      . get_elapsed_time($start,get_moment())
694      . ' -->');
695  }// end if sync files
696}
697
698// +-----------------------------------------------------------------------+
699// |                          synchronize files                            |
700// +-----------------------------------------------------------------------+
701if (isset($_POST['submit'])
702    and ($_POST['sync'] == 'dirs' or $_POST['sync'] == 'files'))
703{
704  $template->assign(
705    'update_result',
706    array(
707      'NB_NEW_CATEGORIES'=>$counts['new_categories'],
708      'NB_DEL_CATEGORIES'=>$counts['del_categories'],
709      'NB_NEW_ELEMENTS'=>$counts['new_elements'],
710      'NB_DEL_ELEMENTS'=>$counts['del_elements'],
711      'NB_UPD_ELEMENTS'=>$counts['upd_elements'],
712      'NB_ERRORS'=>count($errors),
713      ));
714}
715
716// +-----------------------------------------------------------------------+
717// |                          synchronize metadata                         |
718// +-----------------------------------------------------------------------+
719if (isset($_POST['submit']) and preg_match('/^metadata/', $_POST['sync'])
720         and !$general_failure)
721{
722  // sync only never synchronized files ?
723  if ($_POST['sync'] == 'metadata_new')
724  {
725    $opts['only_new'] = true;
726  }
727  else
728  {
729    $opts['only_new'] = false;
730  }
731  $opts['category_id'] = '';
732  $opts['recursive'] = true;
733
734  if (isset($_POST['cat']))
735  {
736    $opts['category_id'] = $_POST['cat'];
737    // recursive ?
738    if (!isset($_POST['subcats-included']) or $_POST['subcats-included'] != 1)
739    {
740      $opts['recursive'] = false;
741    }
742  }
743  $start = get_moment();
744  $files = get_filelist($opts['category_id'], $site_id,
745                        $opts['recursive'],
746                        $opts['only_new']);
747
748  $template->append('footer_elements', '<!-- get_filelist : '
749    . get_elapsed_time($start, get_moment())
750    . ' -->');
751
752  $start = get_moment();
753  $datas = array();
754  $tags_of = array();
755
756  $has_high_images = array();
757
758  $image_ids = array();
759  foreach ($files as $id => $file)
760  {
761    array_push($image_ids, $id);
762  }
763
764  if (count($image_ids) > 0)
765  {
766    $query = '
767SELECT id
768  FROM '.IMAGES_TABLE.'
769  WHERE has_high = \'true\'
770    AND id IN (
771'.wordwrap(implode(', ', $image_ids), 80, "\n").'
772)
773;';
774
775    $result = pwg_query($query);
776    while ($row = mysql_fetch_array($result))
777    {
778      array_push($has_high_images, $row['id']);
779    }
780  }
781
782  foreach ( $files as $id=>$file )
783  {
784    $data = $site_reader->get_element_metadata(
785      $file,
786      in_array($id, $has_high_images)
787      );
788
789    if ( is_array($data) )
790    {
791      $data['date_metadata_update'] = CURRENT_DATE;
792      $data['id']=$id;
793      array_push($datas, $data);
794
795      foreach (array('keywords', 'tags') as $key)
796      {
797        if (isset($data[$key]))
798        {
799          if (!isset($tags_of[$id]))
800          {
801            $tags_of[$id] = array();
802          }
803
804          foreach (explode(',', $data[$key]) as $tag_name)
805          {
806            array_push(
807              $tags_of[$id],
808              tag_id_from_tag_name($tag_name)
809              );
810          }
811        }
812      }
813    }
814    else
815    {
816      array_push($errors, array('path' => $file, 'type' => 'PWG-ERROR-NO-FS'));
817    }
818  }
819
820  if (!$simulate)
821  {
822    if (count($datas) > 0)
823    {
824      mass_updates(
825        IMAGES_TABLE,
826        // fields
827        array(
828          'primary' => array('id'),
829          'update'  => array_unique(
830            array_merge(
831              array_diff(
832                $site_reader->get_metadata_attributes(),
833                // keywords and tags fields are managed separately
834                array('keywords', 'tags')
835                ),
836              array('date_metadata_update'))
837            )
838          ),
839        $datas
840        );
841    }
842    set_tags_of($tags_of);
843  }
844
845  $template->append('footer_elements', '<!-- metadata update : '
846    . get_elapsed_time($start, get_moment())
847    . ' -->');
848
849  $template->assign(
850    'metadata_result',
851    array(
852      'NB_ELEMENTS_DONE' => count($datas),
853      'NB_ELEMENTS_CANDIDATES' => count($files),
854      'NB_ERRORS' => count($errors),
855      ));
856}
857
858// +-----------------------------------------------------------------------+
859// |                        template initialization                        |
860// +-----------------------------------------------------------------------+
861$template->set_filenames(array('update'=>'admin/site_update.tpl'));
862$result_title = '';
863if (isset($simulate) and $simulate)
864{
865  $result_title.= l10n('update_simulation_title').' ';
866}
867
868// used_metadata string is displayed to inform admin which metadata will be
869// used from files for synchronization
870$used_metadata = implode( ', ', $site_reader->get_metadata_attributes());
871if ($site_is_remote and !isset($_POST['submit']) )
872{
873  $used_metadata.= ' + ...';
874}
875
876$template->assign(
877  array(
878    'SITE_URL'=>$site_url,
879    'U_SITE_MANAGER'=> get_root_url().'admin.php?page=site_manager',
880    'L_RESULT_UPDATE'=>$result_title.l10n('update_part_research'),
881    'L_RESULT_METADATA'=>$result_title.l10n('update_result_metadata'),
882    'METADATA_LIST' => $used_metadata,
883    'U_HELP' => get_root_url().'popuphelp.php?page=synchronize',
884    ));
885
886// +-----------------------------------------------------------------------+
887// |                        introduction : choices                         |
888// +-----------------------------------------------------------------------+
889if (!isset($_POST['submit']) or (isset($simulate) and $simulate))
890{
891  if (isset($simulate) and $simulate)
892  {
893    $tpl_introduction = array(
894        'sync'  => $_POST['sync'],
895        'display_info' => isset($_POST['display_info']) and $_POST['display_info']==1,
896        'add_to_caddie' => isset($_POST['add_to_caddie']) and $_POST['add_to_caddie']==1,
897        'subcats_included' => isset($_POST['subcats-included']) and $_POST['subcats-included']==1,
898        'privacy_level_selected' => (int)@$_POST['privacy_level'],
899      );
900
901    if (isset($_POST['cat']) and is_numeric($_POST['cat']))
902    {
903      $cat_selected = array($_POST['cat']);
904    }
905    else
906    {
907      $cat_selected = array();
908    }
909  }
910  else
911  {
912    $tpl_introduction = array(
913        'sync'  => 'dirs',
914        'display_info' => false,
915        'add_to_caddie' => false,
916        'subcats_included' => true,
917        'privacy_level_selected' => 0,
918      );
919
920    $cat_selected = array();
921  }
922
923  $tpl_introduction['privacy_level_options']=array();
924  foreach ($conf['available_permission_levels'] as $level)
925  {
926    $tpl_introduction['privacy_level_options'][$level] = l10n( sprintf('Level %d', $level) );
927  }
928
929  $template->assign('introduction', $tpl_introduction);
930
931  $query = '
932SELECT id,name,uppercats,global_rank
933  FROM '.CATEGORIES_TABLE.'
934  WHERE site_id = '.$site_id.'
935;';
936  display_select_cat_wrapper($query,
937                             $cat_selected,
938                             'category_options',
939                             false);
940}
941
942if (count($errors) > 0)
943{
944  foreach ($errors as $error)
945  {
946    $template->append(
947      'sync_errors',
948      array(
949        'ELEMENT' => $error['path'],
950        'LABEL' => $error['type'].' ('.$error_labels[$error['type']][0].')'
951        ));
952  }
953
954  foreach ($error_labels as $error_type=>$error_description)
955  {
956    $template->append(
957      'sync_error_captions',
958      array(
959        'TYPE' => $error_type,
960        'LABEL' => $error_description[1]
961        ));
962  }
963}
964
965if (count($infos) > 0
966    and isset($_POST['display_info'])
967    and $_POST['display_info'] == 1)
968{
969  foreach ($infos as $info)
970  {
971    $template->append(
972      'sync_infos',
973      array(
974        'ELEMENT' => $info['path'],
975        'LABEL' => $info['info']
976        ));
977  }
978}
979
980// +-----------------------------------------------------------------------+
981// |                          sending html code                            |
982// +-----------------------------------------------------------------------+
983$template->assign_var_from_handle('ADMIN_CONTENT', 'update');
984?>
Note: See TracBrowser for help on using the repository browser.