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

Last change on this file since 563 was 562, checked in by z0rglub, 20 years ago

clean keywords string from IPTC fields

  • 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// |                          functions_metadata.php                       |
4// +-----------------------------------------------------------------------+
5// | application   : PhpWebGallery <http://phpwebgallery.net>              |
6// | branch        : BSF (Best So Far)                                     |
7// +-----------------------------------------------------------------------+
8// | file          : $RCSfile$
9// | last update   : $Date: 2004-10-09 10:04:23 +0000 (Sat, 09 Oct 2004) $
10// | last modifier : $Author: z0rglub $
11// | revision      : $Revision: 562 $
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  $map = array(
33    'keywords'        => '2#025',
34    'date_creation'   => '2#055',
35    'author'          => '2#122',
36    'name'            => '2#085',
37    'comment'         => '2#120'
38    );
39  $datefields = array('date_creation', 'date_available');
40 
41  $iptc = get_iptc_data($file, $map);
42
43  foreach ($iptc as $pwg_key => $value)
44  {
45    if (in_array($pwg_key, $datefields))
46    {
47      if ( preg_match('/(\d{4})(\d{2})(\d{2})/', $value, $matches))
48      {
49        $iptc[$pwg_key] = $matches[1].'-'.$matches[2].'-'.$matches[3];
50      }
51    }
52  }
53
54  if (isset($iptc['keywords']))
55  {
56    // keywords separator is the comma, nothing else. Allowed characters in
57    // keywords : [A-Za-z0-9], "-" and "_". All other characters will be
58    // considered as separators
59    $iptc['keywords'] = preg_replace('/[^\w-]+/', ',', $iptc['keywords']);
60    $iptc['keywords'] = preg_replace('/^,+|,+$/', '', $iptc['keywords']);
61  }
62
63  return $iptc;
64}
65
66function update_metadata($files)
67{
68  global $conf;
69 
70  $inserts = array();
71
72  foreach ($files as $id => $file)
73  {
74    $insert = array();
75    $insert['id'] = $id;
76    $insert['filesize'] = floor(filesize($file)/1024);
77 
78    if ($image_size = @getimagesize($file))
79    {
80      $insert['width'] = $image_size[0];
81      $insert['height'] = $image_size[1];
82    }
83 
84    if ($conf['use_exif'])
85    {
86      if ($exif = @read_exif_data($file))
87      {
88        if (isset($exif['DateTime']))
89        {
90          preg_match('/^(\d{4}).(\d{2}).(\d{2})/'
91                     ,$exif['DateTime']
92                     ,$matches);
93          $insert['date_creation'] =
94            "'".$matches[1].'-'.$matches[2].'-'.$matches[3]."'";
95        }
96      }
97    }
98
99    if ($conf['use_iptc'])
100    {
101      $iptc = get_sync_iptc_data($file);
102      if (count($iptc) > 0)
103      {
104        foreach (array_keys($iptc) as $key)
105        {
106          $insert[$key] = "'".addslashes($iptc[$key])."'";
107        }
108      }
109    }
110
111    $insert['date_metadata_update'] = CURRENT_DATE;
112
113    array_push($inserts, $insert);
114  }
115 
116  if (count($inserts) > 0)
117  {
118    $dbfields = array(
119      'id','filesize','width','height','name','author','comment'
120      ,'date_creation','keywords','date_metadata_update'
121      );
122
123    // depending on the MySQL version, we use the multi table update or N
124    // update queries
125    $query = 'SELECT VERSION() AS version;';
126    $row = mysql_fetch_array(mysql_query($query));
127    if (version_compare($row['version'],'4.0.4') < 0)
128    {
129      // MySQL is prior to version 4.0.4, multi table update feature is not
130      // available
131      echo 'MySQL is prior to version 4.0.4, multi table update feature is not available<br />';
132      foreach ($inserts as $insert)
133      {
134        $query = '
135UPDATE '.IMAGES_TABLE.'
136  SET ';
137        foreach (array_diff(array_keys($insert),array('id')) as $num => $key)
138        {
139          if ($num > 1)
140          {
141            $query.= ', ';
142          }
143          $query.= $key.' = '.$insert[$key];
144        }
145        $query.= '
146  WHERE id = '.$insert['id'].'
147;';
148        // echo '<pre>'.$query.'</pre>';
149        mysql_query($query);
150      }
151    }
152    else
153    {
154      // creation of the temporary table
155      $query = '
156DESCRIBE '.IMAGES_TABLE.'
157;';
158      $result = mysql_query($query);
159      $columns = array();
160      while ($row = mysql_fetch_array($result))
161      {
162        if (in_array($row['Field'], $dbfields))
163        {
164          $column = $row['Field'];
165          $column.= ' '.$row['Type'];
166          if (!isset($row['Null']) or $row['Null'] == '')
167          {
168            $column.= ' NOT NULL';
169          }
170          if (isset($row['Default']))
171          {
172            $column.= " default '".$row['Default']."'";
173          }
174          array_push($columns, $column);
175        }
176      }
177      $query = '
178CREATE TEMPORARY TABLE '.IMAGE_METADATA_TABLE.'
179(
180'.implode(",\n", $columns).',
181PRIMARY KEY (id)
182)
183;';
184      // echo '<pre>'.$query.'</pre>';
185      mysql_query($query);
186      // inserts all found pictures
187      $query = '
188INSERT INTO '.IMAGE_METADATA_TABLE.'
189  ('.implode(',', $dbfields).')
190   VALUES
191   ';
192      foreach ($inserts as $insert_id => $insert)
193      {
194        $query.= '
195';
196        if ($insert_id > 0)
197        {
198          $query.= ',';
199        }
200        $query.= '(';
201        foreach ($dbfields as $field_id => $dbfield)
202        {
203          if ($field_id > 0)
204          {
205            $query.= ',';
206          }
207         
208          if (!isset($insert[$dbfield]) or $insert[$dbfield] == '')
209          {
210            $query.= 'NULL';
211          }
212          else
213          {
214            $query.= $insert[$dbfield];
215          }
216        }
217        $query.=')';
218      }
219      $query.= '
220;';
221      // echo '<pre>'.$query.'</pre>';
222      mysql_query($query);
223      // update of images table by joining with temporary table
224      $query = '
225UPDATE '.IMAGES_TABLE.' AS images, '.IMAGE_METADATA_TABLE.' as metadata
226  SET '.implode("\n    , ",
227                array_map(
228                  create_function('$s', 'return "images.$s = metadata.$s";')
229                  , array_diff($dbfields, array('id')))).'
230  WHERE images.id = metadata.id
231;';
232      echo '<pre>'.$query.'</pre>';
233      mysql_query($query);
234    }
235  }
236}
237
238/**
239 * returns an array associating element id (images.id) with its complete
240 * path in the filesystem
241 *
242 * @param int id_uppercat
243 * @param boolean recursive ?
244 * @param boolean only newly added files ?
245 * @return array
246 */
247function get_filelist($category_id = '', $recursive = false, $only_new = false)
248{
249  $files = array();
250
251  $query = '
252SELECT id, dir
253  FROM '.CATEGORIES_TABLE.'
254;';
255  $result = mysql_query($query);
256  $cat_dirs = array();
257  while ($row = mysql_fetch_array($result))
258  {
259    $cat_dirs[$row['id']] = $row['dir'];
260  }
261
262  // filling $uppercats_array : to each category id the uppercats list is
263  // associated
264  $uppercats_array = array();
265 
266  $query = '
267SELECT id, uppercats
268  FROM '.CATEGORIES_TABLE;
269  if (is_numeric($category_id))
270  {
271    if ($recursive)
272    {
273      $query.= '
274  WHERE uppercats REGEXP \'(^|,)'.$category_id.'(,|$)\'
275';
276    }
277    else
278    {
279      $query.= '
280  WHERE id = '.$category_id.'
281';
282    }
283  }
284  $query.= '
285;';
286  $result = mysql_query($query);
287  while ($row = mysql_fetch_array($result))
288  {
289    $uppercats_array[$row['id']] =  $row['uppercats'];
290  }
291
292  $query = '
293SELECT galleries_url
294  FROM '.SITES_TABLE.'
295  WHERE id = 1
296';
297  $row = mysql_fetch_array(mysql_query($query));
298  $basedir = $row['galleries_url'];
299 
300  // filling $cat_fulldirs
301  $cat_fulldirs = array();
302  foreach ($uppercats_array as $cat_id => $uppercats)
303  {
304    $uppercats = str_replace(',', '/', $uppercats);
305    $cat_fulldirs[$cat_id] = $basedir.preg_replace('/(\d+)/e',
306                                                   "\$cat_dirs['$1']",
307                                                   $uppercats);
308  }
309
310  $query = '
311SELECT id, file, storage_category_id
312  FROM '.IMAGES_TABLE.'
313  WHERE storage_category_id IN ('.implode(','
314                                          ,array_keys($uppercats_array)).')';
315  if ($only_new)
316  {
317    $query.= '
318    AND date_metadata_update IS NULL
319';
320  }
321  $query.= '
322;';
323  $result = mysql_query($query);
324  while ($row = mysql_fetch_array($result))
325  {
326    $files[$row['id']]
327      = $cat_fulldirs[$row['storage_category_id']].'/'.$row['file'];
328  }
329 
330  return $files;
331}
332?>
Note: See TracBrowser for help on using the repository browser.