Changeset 12831


Ignore:
Timestamp:
Jan 3, 2012, 9:21:13 PM (12 years ago)
Author:
rvelices
Message:

feature 2548 multisize

  • rewrote local site sync + metadata sync
Location:
trunk
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • trunk/admin/batch_manager_global.php

    r12796 r12831  
    383383  if ('metadata' == $action)
    384384  {
    385     $query = '
    386 SELECT id, path
    387   FROM '.IMAGES_TABLE.'
    388   WHERE id IN ('.implode(',', $collection).')
    389 ;';
    390     $id_to_path = array();
    391     $result = pwg_query($query);
    392     while ($row = pwg_db_fetch_assoc($result))
    393     {
    394       $id_to_path[$row['id']] = $row['path'];
    395     }
    396    
    397     update_metadata($id_to_path);
     385    sync_metadata($collection);
    398386
    399387    array_push(
  • trunk/admin/include/functions_metadata.php

    r11748 r12831  
    4949          $day = 1;
    5050        }
    51        
     51
    5252        $iptc[$pwg_key] = $year.'-'.$month.'-'.$day;
    5353      }
     
    110110}
    111111
    112 function update_metadata($files)
     112
     113function get_sync_metadata_attributes()
     114{
     115  global $conf;
     116
     117  $update_fields = array('filesize', 'width', 'height');
     118
     119  if ($conf['use_exif'])
     120  {
     121    $update_fields =
     122      array_merge(
     123        $update_fields,
     124        array_keys($conf['use_exif_mapping'])
     125        );
     126  }
     127
     128  if ($conf['use_iptc'])
     129  {
     130    $update_fields =
     131      array_merge(
     132        $update_fields,
     133        array_keys($conf['use_iptc_mapping'])
     134        );
     135  }
     136
     137  return array_unique($update_fields);
     138}
     139
     140function get_sync_metadata($infos)
     141{
     142  global $conf;
     143  $file = PHPWG_ROOT_PATH.$infos['path'];
     144  $fs = @filesize($file);
     145
     146  if ($fs===false)
     147  {
     148    return false;
     149  }
     150
     151  $infos['filesize'] = floor($fs/1024);
     152
     153  if (isset($infos['representative_ext']))
     154  {
     155    $file = original_to_representative($file, $infos['representative_ext']);
     156  }
     157
     158  if ($image_size = @getimagesize($file))
     159  {
     160    $infos['width'] = $image_size[0];
     161    $infos['height'] = $image_size[1];
     162  }
     163
     164  if ($conf['use_exif'])
     165  {
     166    $exif = get_sync_exif_data($file);
     167    $infos = array_merge($infos, $exif);
     168  }
     169
     170  if ($conf['use_iptc'])
     171  {
     172    $iptc = get_sync_iptc_data($file);
     173    $infos = array_merge($infos, $iptc);
     174  }
     175
     176  return $infos;
     177}
     178
     179
     180function sync_metadata($ids)
    113181{
    114182  global $conf;
     
    121189  $datas = array();
    122190  $tags_of = array();
    123   $has_high_images = array();
    124 
    125   $image_ids = array();
    126   foreach ($files as $id => $file)
    127   {
    128     array_push($image_ids, $id);
    129   }
    130191
    131192  $query = '
    132 SELECT id
     193SELECT id, path, representative_ext
    133194  FROM '.IMAGES_TABLE.'
    134   WHERE has_high = \'true\'
    135     AND id IN (
    136 '.wordwrap(implode(', ', $image_ids), 80, "\n").'
     195  WHERE id IN (
     196'.wordwrap(implode(', ', $ids), 160, "\n").'
    137197)
    138198;';
    139199
    140   $has_high_images = array_from_query($query, 'id');
    141 
    142   foreach ($files as $id => $file)
    143   {
    144     $data = array();
    145     $data['id'] = $id;
    146     $data['filesize'] = floor(filesize($file)/1024);
    147 
    148     if ($image_size = @getimagesize($file))
    149     {
    150       $data['width'] = $image_size[0];
    151       $data['height'] = $image_size[1];
    152     }
    153 
    154     if (in_array($id, $has_high_images))
    155     {
    156       $high_file = dirname($file).'/pwg_high/'.basename($file);
    157 
    158       $data['high_filesize'] = floor(filesize($high_file)/1024);
    159     }
    160 
    161     if ($conf['use_exif'])
    162     {
    163       $exif = get_sync_exif_data($file);
    164       if (count($exif) == 0 and isset($data['high_filesize']))
    165       {
    166         $exif = get_sync_exif_data($high_file);
    167       }
    168       $data = array_merge($data, $exif);
    169     }
    170 
    171     if ($conf['use_iptc'])
    172     {
    173       $iptc = get_sync_iptc_data($file);
    174       if (count($iptc) == 0 and isset($data['high_filesize']))
    175       {
    176         $iptc = get_sync_iptc_data($high_file);
    177       }
    178       $data = array_merge($data, $iptc);
    179      
    180       if (count($iptc) > 0)
    181       {
    182         foreach (array_keys($iptc) as $key)
     200  $result = pwg_query($query);
     201  while ($data = pwg_db_fetch_assoc($result))
     202  {
     203    $data = get_sync_metadata($data);
     204    if ($data === false)
     205    {
     206      continue;
     207    }
     208
     209    $id = $data['id'];
     210    foreach (array('keywords', 'tags') as $key)
     211    {
     212      if (isset($data[$key]))
     213      {
     214        if (!isset($tags_of[$id]))
    183215        {
    184           if ($key == 'keywords' or $key == 'tags')
    185           {
    186             if (!isset($tags_of[$id]))
    187             {
    188               $tags_of[$id] = array();
    189             }
    190 
    191             foreach (explode(',', $iptc[$key]) as $tag_name)
    192             {
    193               array_push(
    194                 $tags_of[$id],
    195                 tag_id_from_tag_name($tag_name)
    196                 );
    197             }
    198           }
     216          $tags_of[$id] = array();
    199217        }
     218
     219        foreach (explode(',', $data[$key]) as $tag_name)
     220        {
     221          array_push(
     222            $tags_of[$id],
     223            tag_id_from_tag_name($tag_name)
     224            );
     225        }
    200226      }
    201227    }
     
    208234  if (count($datas) > 0)
    209235  {
    210     $update_fields =
    211       array(
    212         'filesize',
    213         'width',
    214         'height',
    215         'high_filesize',
    216         'date_metadata_update'
    217         );
    218 
    219     if ($conf['use_exif'])
    220     {
    221       $update_fields =
    222         array_merge(
    223           $update_fields,
    224           array_keys($conf['use_exif_mapping'])
    225           );
    226     }
    227 
    228     if ($conf['use_iptc'])
    229     {
    230       $update_fields =
    231         array_merge(
    232           $update_fields,
    233           array_diff(
    234             array_keys($conf['use_iptc_mapping']),
    235             array('tags', 'keywords')
    236             )
    237           );
    238     }
     236    $update_fields = get_sync_metadata_attributes();
     237    array_push($update_fields, 'date_metadata_update');
     238
     239    $update_fields = array_diff(
     240      $update_fields,
     241      array('tags', 'keywords')
     242            );
    239243
    240244    mass_updates(
     
    242246      array(
    243247        'primary' => array('id'),
    244         'update'  => array_unique($update_fields)
     248        'update'  => $update_fields
    245249        ),
    246250      $datas,
     
    301305  }
    302306
    303   $files = array();
    304 
    305307  $query = '
    306 SELECT id, path
     308SELECT id, path, representative_ext
    307309  FROM '.IMAGES_TABLE.'
    308310  WHERE storage_category_id IN ('.implode(',', $cat_ids).')';
     
    315317  $query.= '
    316318;';
    317   $result = pwg_query($query);
    318   while ($row = pwg_db_fetch_assoc($result))
    319   {
    320     $files[$row['id']] = $row['path'];
    321   }
    322 
    323   return $files;
     319  return hash_from_query($query, 'id');
    324320}
    325321?>
  • trunk/admin/include/functions_upload.inc.php

    r12443 r12831  
    481481    $conf['use_exif'] = false;
    482482  }
    483   update_metadata(array($image_id=>$file_path));
     483  sync_metadata(array($image_id));
    484484
    485485  invalidate_user_cache();
  • trunk/admin/picture_modify.php

    r12796 r12831  
    9595if (isset($_GET['sync_metadata']))
    9696{
    97   $query = '
    98 SELECT path
    99   FROM '.IMAGES_TABLE.'
    100   WHERE id = '.$_GET['image_id'].'
    101 ;';
    102   list($path) = pwg_db_fetch_row(pwg_query($query));
    103   update_metadata(array($_GET['image_id'] => $path));
    104 
     97  sync_metadata(array( intval($_GET['image_id'])));
    10598  array_push($page['infos'], l10n('Metadata synchronized from file'));
    10699}
  • trunk/admin/site_reader_local.php

    r12642 r12831  
    3131{
    3232  $this->site_url = $url;
     33  global $conf;
     34  if (!isset($conf['flip_file_ext']))
     35  {
     36    $conf['flip_file_ext'] = array_flip($conf['file_ext']);
     37  }
     38  if (!isset($conf['flip_picture_ext']))
     39  {
     40    $conf['flip_picture_ext'] = array_flip($conf['picture_ext']);
     41  }
    3342}
    3443
     
    6978 * and $conf['picture_ext']
    7079 * @param string $path recurse in this directory
    71  * @return array like "pic.jpg"=>array('tn_ext'=>'jpg' ... )
     80 * @return array like "pic.jpg"=>array('representative_ext'=>'jpg' ... )
    7281 */
    7382function get_elements($path)
    7483{
    7584  global $conf;
    76   if (!isset($conf['flip_file_ext']))
    77   {
    78     $conf['flip_file_ext'] = array_flip($conf['file_ext']);
    79   }
    8085
    8186  $subdirs = array();
     
    9499        if ( isset($conf['flip_file_ext'][$extension]) )
    95100        {
    96           $tn_ext = $this->get_tn_ext($path, $filename_wo_ext);
     101          $representative_ext = null;
     102          if (! isset($conf['flip_picture_ext'][$extension]) )
     103          {
     104            $representative_ext = $this->get_representative_ext($path, $filename_wo_ext);
     105          }
    97106          $fs[ $path.'/'.$node ] = array(
    98             'tn_ext' => $tn_ext,
     107            'representative_ext' => $representative_ext,
    99108            );
    100109        }
     
    124133function get_update_attributes()
    125134{
    126   return array('tn_ext', 'has_high', 'representative_ext');
     135  return array('representative_ext');
    127136}
    128137
     
    133142
    134143  $filename = basename($file);
    135   $dirname = dirname($file);
    136   $filename_wo_ext = get_filename_wo_extension($filename);
    137144  $extension = get_extension($filename);
    138145
    139   $data['tn_ext'] = $this->get_tn_ext($dirname, $filename_wo_ext);
    140   $data['has_high'] = $this->get_has_high($dirname, $filename);
     146  $representative_ext = null;
     147  if (! isset($conf['flip_picture_ext'][$extension]) )
     148  {
     149    $dirname = dirname($file);
     150    $filename_wo_ext = get_filename_wo_extension($filename);
     151    $representative_ext = $this->get_representative_ext($dirname, $filename_wo_ext);
     152  }
    141153
    142   if ( !isset($conf['flip_picture_ext'][$extension]) )
    143   {
    144     $data['representative_ext'] = $this->get_representative_ext(
    145         $dirname, $filename_wo_ext
    146       );
    147   }
     154  $data['representative_ext'] = $representative_ext;
    148155  return $data;
    149156}
     
    153160function get_metadata_attributes()
    154161{
    155   global $conf;
    156 
    157   $update_fields = array('filesize', 'width', 'height', 'high_filesize', 'high_width', 'high_height');
    158 
    159   if ($conf['use_exif'])
    160   {
    161     $update_fields =
    162       array_merge(
    163         $update_fields,
    164         array_keys($conf['use_exif_mapping'])
    165         );
    166   }
    167 
    168   if ($conf['use_iptc'])
    169   {
    170     $update_fields =
    171       array_merge(
    172         $update_fields,
    173         array_keys($conf['use_iptc_mapping'])
    174         );
    175   }
    176 
    177   return $update_fields;
     162  return get_sync_metadata_attributes();
    178163}
    179164
    180165// returns a hash of attributes (metadata+filesize+width,...) for file
    181 function get_element_metadata($file, $has_high = false)
     166function get_element_metadata($infos)
    182167{
    183   global $conf;
    184   if (!is_file($file))
    185   {
    186     return null;
    187   }
    188 
    189   $data = array();
    190 
    191   $data['filesize'] = floor(filesize($file)/1024);
    192 
    193   if ($image_size = @getimagesize($file))
    194   {
    195     $data['width'] = $image_size[0];
    196     $data['height'] = $image_size[1];
    197   }
    198 
    199   if ($has_high)
    200   {
    201     $high_file = dirname($file).'/pwg_high/'.basename($file);
    202     $data['high_filesize'] = floor(filesize($high_file)/1024);
    203    
    204     if ($high_size = @getimagesize($high_file))
    205     {
    206       $data['high_width'] = $high_size[0];
    207       $data['high_height'] = $high_size[1];
    208     }
    209   }
    210 
    211   if ($conf['use_exif'])
    212   {
    213     $exif = get_sync_exif_data($file);
    214     if (count($exif) == 0 and isset($data['high_filesize']))
    215     {
    216       $exif = get_sync_exif_data($high_file);
    217     }
    218     $data = array_merge($data, $exif);
    219   }
    220 
    221   if ($conf['use_iptc'])
    222   {
    223     $iptc = get_sync_iptc_data($file);
    224     if (count($iptc) == 0 and isset($data['high_filesize']))
    225     {
    226       $iptc = get_sync_iptc_data($high_file);
    227     }
    228     $data = array_merge($data, $iptc);
    229   }
    230 
    231   return $data;
     168  return get_sync_metadata($infos);
    232169}
    233170
     
    249186}
    250187
    251 function get_tn_ext($path, $filename_wo_ext)
    252 {
    253   global $conf;
    254 
    255   $base_test =
    256     $path.'/thumbnail/'.$conf['prefix_thumbnail'].$filename_wo_ext.'.';
    257 
    258   foreach ($conf['picture_ext'] as $ext)
    259   {
    260     $test = $base_test.$ext;
    261     if (is_file($test))
    262     {
    263       return $ext;
    264     }
    265   }
    266 
    267   return null;
    268 }
    269 
    270 function get_has_high($path, $filename)
    271 {
    272   if (is_file($path.'/pwg_high/'.$filename))
    273   {
    274     return 'true';
    275   }
    276 
    277   return null;
    278 }
    279188
    280189}
  • trunk/admin/site_update.php

    r12681 r12831  
    8181if ($site_is_remote)
    8282{
     83  fatal_error('remote sites not supported');
    8384  include_once(PHPWG_ROOT_PATH.'admin/site_reader_remote.php');
    8485  $local_listing = null;
     
    99100if (isset($_POST['submit']))
    100101{
    101   if (!isset($conf['flip_picture_ext']))
    102   {
    103     $conf['flip_picture_ext'] = array_flip($conf['picture_ext']);
    104   }
    105102  if ($site_reader->open())
    106103  {
     
    313310        );
    314311      mass_inserts(CATEGORIES_TABLE, $dbfields, $inserts);
    315      
     312
    316313      // add default permissions to categories
    317314      $category_ids = array();
     
    322319      add_permission_on_category($category_ids, get_admins());
    323320    }
    324    
     321
    325322    $counts['new_categories'] = count($inserts);
    326323  }
     
    374371      .wordwrap(
    375372        implode(', ', $cat_ids),
    376         80,
     373        160,
    377374        "\n"
    378375        ).')';
     
    411408    }
    412409
    413     if ( isset( $conf['flip_picture_ext'][get_extension($filename)] )
    414           and !isset($fs[$path]['tn_ext']) )
    415     { // For a picture thumbnail is mandatory and for non picture element,
    416       // thumbnail and representative are optionnal
    417       array_push(
    418         $errors,
    419         array(
    420           'path' => $path,
    421           'type' => 'PWG-UPDATE-2'
    422           )
    423         );
    424     }
    425     else
    426     {
    427       $insert = array(
    428         'id'             => $next_element_id++,
    429         'file'           => $filename,
    430         'date_available' => CURRENT_DATE,
    431         'path'           => $path,
    432         'tn_ext'         => isset($fs[$path]['tn_ext'])
    433           ? $fs[$path]['tn_ext']
    434           : null,
    435         'storage_category_id' => $db_fulldirs[$dirname],
    436         'added_by' => $user['id'],
    437         );
    438 
    439       if ( $_POST['privacy_level']!=0 )
    440       {
    441         $insert['level'] = $_POST['privacy_level'];
    442       }
    443 
    444       array_push(
    445         $inserts,
    446         $insert
    447         );
    448 
    449       array_push(
    450         $insert_links,
    451         array(
    452           'image_id'    => $insert['id'],
    453           'category_id' => $insert['storage_category_id'],
    454           )
    455         );
    456 
    457       array_push(
    458         $infos,
    459         array(
    460           'path' => $insert['path'],
    461           'info' => l10n('added')
    462           )
    463         );
    464 
    465       $caddiables[] = $insert['id'];
    466     }
     410    $insert = array(
     411      'id'             => $next_element_id++,
     412      'file'           => $filename,
     413      'date_available' => CURRENT_DATE,
     414      'path'           => $path,
     415      'representative_ext'  => $fs[$path]['representative_ext'],
     416      'storage_category_id' => $db_fulldirs[$dirname],
     417      'added_by'       => $user['id'],
     418      );
     419
     420    if ( $_POST['privacy_level']!=0 )
     421    {
     422      $insert['level'] = $_POST['privacy_level'];
     423    }
     424
     425    array_push(
     426      $inserts,
     427      $insert
     428      );
     429
     430    array_push(
     431      $insert_links,
     432      array(
     433        'image_id'    => $insert['id'],
     434        'category_id' => $insert['storage_category_id'],
     435        )
     436      );
     437
     438    array_push(
     439      $infos,
     440      array(
     441        'path' => $insert['path'],
     442        'info' => l10n('added')
     443        )
     444      );
     445
     446    $caddiables[] = $insert['id'];
    467447  }
    468448
     
    561541    foreach ( $files as $id=>$file )
    562542    {
     543      $file = $file['path'];
    563544      $data = $site_reader->get_element_update_attributes($file);
    564545      if ( !is_array($data) )
    565546      {
    566547        continue;
    567       }
    568       $extension = get_extension($file);
    569       if ( isset($conf['flip_picture_ext'][$extension]) )
    570       {
    571         if ( !isset($data['tn_ext']) )
    572         {
    573           array_push(
    574             $errors,
    575             array(
    576               'path' => $file,
    577               'type' => 'PWG-UPDATE-2'
    578               )
    579             );
    580           continue;
    581         }
    582548      }
    583549
     
    656622  $tags_of = array();
    657623
    658   $has_high_images = array();
    659 
    660   $image_ids = array();
    661   foreach ($files as $id => $file)
    662   {
    663     array_push($image_ids, $id);
    664   }
    665 
    666   if (count($image_ids) > 0)
    667   {
    668     $query = '
    669 SELECT id
    670   FROM '.IMAGES_TABLE.'
    671   WHERE has_high = \'true\'
    672     AND id IN (
    673 '.wordwrap(implode(', ', $image_ids), 80, "\n").'
    674 )';
    675     $has_high_images = array_from_query($query, 'id' );
    676   }
    677 
    678   foreach ( $files as $id=>$file )
    679   {
    680     $data = $site_reader->get_element_metadata(
    681       $file,
    682       in_array($id, $has_high_images)
    683       );
     624  foreach ( $files as $id => $element_infos )
     625  {
     626    $data = $site_reader->get_element_metadata($element_infos);
    684627
    685628    if ( is_array($data) )
     
    710653    else
    711654    {
    712       array_push($errors, array('path' => $file, 'type' => 'PWG-ERROR-NO-FS'));
     655      array_push($errors, array('path' => $element_infos['path'], 'type' => 'PWG-ERROR-NO-FS'));
    713656    }
    714657  }
     
    818761      'meta_empty_overrides'  => false,
    819762    );
    820  
     763
    821764  $cat_selected = array();
    822765
  • trunk/include/derivative.inc.php

    r12797 r12831  
    4343    elseif (!empty($infos['representative_ext']))
    4444    {
    45       $pi = pathinfo($infos['path']);
    46       $file_wo_ext = get_filename_wo_extension($pi['basename']);
    47       $this->rel_path = $pi['dirname'].'/pwg_representative/'
    48         .$file_wo_ext.'.'.$infos['representative_ext'];
     45      $this->rel_path = original_to_representative($infos['path'], $infos['representative_ext']);
    4946    }
    5047    else
  • trunk/include/functions.inc.php

    r12798 r12831  
    732732}
    733733
     734/** Transforms an original path to its pwg representative */
     735function original_to_representative($path, $representative_ext)
     736{
     737  $pos = strrpos($path, '/');
     738  $path = substr_replace($path, 'pwg_representative/', $pos+1, 0);
     739  $pos = strrpos($path, '.');
     740  return substr_replace($path, $representative_ext, $pos+1);
     741}
     742
     743
    734744/* Returns the PATH to the thumbnail to be displayed. If the element does not
    735745 * have a thumbnail, the default mime image path is returned. The PATH can be
  • trunk/include/ws_functions.inc.php

    r12810 r12831  
    17421742    // update metadata from the uploaded file (exif/iptc)
    17431743    require_once(PHPWG_ROOT_PATH.'admin/include/functions_metadata.php');
    1744     update_metadata(array($image_id=>$file_path));
     1744    sync_metadata(array($image_id));
    17451745  }
    17461746
     
    19261926  // update metadata from the uploaded file (exif/iptc), even if the sync
    19271927  // was already performed by add_uploaded_file().
    1928   $query = '
    1929 SELECT
    1930     path
    1931   FROM '.IMAGES_TABLE.'
    1932   WHERE id = '.$image_id.'
    1933 ;';
    1934   list($file_path) = pwg_db_fetch_row(pwg_query($query));
    19351928
    19361929  require_once(PHPWG_ROOT_PATH.'admin/include/functions_metadata.php');
    1937   update_metadata(array($image_id=>$file_path));
     1930  sync_metadata(array($image_id));
    19381931
    19391932  return array(
     
    33293322  $conf['use_exif'] = false;
    33303323  $conf['use_iptc'] = false;
    3331   update_metadata(array($image['id'] => $image['path']));
     3324  sync_metadata(array($image['id']));
    33323325
    33333326  return $result;
Note: See TracChangeset for help on using the changeset viewer.