source: trunk/admin/site_update.php @ 1296

Last change on this file since 1296 was 1296, checked in by rvelices, 18 years ago

bug 362: remote site update fails when inserting new cat (MySql err 1048)

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