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

Last change on this file since 12831 was 12831, checked in by rvelices, 12 years ago

feature 2548 multisize

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