source: trunk/admin/site_update.php @ 2114

Last change on this file since 2114 was 2114, checked in by patdenice, 17 years ago

0000749: Add an option to add new elements to caddie when synchronize database.
Add a redirection on index.php after "add to caddie action" (like on picture.php)

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