source: trunk/admin/site_update.php @ 1883

Last change on this file since 1883 was 1883, checked in by plg, 17 years ago

New: #images.high_filesize was added so that we can sum the filesizes in the
filtered history. #images.high_filesize is filled during metadata
synchronization.

Bug fixed: in getAttribute XML function, when asking "filesize", it was
returning high_filesize. The regex was too simple.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 27.7 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: 2007-03-09 16:28:49 +0000 (Fri, 09 Mar 2007) $
10// | last modifier : $Author: plg $
11// | revision      : $Revision: 1883 $
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
115  // shall we simulate only
116  if ((isset($_POST['simulate']) and $_POST['simulate'] == 1) or is_adviser())
117  {
118    $simulate = true;
119  }
120  else
121  {
122    $simulate = false;
123  }
124}
125
126// +-----------------------------------------------------------------------+
127// |                      directories / categories                         |
128// +-----------------------------------------------------------------------+
129if (isset($_POST['submit'])
130    and ($_POST['sync'] == 'dirs' or $_POST['sync'] == 'files')
131    and !$general_failure)
132{
133  $counts['new_categories'] = 0;
134  $counts['del_categories'] = 0;
135  $counts['del_elements'] = 0;
136  $counts['new_elements'] = 0;
137  $counts['upd_elements'] = 0;
138
139  $start = get_moment();
140  // which categories to update ?
141  $cat_ids = array();
142
143  $query = '
144SELECT id, uppercats, global_rank, status, visible
145  FROM '.CATEGORIES_TABLE.'
146  WHERE dir IS NOT NULL
147    AND site_id = '.$site_id;
148  if (isset($_POST['cat']) and is_numeric($_POST['cat']))
149  {
150    if (isset($_POST['subcats-included']) and $_POST['subcats-included'] == 1)
151    {
152      $query.= '
153    AND uppercats REGEXP \'(^|,)'.$_POST['cat'].'(,|$)\'
154';
155    }
156    else
157    {
158      $query.= '
159    AND id = '.$_POST['cat'].'
160';
161    }
162  }
163  $query.= '
164;';
165  $result = pwg_query($query);
166
167  $db_categories = array();
168  while ($row = mysql_fetch_array($result))
169  {
170    $db_categories[$row['id']] = $row;
171  }
172
173  // get categort full directories in an array for comparison with file
174  // system directory tree
175  $db_fulldirs = get_fulldirs(array_keys($db_categories));
176
177  // what is the base directory to search file system sub-directories ?
178  if (isset($_POST['cat']) and is_numeric($_POST['cat']))
179  {
180    $basedir = $db_fulldirs[$_POST['cat']];
181  }
182  else
183  {
184    $basedir = preg_replace('#/*$#', '', $site_url);
185  }
186
187  // we need to have fulldirs as keys to make efficient comparison
188  $db_fulldirs = array_flip($db_fulldirs);
189
190  // finding next rank for each id_uppercat. By default, each category id
191  // has 1 for next rank on its sub-categories to create
192  $next_rank['NULL'] = 1;
193
194  $query = '
195SELECT id
196  FROM '.CATEGORIES_TABLE.'
197;';
198  $result = pwg_query($query);
199  while ($row = mysql_fetch_array($result))
200  {
201    $next_rank[$row['id']] = 1;
202  }
203
204  // let's see if some categories already have some sub-categories...
205  $query = '
206SELECT id_uppercat, MAX(rank)+1 AS next_rank
207  FROM '.CATEGORIES_TABLE.'
208  GROUP BY id_uppercat
209;';
210  $result = pwg_query($query);
211  while ($row = mysql_fetch_array($result))
212  {
213    // for the id_uppercat NULL, we write 'NULL' and not the empty string
214    if (!isset($row['id_uppercat']) or $row['id_uppercat'] == '')
215    {
216      $row['id_uppercat'] = 'NULL';
217    }
218    $next_rank[$row['id_uppercat']] = $row['next_rank'];
219  }
220
221  // next category id available
222  $query = '
223SELECT IF(MAX(id)+1 IS NULL, 1, MAX(id)+1) AS next_id
224  FROM '.CATEGORIES_TABLE.'
225;';
226  list($next_id) = mysql_fetch_array(pwg_query($query));
227
228  // retrieve sub-directories fulldirs from the site reader
229  $fs_fulldirs = $site_reader->get_full_directories($basedir);
230  //print_r( $fs_fulldirs ); echo "<br>";
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  echo '<!-- scanning dirs : ';
352  echo get_elapsed_time($start, get_moment());
353  echo ' -->'."\n";
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  //print_r($fs); echo "<br>";
366  echo '<!-- get_elements: '.get_elapsed_time($start, get_moment())." -->\n";
367
368  $cat_ids = array_diff(array_keys($db_categories), $to_delete);
369
370  $db_elements = array();
371  $db_unvalidated = array();
372
373  if (count($cat_ids) > 0)
374  {
375    $query = '
376SELECT id, path
377  FROM '.IMAGES_TABLE.'
378  WHERE storage_category_id IN ('
379      .wordwrap(
380        implode(', ', $cat_ids),
381        80,
382        "\n"
383        ).')
384;';
385    $result = pwg_query($query);
386    while ($row = mysql_fetch_array($result))
387    {
388      $db_elements[$row['id']] = $row['path'];
389    }
390
391    // searching the unvalidated waiting elements (they must not be taken into
392    // account)
393    $query = '
394SELECT file,storage_category_id
395  FROM '.WAITING_TABLE.'
396  WHERE storage_category_id IN (
397'.wordwrap(implode(', ', $cat_ids), 80, "\n").')
398    AND validated = \'false\'
399;';
400    $result = pwg_query($query);
401    while ($row = mysql_fetch_array($result))
402    {
403      array_push(
404        $db_unvalidated,
405        array_search(
406          $row['storage_category_id'],
407          $db_fulldirs)
408        .'/'.$row['file']
409        );
410    }
411  }
412
413  // next element id available
414  $query = '
415SELECT IF(MAX(id)+1 IS NULL, 1, MAX(id)+1) AS next_element_id
416  FROM '.IMAGES_TABLE.'
417;';
418  list($next_element_id) = mysql_fetch_array(pwg_query($query));
419
420  $start = get_moment();
421
422  $inserts = array();
423  $insert_links = array();
424
425  foreach (array_diff(array_keys($fs), $db_elements, $db_unvalidated) as $path)
426  {
427    $insert = array();
428    // storage category must exist
429    $dirname = dirname($path);
430    if (!isset($db_fulldirs[$dirname]))
431    {
432      continue;
433    }
434    $filename = basename($path);
435    if (!preg_match('/^[a-zA-Z0-9-_.]+$/', $filename))
436    {
437      array_push(
438        $errors,
439        array(
440          'path' => $path,
441          'type' => 'PWG-UPDATE-1'
442          )
443        );
444
445      continue;
446    }
447
448    // 2 cases : the element is a picture or not. Indeed, for a picture
449    // thumbnail is mandatory and for non picture element, thumbnail and
450    // representative are optionnal
451    if ( isset( $conf['flip_picture_ext'][get_extension($filename)] ) )
452    {
453      // if we found a thumnbnail corresponding to our picture...
454      if (isset($fs[$path]['tn_ext']))
455      {
456        $insert = array(
457          'id'             => $next_element_id++,
458          'file'           => $filename,
459          'date_available' => CURRENT_DATE,
460          'tn_ext'         => $fs[$path]['tn_ext'],
461          'path'           => $path,
462          'storage_category_id' => $db_fulldirs[$dirname],
463          );
464
465        array_push(
466          $inserts,
467          $insert
468          );
469
470        array_push(
471          $insert_links,
472          array(
473            'image_id'    => $insert{'id'},
474            'category_id' => $insert['storage_category_id'],
475            )
476          );
477        array_push(
478          $infos,
479          array(
480            'path' => $insert{'path'},
481            'info' => l10n('update_research_added')
482            )
483          );
484      }
485      else
486      {
487        array_push(
488          $errors,
489          array(
490            'path' => $path,
491            'type' => 'PWG-UPDATE-2'
492            )
493          );
494      }
495    }
496    else
497    {
498      $insert = array(
499        'id'             => $next_element_id++,
500        'file'           => $filename,
501        'date_available' => CURRENT_DATE,
502        'path'           => $path,
503        'tn_ext'         => isset($fs[$path]['tn_ext'])
504          ? $fs[$path]['tn_ext']
505          : null,
506        'storage_category_id' => $db_fulldirs[$dirname],
507        );
508
509      array_push(
510        $inserts,
511        $insert
512        );
513
514      array_push(
515        $insert_links,
516        array(
517          'image_id'    => $insert{'id'},
518          'category_id' => $insert['storage_category_id'],
519          )
520        );
521
522      array_push(
523        $infos,
524        array(
525          'path' => $insert{'path'},
526          'info' => l10n('update_research_added')
527          )
528        );
529    }
530  }
531
532  if (count($inserts) > 0)
533  {
534    if (!$simulate)
535    {
536      // inserts all new elements
537      mass_inserts(
538        IMAGES_TABLE,
539        array_keys($inserts[0]),
540        $inserts
541        );
542
543      // inserts all links between new elements and their storage category
544      mass_inserts(
545        IMAGE_CATEGORY_TABLE,
546        array_keys($insert_links[0]),
547        $insert_links
548        );
549    }
550    $counts['new_elements'] = count($inserts);
551  }
552
553  // delete elements that are in database but not in the filesystem
554  $to_delete_elements = array();
555  foreach (array_diff($db_elements, array_keys($fs)) as $path)
556  {
557    array_push($to_delete_elements, array_search($path, $db_elements));
558    array_push($infos, array('path' => $path,
559                             'info' => l10n('update_research_deleted')));
560  }
561  if (count($to_delete_elements) > 0)
562  {
563    if (!$simulate)
564    {
565      delete_elements($to_delete_elements);
566    }
567    $counts['del_elements'] = count($to_delete_elements);
568  }
569
570  echo '<!-- scanning files : ';
571  echo get_elapsed_time($start_files, get_moment());
572  echo ' -->'."\n";
573
574  // retrieving informations given by uploaders
575  if (!$simulate and count($cat_ids) > 0)
576  {
577    $query = '
578SELECT id,file,storage_category_id,infos
579  FROM '.WAITING_TABLE.'
580  WHERE storage_category_id IN (
581'.wordwrap(implode(', ', $cat_ids), 80, "\n").')
582    AND validated = \'true\'
583;';
584    $result = pwg_query($query);
585
586    $datas = array();
587    $fields =
588      array(
589        'primary' => array('id'),
590        'update'  => array('date_creation', 'author', 'name', 'comment')
591        );
592
593    $waiting_to_delete = array();
594
595    while ($row = mysql_fetch_array($result))
596    {
597      $data = array();
598
599      $query = '
600SELECT id
601  FROM '.IMAGES_TABLE.'
602  WHERE storage_category_id = '.$row['storage_category_id'].'
603    AND file = \''.$row['file'].'\'
604;';
605      list($data['id']) = mysql_fetch_array(pwg_query($query));
606
607      foreach ($fields['update'] as $field)
608      {
609        $data[$field] = addslashes( getAttribute($row['infos'], $field) );
610      }
611
612      array_push($datas, $data);
613      array_push($waiting_to_delete, $row['id']);
614    }
615
616    if (count($datas) > 0)
617    {
618      mass_updates(IMAGES_TABLE, $fields, $datas);
619
620      // delete now useless waiting elements
621      $query = '
622DELETE
623  FROM '.WAITING_TABLE.'
624  WHERE id IN ('.implode(',', $waiting_to_delete).')
625;';
626      pwg_query($query);
627    }
628  }
629}
630
631// +-----------------------------------------------------------------------+
632// |                          synchronize files                            |
633// +-----------------------------------------------------------------------+
634if (isset($_POST['submit'])
635    and ($_POST['sync'] == 'dirs' or $_POST['sync'] == 'files')
636    and !$general_failure )
637{
638  if (!$simulate)
639  {
640    $start = get_moment();
641    update_category('all');
642    echo '<!-- update_category(all) : ';
643    echo get_elapsed_time($start,get_moment());
644    echo ' -->'."\n";
645    $start = get_moment();
646    ordering();
647    update_global_rank();
648    echo '<!-- ordering categories : ';
649    echo get_elapsed_time($start, get_moment());
650    echo ' -->'."\n";
651  }
652
653  if ($_POST['sync'] == 'files')
654  {
655    $start = get_moment();
656    $opts['category_id'] = '';
657    $opts['recursive'] = true;
658    if (isset($_POST['cat']))
659    {
660      $opts['category_id'] = $_POST['cat'];
661      if (!isset($_POST['subcats-included']) or $_POST['subcats-included'] != 1)
662      {
663        $opts['recursive'] = false;
664      }
665    }
666    $files = get_filelist($opts['category_id'], $site_id,
667                          $opts['recursive'],
668                          false);
669    echo '<!-- get_filelist : ';
670    echo get_elapsed_time($start, get_moment());
671    echo ' -->'."\n";
672    $start = get_moment();
673
674    $datas = array();
675    foreach ( $files as $id=>$file )
676    {
677      $data = $site_reader->get_element_update_attributes($file);
678      if ( !is_array($data) )
679      {
680        continue;
681      }
682      $extension = get_extension($file);
683      if ( isset($conf['flip_picture_ext'][$extension]) )
684      {
685        if ( !isset($data['tn_ext']) )
686        {
687          array_push(
688            $errors,
689            array(
690              'path' => $file,
691              'type' => 'PWG-UPDATE-2'
692              )
693            );
694          continue;
695        }
696      }
697
698      $data['id']=$id;
699      array_push($datas, $data);
700    } // end foreach file
701
702    $counts['upd_elements'] = count($datas);
703    if (!$simulate and count($datas)>0 )
704    {
705      mass_updates(
706        IMAGES_TABLE,
707        // fields
708        array(
709          'primary' => array('id'),
710          'update'  => $site_reader->get_update_attributes(),
711          ),
712        $datas
713        );
714    }
715    echo '<!-- update files : ';
716    echo get_elapsed_time($start,get_moment());
717    echo ' -->'."\n";
718  }// end if sync files
719}
720
721// +-----------------------------------------------------------------------+
722// |                          synchronize files                            |
723// +-----------------------------------------------------------------------+
724if (isset($_POST['submit'])
725    and ($_POST['sync'] == 'dirs' or $_POST['sync'] == 'files'))
726{
727  $template->assign_block_vars(
728    'update_result',
729    array(
730      'NB_NEW_CATEGORIES'=>$counts['new_categories'],
731      'NB_DEL_CATEGORIES'=>$counts['del_categories'],
732      'NB_NEW_ELEMENTS'=>$counts['new_elements'],
733      'NB_DEL_ELEMENTS'=>$counts['del_elements'],
734      'NB_UPD_ELEMENTS'=>$counts['upd_elements'],
735      'NB_ERRORS'=>count($errors),
736      ));
737}
738
739// +-----------------------------------------------------------------------+
740// |                          synchronize metadata                         |
741// +-----------------------------------------------------------------------+
742if (isset($_POST['submit']) and preg_match('/^metadata/', $_POST['sync'])
743         and !$general_failure)
744{
745  // sync only never synchronized files ?
746  if ($_POST['sync'] == 'metadata_new')
747  {
748    $opts['only_new'] = true;
749  }
750  else
751  {
752    $opts['only_new'] = false;
753  }
754  $opts['category_id'] = '';
755  $opts['recursive'] = true;
756
757  if (isset($_POST['cat']))
758  {
759    $opts['category_id'] = $_POST['cat'];
760    // recursive ?
761    if (!isset($_POST['subcats-included']) or $_POST['subcats-included'] != 1)
762    {
763      $opts['recursive'] = false;
764    }
765  }
766  $start = get_moment();
767  $files = get_filelist($opts['category_id'], $site_id,
768                        $opts['recursive'],
769                        $opts['only_new']);
770
771  echo '<!-- get_filelist : ';
772  echo get_elapsed_time($start, get_moment());
773  echo ' -->'."\n";
774
775  $start = get_moment();
776  $datas = array();
777  $tags_of = array();
778
779  $has_high_images = array();
780
781  $image_ids = array();
782  foreach ($files as $id => $file)
783  {
784    array_push($image_ids, $id);
785  }
786
787  if (count($image_ids) > 0)
788  {
789    $query = '
790SELECT id
791  FROM '.IMAGES_TABLE.'
792  WHERE has_high = \'true\'
793    AND id IN (
794'.wordwrap(implode(', ', $image_ids), 80, "\n").'
795)
796;';
797
798    $result = pwg_query($query);
799    while ($row = mysql_fetch_array($result))
800    {
801      array_push($has_high_images, $row['id']);
802    }
803  }
804 
805  foreach ( $files as $id=>$file )
806  {
807    $data = $site_reader->get_element_metadata(
808      $file,
809      in_array($id, $has_high_images)
810      );
811   
812    if ( is_array($data) )
813    {
814      $data['date_metadata_update'] = CURRENT_DATE;
815      $data['id']=$id;
816      array_push($datas, $data);
817
818      foreach (array('keywords', 'tags') as $key)
819      {
820        if (isset($data[$key]))
821        {
822          if (!isset($tags_of[$id]))
823          {
824            $tags_of[$id] = array();
825          }
826
827          foreach (explode(',', $data[$key]) as $tag_name)
828          {
829            array_push(
830              $tags_of[$id],
831              tag_id_from_tag_name($tag_name)
832              );
833          }
834        }
835      }
836    }
837    else
838    {
839      array_push($errors, array('path' => $file, 'type' => 'PWG-ERROR-NO-FS'));
840    }
841  }
842
843  if (!$simulate)
844  {
845    if (count($datas) > 0)
846    {
847      // echo '<pre>', print_r($datas); echo '</pre>';
848     
849      mass_updates(
850        IMAGES_TABLE,
851        // fields
852        array(
853          'primary' => array('id'),
854          'update'  => array_unique(
855            array_merge(
856              array_diff(
857                $site_reader->get_metadata_attributes(),
858                // keywords and tags fields are managed separately
859                array('keywords', 'tags')
860                ),
861              array('date_metadata_update'))
862            )
863          ),
864        $datas
865        );
866    }
867    set_tags_of($tags_of);
868  }
869
870  echo '<!-- metadata update : ';
871  echo get_elapsed_time($start, get_moment());
872  echo ' -->'."\n";
873
874  $template->assign_block_vars(
875    'metadata_result',
876    array(
877      'NB_ELEMENTS_DONE' => count($datas),
878      'NB_ELEMENTS_CANDIDATES' => count($files),
879      'NB_ERRORS' => count($errors),
880      ));
881}
882
883// +-----------------------------------------------------------------------+
884// |                        template initialization                        |
885// +-----------------------------------------------------------------------+
886$template->set_filenames(array('update'=>'admin/site_update.tpl'));
887$result_title = '';
888if (isset($simulate) and $simulate)
889{
890  $result_title.= l10n('update_simulation_title').' ';
891}
892
893// used_metadata string is displayed to inform admin which metadata will be
894// used from files for synchronization
895$used_metadata = implode( ', ', $site_reader->get_metadata_attributes());
896if ($site_is_remote and !isset($_POST['submit']) )
897{
898  $used_metadata.= ' + ...';
899}
900
901$template->assign_vars(
902  array(
903    'SITE_URL'=>$site_url,
904    'U_SITE_MANAGER'=> PHPWG_ROOT_PATH.'admin.php?page=site_manager',
905    'L_RESULT_UPDATE'=>$result_title.l10n('update_part_research'),
906    'L_RESULT_METADATA'=>$result_title.l10n('update_result_metadata'),
907    'METADATA_LIST' => $used_metadata
908    ));
909
910$template->assign_vars(
911  array(
912    'U_HELP' => PHPWG_ROOT_PATH.'popuphelp.php?page=synchronize'
913    )
914  );
915// +-----------------------------------------------------------------------+
916// |                        introduction : choices                         |
917// +-----------------------------------------------------------------------+
918if (!isset($_POST['submit']) or (isset($simulate) and $simulate))
919{
920  $template->assign_block_vars('introduction', array());
921
922  if (isset($simulate) and $simulate)
923  {
924    switch ($_POST['sync'])
925    {
926      case 'dirs' :
927      {
928        $template->assign_vars(
929          array('SYNC_DIRS_CHECKED'=>'checked="checked"'));
930        break;
931      }
932      case 'files' :
933      {
934        $template->assign_vars(
935          array('SYNC_ALL_CHECKED'=>'checked="checked"'));
936        break;
937      }
938      case 'metadata_new' :
939      {
940        $template->assign_vars(
941          array('SYNC_META_NEW_CHECKED'=>'checked="checked"'));
942        break;
943      }
944      case 'metadata_all' :
945      {
946        $template->assign_vars(
947          array('SYNC_META_ALL_CHECKED'=>'checked="checked"'));
948        break;
949      }
950    }
951
952    if (isset($_POST['display_info']) and $_POST['display_info'] == 1)
953    {
954      $template->assign_vars(
955        array('DISPLAY_INFO_CHECKED'=>'checked="checked"'));
956    }
957
958    if (isset($_POST['subcats-included']) and $_POST['subcats-included'] == 1)
959    {
960      $template->assign_vars(
961        array('SUBCATS_INCLUDED_CHECKED'=>'checked="checked"'));
962    }
963
964    if (isset($_POST['cat']) and is_numeric($_POST['cat']))
965    {
966      $cat_selected = array($_POST['cat']);
967    }
968    else
969    {
970      $cat_selected = array();
971    }
972  }
973  else
974  {
975    $template->assign_vars(
976      array('SYNC_DIRS_CHECKED' => 'checked="checked"',
977            'SUBCATS_INCLUDED_CHECKED'=>'checked="checked"'));
978
979    $cat_selected = array();
980  }
981
982  $query = '
983SELECT id,name,uppercats,global_rank
984  FROM '.CATEGORIES_TABLE.'
985  WHERE site_id = '.$site_id.'
986;';
987  display_select_cat_wrapper($query,
988                             $cat_selected,
989                             'introduction.category_option',
990                             false);
991}
992
993if (count($errors) > 0)
994{
995  $template->assign_block_vars('sync_errors', array());
996  foreach ($errors as $error)
997  {
998    $template->assign_block_vars(
999      'sync_errors.error',
1000      array(
1001        'ELEMENT' => $error['path'],
1002        'LABEL' => $error['type'].' ('.$error_labels[$error['type']][0].')'
1003        ));
1004  }
1005
1006  foreach ($error_labels as $error_type=>$error_description)
1007  {
1008    $template->assign_block_vars(
1009      'sync_errors.error_caption',
1010      array(
1011        'TYPE' => $error_type,
1012        'LABEL' => $error_description[1]
1013        ));
1014  }
1015
1016}
1017if (count($infos) > 0
1018    and isset($_POST['display_info'])
1019    and $_POST['display_info'] == 1)
1020{
1021  $template->assign_block_vars('sync_infos', array());
1022  foreach ($infos as $info)
1023  {
1024    $template->assign_block_vars(
1025      'sync_infos.info',
1026      array(
1027        'ELEMENT' => $info['path'],
1028        'LABEL' => $info['info']
1029        ));
1030  }
1031}
1032
1033// +-----------------------------------------------------------------------+
1034// |                          sending html code                            |
1035// +-----------------------------------------------------------------------+
1036$template->assign_var_from_handle('ADMIN_CONTENT', 'update');
1037?>
Note: See TracBrowser for help on using the repository browser.