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

Last change on this file since 26461 was 26461, checked in by mistic100, 10 years ago

Update headers to 2014. Happy new year!!

  • 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-2014 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        array('latitude', 'longitude')
127        );
128  }
129
130  if ($conf['use_iptc'])
131  {
132    $update_fields =
133      array_merge(
134        $update_fields,
135        array_keys($conf['use_iptc_mapping'])
136        );
137  }
138
139  return array_unique($update_fields);
140}
141
142function get_sync_metadata($infos)
143{
144  global $conf;
145  $file = PHPWG_ROOT_PATH.$infos['path'];
146  $fs = @filesize($file);
147
148  if ($fs===false)
149  {
150    return false;
151  }
152
153  $infos['filesize'] = floor($fs/1024);
154
155  if (isset($infos['representative_ext']))
156  {
157    $file = original_to_representative($file, $infos['representative_ext']);
158  }
159
160  if ($image_size = @getimagesize($file))
161  {
162    $infos['width'] = $image_size[0];
163    $infos['height'] = $image_size[1];
164  }
165
166  if ($conf['use_exif'])
167  {
168    $exif = get_sync_exif_data($file);
169    $infos = array_merge($infos, $exif);
170  }
171
172  if ($conf['use_iptc'])
173  {
174    $iptc = get_sync_iptc_data($file);
175    $infos = array_merge($infos, $iptc);
176  }
177
178  return $infos;
179}
180
181
182function sync_metadata($ids)
183{
184  global $conf;
185
186  if (!defined('CURRENT_DATE'))
187  {
188    define('CURRENT_DATE', date('Y-m-d'));
189  }
190
191  $datas = array();
192  $tags_of = array();
193
194  $query = '
195SELECT id, path, representative_ext
196  FROM '.IMAGES_TABLE.'
197  WHERE id IN (
198'.wordwrap(implode(', ', $ids), 160, "\n").'
199)
200;';
201
202  $result = pwg_query($query);
203  while ($data = pwg_db_fetch_assoc($result))
204  {
205    $data = get_sync_metadata($data);
206    if ($data === false)
207    {
208      continue;
209    }
210
211    $id = $data['id'];
212    foreach (array('keywords', 'tags') as $key)
213    {
214      if (isset($data[$key]))
215      {
216        if (!isset($tags_of[$id]))
217        {
218          $tags_of[$id] = array();
219        }
220
221        foreach (explode(',', $data[$key]) as $tag_name)
222        {
223          $tags_of[$id][] = tag_id_from_tag_name($tag_name);
224        }
225      }
226    }
227
228    $data['date_metadata_update'] = CURRENT_DATE;
229
230    $datas[] = $data;
231  }
232
233  if (count($datas) > 0)
234  {
235    $update_fields = get_sync_metadata_attributes();
236    $update_fields[] = 'date_metadata_update';
237
238    $update_fields = array_diff(
239      $update_fields,
240      array('tags', 'keywords')
241            );
242
243    mass_updates(
244      IMAGES_TABLE,
245      array(
246        'primary' => array('id'),
247        'update'  => $update_fields
248        ),
249      $datas,
250      MASS_UPDATES_SKIP_EMPTY
251      );
252  }
253
254  set_tags_of($tags_of);
255}
256
257/**
258 * returns an array associating element id (images.id) with its complete
259 * path in the filesystem
260 *
261 * @param int id_uppercat
262 * @param int site_id
263 * @param boolean recursive ?
264 * @param boolean only newly added files ?
265 * @return array
266 */
267function get_filelist($category_id = '', $site_id=1, $recursive = false,
268                      $only_new = false)
269{
270  // filling $cat_ids : all categories required
271  $cat_ids = array();
272
273  $query = '
274SELECT id
275  FROM '.CATEGORIES_TABLE.'
276  WHERE site_id = '.$site_id.'
277    AND dir IS NOT NULL';
278  if (is_numeric($category_id))
279  {
280    if ($recursive)
281    {
282      $query.= '
283    AND uppercats '.DB_REGEX_OPERATOR.' \'(^|,)'.$category_id.'(,|$)\'
284';
285    }
286    else
287    {
288      $query.= '
289    AND id = '.$category_id.'
290';
291    }
292  }
293  $query.= '
294;';
295  $result = pwg_query($query);
296  while ($row = pwg_db_fetch_assoc($result))
297  {
298    $cat_ids[] = $row['id'];
299  }
300
301  if (count($cat_ids) == 0)
302  {
303    return array();
304  }
305
306  $query = '
307SELECT id, path, representative_ext
308  FROM '.IMAGES_TABLE.'
309  WHERE storage_category_id IN ('.implode(',', $cat_ids).')';
310  if ($only_new)
311  {
312    $query.= '
313    AND date_metadata_update IS NULL
314';
315  }
316  $query.= '
317;';
318  return hash_from_query($query, 'id');
319}
320?>
Note: See TracBrowser for help on using the repository browser.