From 8e098d502a8f0e413b9c085db27d6a62a6c0909f Mon Sep 17 00:00:00 2001 From: plegall Date: Thu, 3 Dec 2015 16:04:02 +0100 Subject: feature #379 multiple format, step 1: add formats * new table piwigo_image_format (each photo can have 0 to many formats) * only compatible with synchronization for now. Formats must be in sub-directory pwg_format * formats are visible on edition page only for now --- admin/include/functions.php | 35 ++++++++++++++++++++++++ admin/picture_modify.php | 19 +++++++++++++ admin/site_reader_local.php | 31 +++++++++++++++++++++ admin/site_update.php | 28 +++++++++++++++++++ admin/themes/default/template/picture_modify.tpl | 1 + 5 files changed, 114 insertions(+) (limited to 'admin') diff --git a/admin/include/functions.php b/admin/include/functions.php index 5cd44b31d..903267153 100644 --- a/admin/include/functions.php +++ b/admin/include/functions.php @@ -180,6 +180,25 @@ function delete_element_files($ids) } $new_ids = array(); + $formats_of = array(); + + $query = ' +SELECT + image_id, + ext + FROM '.IMAGE_FORMAT_TABLE.' + WHERE image_id IN ('.implode(',', $ids).') +;'; + $result = pwg_query($query); + while ($row = pwg_db_fetch_assoc($result)) + { + if (!isset($formats_of[ $row['image_id'] ])) + { + $formats_of[ $row['image_id'] ] = array(); + } + + $formats_of[ $row['image_id'] ][] = $row['ext']; + } $query = ' SELECT @@ -205,6 +224,14 @@ SELECT $files[] = original_to_representative( $files[0], $row['representative_ext']); } + if (isset($formats_of[ $row['id'] ])) + { + foreach ($formats_of[ $row['id'] ] as $format_ext) + { + $files[] = original_to_format($files[0], $format_ext); + } + } + $ok = true; if (!isset($conf['never_delete_originals'])) { @@ -277,6 +304,13 @@ DELETE FROM '.IMAGE_CATEGORY_TABLE.' ;'; pwg_query($query); + // destruction of the formats + $query = ' +DELETE FROM '.IMAGE_FORMAT_TABLE.' + WHERE image_id IN ('. $ids_str .') +;'; + pwg_query($query); + // destruction of the links between images and tags $query = ' DELETE FROM '.IMAGE_TAG_TABLE.' @@ -540,6 +574,7 @@ function get_fs_directories($path, $recursive = true) '.', '..', '.svn', 'thumbnail', 'pwg_high', 'pwg_representative', + 'pwg_format', ) ); $exclude_folders = array_flip($exclude_folders); diff --git a/admin/picture_modify.php b/admin/picture_modify.php index bf0e458e6..cdb7ccbbe 100644 --- a/admin/picture_modify.php +++ b/admin/picture_modify.php @@ -306,6 +306,25 @@ SELECT $intro_vars['stats'].= ', '.sprintf(l10n('Rated %d times, score : %.2f'), $row['nb_rates'], $row['rating_score']); } +$query = ' +SELECT * + FROM '.IMAGE_FORMAT_TABLE.' + WHERE image_id = '.$row['id'].' +;'; +$formats = query2array($query); + +if (!empty($formats)) +{ + $format_strings = array(); + + foreach ($formats as $format) + { + $format_strings[] = sprintf('%s (%.2fMB)', $format['ext'], $format['filesize']/1024); + } + + $intro_vars['formats'] = l10n('Formats: %s', implode(', ', $format_strings)); +} + $template->assign('INTRO', $intro_vars); diff --git a/admin/site_reader_local.php b/admin/site_reader_local.php index 7e618ca17..051e574a6 100644 --- a/admin/site_reader_local.php +++ b/admin/site_reader_local.php @@ -100,14 +100,19 @@ function get_elements($path) { $representative_ext = $this->get_representative_ext($path, $filename_wo_ext); } + + $formats = $this->get_formats($path, $filename_wo_ext); + $fs[ $path.'/'.$node ] = array( 'representative_ext' => $representative_ext, + 'formats' => $formats, ); } } else if (is_dir($path.'/'.$node) and $node != 'pwg_high' and $node != 'pwg_representative' + and $node != 'pwg_format' and $node != 'thumbnail' ) { $subdirs[] = $node; @@ -182,6 +187,32 @@ function get_representative_ext($path, $filename_wo_ext) return null; } +function get_formats($path, $filename_wo_ext) +{ + global $conf; + + $formats = array(); + + $base_test = $path.'/pwg_format/'.$filename_wo_ext.'.'; + + foreach ($conf['format_ext'] as $ext) + { + $test = $base_test.$ext; + + if (is_file($test)) + { + // $formats[] = array( + // 'ext' => $ext, + // 'filesize' => floor(filesize($file) / 1024), + // ); + + // we return a "/" splitted string instead of an array with 2 keys, to reduce memory usage + $formats[] = $ext.'/'.floor(filesize($test) / 1024); + } + } + + return $formats; +} } ?> \ No newline at end of file diff --git a/admin/site_update.php b/admin/site_update.php index 5bcaea12a..3a12965af 100644 --- a/admin/site_update.php +++ b/admin/site_update.php @@ -457,6 +457,7 @@ if (isset($_POST['submit']) and $_POST['sync'] == 'files' $start= $start_files; $fs = $site_reader->get_elements($basedir); + $template->append('footer_elements', '' ); @@ -486,6 +487,7 @@ SELECT id, path $inserts = array(); $insert_links = array(); + $insert_formats = array(); foreach (array_diff(array_keys($fs), $db_elements) as $path) { @@ -535,6 +537,22 @@ SELECT id, path 'info' => l10n('added') ); + foreach ($fs[$path]['formats'] as $format) + { + list($ext, $filesize) = explode('/', $format); + + $insert_formats[] = array( + 'image_id' => $insert['id'], + 'ext' => $ext, + 'filesize' => $filesize, + ); + + $infos[] = array( + 'path' => $insert['path'], + 'info' => l10n('format %s added', $ext) + ); + } + $caddiables[] = $insert['id']; } @@ -555,6 +573,16 @@ SELECT id, path array_keys($insert_links[0]), $insert_links ); + + // inserts all formats + if (count($insert_formats) > 0) + { + mass_inserts( + IMAGE_FORMAT_TABLE, + array_keys($insert_formats[0]), + $insert_formats + ); + } // add new photos to caddie if (isset($_POST['add_to_caddie']) and $_POST['add_to_caddie'] == 1) diff --git a/admin/themes/default/template/picture_modify.tpl b/admin/themes/default/template/picture_modify.tpl index aeba2dd6f..71b3d2ba4 100644 --- a/admin/themes/default/template/picture_modify.tpl +++ b/admin/themes/default/template/picture_modify.tpl @@ -63,6 +63,7 @@ jQuery("a.preview-box").colorbox({
  • {$INTRO.add_date}
  • {$INTRO.added_by}
  • {$INTRO.size}
  • +
  • {$INTRO.formats}
  • {$INTRO.stats}
  • {$INTRO.id}
  • -- cgit v1.2.3 From ca238de66d4b6801c4cd817c27effcf18a211da2 Mon Sep 17 00:00:00 2001 From: plegall Date: Wed, 16 Dec 2015 18:16:16 +0100 Subject: feature #379 multiple format, step 3: add/remove * during sync, Piwigo will detect new/removed formats for an existing photo * multiple formats features is disabled by default --- admin/site_reader_local.php | 20 +++--- admin/site_update.php | 145 ++++++++++++++++++++++++++++++++++---------- 2 files changed, 121 insertions(+), 44 deletions(-) (limited to 'admin') diff --git a/admin/site_reader_local.php b/admin/site_reader_local.php index 051e574a6..69f644467 100644 --- a/admin/site_reader_local.php +++ b/admin/site_reader_local.php @@ -101,12 +101,12 @@ function get_elements($path) $representative_ext = $this->get_representative_ext($path, $filename_wo_ext); } - $formats = $this->get_formats($path, $filename_wo_ext); - - $fs[ $path.'/'.$node ] = array( - 'representative_ext' => $representative_ext, - 'formats' => $formats, - ); + $fs[ $path.'/'.$node ] = array('representative_ext' => $representative_ext); + + if ($conf['enable_formats']) + { + $fs[ $path.'/'.$node ]['formats'] = $this->get_formats($path, $filename_wo_ext); + } } } else if (is_dir($path.'/'.$node) @@ -201,13 +201,7 @@ function get_formats($path, $filename_wo_ext) if (is_file($test)) { - // $formats[] = array( - // 'ext' => $ext, - // 'filesize' => floor(filesize($file) / 1024), - // ); - - // we return a "/" splitted string instead of an array with 2 keys, to reduce memory usage - $formats[] = $ext.'/'.floor(filesize($test) / 1024); + $formats[$ext] = floor(filesize($test) / 1024); } } diff --git a/admin/site_update.php b/admin/site_update.php index 3a12965af..ae77960ae 100644 --- a/admin/site_update.php +++ b/admin/site_update.php @@ -537,62 +537,145 @@ SELECT id, path 'info' => l10n('added') ); - foreach ($fs[$path]['formats'] as $format) + if ($conf['enable_formats']) { - list($ext, $filesize) = explode('/', $format); - - $insert_formats[] = array( - 'image_id' => $insert['id'], - 'ext' => $ext, - 'filesize' => $filesize, - ); + foreach ($fs[$path]['formats'] as $ext => $filesize) + { + $insert_formats[] = array( + 'image_id' => $insert['id'], + 'ext' => $ext, + 'filesize' => $filesize, + ); - $infos[] = array( - 'path' => $insert['path'], - 'info' => l10n('format %s added', $ext) - ); + $infos[] = array( + 'path' => $insert['path'], + 'info' => l10n('format %s added', $ext) + ); + } } $caddiables[] = $insert['id']; } - if (count($inserts) > 0) + // search new/removed formats on photos already registered in database + if ($conf['enable_formats']) { - if (!$simulate) + $db_elements_flip = array_flip($db_elements); + + $existing_ids = array(); + + foreach (array_intersect_key($fs, $db_elements_flip) as $path => $existing) + { + $existing_ids[] = $db_elements_flip[$path]; + } + + $logger->debug('existing_ids', 'sync', $existing_ids); + + if (count($existing_ids) > 0) + { + $db_formats = array(); + + // find formats for existing photos + $query = ' +SELECT * + FROM '.IMAGE_FORMAT_TABLE.' + WHERE image_id IN ('.implode(',', $existing_ids).') +;'; + $result = pwg_query($query); + while ($row = pwg_db_fetch_assoc($result)) + { + if (!isset($db_formats[$row['image_id']])) + { + $db_formats[$row['image_id']] = array(); + } + + $db_formats[$row['image_id']][$row['ext']] = $row['format_id']; + } + + $formats_to_delete = array(); + + foreach ($db_formats as $image_id => $formats) + { + $image_formats_to_delete = array_diff_key($formats, $fs[ $db_elements[$image_id] ]['formats']); + $logger->debug('image_formats_to_delete', 'sync', $image_formats_to_delete); + foreach ($image_formats_to_delete as $ext => $format_id) + { + $formats_to_delete[] = $format_id; + + $infos[] = array( + 'path' => $db_elements[$image_id], + 'info' => l10n('format %s removed', $ext) + ); + } + + $image_formats_to_insert = array_diff_key($fs[ $db_elements[$image_id] ]['formats'], $formats); + $logger->debug('image_formats_to_insert', 'sync', $image_formats_to_insert); + foreach ($image_formats_to_insert as $ext => $filesize) + { + $insert_formats[] = array( + 'image_id' => $image_id, + 'ext' => $ext, + 'filesize' => $filesize, + ); + + $infos[] = array( + 'path' => $db_elements[$image_id], + 'info' => l10n('format %s added', $ext) + ); + } + } + } + } + + + if (!$simulate) + { + // inserts all new elements + if (count($inserts) > 0) { - // inserts all new elements mass_inserts( IMAGES_TABLE, array_keys($inserts[0]), $inserts ); - + // inserts all links between new elements and their storage category mass_inserts( IMAGE_CATEGORY_TABLE, array_keys($insert_links[0]), $insert_links ); + } - // inserts all formats - if (count($insert_formats) > 0) - { - mass_inserts( - IMAGE_FORMAT_TABLE, - array_keys($insert_formats[0]), - $insert_formats - ); - } + // inserts all formats + if (count($insert_formats) > 0) + { + mass_inserts( + IMAGE_FORMAT_TABLE, + array_keys($insert_formats[0]), + $insert_formats + ); + } - // add new photos to caddie - if (isset($_POST['add_to_caddie']) and $_POST['add_to_caddie'] == 1) - { - fill_caddie($caddiables); - } + if (count($formats_to_delete) > 0) + { + $query = ' +DELETE + FROM '.IMAGE_FORMAT_TABLE.' + WHERE format_id IN ('.implode(',', $formats_to_delete).') +;'; + pwg_query($query); + } + + // add new photos to caddie + if (isset($_POST['add_to_caddie']) and $_POST['add_to_caddie'] == 1) + { + fill_caddie($caddiables); } - $counts['new_elements'] = count($inserts); } + $counts['new_elements'] = count($inserts); + // delete elements that are in database but not in the filesystem $to_delete_elements = array(); foreach (array_diff($db_elements, array_keys($fs)) as $path) -- cgit v1.2.3 From f66c27b731e5b6ed4d8c58fd89cf2156ebeef75a Mon Sep 17 00:00:00 2001 From: plegall Date: Thu, 17 Dec 2015 14:00:17 +0100 Subject: indentation --- admin/site_update.php | 80 +++++++++++++++++++++++++-------------------------- 1 file changed, 40 insertions(+), 40 deletions(-) (limited to 'admin') diff --git a/admin/site_update.php b/admin/site_update.php index ae77960ae..411a527c9 100644 --- a/admin/site_update.php +++ b/admin/site_update.php @@ -571,61 +571,61 @@ SELECT id, path $logger->debug('existing_ids', 'sync', $existing_ids); - if (count($existing_ids) > 0) - { - $db_formats = array(); + if (count($existing_ids) > 0) + { + $db_formats = array(); - // find formats for existing photos - $query = ' + // find formats for existing photos (already in database) + $query = ' SELECT * FROM '.IMAGE_FORMAT_TABLE.' WHERE image_id IN ('.implode(',', $existing_ids).') ;'; - $result = pwg_query($query); - while ($row = pwg_db_fetch_assoc($result)) - { - if (!isset($db_formats[$row['image_id']])) + $result = pwg_query($query); + while ($row = pwg_db_fetch_assoc($result)) { - $db_formats[$row['image_id']] = array(); + if (!isset($db_formats[$row['image_id']])) + { + $db_formats[$row['image_id']] = array(); + } + + $db_formats[$row['image_id']][$row['ext']] = $row['format_id']; } - $db_formats[$row['image_id']][$row['ext']] = $row['format_id']; - } - - $formats_to_delete = array(); + $formats_to_delete = array(); - foreach ($db_formats as $image_id => $formats) - { - $image_formats_to_delete = array_diff_key($formats, $fs[ $db_elements[$image_id] ]['formats']); - $logger->debug('image_formats_to_delete', 'sync', $image_formats_to_delete); - foreach ($image_formats_to_delete as $ext => $format_id) + foreach ($db_formats as $image_id => $formats) { - $formats_to_delete[] = $format_id; - - $infos[] = array( - 'path' => $db_elements[$image_id], - 'info' => l10n('format %s removed', $ext) - ); - } + $image_formats_to_delete = array_diff_key($formats, $fs[ $db_elements[$image_id] ]['formats']); + $logger->debug('image_formats_to_delete', 'sync', $image_formats_to_delete); + foreach ($image_formats_to_delete as $ext => $format_id) + { + $formats_to_delete[] = $format_id; + + $infos[] = array( + 'path' => $db_elements[$image_id], + 'info' => l10n('format %s removed', $ext) + ); + } - $image_formats_to_insert = array_diff_key($fs[ $db_elements[$image_id] ]['formats'], $formats); - $logger->debug('image_formats_to_insert', 'sync', $image_formats_to_insert); - foreach ($image_formats_to_insert as $ext => $filesize) - { - $insert_formats[] = array( - 'image_id' => $image_id, - 'ext' => $ext, - 'filesize' => $filesize, - ); + $image_formats_to_insert = array_diff_key($fs[ $db_elements[$image_id] ]['formats'], $formats); + $logger->debug('image_formats_to_insert', 'sync', $image_formats_to_insert); + foreach ($image_formats_to_insert as $ext => $filesize) + { + $insert_formats[] = array( + 'image_id' => $image_id, + 'ext' => $ext, + 'filesize' => $filesize, + ); - $infos[] = array( - 'path' => $db_elements[$image_id], - 'info' => l10n('format %s added', $ext) - ); + $infos[] = array( + 'path' => $db_elements[$image_id], + 'info' => l10n('format %s added', $ext) + ); + } } } } - } if (!$simulate) -- cgit v1.2.3 From 99b1fd270445c719d85aacc9e3a8e7add1cd5106 Mon Sep 17 00:00:00 2001 From: plegall Date: Wed, 23 Dec 2015 10:10:34 +0100 Subject: feature #379, multiple format, bug fixed on sync New formats were not detected if the photo had not already at least one format. --- admin/site_update.php | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'admin') diff --git a/admin/site_update.php b/admin/site_update.php index 411a527c9..2a70ae5d9 100644 --- a/admin/site_update.php +++ b/admin/site_update.php @@ -592,6 +592,7 @@ SELECT * $db_formats[$row['image_id']][$row['ext']] = $row['format_id']; } + // first we search the formats that were removed $formats_to_delete = array(); foreach ($db_formats as $image_id => $formats) @@ -607,8 +608,20 @@ SELECT * 'info' => l10n('format %s removed', $ext) ); } + } - $image_formats_to_insert = array_diff_key($fs[ $db_elements[$image_id] ]['formats'], $formats); + // then we search for new formats on existing photos + foreach ($existing_ids as $image_id) + { + $path = $db_elements[$image_id]; + + $formats = array(); + if (isset($db_formats[$image_id])) + { + $formats = $db_formats[$image_id]; + } + + $image_formats_to_insert = array_diff_key($fs[$path]['formats'], $formats); $logger->debug('image_formats_to_insert', 'sync', $image_formats_to_insert); foreach ($image_formats_to_insert as $ext => $filesize) { -- cgit v1.2.3