Index: trunk/admin/batch_manager_global.php
===================================================================
--- trunk/admin/batch_manager_global.php	(revision 12829)
+++ trunk/admin/batch_manager_global.php	(revision 12831)
@@ -383,17 +383,5 @@
   if ('metadata' == $action)
   {
-    $query = '
-SELECT id, path
-  FROM '.IMAGES_TABLE.'
-  WHERE id IN ('.implode(',', $collection).')
-;';
-    $id_to_path = array();
-    $result = pwg_query($query);
-    while ($row = pwg_db_fetch_assoc($result))
-    {
-      $id_to_path[$row['id']] = $row['path'];
-    }
-    
-    update_metadata($id_to_path);
+    sync_metadata($collection);
 
     array_push(
Index: trunk/admin/include/functions_metadata.php
===================================================================
--- trunk/admin/include/functions_metadata.php	(revision 12829)
+++ trunk/admin/include/functions_metadata.php	(revision 12831)
@@ -49,5 +49,5 @@
           $day = 1;
         }
-        
+
         $iptc[$pwg_key] = $year.'-'.$month.'-'.$day;
       }
@@ -110,5 +110,73 @@
 }
 
-function update_metadata($files)
+
+function get_sync_metadata_attributes()
+{
+  global $conf;
+
+  $update_fields = array('filesize', 'width', 'height');
+
+  if ($conf['use_exif'])
+  {
+    $update_fields =
+      array_merge(
+        $update_fields,
+        array_keys($conf['use_exif_mapping'])
+        );
+  }
+
+  if ($conf['use_iptc'])
+  {
+    $update_fields =
+      array_merge(
+        $update_fields,
+        array_keys($conf['use_iptc_mapping'])
+        );
+  }
+
+  return array_unique($update_fields);
+}
+
+function get_sync_metadata($infos)
+{
+  global $conf;
+  $file = PHPWG_ROOT_PATH.$infos['path'];
+  $fs = @filesize($file);
+
+  if ($fs===false)
+  {
+    return false;
+  }
+
+  $infos['filesize'] = floor($fs/1024);
+
+  if (isset($infos['representative_ext']))
+  {
+    $file = original_to_representative($file, $infos['representative_ext']);
+  }
+
+  if ($image_size = @getimagesize($file))
+  {
+    $infos['width'] = $image_size[0];
+    $infos['height'] = $image_size[1];
+  }
+
+  if ($conf['use_exif'])
+  {
+    $exif = get_sync_exif_data($file);
+    $infos = array_merge($infos, $exif);
+  }
+
+  if ($conf['use_iptc'])
+  {
+    $iptc = get_sync_iptc_data($file);
+    $infos = array_merge($infos, $iptc);
+  }
+
+  return $infos;
+}
+
+
+function sync_metadata($ids)
 {
   global $conf;
@@ -121,81 +189,39 @@
   $datas = array();
   $tags_of = array();
-  $has_high_images = array();
-
-  $image_ids = array();
-  foreach ($files as $id => $file)
-  {
-    array_push($image_ids, $id);
-  }
 
   $query = '
-SELECT id
+SELECT id, path, representative_ext
   FROM '.IMAGES_TABLE.'
-  WHERE has_high = \'true\'
-    AND id IN (
-'.wordwrap(implode(', ', $image_ids), 80, "\n").'
+  WHERE id IN (
+'.wordwrap(implode(', ', $ids), 160, "\n").'
 )
 ;';
 
-  $has_high_images = array_from_query($query, 'id');
-
-  foreach ($files as $id => $file)
-  {
-    $data = array();
-    $data['id'] = $id;
-    $data['filesize'] = floor(filesize($file)/1024);
-
-    if ($image_size = @getimagesize($file))
-    {
-      $data['width'] = $image_size[0];
-      $data['height'] = $image_size[1];
-    }
-
-    if (in_array($id, $has_high_images))
-    {
-      $high_file = dirname($file).'/pwg_high/'.basename($file);
-
-      $data['high_filesize'] = floor(filesize($high_file)/1024);
-    }
-
-    if ($conf['use_exif'])
-    {
-      $exif = get_sync_exif_data($file);
-      if (count($exif) == 0 and isset($data['high_filesize']))
-      {
-        $exif = get_sync_exif_data($high_file);
-      }
-      $data = array_merge($data, $exif);
-    }
-
-    if ($conf['use_iptc'])
-    {
-      $iptc = get_sync_iptc_data($file);
-      if (count($iptc) == 0 and isset($data['high_filesize']))
-      {
-        $iptc = get_sync_iptc_data($high_file);
-      }
-      $data = array_merge($data, $iptc);
-      
-      if (count($iptc) > 0)
-      {
-        foreach (array_keys($iptc) as $key)
+  $result = pwg_query($query);
+  while ($data = pwg_db_fetch_assoc($result))
+  {
+    $data = get_sync_metadata($data);
+    if ($data === false)
+    {
+      continue;
+    }
+
+    $id = $data['id'];
+    foreach (array('keywords', 'tags') as $key)
+    {
+      if (isset($data[$key]))
+      {
+        if (!isset($tags_of[$id]))
         {
-          if ($key == 'keywords' or $key == 'tags')
-          {
-            if (!isset($tags_of[$id]))
-            {
-              $tags_of[$id] = array();
-            }
-
-            foreach (explode(',', $iptc[$key]) as $tag_name)
-            {
-              array_push(
-                $tags_of[$id],
-                tag_id_from_tag_name($tag_name)
-                );
-            }
-          }
+          $tags_of[$id] = array();
         }
+
+        foreach (explode(',', $data[$key]) as $tag_name)
+        {
+          array_push(
+            $tags_of[$id],
+            tag_id_from_tag_name($tag_name)
+            );
+        }
       }
     }
@@ -208,33 +234,11 @@
   if (count($datas) > 0)
   {
-    $update_fields =
-      array(
-        'filesize',
-        'width',
-        'height',
-        'high_filesize',
-        'date_metadata_update'
-        );
-
-    if ($conf['use_exif'])
-    {
-      $update_fields =
-        array_merge(
-          $update_fields,
-          array_keys($conf['use_exif_mapping'])
-          );
-    }
-
-    if ($conf['use_iptc'])
-    {
-      $update_fields =
-        array_merge(
-          $update_fields,
-          array_diff(
-            array_keys($conf['use_iptc_mapping']),
-            array('tags', 'keywords')
-            )
-          );
-    }
+    $update_fields = get_sync_metadata_attributes();
+    array_push($update_fields, 'date_metadata_update');
+
+    $update_fields = array_diff(
+      $update_fields,
+      array('tags', 'keywords')
+            );
 
     mass_updates(
@@ -242,5 +246,5 @@
       array(
         'primary' => array('id'),
-        'update'  => array_unique($update_fields)
+        'update'  => $update_fields
         ),
       $datas,
@@ -301,8 +305,6 @@
   }
 
-  $files = array();
-
   $query = '
-SELECT id, path
+SELECT id, path, representative_ext
   FROM '.IMAGES_TABLE.'
   WHERE storage_category_id IN ('.implode(',', $cat_ids).')';
@@ -315,11 +317,5 @@
   $query.= '
 ;';
-  $result = pwg_query($query);
-  while ($row = pwg_db_fetch_assoc($result))
-  {
-    $files[$row['id']] = $row['path'];
-  }
-
-  return $files;
+  return hash_from_query($query, 'id');
 }
 ?>
Index: trunk/admin/include/functions_upload.inc.php
===================================================================
--- trunk/admin/include/functions_upload.inc.php	(revision 12829)
+++ trunk/admin/include/functions_upload.inc.php	(revision 12831)
@@ -481,5 +481,5 @@
     $conf['use_exif'] = false;
   }
-  update_metadata(array($image_id=>$file_path));
+  sync_metadata(array($image_id));
 
   invalidate_user_cache();
Index: trunk/admin/picture_modify.php
===================================================================
--- trunk/admin/picture_modify.php	(revision 12829)
+++ trunk/admin/picture_modify.php	(revision 12831)
@@ -95,12 +95,5 @@
 if (isset($_GET['sync_metadata']))
 {
-  $query = '
-SELECT path
-  FROM '.IMAGES_TABLE.'
-  WHERE id = '.$_GET['image_id'].'
-;';
-  list($path) = pwg_db_fetch_row(pwg_query($query));
-  update_metadata(array($_GET['image_id'] => $path));
-
+  sync_metadata(array( intval($_GET['image_id'])));
   array_push($page['infos'], l10n('Metadata synchronized from file'));
 }
Index: trunk/admin/site_reader_local.php
===================================================================
--- trunk/admin/site_reader_local.php	(revision 12829)
+++ trunk/admin/site_reader_local.php	(revision 12831)
@@ -31,4 +31,13 @@
 {
   $this->site_url = $url;
+  global $conf;
+  if (!isset($conf['flip_file_ext']))
+  {
+    $conf['flip_file_ext'] = array_flip($conf['file_ext']);
+  }
+  if (!isset($conf['flip_picture_ext']))
+  {
+    $conf['flip_picture_ext'] = array_flip($conf['picture_ext']);
+  }
 }
 
@@ -69,13 +78,9 @@
  * and $conf['picture_ext']
  * @param string $path recurse in this directory
- * @return array like "pic.jpg"=>array('tn_ext'=>'jpg' ... )
+ * @return array like "pic.jpg"=>array('representative_ext'=>'jpg' ... )
  */
 function get_elements($path)
 {
   global $conf;
-  if (!isset($conf['flip_file_ext']))
-  {
-    $conf['flip_file_ext'] = array_flip($conf['file_ext']);
-  }
 
   $subdirs = array();
@@ -94,7 +99,11 @@
         if ( isset($conf['flip_file_ext'][$extension]) )
         {
-          $tn_ext = $this->get_tn_ext($path, $filename_wo_ext);
+          $representative_ext = null;
+          if (! isset($conf['flip_picture_ext'][$extension]) )
+          {
+            $representative_ext = $this->get_representative_ext($path, $filename_wo_ext);
+          }
           $fs[ $path.'/'.$node ] = array(
-            'tn_ext' => $tn_ext,
+            'representative_ext' => $representative_ext,
             );
         }
@@ -124,5 +133,5 @@
 function get_update_attributes()
 {
-  return array('tn_ext', 'has_high', 'representative_ext');
+  return array('representative_ext');
 }
 
@@ -133,17 +142,15 @@
 
   $filename = basename($file);
-  $dirname = dirname($file);
-  $filename_wo_ext = get_filename_wo_extension($filename);
   $extension = get_extension($filename);
 
-  $data['tn_ext'] = $this->get_tn_ext($dirname, $filename_wo_ext);
-  $data['has_high'] = $this->get_has_high($dirname, $filename);
+  $representative_ext = null;
+  if (! isset($conf['flip_picture_ext'][$extension]) )
+  {
+    $dirname = dirname($file);
+    $filename_wo_ext = get_filename_wo_extension($filename);
+    $representative_ext = $this->get_representative_ext($dirname, $filename_wo_ext);
+  }
 
-  if ( !isset($conf['flip_picture_ext'][$extension]) )
-  {
-    $data['representative_ext'] = $this->get_representative_ext(
-        $dirname, $filename_wo_ext
-      );
-  }
+  $data['representative_ext'] = $representative_ext;
   return $data;
 }
@@ -153,81 +160,11 @@
 function get_metadata_attributes()
 {
-  global $conf;
-
-  $update_fields = array('filesize', 'width', 'height', 'high_filesize', 'high_width', 'high_height');
-
-  if ($conf['use_exif'])
-  {
-    $update_fields =
-      array_merge(
-        $update_fields,
-        array_keys($conf['use_exif_mapping'])
-        );
-  }
-
-  if ($conf['use_iptc'])
-  {
-    $update_fields =
-      array_merge(
-        $update_fields,
-        array_keys($conf['use_iptc_mapping'])
-        );
-  }
-
-  return $update_fields;
+  return get_sync_metadata_attributes();
 }
 
 // returns a hash of attributes (metadata+filesize+width,...) for file
-function get_element_metadata($file, $has_high = false)
+function get_element_metadata($infos)
 {
-  global $conf;
-  if (!is_file($file))
-  {
-    return null;
-  }
-
-  $data = array();
-
-  $data['filesize'] = floor(filesize($file)/1024);
-
-  if ($image_size = @getimagesize($file))
-  {
-    $data['width'] = $image_size[0];
-    $data['height'] = $image_size[1];
-  }
-
-  if ($has_high)
-  {
-    $high_file = dirname($file).'/pwg_high/'.basename($file);
-    $data['high_filesize'] = floor(filesize($high_file)/1024);
-    
-    if ($high_size = @getimagesize($high_file))
-    {
-      $data['high_width'] = $high_size[0];
-      $data['high_height'] = $high_size[1];
-    }
-  }
-
-  if ($conf['use_exif'])
-  {
-    $exif = get_sync_exif_data($file);
-    if (count($exif) == 0 and isset($data['high_filesize']))
-    {
-      $exif = get_sync_exif_data($high_file);
-    }
-    $data = array_merge($data, $exif);
-  }
-
-  if ($conf['use_iptc'])
-  {
-    $iptc = get_sync_iptc_data($file);
-    if (count($iptc) == 0 and isset($data['high_filesize']))
-    {
-      $iptc = get_sync_iptc_data($high_file);
-    }
-    $data = array_merge($data, $iptc);
-  }
-
-  return $data;
+  return get_sync_metadata($infos);
 }
 
@@ -249,32 +186,4 @@
 }
 
-function get_tn_ext($path, $filename_wo_ext)
-{
-  global $conf;
-
-  $base_test =
-    $path.'/thumbnail/'.$conf['prefix_thumbnail'].$filename_wo_ext.'.';
-
-  foreach ($conf['picture_ext'] as $ext)
-  {
-    $test = $base_test.$ext;
-    if (is_file($test))
-    {
-      return $ext;
-    }
-  }
-
-  return null;
-}
-
-function get_has_high($path, $filename)
-{
-  if (is_file($path.'/pwg_high/'.$filename))
-  {
-    return 'true';
-  }
-
-  return null;
-}
 
 }
Index: trunk/admin/site_update.php
===================================================================
--- trunk/admin/site_update.php	(revision 12829)
+++ trunk/admin/site_update.php	(revision 12831)
@@ -81,4 +81,5 @@
 if ($site_is_remote)
 {
+  fatal_error('remote sites not supported');
   include_once(PHPWG_ROOT_PATH.'admin/site_reader_remote.php');
   $local_listing = null;
@@ -99,8 +100,4 @@
 if (isset($_POST['submit']))
 {
-  if (!isset($conf['flip_picture_ext']))
-  {
-    $conf['flip_picture_ext'] = array_flip($conf['picture_ext']);
-  }
   if ($site_reader->open())
   {
@@ -313,5 +310,5 @@
         );
       mass_inserts(CATEGORIES_TABLE, $dbfields, $inserts);
-      
+
       // add default permissions to categories
       $category_ids = array();
@@ -322,5 +319,5 @@
       add_permission_on_category($category_ids, get_admins());
     }
-    
+
     $counts['new_categories'] = count($inserts);
   }
@@ -374,5 +371,5 @@
       .wordwrap(
         implode(', ', $cat_ids),
-        80,
+        160,
         "\n"
         ).')';
@@ -411,58 +408,41 @@
     }
 
-    if ( isset( $conf['flip_picture_ext'][get_extension($filename)] )
-          and !isset($fs[$path]['tn_ext']) )
-    { // For a picture thumbnail is mandatory and for non picture element,
-      // thumbnail and representative are optionnal
-      array_push(
-        $errors,
-        array(
-          'path' => $path,
-          'type' => 'PWG-UPDATE-2'
-          )
-        );
-    }
-    else
-    {
-      $insert = array(
-        'id'             => $next_element_id++,
-        'file'           => $filename,
-        'date_available' => CURRENT_DATE,
-        'path'           => $path,
-        'tn_ext'         => isset($fs[$path]['tn_ext'])
-          ? $fs[$path]['tn_ext']
-          : null,
-        'storage_category_id' => $db_fulldirs[$dirname],
-        'added_by' => $user['id'],
-        );
-
-      if ( $_POST['privacy_level']!=0 )
-      {
-        $insert['level'] = $_POST['privacy_level'];
-      }
-
-      array_push(
-        $inserts,
-        $insert
-        );
-
-      array_push(
-        $insert_links,
-        array(
-          'image_id'    => $insert['id'],
-          'category_id' => $insert['storage_category_id'],
-          )
-        );
-
-      array_push(
-        $infos,
-        array(
-          'path' => $insert['path'],
-          'info' => l10n('added')
-          )
-        );
-
-      $caddiables[] = $insert['id'];
-    }
+    $insert = array(
+      'id'             => $next_element_id++,
+      'file'           => $filename,
+      'date_available' => CURRENT_DATE,
+      'path'           => $path,
+      'representative_ext'  => $fs[$path]['representative_ext'],
+      'storage_category_id' => $db_fulldirs[$dirname],
+      'added_by'       => $user['id'],
+      );
+
+    if ( $_POST['privacy_level']!=0 )
+    {
+      $insert['level'] = $_POST['privacy_level'];
+    }
+
+    array_push(
+      $inserts,
+      $insert
+      );
+
+    array_push(
+      $insert_links,
+      array(
+        'image_id'    => $insert['id'],
+        'category_id' => $insert['storage_category_id'],
+        )
+      );
+
+    array_push(
+      $infos,
+      array(
+        'path' => $insert['path'],
+        'info' => l10n('added')
+        )
+      );
+
+    $caddiables[] = $insert['id'];
   }
 
@@ -561,23 +541,9 @@
     foreach ( $files as $id=>$file )
     {
+      $file = $file['path'];
       $data = $site_reader->get_element_update_attributes($file);
       if ( !is_array($data) )
       {
         continue;
-      }
-      $extension = get_extension($file);
-      if ( isset($conf['flip_picture_ext'][$extension]) )
-      {
-        if ( !isset($data['tn_ext']) )
-        {
-          array_push(
-            $errors,
-            array(
-              'path' => $file,
-              'type' => 'PWG-UPDATE-2'
-              )
-            );
-          continue;
-        }
       }
 
@@ -656,30 +622,7 @@
   $tags_of = array();
 
-  $has_high_images = array();
-
-  $image_ids = array();
-  foreach ($files as $id => $file)
-  {
-    array_push($image_ids, $id);
-  }
-
-  if (count($image_ids) > 0)
-  {
-    $query = '
-SELECT id
-  FROM '.IMAGES_TABLE.'
-  WHERE has_high = \'true\'
-    AND id IN (
-'.wordwrap(implode(', ', $image_ids), 80, "\n").'
-)';
-    $has_high_images = array_from_query($query, 'id' );
-  }
-
-  foreach ( $files as $id=>$file )
-  {
-    $data = $site_reader->get_element_metadata(
-      $file,
-      in_array($id, $has_high_images)
-      );
+  foreach ( $files as $id => $element_infos )
+  {
+    $data = $site_reader->get_element_metadata($element_infos);
 
     if ( is_array($data) )
@@ -710,5 +653,5 @@
     else
     {
-      array_push($errors, array('path' => $file, 'type' => 'PWG-ERROR-NO-FS'));
+      array_push($errors, array('path' => $element_infos['path'], 'type' => 'PWG-ERROR-NO-FS'));
     }
   }
@@ -818,5 +761,5 @@
       'meta_empty_overrides'  => false,
     );
-  
+
   $cat_selected = array();
 
