feature 2548 multisize

- rewrote local site sync + metadata sync

git-svn-id: http://piwigo.org/svn/trunk@12831 68402e56-0260-453c-a942-63ccdbb3a9ee
This commit is contained in:
rvelices 2012-01-03 20:21:13 +00:00
parent 6c3ff240cb
commit d0b5df605c
9 changed files with 187 additions and 358 deletions

View file

@ -382,19 +382,7 @@ DELETE
// synchronize metadata // synchronize metadata
if ('metadata' == $action) if ('metadata' == $action)
{ {
$query = ' sync_metadata($collection);
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);
array_push( array_push(
$page['infos'], $page['infos'],

View file

@ -48,7 +48,7 @@ function get_sync_iptc_data($file)
$month = 1; $month = 1;
$day = 1; $day = 1;
} }
$iptc[$pwg_key] = $year.'-'.$month.'-'.$day; $iptc[$pwg_key] = $year.'-'.$month.'-'.$day;
} }
} }
@ -109,7 +109,75 @@ function get_sync_exif_data($file)
return $exif; return $exif;
} }
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; global $conf;
@ -120,82 +188,40 @@ function update_metadata($files)
$datas = array(); $datas = array();
$tags_of = array(); $tags_of = array();
$has_high_images = array();
$image_ids = array();
foreach ($files as $id => $file)
{
array_push($image_ids, $id);
}
$query = ' $query = '
SELECT id SELECT id, path, representative_ext
FROM '.IMAGES_TABLE.' FROM '.IMAGES_TABLE.'
WHERE has_high = \'true\' WHERE id IN (
AND id IN ( '.wordwrap(implode(', ', $ids), 160, "\n").'
'.wordwrap(implode(', ', $image_ids), 80, "\n").'
) )
;'; ;';
$has_high_images = array_from_query($query, 'id'); $result = pwg_query($query);
while ($data = pwg_db_fetch_assoc($result))
foreach ($files as $id => $file)
{ {
$data = array(); $data = get_sync_metadata($data);
$data['id'] = $id; if ($data === false)
$data['filesize'] = floor(filesize($file)/1024);
if ($image_size = @getimagesize($file))
{ {
$data['width'] = $image_size[0]; continue;
$data['height'] = $image_size[1];
} }
if (in_array($id, $has_high_images)) $id = $data['id'];
foreach (array('keywords', 'tags') as $key)
{ {
$high_file = dirname($file).'/pwg_high/'.basename($file); if (isset($data[$key]))
$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); if (!isset($tags_of[$id]))
}
$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)
{ {
if ($key == 'keywords' or $key == 'tags') $tags_of[$id] = array();
{ }
if (!isset($tags_of[$id]))
{
$tags_of[$id] = array();
}
foreach (explode(',', $iptc[$key]) as $tag_name) foreach (explode(',', $data[$key]) as $tag_name)
{ {
array_push( array_push(
$tags_of[$id], $tags_of[$id],
tag_id_from_tag_name($tag_name) tag_id_from_tag_name($tag_name)
); );
}
}
} }
} }
} }
@ -207,41 +233,19 @@ SELECT id
if (count($datas) > 0) if (count($datas) > 0)
{ {
$update_fields = $update_fields = get_sync_metadata_attributes();
array( array_push($update_fields, 'date_metadata_update');
'filesize',
'width',
'height',
'high_filesize',
'date_metadata_update'
);
if ($conf['use_exif']) $update_fields = array_diff(
{ $update_fields,
$update_fields = array('tags', 'keywords')
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')
)
);
}
mass_updates( mass_updates(
IMAGES_TABLE, IMAGES_TABLE,
array( array(
'primary' => array('id'), 'primary' => array('id'),
'update' => array_unique($update_fields) 'update' => $update_fields
), ),
$datas, $datas,
MASS_UPDATES_SKIP_EMPTY MASS_UPDATES_SKIP_EMPTY
@ -300,10 +304,8 @@ SELECT id
return array(); return array();
} }
$files = array();
$query = ' $query = '
SELECT id, path SELECT id, path, representative_ext
FROM '.IMAGES_TABLE.' FROM '.IMAGES_TABLE.'
WHERE storage_category_id IN ('.implode(',', $cat_ids).')'; WHERE storage_category_id IN ('.implode(',', $cat_ids).')';
if ($only_new) if ($only_new)
@ -314,12 +316,6 @@ SELECT id, path
} }
$query.= ' $query.= '
;'; ;';
$result = pwg_query($query); return hash_from_query($query, 'id');
while ($row = pwg_db_fetch_assoc($result))
{
$files[$row['id']] = $row['path'];
}
return $files;
} }
?> ?>

