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

Last change on this file since 582 was 582, checked in by z0rglub, 20 years ago
  • bug correction : only local categories can be metadata synchronized
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.1 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-24 18:57:19 +0000 (Sun, 24 Oct 2004) $
10// | last modifier : $Author: z0rglub $
11// | revision      : $Revision: 582 $
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  if (!defined('CURRENT_DATE'))
71  {
72    define('CURRENT_DATE', "'".date('Y-m-d')."'");
73  }
74
75  $inserts = array();
76
77  foreach ($files as $id => $file)
78  {
79    $insert = array();
80    $insert['id'] = $id;
81    $insert['filesize'] = floor(filesize($file)/1024);
82 
83    if ($image_size = @getimagesize($file))
84    {
85      $insert['width'] = $image_size[0];
86      $insert['height'] = $image_size[1];
87    }
88 
89    if ($conf['use_exif'])
90    {
91      if ($exif = @read_exif_data($file))
92      {
93        if (isset($exif['DateTime']))
94        {
95          preg_match('/^(\d{4}).(\d{2}).(\d{2})/'
96                     ,$exif['DateTime']
97                     ,$matches);
98          $insert['date_creation'] =
99            "'".$matches[1].'-'.$matches[2].'-'.$matches[3]."'";
100        }
101      }
102    }
103
104    if ($conf['use_iptc'])
105    {
106      $iptc = get_sync_iptc_data($file);
107      if (count($iptc) > 0)
108      {
109        foreach (array_keys($iptc) as $key)
110        {
111          $insert[$key] = "'".addslashes($iptc[$key])."'";
112        }
113      }
114    }
115
116    $insert['date_metadata_update'] = CURRENT_DATE;
117
118    array_push($inserts, $insert);
119  }
120 
121  if (count($inserts) > 0)
122  {
123    $dbfields = array(
124      'id','filesize','width','height','name','author','comment'
125      ,'date_creation','keywords','date_metadata_update'
126      );
127
128    // depending on the MySQL version, we use the multi table update or N
129    // update queries
130    $query = 'SELECT VERSION() AS version;';
131    $row = mysql_fetch_array(mysql_query($query));
132    if (version_compare($row['version'],'4.0.4') < 0)
133    {
134      // MySQL is prior to version 4.0.4, multi table update feature is not
135      // available
136      echo 'MySQL is prior to version 4.0.4, multi table update feature is not available<br />';
137      foreach ($inserts as $insert)
138      {
139        $query = '
140UPDATE '.IMAGES_TABLE.'
141  SET ';
142        foreach (array_diff(array_keys($insert),array('id')) as $num => $key)
143        {
144          if ($num > 1)
145          {
146            $query.= ', ';
147          }
148          $query.= $key.' = '.$insert[$key];
149        }
150        $query.= '
151  WHERE id = '.$insert['id'].'
152;';
153        // echo '<pre>'.$query.'</pre>';
154        mysql_query($query);
155      }
156    }
157    else
158    {
159      // creation of the temporary table
160      $query = '
161DESCRIBE '.IMAGES_TABLE.'
162;';
163      $result = mysql_query($query);
164      $columns = array();
165      while ($row = mysql_fetch_array($result))
166      {
167        if (in_array($row['Field'], $dbfields))
168        {
169          $column = $row['Field'];
170          $column.= ' '.$row['Type'];
171          if (!isset($row['Null']) or $row['Null'] == '')
172          {
173            $column.= ' NOT NULL';
174          }
175          if (isset($row['Default']))
176          {
177            $column.= " default '".$row['Default']."'";
178          }
179          array_push($columns, $column);
180        }
181      }
182      $query = '
183CREATE TEMPORARY TABLE '.IMAGE_METADATA_TABLE.'
184(
185'.implode(",\n", $columns).',
186PRIMARY KEY (id)
187)
188;';
189      // echo '<pre>'.$query.'</pre>';
190      mysql_query($query);
191      // inserts all found pictures
192      $query = '
193INSERT INTO '.IMAGE_METADATA_TABLE.'
194  ('.implode(',', $dbfields).')
195   VALUES
196   ';
197      foreach ($inserts as $insert_id => $insert)
198      {
199        $query.= '
200';
201        if ($insert_id > 0)
202        {
203          $query.= ',';
204        }
205        $query.= '(';
206        foreach ($dbfields as $field_id => $dbfield)
207        {
208          if ($field_id > 0)
209          {
210            $query.= ',';
211          }
212         
213          if (!isset($insert[$dbfield]) or $insert[$dbfield] == '')
214          {
215            $query.= 'NULL';
216          }
217          else
218          {
219            $query.= $insert[$dbfield];
220          }
221        }
222        $query.=')';
223      }
224      $query.= '
225;';
226      // echo '<pre>'.$query.'</pre>';
227      mysql_query($query);
228      // update of images table by joining with temporary table
229      $query = '
230UPDATE '.IMAGES_TABLE.' AS images, '.IMAGE_METADATA_TABLE.' as metadata
231  SET '.implode("\n    , ",
232                array_map(
233                  create_function('$s', 'return "images.$s = metadata.$s";')
234                  , array_diff($dbfields, array('id')))).'
235  WHERE images.id = metadata.id
236;';
237      echo '<pre>'.$query.'</pre>';
238      mysql_query($query);
239    }
240  }
241}
242
243/**
244 * returns an array associating element id (images.id) with its complete
245 * path in the filesystem
246 *
247 * @param int id_uppercat
248 * @param boolean recursive ?
249 * @param boolean only newly added files ?
250 * @return array
251 */
252function get_filelist($category_id = '', $recursive = false, $only_new = false)
253{
254  $files = array();
255
256  $query = '
257SELECT id, dir
258  FROM '.CATEGORIES_TABLE.'
259  WHERE dir IS NOT NULL
260;';
261  $result = mysql_query($query);
262  $cat_dirs = array();
263  while ($row = mysql_fetch_array($result))
264  {
265    $cat_dirs[$row['id']] = $row['dir'];
266  }
267
268  // filling $uppercats_array : to each category id the uppercats list is
269  // associated
270  $uppercats_array = array();
271 
272  $query = '
273SELECT id, uppercats
274  FROM '.CATEGORIES_TABLE.'
275  WHERE site_id = 1
276    AND dir IS NOT NULL';
277  if (is_numeric($category_id))
278  {
279    if ($recursive)
280    {
281      $query.= '
282    AND uppercats REGEXP \'(^|,)'.$category_id.'(,|$)\'
283';
284    }
285    else
286    {
287      $query.= '
288    AND id = '.$category_id.'
289';
290    }
291  }
292  $query.= '
293;';
294  $result = mysql_query($query);
295  while ($row = mysql_fetch_array($result))
296  {
297    $uppercats_array[$row['id']] =  $row['uppercats'];
298  }
299
300  $query = '
301SELECT galleries_url
302  FROM '.SITES_TABLE.'
303  WHERE id = 1
304';
305  $row = mysql_fetch_array(mysql_query($query));
306  $basedir = $row['galleries_url'];
307 
308  // filling $cat_fulldirs
309  $cat_fulldirs = array();
310  foreach ($uppercats_array as $cat_id => $uppercats)
311  {
312    $uppercats = str_replace(',', '/', $uppercats);
313    $cat_fulldirs[$cat_id] = $basedir.preg_replace('/(\d+)/e',
314                                                   "\$cat_dirs['$1']",
315                                                   $uppercats);
316  }
317
318  $query = '
319SELECT id, file, storage_category_id
320  FROM '.IMAGES_TABLE.'
321  WHERE storage_category_id IN ('.implode(','
322                                          ,array_keys($uppercats_array)).')';
323  if ($only_new)
324  {
325    $query.= '
326    AND date_metadata_update IS NULL
327';
328  }
329  $query.= '
330;';
331  $result = mysql_query($query);
332  while ($row = mysql_fetch_array($result))
333  {
334    $files[$row['id']]
335      = $cat_fulldirs[$row['storage_category_id']].'/'.$row['file'];
336  }
337 
338  return $files;
339}
340?>
Note: See TracBrowser for help on using the repository browser.