aboutsummaryrefslogtreecommitdiffstats
path: root/admin/site_update.php
diff options
context:
space:
mode:
authorplegall <plg@piwigo.org>2015-12-30 16:08:54 +0100
committerplegall <plg@piwigo.org>2015-12-30 16:08:54 +0100
commit6ba0148e646b2a193dc4111bb0a443d8c193e646 (patch)
tree382ecd35afae3522d07377df332f0ecfee1f5045 /admin/site_update.php
parentf28420acfeb5d69e9ea39cd8d00cf9775d6cea54 (diff)
parent7b653c04d6cfb20366c3bb0e183a521b3c9d22d2 (diff)
Merge branch 'feature/379-multiple-format'
Diffstat (limited to 'admin/site_update.php')
-rw-r--r--admin/site_update.php144
1 files changed, 134 insertions, 10 deletions
diff --git a/admin/site_update.php b/admin/site_update.php
index 5bcaea12a..2a70ae5d9 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', '<!-- get_elements: '
. get_elapsed_time($start, get_moment())
. ' -->' );
@@ -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,36 +537,158 @@ SELECT id, path
'info' => l10n('added')
);
+ if ($conf['enable_formats'])
+ {
+ 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)
+ );
+ }
+ }
+
$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 (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']]))
+ {
+ $db_formats[$row['image_id']] = array();
+ }
+
+ $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)
+ {
+ $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)
+ );
+ }
+ }
+
+ // 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)
+ {
+ $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
+ );
+ }
- // 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)