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

Last change on this file since 2502 was 2299, checked in by plg, 16 years ago

Bug fixed: as rvelices notified me by email, my header replacement script was
bugged (r2297 was repeating new and old header).

By the way, I've also removed the replacement keywords. We were using them
because it was a common usage with CVS but it is advised not to use them with
Subversion. Personnaly, it is a problem when I search differences between 2
Piwigo installations outside Subversion.

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