View file

@ -480,7 +480,7 @@ SELECT
{ {
$conf['use_exif'] = false; $conf['use_exif'] = false;
} }
update_metadata(array($image_id=>$file_path)); sync_metadata(array($image_id));
invalidate_user_cache(); invalidate_user_cache();

View file

@ -94,14 +94,7 @@ SELECT category_id
if (isset($_GET['sync_metadata'])) if (isset($_GET['sync_metadata']))
{ {
$query = ' sync_metadata(array( intval($_GET['image_id'])));
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));
array_push($page['infos'], l10n('Metadata synchronized from file')); array_push($page['infos'], l10n('Metadata synchronized from file'));
} }

View file

@ -30,6 +30,15 @@ var $site_url;
function LocalSiteReader($url) function LocalSiteReader($url)
{ {
$this->site_url = $url; $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']);
}
} }
/** /**
@ -68,15 +77,11 @@ function get_full_directories($basedir)
* Returns an array with all file system files according to $conf['file_ext'] * Returns an array with all file system files according to $conf['file_ext']
* and $conf['picture_ext'] * and $conf['picture_ext']
* @param string $path recurse in this directory * @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) function get_elements($path)
{ {
global $conf; global $conf;
if (!isset($conf['flip_file_ext']))
{
$conf['flip_file_ext'] = array_flip($conf['file_ext']);
}
$subdirs = array(); $subdirs = array();
$fs = array(); $fs = array();
@ -93,9 +98,13 @@ function get_elements($path)
if ( isset($conf['flip_file_ext'][$extension]) ) 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( $fs[ $path.'/'.$node ] = array(
'tn_ext' => $tn_ext, 'representative_ext' => $representative_ext,
); );
} }
} }
@ -123,7 +132,7 @@ function get_elements($path)
// files update/synchronization // files update/synchronization
function get_update_attributes() function get_update_attributes()
{ {
return array('tn_ext', 'has_high', 'representative_ext'); return array('representative_ext');
} }
function get_element_update_attributes($file) function get_element_update_attributes($file)
@ -132,19 +141,17 @@ function get_element_update_attributes($file)
$data = array(); $data = array();
$filename = basename($file); $filename = basename($file);
$dirname = dirname($file);
$filename_wo_ext = get_filename_wo_extension($filename);
$extension = get_extension($filename); $extension = get_extension($filename);
$data['tn_ext'] = $this->get_tn_ext($dirname, $filename_wo_ext); $representative_ext = null;
$data['has_high'] = $this->get_has_high($dirname, $filename); if (! isset($conf['flip_picture_ext'][$extension]) )
if ( !isset($conf['flip_picture_ext'][$extension]) )
{ {
$data['representative_ext'] = $this->get_representative_ext( $dirname = dirname($file);
$dirname, $filename_wo_ext $filename_wo_ext = get_filename_wo_extension($filename);
); $representative_ext = $this->get_representative_ext($dirname, $filename_wo_ext);
} }
$data['representative_ext'] = $representative_ext;
return $data; return $data;
} }
@ -152,83 +159,13 @@ function get_element_update_attributes($file)
// metadata update/synchronization according to configuration // metadata update/synchronization according to configuration
function get_metadata_attributes() function get_metadata_attributes()
{ {
global $conf; return get_sync_metadata_attributes();
$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;
} }
// returns a hash of attributes (metadata+filesize+width,...) for file // 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; return get_sync_metadata($infos);
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;
} }
@ -248,34 +185,6 @@ function get_representative_ext($path, $filename_wo_ext)
return null; return null;
} }
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;
}
} }
?> ?>

View file

@ -80,6 +80,7 @@ $infos = array();
if ($site_is_remote) if ($site_is_remote)
{ {
fatal_error('remote sites not supported');
include_once(PHPWG_ROOT_PATH.'admin/site_reader_remote.php'); include_once(PHPWG_ROOT_PATH.'admin/site_reader_remote.php');
$local_listing = null; $local_listing = null;
if ( isset($_GET['local_listing']) if ( isset($_GET['local_listing'])
@ -98,10 +99,6 @@ else
$general_failure = true; $general_failure = true;
if (isset($_POST['submit'])) if (isset($_POST['submit']))
{ {
if (!isset($conf['flip_picture_ext']))
{
$conf['flip_picture_ext'] = array_flip($conf['picture_ext']);
}
if ($site_reader->open()) if ($site_reader->open())
{ {
$general_failure = false; $general_failure = false;
@ -312,7 +309,7 @@ SELECT id_uppercat, MAX(rank)+1 AS next_rank
'visible','status','rank','global_rank' 'visible','status','rank','global_rank'
); );
mass_inserts(CATEGORIES_TABLE, $dbfields, $inserts); mass_inserts(CATEGORIES_TABLE, $dbfields, $inserts);
// add default permissions to categories // add default permissions to categories
$category_ids = array(); $category_ids = array();
foreach ($inserts as $category) foreach ($inserts as $category)
@ -321,7 +318,7 @@ SELECT id_uppercat, MAX(rank)+1 AS next_rank
} }
add_permission_on_category($category_ids, get_admins()); add_permission_on_category($category_ids, get_admins());
} }
$counts['new_categories'] = count($inserts); $counts['new_categories'] = count($inserts);
} }
@ -373,7 +370,7 @@ SELECT id, path
WHERE storage_category_id IN (' WHERE storage_category_id IN ('
.wordwrap( .wordwrap(
implode(', ', $cat_ids), implode(', ', $cat_ids),
80, 160,
"\n" "\n"
).')'; ).')';
$db_elements = simple_hash_from_query($query, 'id', 'path'); $db_elements = simple_hash_from_query($query, 'id', 'path');
@ -410,60 +407,43 @@ SELECT id, path
continue; continue;
} }
if ( isset( $conf['flip_picture_ext'][get_extension($filename)] ) $insert = array(
and !isset($fs[$path]['tn_ext']) ) 'id' => $next_element_id++,
{ // For a picture thumbnail is mandatory and for non picture element, 'file' => $filename,
// thumbnail and representative are optionnal 'date_available' => CURRENT_DATE,
array_push( 'path' => $path,
$errors, 'representative_ext' => $fs[$path]['representative_ext'],
array( 'storage_category_id' => $db_fulldirs[$dirname],
'path' => $path, 'added_by' => $user['id'],
'type' => 'PWG-UPDATE-2' );
)
); if ( $_POST['privacy_level']!=0 )
}
else
{ {
$insert = array( $insert['level'] = $_POST['privacy_level'];
'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'];
} }
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'];
} }
if (count($inserts) > 0) if (count($inserts) > 0)
@ -560,26 +540,12 @@ if (isset($_POST['submit'])
$datas = array(); $datas = array();
foreach ( $files as $id=>$file ) foreach ( $files as $id=>$file )
{ {
$file = $file['path'];
$data = $site_reader->get_element_update_attributes($file); $data = $site_reader->get_element_update_attributes($file);
if ( !is_array($data) ) if ( !is_array($data) )
{ {
continue; 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;
}
}
$data['id']=$id; $data['id']=$id;
array_push($datas, $data); array_push($datas, $data);
@ -655,32 +621,9 @@ if (isset($_POST['submit']) and isset($_POST['sync_meta'])
$datas = array(); $datas = array();
$tags_of = array(); $tags_of = array();
$has_high_images = array(); foreach ( $files as $id => $element_infos )
$image_ids = array();
foreach ($files as $id => $file)
{ {
array_push($image_ids, $id); $data = $site_reader->get_element_metadata($element_infos);
}
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)
);
if ( is_array($data) ) if ( is_array($data) )
{ {
@ -709,7 +652,7 @@ SELECT id
} }
else 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'));
} }
} }
@ -817,7 +760,7 @@ else
'meta_all' => false, 'meta_all' => false,
'meta_empty_overrides' => false, 'meta_empty_overrides' => false,
); );
$cat_selected = array(); $cat_selected = array();
if (isset($_GET['cat_id'])) if (isset($_GET['cat_id']))

View file

@ -42,10 +42,7 @@ final class SrcImage
} }
elseif (!empty($infos['representative_ext'])) elseif (!empty($infos['representative_ext']))
{ {
$pi = pathinfo($infos['path']); $this->rel_path = original_to_representative($infos['path'], $infos['representative_ext']);
$file_wo_ext = get_filename_wo_extension($pi['basename']);
$this->rel_path = $pi['dirname'].'/pwg_representative/'
.$file_wo_ext.'.'.$infos['representative_ext'];
} }
else else
{ {

View file

@ -731,6 +731,16 @@ function check_theme_installed($theme_id)
return file_exists($conf['themes_dir'].'/'.$theme_id.'/'.'themeconf.inc.php'); return file_exists($conf['themes_dir'].'/'.$theme_id.'/'.'themeconf.inc.php');
} }
/** Transforms an original path to its pwg representative */
function original_to_representative($path, $representative_ext)
{
$pos = strrpos($path, '/');
$path = substr_replace($path, 'pwg_representative/', $pos+1, 0);
$pos = strrpos($path, '.');
return substr_replace($path, $representative_ext, $pos+1);
}
/* Returns the PATH to the thumbnail to be displayed. If the element does not /* Returns the PATH to the thumbnail to be displayed. If the element does not
* have a thumbnail, the default mime image path is returned. The PATH can be * have a thumbnail, the default mime image path is returned. The PATH can be
* used in the php script, but not sent to the browser. * used in the php script, but not sent to the browser.

View file

@ -1741,7 +1741,7 @@ SELECT
// update metadata from the uploaded file (exif/iptc) // update metadata from the uploaded file (exif/iptc)
require_once(PHPWG_ROOT_PATH.'admin/include/functions_metadata.php'); require_once(PHPWG_ROOT_PATH.'admin/include/functions_metadata.php');
update_metadata(array($image_id=>$file_path)); sync_metadata(array($image_id));
} }
$info_columns = array( $info_columns = array(
@ -1925,16 +1925,9 @@ SELECT id, name, permalink
// update metadata from the uploaded file (exif/iptc), even if the sync // update metadata from the uploaded file (exif/iptc), even if the sync
// was already performed by add_uploaded_file(). // was already performed by add_uploaded_file().
$query = '
SELECT
path
FROM '.IMAGES_TABLE.'
WHERE id = '.$image_id.'
;';
list($file_path) = pwg_db_fetch_row(pwg_query($query));
require_once(PHPWG_ROOT_PATH.'admin/include/functions_metadata.php'); require_once(PHPWG_ROOT_PATH.'admin/include/functions_metadata.php');
update_metadata(array($image_id=>$file_path)); sync_metadata(array($image_id));
return array( return array(
'image_id' => $image_id, 'image_id' => $image_id,
@ -3328,7 +3321,7 @@ SELECT id, path, tn_ext, has_high, width, height
global $conf; global $conf;
$conf['use_exif'] = false; $conf['use_exif'] = false;
$conf['use_iptc'] = false; $conf['use_iptc'] = false;
update_metadata(array($image['id'] => $image['path'])); sync_metadata(array($image['id']));
return $result; return $result;
} }