source: branches/2.4/admin/include/functions_metadata.php @ 16926

Last change on this file since 16926 was 13081, checked in by plg, 12 years ago

merge r13080 from branch 2.3 to trunk

bug 559 fixed: avoid duplicate insert on tags when synchronizing metadata if
the IPTC keywords contains repeated separators like "tag1tag2".

  • Property svn:eol-style set to LF
File size: 7.4 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based photo gallery                                    |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2012 Piwigo Team                  http://piwigo.org |
6// | Copyright(C) 2003-2008 PhpWebGallery Team    http://phpwebgallery.net |
7// | Copyright(C) 2002-2003 Pierrick LE GALL   http://le-gall.net/pierrick |
8// +-----------------------------------------------------------------------+
9// | This program is free software; you can redistribute it and/or modify  |
10// | it under the terms of the GNU General Public License as published by  |
11// | the Free Software Foundation                                          |
12// |                                                                       |
13// | This program is distributed in the hope that it will be useful, but   |
14// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
15// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
16// | General Public License for more details.                              |
17// |                                                                       |
18// | You should have received a copy of the GNU General Public License     |
19// | along with this program; if not, write to the Free Software           |
20// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
21// | USA.                                                                  |
22// +-----------------------------------------------------------------------+
23
24include_once(PHPWG_ROOT_PATH.'/include/functions_metadata.inc.php');
25
26
27function get_sync_iptc_data($file)
28{
29  global $conf;
30
31  $map = $conf['use_iptc_mapping'];
32
33  $iptc = get_iptc_data($file, $map);
34
35  foreach ($iptc as $pwg_key => $value)
36  {
37    if (in_array($pwg_key, array('date_creation', 'date_available')))
38    {
39      if (preg_match('/(\d{4})(\d{2})(\d{2})/', $value, $matches))
40      {
41        $year = $matches[1];
42        $month = $matches[2];
43        $day = $matches[3];
44
45        if (!checkdate($month, $day, $year))
46        {
47          // we suppose the year is correct
48          $month = 1;
49          $day = 1;
50        }
51
52        $iptc[$pwg_key] = $year.'-'.$month.'-'.$day;
53      }
54    }
55  }
56
57  if (isset($iptc['keywords']))
58  {
59    // official keywords separator is the comma
60    $iptc['keywords'] = preg_replace('/[.;]/', ',', $iptc['keywords']);
61    $iptc['keywords'] = preg_replace('/,+/', ',', $iptc['keywords']);
62    $iptc['keywords'] = preg_replace('/^,+|,+$/', '', $iptc['keywords']);
63
64    $iptc['keywords'] = implode(
65      ',',
66      array_unique(
67        explode(
68          ',',
69          $iptc['keywords']
70          )
71        )
72      );
73  }
74
75  foreach ($iptc as $pwg_key => $value)
76  {
77    $iptc[$pwg_key] = addslashes($iptc[$pwg_key]);
78  }
79
80  return $iptc;
81}
82
83function get_sync_exif_data($file)
84{
85  global $conf;
86
87  $exif = get_exif_data($file, $conf['use_exif_mapping']);
88
89  foreach ($exif as $pwg_key => $value)
90  {
91    if (in_array($pwg_key, array('date_creation', 'date_available')))
92    {
93      if (preg_match('/^(\d{4}).(\d{2}).(\d{2}) (\d{2}).(\d{2}).(\d{2})/', $value, $matches))
94      {
95        $exif[$pwg_key] = $matches[1].'-'.$matches[2].'-'.$matches[3].' '.$matches[4].':'.$matches[5].':'.$matches[6];
96      }
97      elseif (preg_match('/^(\d{4}).(\d{2}).(\d{2})/', $value, $matches))
98      {
99        $exif[$pwg_key] = $matches[1].'-'.$matches[2].'-'.$matches[3];
100      }
101      else
102      {
103        unset($exif[$pwg_key]);
104        continue;
105      }
106    }
107    $exif[$pwg_key] = addslashes($exif[$pwg_key]);
108  }
109
110  return $exif;
111}
112
113
114function get_sync_metadata_attributes()
115{
116  global $conf;
117
118  $update_fields = array('filesize', 'width', 'height');
119
120  if ($conf['use_exif'])
121  {
122    $update_fields =
123      array_merge(
124        $update_fields,
125        array_keys($conf['use_exif_mapping'])
126        );
127  }
128
129  if ($conf['use_iptc'])
130  {
131    $update_fields =
132      array_merge(
133        $update_fields,
134        array_keys($conf['use_iptc_mapping'])
135        );
136  }
137
138  return array_unique($update_fields);
139}
140
141function get_sync_metadata($infos)
142{
143  global $conf;
144  $file = PHPWG_ROOT_PATH.$infos['path'];
145  $fs = @filesize($file);
146
147  if ($fs===false)
148  {
149    return false;
150  }
151
152  $infos['filesize'] = floor($fs/1024);
153
154  if (isset($infos['representative_ext']))
155  {
156    $file = original_to_representative($file, $infos['representative_ext']);
157  }
158
159  if ($image_size = @getimagesize($file))
160  {
161    $infos['width'] = $image_size[0];
162    $infos['height'] = $image_size[1];
163  }
164
165  if ($conf['use_exif'])
166  {
167    $exif = get_sync_exif_data($file);
168    $infos = array_merge($infos, $exif);
169  }
170
171  if ($conf['use_iptc'])
172  {
173    $iptc = get_sync_iptc_data($file);
174    $infos = array_merge($infos, $iptc);
175  }
176
177  return $infos;
178}
179
180
181function sync_metadata($ids)
182{
183  global $conf;
184
185  if (!defined('CURRENT_DATE'))
186  {
187    define('CURRENT_DATE', date('Y-m-d'));
188  }
189
190  $datas = array();
191  $tags_of = array();
192
193  $query = '
194SELECT id, path, representative_ext
195  FROM '.IMAGES_TABLE.'
196  WHERE id IN (
197'.wordwrap(implode(', ', $ids), 160, "\n").'
198)
199;';
200
201  $result = pwg_query($query);
202  while ($data = pwg_db_fetch_assoc($result))
203  {
204    $data = get_sync_metadata($data);
205    if ($data === false)
206    {
207      continue;
208    }
209
210    $id = $data['id'];
211    foreach (array('keywords', 'tags') as $key)
212    {
213      if (isset($data[$key]))
214      {
215        if (!isset($tags_of[$id]))
216        {
217          $tags_of[$id] = array();
218        }
219
220        foreach (explode(',', $data[$key]) as $tag_name)
221        {
222          array_push(
223            $tags_of[$id],
224            tag_id_from_tag_name($tag_name)
225            );
226        }
227      }
228    }
229
230    $data['date_metadata_update'] = CURRENT_DATE;
231
232    array_push($datas, $data);
233  }
234
235  if (count($datas) > 0)
236  {
237    $update_fields = get_sync_metadata_attributes();
238    array_push($update_fields, 'date_metadata_update');
239
240    $update_fields = array_diff(
241      $update_fields,
242      array('tags', 'keywords')
243            );
244
245    mass_updates(
246      IMAGES_TABLE,
247      array(
248        'primary' => array('id'),
249        'update'  => $update_fields
250        ),
251      $datas,
252      MASS_UPDATES_SKIP_EMPTY
253      );
254  }
255
256  set_tags_of($tags_of);
257}
258
259/**
260 * returns an array associating element id (images.id) with its complete
261 * path in the filesystem
262 *
263 * @param int id_uppercat
264 * @param int site_id
265 * @param boolean recursive ?
266 * @param boolean only newly added files ?
267 * @return array
268 */
269function get_filelist($category_id = '', $site_id=1, $recursive = false,
270                      $only_new = false)
271{
272  // filling $cat_ids : all categories required
273  $cat_ids = array();
274
275  $query = '
276SELECT id
277  FROM '.CATEGORIES_TABLE.'
278  WHERE site_id = '.$site_id.'
279    AND dir IS NOT NULL';
280  if (is_numeric($category_id))
281  {
282    if ($recursive)
283    {
284      $query.= '
285    AND uppercats '.DB_REGEX_OPERATOR.' \'(^|,)'.$category_id.'(,|$)\'
286';
287    }
288    else
289    {
290      $query.= '
291    AND id = '.$category_id.'
292';
293    }
294  }
295  $query.= '
296;';
297  $result = pwg_query($query);
298  while ($row = pwg_db_fetch_assoc($result))
299  {
300    array_push($cat_ids, $row['id']);
301  }
302
303  if (count($cat_ids) == 0)
304  {
305    return array();
306  }
307
308  $query = '
309SELECT id, path, representative_ext
310  FROM '.IMAGES_TABLE.'
311  WHERE storage_category_id IN ('.implode(',', $cat_ids).')';
312  if ($only_new)
313  {
314    $query.= '
315    AND date_metadata_update IS NULL
316';
317  }
318  $query.= '
319;';
320  return hash_from_query($query, 'id');
321}
322?>
Note: See TracBrowser for help on using the repository browser.