source: trunk/admin/include/functions_metadata.php @ 4681

Last change on this file since 4681 was 4681, checked in by plg, 14 years ago

merge r4680 from branch 2.0 to trunk

bug 1380: now correctly synchronizes metadata on a single photo, needs a nicer
fix on trunk.

  • Property svn:eol-style set to LF
File size: 6.9 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based picture gallery                                  |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2009 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        $iptc[$pwg_key] = $matches[1].'-'.$matches[2].'-'.$matches[3];
42      }
43    }
44  }
45
46  if (isset($iptc['keywords']))
47  {
48    // official keywords separator is the comma
49    $iptc['keywords'] = preg_replace('/[.;]/', ',', $iptc['keywords']);
50    $iptc['keywords'] = preg_replace('/^,+|,+$/', '', $iptc['keywords']);
51
52    $iptc['keywords'] = implode(
53      ',',
54      array_unique(
55        explode(
56          ',',
57          $iptc['keywords']
58          )
59        )
60      );
61  }
62
63  foreach ($iptc as $pwg_key => $value)
64  {
65    $iptc[$pwg_key] = addslashes($iptc[$pwg_key]);
66  }
67
68  return $iptc;
69}
70
71function get_sync_exif_data($file)
72{
73  global $conf;
74
75  $exif = get_exif_data($file, $conf['use_exif_mapping']);
76
77  foreach ($exif as $pwg_key => $value)
78  {
79    if (in_array($pwg_key, array('date_creation', 'date_available')))
80    {
81      if (preg_match('/^(\d{4}).(\d{2}).(\d{2})/', $value, $matches))
82      {
83        $exif[$pwg_key] = $matches[1].'-'.$matches[2].'-'.$matches[3];
84      }
85    }
86    $exif[$pwg_key] = addslashes($exif[$pwg_key]);
87  }
88
89  return $exif;
90}
91
92function update_metadata($files)
93{
94  global $conf;
95
96  if (!defined('CURRENT_DATE'))
97  {
98    define('CURRENT_DATE', date('Y-m-d'));
99  }
100
101  $datas = array();
102  $tags_of = array();
103  $has_high_images = array();
104
105  $image_ids = array();
106  foreach ($files as $id => $file)
107  {
108    array_push($image_ids, $id);
109  }
110
111  $query = '
112SELECT id
113  FROM '.IMAGES_TABLE.'
114  WHERE has_high = \'true\'
115    AND id IN (
116'.wordwrap(implode(', ', $image_ids), 80, "\n").'
117)
118;';
119
120  $has_high_images = array_from_query($query, 'id');
121
122  foreach ($files as $id => $file)
123  {
124    $data = array();
125    $data['id'] = $id;
126    $data['filesize'] = floor(filesize($file)/1024);
127
128    if ($image_size = @getimagesize($file))
129    {
130      $data['width'] = $image_size[0];
131      $data['height'] = $image_size[1];
132    }
133
134    if (in_array($id, $has_high_images))
135    {
136      $high_file = dirname($file).'/pwg_high/'.basename($file);
137
138      $data['high_filesize'] = floor(filesize($high_file)/1024);
139    }
140
141    if ($conf['use_exif'])
142    {
143      $exif = get_sync_exif_data($file);
144      $data = array_merge($data, $exif);
145    }
146
147    if ($conf['use_iptc'])
148    {
149      $iptc = get_sync_iptc_data($file);
150      $data = array_merge($data, $iptc);
151     
152      if (count($iptc) > 0)
153      {
154        foreach (array_keys($iptc) as $key)
155        {
156          if ($key == 'keywords' or $key == 'tags')
157          {
158            if (!isset($tags_of[$id]))
159            {
160              $tags_of[$id] = array();
161            }
162
163            foreach (explode(',', $iptc[$key]) as $tag_name)
164            {
165              array_push(
166                $tags_of[$id],
167                tag_id_from_tag_name($tag_name)
168                );
169            }
170          }
171        }
172      }
173    }
174
175    $data['date_metadata_update'] = CURRENT_DATE;
176
177    array_push($datas, $data);
178  }
179
180  if (count($datas) > 0)
181  {
182    $update_fields =
183      array(
184        'filesize',
185        'width',
186        'height',
187        'high_filesize',
188        'date_metadata_update'
189        );
190
191    if ($conf['use_exif'])
192    {
193      $update_fields =
194        array_merge(
195          $update_fields,
196          array_keys($conf['use_exif_mapping'])
197          );
198    }
199
200    if ($conf['use_iptc'])
201    {
202      $update_fields =
203        array_merge(
204          $update_fields,
205          array_diff(
206            array_keys($conf['use_iptc_mapping']),
207            array('tags', 'keywords')
208            )
209          );
210    }
211
212    mass_updates(
213      IMAGES_TABLE,
214      array(
215        'primary' => array('id'),
216        'update'  => array_unique($update_fields)
217        ),
218      $datas,
219      MASS_UPDATES_SKIP_EMPTY
220      );
221  }
222
223  set_tags_of($tags_of);
224}
225
226/**
227 * returns an array associating element id (images.id) with its complete
228 * path in the filesystem
229 *
230 * @param int id_uppercat
231 * @param int site_id
232 * @param boolean recursive ?
233 * @param boolean only newly added files ?
234 * @return array
235 */
236function get_filelist($category_id = '', $site_id=1, $recursive = false,
237                      $only_new = false)
238{
239  // filling $cat_ids : all categories required
240  $cat_ids = array();
241
242  $query = '
243SELECT id
244  FROM '.CATEGORIES_TABLE.'
245  WHERE site_id = '.$site_id.'
246    AND dir IS NOT NULL';
247  if (is_numeric($category_id))
248  {
249    if ($recursive)
250    {
251      $query.= '
252    AND uppercats '.DB_REGEX_OPERATOR.' \'(^|,)'.$category_id.'(,|$)\'
253';
254    }
255    else
256    {
257      $query.= '
258    AND id = '.$category_id.'
259';
260    }
261  }
262  $query.= '
263;';
264  $result = pwg_query($query);
265  while ($row = pwg_db_fetch_assoc($result))
266  {
267    array_push($cat_ids, $row['id']);
268  }
269
270  if (count($cat_ids) == 0)
271  {
272    return array();
273  }
274
275  $files = array();
276
277  $query = '
278SELECT id, path
279  FROM '.IMAGES_TABLE.'
280  WHERE storage_category_id IN ('.implode(',', $cat_ids).')';
281  if ($only_new)
282  {
283    $query.= '
284    AND date_metadata_update IS NULL
285';
286  }
287  $query.= '
288;';
289  $result = pwg_query($query);
290  while ($row = pwg_db_fetch_assoc($result))
291  {
292    $files[$row['id']] = $row['path'];
293  }
294
295  return $files;
296}
297?>
Note: See TracBrowser for help on using the repository browser.