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

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

increase copyright year to 2010

  • Property svn:eol-style set to LF
File size: 7.2 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based picture gallery                                  |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2010 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      if (count($exif) == 0 and isset($data['high_filesize']))
145      {
146        $exif = get_sync_exif_data($high_file);
147      }
148      $data = array_merge($data, $exif);
149    }
150
151    if ($conf['use_iptc'])
152    {
153      $iptc = get_sync_iptc_data($file);
154      if (count($iptc) == 0 and isset($data['high_filesize']))
155      {
156        $iptc = get_sync_iptc_data($high_file);
157      }
158      $data = array_merge($data, $iptc);
159     
160      if (count($iptc) > 0)
161      {
162        foreach (array_keys($iptc) as $key)
163        {
164          if ($key == 'keywords' or $key == 'tags')
165          {
166            if (!isset($tags_of[$id]))
167            {
168              $tags_of[$id] = array();
169            }
170
171            foreach (explode(',', $iptc[$key]) as $tag_name)
172            {
173              array_push(
174                $tags_of[$id],
175                tag_id_from_tag_name($tag_name)
176                );
177            }
178          }
179        }
180      }
181    }
182
183    $data['date_metadata_update'] = CURRENT_DATE;
184
185    array_push($datas, $data);
186  }
187
188  if (count($datas) > 0)
189  {
190    $update_fields =
191      array(
192        'filesize',
193        'width',
194        'height',
195        'high_filesize',
196        'date_metadata_update'
197        );
198
199    if ($conf['use_exif'])
200    {
201      $update_fields =
202        array_merge(
203          $update_fields,
204          array_keys($conf['use_exif_mapping'])
205          );
206    }
207
208    if ($conf['use_iptc'])
209    {
210      $update_fields =
211        array_merge(
212          $update_fields,
213          array_diff(
214            array_keys($conf['use_iptc_mapping']),
215            array('tags', 'keywords')
216            )
217          );
218    }
219
220    mass_updates(
221      IMAGES_TABLE,
222      array(
223        'primary' => array('id'),
224        'update'  => array_unique($update_fields)
225        ),
226      $datas,
227      MASS_UPDATES_SKIP_EMPTY
228      );
229  }
230
231  set_tags_of($tags_of);
232}
233
234/**
235 * returns an array associating element id (images.id) with its complete
236 * path in the filesystem
237 *
238 * @param int id_uppercat
239 * @param int site_id
240 * @param boolean recursive ?
241 * @param boolean only newly added files ?
242 * @return array
243 */
244function get_filelist($category_id = '', $site_id=1, $recursive = false,
245                      $only_new = false)
246{
247  // filling $cat_ids : all categories required
248  $cat_ids = array();
249
250  $query = '
251SELECT id
252  FROM '.CATEGORIES_TABLE.'
253  WHERE site_id = '.$site_id.'
254    AND dir IS NOT NULL';
255  if (is_numeric($category_id))
256  {
257    if ($recursive)
258    {
259      $query.= '
260    AND uppercats '.DB_REGEX_OPERATOR.' \'(^|,)'.$category_id.'(,|$)\'
261';
262    }
263    else
264    {
265      $query.= '
266    AND id = '.$category_id.'
267';
268    }
269  }
270  $query.= '
271;';
272  $result = pwg_query($query);
273  while ($row = pwg_db_fetch_assoc($result))
274  {
275    array_push($cat_ids, $row['id']);
276  }
277
278  if (count($cat_ids) == 0)
279  {
280    return array();
281  }
282
283  $files = array();
284
285  $query = '
286SELECT id, path
287  FROM '.IMAGES_TABLE.'
288  WHERE storage_category_id IN ('.implode(',', $cat_ids).')';
289  if ($only_new)
290  {
291    $query.= '
292    AND date_metadata_update IS NULL
293';
294  }
295  $query.= '
296;';
297  $result = pwg_query($query);
298  while ($row = pwg_db_fetch_assoc($result))
299  {
300    $files[$row['id']] = $row['path'];
301  }
302
303  return $files;
304}
305?>
Note: See TracBrowser for help on using the repository browser.