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