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

Last change on this file since 4325 was 4325, checked in by nikrou, 14 years ago

Feature 1244 resolved
Replace all mysql functions in core code by ones independant of database engine

Fix small php code synxtax : hash must be accessed with [ ] and not { }.

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