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

Last change on this file since 600 was 600, checked in by plg, 19 years ago

IPTC mapping done in include/config.inc.php

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.0 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | PhpWebGallery - a PHP based picture gallery                           |
4// | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net |
5// | Copyright (C) 2003-2004 PhpWebGallery Team - http://phpwebgallery.net |
6// +-----------------------------------------------------------------------+
7// | branch        : BSF (Best So Far)
8// | file          : $RCSfile$
9// | last update   : $Date: 2004-11-10 22:31:51 +0000 (Wed, 10 Nov 2004) $
10// | last modifier : $Author: plg $
11// | revision      : $Revision: 600 $
12// +-----------------------------------------------------------------------+
13// | This program is free software; you can redistribute it and/or modify  |
14// | it under the terms of the GNU General Public License as published by  |
15// | the Free Software Foundation                                          |
16// |                                                                       |
17// | This program is distributed in the hope that it will be useful, but   |
18// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
19// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
20// | General Public License for more details.                              |
21// |                                                                       |
22// | You should have received a copy of the GNU General Public License     |
23// | along with this program; if not, write to the Free Software           |
24// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
25// | USA.                                                                  |
26// +-----------------------------------------------------------------------+
27
28include_once(PHPWG_ROOT_PATH.'/include/functions_metadata.inc.php');
29
30function get_sync_iptc_data($file)
31{
32  global $conf;
33 
34  $map = $conf['use_iptc_mapping'];
35  $datefields = array('date_creation', 'date_available');
36 
37  $iptc = get_iptc_data($file, $map);
38
39  foreach ($iptc as $pwg_key => $value)
40  {
41    if (in_array($pwg_key, $datefields))
42    {
43      if ( preg_match('/(\d{4})(\d{2})(\d{2})/', $value, $matches))
44      {
45        $iptc[$pwg_key] = $matches[1].'-'.$matches[2].'-'.$matches[3];
46      }
47    }
48  }
49
50  if (isset($iptc['keywords']))
51  {
52    // keywords separator is the comma, nothing else. Allowed characters in
53    // keywords : [A-Za-z0-9], "-" and "_". All other characters will be
54    // considered as separators
55    $iptc['keywords'] = preg_replace('/[^\w-]+/', ',', $iptc['keywords']);
56    $iptc['keywords'] = preg_replace('/^,+|,+$/', '', $iptc['keywords']);
57  }
58
59  return $iptc;
60}
61
62function update_metadata($files)
63{
64  global $conf;
65
66  if (!defined('CURRENT_DATE'))
67  {
68    define('CURRENT_DATE', "'".date('Y-m-d')."'");
69  }
70
71  $inserts = array();
72
73  foreach ($files as $id => $file)
74  {
75    $insert = array();
76    $insert['id'] = $id;
77    $insert['filesize'] = floor(filesize($file)/1024);
78 
79    if ($image_size = @getimagesize($file))
80    {
81      $insert['width'] = $image_size[0];
82      $insert['height'] = $image_size[1];
83    }
84 
85    if ($conf['use_exif'])
86    {
87      if ($exif = @read_exif_data($file))
88      {
89        if (isset($exif['DateTime']))
90        {
91          preg_match('/^(\d{4}).(\d{2}).(\d{2})/'
92                     ,$exif['DateTime']
93                     ,$matches);
94          $insert['date_creation'] =
95            "'".$matches[1].'-'.$matches[2].'-'.$matches[3]."'";
96        }
97      }
98    }
99
100    if ($conf['use_iptc'])
101    {
102      $iptc = get_sync_iptc_data($file);
103      if (count($iptc) > 0)
104      {
105        foreach (array_keys($iptc) as $key)
106        {
107          $insert[$key] = "'".addslashes($iptc[$key])."'";
108        }
109      }
110    }
111
112    $insert['date_metadata_update'] = CURRENT_DATE;
113
114    array_push($inserts, $insert);
115  }
116 
117  if (count($inserts) > 0)
118  {
119    $dbfields = array(
120      'id','filesize','width','height','name','author','comment'
121      ,'date_creation','keywords','date_metadata_update'
122      );
123
124    // depending on the MySQL version, we use the multi table update or N
125    // update queries
126    $query = 'SELECT VERSION() AS version;';
127    $row = mysql_fetch_array(pwg_query($query));
128    if (version_compare($row['version'],'4.0.4') < 0)
129    {
130      // MySQL is prior to version 4.0.4, multi table update feature is not
131      // available
132      echo 'MySQL is prior to version 4.0.4, multi table update feature is not available<br />';
133      foreach ($inserts as $insert)
134      {
135        $query = '
136UPDATE '.IMAGES_TABLE.'
137  SET ';
138        foreach (array_diff(array_keys($insert),array('id')) as $num => $key)
139        {
140          if ($num > 1)
141          {
142            $query.= ', ';
143          }
144          $query.= $key.' = '.$insert[$key];
145        }
146        $query.= '
147  WHERE id = '.$insert['id'].'
148;';
149        // echo '<pre>'.$query.'</pre>';
150        pwg_query($query);
151      }
152    }
153    else
154    {
155      // creation of the temporary table
156      $query = '
157DESCRIBE '.IMAGES_TABLE.'
158;';
159      $result = pwg_query($query);
160      $columns = array();
161      while ($row = mysql_fetch_array($result))
162      {
163        if (in_array($row['Field'], $dbfields))
164        {
165          $column = $row['Field'];
166          $column.= ' '.$row['Type'];
167          if (!isset($row['Null']) or $row['Null'] == '')
168          {
169            $column.= ' NOT NULL';
170          }
171          if (isset($row['Default']))
172          {
173            $column.= " default '".$row['Default']."'";
174          }
175          array_push($columns, $column);
176        }
177      }
178      $query = '
179CREATE TEMPORARY TABLE '.IMAGE_METADATA_TABLE.'
180(
181'.implode(",\n", $columns).',
182PRIMARY KEY (id)
183)
184;';
185      // echo '<pre>'.$query.'</pre>';
186      pwg_query($query);
187      // inserts all found pictures
188      $query = '
189INSERT INTO '.IMAGE_METADATA_TABLE.'
190  ('.implode(',', $dbfields).')
191   VALUES
192   ';
193      foreach ($inserts as $insert_id => $insert)
194      {
195        $query.= '
196';
197        if ($insert_id > 0)
198        {
199          $query.= ',';
200        }
201        $query.= '(';
202        foreach ($dbfields as $field_id => $dbfield)
203        {
204          if ($field_id > 0)
205          {
206            $query.= ',';
207          }
208         
209          if (!isset($insert[$dbfield]) or $insert[$dbfield] == '')
210          {
211            $query.= 'NULL';
212          }
213          else
214          {
215            $query.= $insert[$dbfield];
216          }
217        }
218        $query.=')';
219      }
220      $query.= '
221;';
222      // echo '<pre>'.$query.'</pre>';
223      pwg_query($query);
224      // update of images table by joining with temporary table
225      $query = '
226UPDATE '.IMAGES_TABLE.' AS images, '.IMAGE_METADATA_TABLE.' as metadata
227  SET '.implode("\n    , ",
228                array_map(
229                  create_function('$s', 'return "images.$s = metadata.$s";')
230                  , array_diff($dbfields, array('id')))).'
231  WHERE images.id = metadata.id
232;';
233      echo '<pre>'.$query.'</pre>';
234      pwg_query($query);
235    }
236  }
237}
238
239/**
240 * returns an array associating element id (images.id) with its complete
241 * path in the filesystem
242 *
243 * @param int id_uppercat
244 * @param boolean recursive ?
245 * @param boolean only newly added files ?
246 * @return array
247 */
248function get_filelist($category_id = '', $recursive = false, $only_new = false)
249{
250  $files = array();
251
252  $query = '
253SELECT id, dir
254  FROM '.CATEGORIES_TABLE.'
255  WHERE dir IS NOT NULL
256;';
257  $result = pwg_query($query);
258  $cat_dirs = array();
259  while ($row = mysql_fetch_array($result))
260  {
261    $cat_dirs[$row['id']] = $row['dir'];
262  }
263
264  // filling $uppercats_array : to each category id the uppercats list is
265  // associated
266  $uppercats_array = array();
267 
268  $query = '
269SELECT id, uppercats
270  FROM '.CATEGORIES_TABLE.'
271  WHERE site_id = 1
272    AND dir IS NOT NULL';
273  if (is_numeric($category_id))
274  {
275    if ($recursive)
276    {
277      $query.= '
278    AND uppercats REGEXP \'(^|,)'.$category_id.'(,|$)\'
279';
280    }
281    else
282    {
283      $query.= '
284    AND id = '.$category_id.'
285';
286    }
287  }
288  $query.= '
289;';
290  $result = pwg_query($query);
291  while ($row = mysql_fetch_array($result))
292  {
293    $uppercats_array[$row['id']] =  $row['uppercats'];
294  }
295
296  if (count($uppercats_array) == 0)
297  {
298    return array();
299  }
300
301  $query = '
302SELECT galleries_url
303  FROM '.SITES_TABLE.'
304  WHERE id = 1
305';
306  $row = mysql_fetch_array(pwg_query($query));
307  $basedir = $row['galleries_url'];
308 
309  // filling $cat_fulldirs
310  $cat_fulldirs = array();
311  foreach ($uppercats_array as $cat_id => $uppercats)
312  {
313    $uppercats = str_replace(',', '/', $uppercats);
314    $cat_fulldirs[$cat_id] = $basedir.preg_replace('/(\d+)/e',
315                                                   "\$cat_dirs['$1']",
316                                                   $uppercats);
317  }
318
319  $query = '
320SELECT id, file, storage_category_id
321  FROM '.IMAGES_TABLE.'
322  WHERE storage_category_id IN ('.implode(','
323                                          ,array_keys($uppercats_array)).')';
324  if ($only_new)
325  {
326    $query.= '
327    AND date_metadata_update IS NULL
328';
329  }
330  $query.= '
331;';
332  $result = pwg_query($query);
333  while ($row = mysql_fetch_array($result))
334  {
335    $files[$row['id']]
336      = $cat_fulldirs[$row['storage_category_id']].'/'.$row['file'];
337  }
338 
339  return $files;
340}
341?>
Note: See TracBrowser for help on using the repository browser.