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-2006 PhpWebGallery Team - http://phpwebgallery.net | |
---|
6 | // +-----------------------------------------------------------------------+ |
---|
7 | // | branch : BSF (Best So Far) |
---|
8 | // | file : $RCSfile$ |
---|
9 | // | last update : $Date: 2006-07-11 20:56:51 +0000 (Tue, 11 Jul 2006) $ |
---|
10 | // | last modifier : $Author: rub $ |
---|
11 | // | revision : $Revision: 1459 $ |
---|
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 | |
---|
28 | if (!defined('PHPWG_ROOT_PATH')) |
---|
29 | { |
---|
30 | die('Hacking attempt!'); |
---|
31 | } |
---|
32 | |
---|
33 | include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); |
---|
34 | |
---|
35 | // +-----------------------------------------------------------------------+ |
---|
36 | // | Check Access and exit when user status is not ok | |
---|
37 | // +-----------------------------------------------------------------------+ |
---|
38 | check_status(ACCESS_ADMINISTRATOR); |
---|
39 | |
---|
40 | if (!is_numeric($_GET['site'])) |
---|
41 | { |
---|
42 | die ('site param missing or invalid'); |
---|
43 | } |
---|
44 | $site_id = $_GET['site']; |
---|
45 | |
---|
46 | $query=' |
---|
47 | SELECT galleries_url |
---|
48 | FROM '.SITES_TABLE.' |
---|
49 | WHERE id = '.$site_id.' |
---|
50 | ;'; |
---|
51 | list($site_url) = mysql_fetch_row(pwg_query($query)); |
---|
52 | if (!isset($site_url)) |
---|
53 | { |
---|
54 | die('site '.$site_id.' does not exist'); |
---|
55 | } |
---|
56 | $site_is_remote = url_is_remote($site_url); |
---|
57 | |
---|
58 | list($dbnow) = mysql_fetch_row(pwg_query('SELECT NOW();')); |
---|
59 | define('CURRENT_DATE', $dbnow); |
---|
60 | |
---|
61 | $error_labels = array( |
---|
62 | 'PWG-UPDATE-1' => array( |
---|
63 | l10n('update_wrong_dirname_short'), |
---|
64 | l10n('update_wrong_dirname_info') |
---|
65 | ), |
---|
66 | 'PWG-UPDATE-2' => array( |
---|
67 | l10n('update_missing_tn_short'), |
---|
68 | l10n('update_missing_tn_info').implode(',', $conf['picture_ext']) |
---|
69 | ), |
---|
70 | 'PWG-ERROR-NO-FS' => array( |
---|
71 | l10n('update_missing_file_or_dir'), |
---|
72 | l10n('update_missing_file_or_dir_info') |
---|
73 | ), |
---|
74 | 'PWG-ERROR-VERSION' => array( |
---|
75 | l10n('update_err_pwg_version_differs'), |
---|
76 | l10n('update_err_pwg_version_differs_info') |
---|
77 | ), |
---|
78 | 'PWG-ERROR-NOLISTING' => array( |
---|
79 | l10n('update_err_remote_listing_not_found'), |
---|
80 | l10n('update_err_remote_listing_not_found_info') |
---|
81 | ) |
---|
82 | ); |
---|
83 | $errors = array(); |
---|
84 | $infos = array(); |
---|
85 | |
---|
86 | if ($site_is_remote) |
---|
87 | { |
---|
88 | include_once(PHPWG_ROOT_PATH.'admin/site_reader_remote.php'); |
---|
89 | $local_listing = null; |
---|
90 | if ( isset($_GET['local_listing']) |
---|
91 | and $_GET['local_listing'] ) |
---|
92 | { |
---|
93 | $local_listing = PHPWG_ROOT_PATH.'listing.xml'; |
---|
94 | } |
---|
95 | $site_reader = new RemoteSiteReader($site_url, $local_listing); |
---|
96 | } |
---|
97 | else |
---|
98 | { |
---|
99 | include_once( PHPWG_ROOT_PATH.'admin/site_reader_local.php'); |
---|
100 | $site_reader = new LocalSiteReader($site_url); |
---|
101 | } |
---|
102 | |
---|
103 | $general_failure = true; |
---|
104 | if (isset($_POST['submit'])) |
---|
105 | { |
---|
106 | if (!isset($conf['flip_picture_ext'])) |
---|
107 | { |
---|
108 | $conf['flip_picture_ext'] = array_flip($conf['picture_ext']); |
---|
109 | } |
---|
110 | if ($site_reader->open()) |
---|
111 | { |
---|
112 | $general_failure = false; |
---|
113 | } |
---|
114 | |
---|
115 | // shall we simulate only |
---|
116 | if ((isset($_POST['simulate']) and $_POST['simulate'] == 1) or is_adviser()) |
---|
117 | { |
---|
118 | $simulate = true; |
---|
119 | } |
---|
120 | else |
---|
121 | { |
---|
122 | $simulate = false; |
---|
123 | } |
---|
124 | } |
---|
125 | |
---|
126 | // +-----------------------------------------------------------------------+ |
---|
127 | // | directories / categories | |
---|
128 | // +-----------------------------------------------------------------------+ |
---|
129 | if (isset($_POST['submit']) |
---|
130 | and ($_POST['sync'] == 'dirs' or $_POST['sync'] == 'files') |
---|
131 | and !$general_failure) |
---|
132 | { |
---|
133 | $counts['new_categories'] = 0; |
---|
134 | $counts['del_categories'] = 0; |
---|
135 | $counts['del_elements'] = 0; |
---|
136 | $counts['new_elements'] = 0; |
---|
137 | $counts['upd_elements'] = 0; |
---|
138 | |
---|
139 | $start = get_moment(); |
---|
140 | // which categories to update ? |
---|
141 | $cat_ids = array(); |
---|
142 | |
---|
143 | $query = ' |
---|
144 | SELECT id, uppercats, global_rank, status, visible |
---|
145 | FROM '.CATEGORIES_TABLE.' |
---|
146 | WHERE dir IS NOT NULL |
---|
147 | AND site_id = '.$site_id; |
---|
148 | if (isset($_POST['cat']) and is_numeric($_POST['cat'])) |
---|
149 | { |
---|
150 | if (isset($_POST['subcats-included']) and $_POST['subcats-included'] == 1) |
---|
151 | { |
---|
152 | $query.= ' |
---|
153 | AND uppercats REGEXP \'(^|,)'.$_POST['cat'].'(,|$)\' |
---|
154 | '; |
---|
155 | } |
---|
156 | else |
---|
157 | { |
---|
158 | $query.= ' |
---|
159 | AND id = '.$_POST['cat'].' |
---|
160 | '; |
---|
161 | } |
---|
162 | } |
---|
163 | $query.= ' |
---|
164 | ;'; |
---|
165 | $result = pwg_query($query); |
---|
166 | |
---|
167 | $db_categories = array(); |
---|
168 | while ($row = mysql_fetch_array($result)) |
---|
169 | { |
---|
170 | $db_categories[$row['id']] = $row; |
---|
171 | } |
---|
172 | |
---|
173 | // get categort full directories in an array for comparison with file |
---|
174 | // system directory tree |
---|
175 | $db_fulldirs = get_fulldirs(array_keys($db_categories)); |
---|
176 | |
---|
177 | // what is the base directory to search file system sub-directories ? |
---|
178 | if (isset($_POST['cat']) and is_numeric($_POST['cat'])) |
---|
179 | { |
---|
180 | $basedir = $db_fulldirs[$_POST['cat']]; |
---|
181 | } |
---|
182 | else |
---|
183 | { |
---|
184 | $basedir = preg_replace('#/*$#', '', $site_url); |
---|
185 | } |
---|
186 | |
---|
187 | // we need to have fulldirs as keys to make efficient comparison |
---|
188 | $db_fulldirs = array_flip($db_fulldirs); |
---|
189 | |
---|
190 | // finding next rank for each id_uppercat. By default, each category id |
---|
191 | // has 1 for next rank on its sub-categories to create |
---|
192 | $next_rank['NULL'] = 1; |
---|
193 | |
---|
194 | $query = ' |
---|
195 | SELECT id |
---|
196 | FROM '.CATEGORIES_TABLE.' |
---|
197 | ;'; |
---|
198 | $result = pwg_query($query); |
---|
199 | while ($row = mysql_fetch_array($result)) |
---|
200 | { |
---|
201 | $next_rank[$row['id']] = 1; |
---|
202 | } |
---|
203 | |
---|
204 | // let's see if some categories already have some sub-categories... |
---|
205 | $query = ' |
---|
206 | SELECT id_uppercat, MAX(rank)+1 AS next_rank |
---|
207 | FROM '.CATEGORIES_TABLE.' |
---|
208 | GROUP BY id_uppercat |
---|
209 | ;'; |
---|
210 | $result = pwg_query($query); |
---|
211 | while ($row = mysql_fetch_array($result)) |
---|
212 | { |
---|
213 | // for the id_uppercat NULL, we write 'NULL' and not the empty string |
---|
214 | if (!isset($row['id_uppercat']) or $row['id_uppercat'] == '') |
---|
215 | { |
---|
216 | $row['id_uppercat'] = 'NULL'; |
---|
217 | } |
---|
218 | $next_rank[$row['id_uppercat']] = $row['next_rank']; |
---|
219 | } |
---|
220 | |
---|
221 | // next category id available |
---|
222 | $query = ' |
---|
223 | SELECT IF(MAX(id)+1 IS NULL, 1, MAX(id)+1) AS next_id |
---|
224 | FROM '.CATEGORIES_TABLE.' |
---|
225 | ;'; |
---|
226 | list($next_id) = mysql_fetch_array(pwg_query($query)); |
---|
227 | |
---|
228 | // retrieve sub-directories fulldirs from the site reader |
---|
229 | $fs_fulldirs = $site_reader->get_full_directories($basedir); |
---|
230 | //print_r( $fs_fulldirs ); echo "<br>"; |
---|
231 | |
---|
232 | // get_full_directories doesn't include the base directory, so if it's a |
---|
233 | // category directory, we need to include it in our array |
---|
234 | if (isset($_POST['cat'])) |
---|
235 | { |
---|
236 | array_push($fs_fulldirs, $basedir); |
---|
237 | } |
---|
238 | |
---|
239 | $inserts = array(); |
---|
240 | // new categories are the directories not present yet in the database |
---|
241 | foreach (array_diff($fs_fulldirs, array_keys($db_fulldirs)) as $fulldir) |
---|
242 | { |
---|
243 | $dir = basename($fulldir); |
---|
244 | if (preg_match('/^[a-zA-Z0-9-_.]+$/', $dir)) |
---|
245 | { |
---|
246 | $insert = array( |
---|
247 | 'id' => $next_id++, |
---|
248 | 'dir' => $dir, |
---|
249 | 'name' => str_replace('_', ' ', $dir), |
---|
250 | 'site_id' => $site_id, |
---|
251 | 'commentable' => |
---|
252 | boolean_to_string($conf['newcat_default_commentable']), |
---|
253 | 'uploadable' => $site_is_remote |
---|
254 | ? 'false' |
---|
255 | : boolean_to_string($conf['newcat_default_uploadable']), |
---|
256 | 'status' => $conf{'newcat_default_status'}, |
---|
257 | 'visible' => boolean_to_string($conf{'newcat_default_visible'}), |
---|
258 | ); |
---|
259 | |
---|
260 | if (isset($db_fulldirs[dirname($fulldir)])) |
---|
261 | { |
---|
262 | $parent = $db_fulldirs[dirname($fulldir)]; |
---|
263 | |
---|
264 | $insert{'id_uppercat'} = $parent; |
---|
265 | $insert{'uppercats'} = |
---|
266 | $db_categories[$parent]['uppercats'].','.$insert{'id'}; |
---|
267 | $insert{'rank'} = $next_rank[$parent]++; |
---|
268 | $insert{'global_rank'} = |
---|
269 | $db_categories[$parent]['global_rank'].'.'.$insert{'rank'}; |
---|
270 | if ('private' == $db_categories[$parent]['status']) |
---|
271 | { |
---|
272 | $insert{'status'} = 'private'; |
---|
273 | } |
---|
274 | if ('false' == $db_categories[$parent]['visible']) |
---|
275 | { |
---|
276 | $insert{'visible'} = 'false'; |
---|
277 | } |
---|
278 | } |
---|
279 | else |
---|
280 | { |
---|
281 | $insert{'uppercats'} = $insert{'id'}; |
---|
282 | $insert{'rank'} = $next_rank['NULL']++; |
---|
283 | $insert{'global_rank'} = $insert{'rank'}; |
---|
284 | } |
---|
285 | |
---|
286 | array_push($inserts, $insert); |
---|
287 | array_push( |
---|
288 | $infos, |
---|
289 | array( |
---|
290 | 'path' => $fulldir, |
---|
291 | 'info' => l10n('update_research_added') |
---|
292 | ) |
---|
293 | ); |
---|
294 | |
---|
295 | // add the new category to $db_categories and $db_fulldirs array |
---|
296 | $db_categories[$insert{'id'}] = |
---|
297 | array( |
---|
298 | 'id' => $insert{'id'}, |
---|
299 | 'status' => $insert{'status'}, |
---|
300 | 'visible' => $insert{'visible'}, |
---|
301 | 'uppercats' => $insert{'uppercats'}, |
---|
302 | 'global_rank' => $insert{'global_rank'} |
---|
303 | ); |
---|
304 | $db_fulldirs[$fulldir] = $insert{'id'}; |
---|
305 | $next_rank[$insert{'id'}] = 1; |
---|
306 | } |
---|
307 | else |
---|
308 | { |
---|
309 | array_push( |
---|
310 | $errors, |
---|
311 | array( |
---|
312 | 'path' => $fulldir, |
---|
313 | 'type' => 'PWG-UPDATE-1' |
---|
314 | ) |
---|
315 | ); |
---|
316 | } |
---|
317 | } |
---|
318 | |
---|
319 | if (count($inserts) > 0) |
---|
320 | { |
---|
321 | if (!$simulate) |
---|
322 | { |
---|
323 | $dbfields = array( |
---|
324 | 'id','dir','name','site_id','id_uppercat','uppercats','commentable', |
---|
325 | 'uploadable','visible','status','rank','global_rank' |
---|
326 | ); |
---|
327 | mass_inserts(CATEGORIES_TABLE, $dbfields, $inserts); |
---|
328 | } |
---|
329 | |
---|
330 | $counts['new_categories'] = count($inserts); |
---|
331 | } |
---|
332 | |
---|
333 | // to delete categories |
---|
334 | $to_delete = array(); |
---|
335 | foreach (array_diff(array_keys($db_fulldirs), $fs_fulldirs) as $fulldir) |
---|
336 | { |
---|
337 | array_push($to_delete, $db_fulldirs[$fulldir]); |
---|
338 | unset($db_fulldirs[$fulldir]); |
---|
339 | array_push($infos, array('path' => $fulldir, |
---|
340 | 'info' => l10n('update_research_deleted'))); |
---|
341 | } |
---|
342 | if (count($to_delete) > 0) |
---|
343 | { |
---|
344 | if (!$simulate) |
---|
345 | { |
---|
346 | delete_categories($to_delete); |
---|
347 | } |
---|
348 | $counts['del_categories'] = count($to_delete); |
---|
349 | } |
---|
350 | |
---|
351 | echo '<!-- scanning dirs : '; |
---|
352 | echo get_elapsed_time($start, get_moment()); |
---|
353 | echo ' -->'."\n"; |
---|
354 | } |
---|
355 | // +-----------------------------------------------------------------------+ |
---|
356 | // | files / elements | |
---|
357 | // +-----------------------------------------------------------------------+ |
---|
358 | if (isset($_POST['submit']) and $_POST['sync'] == 'files' |
---|
359 | and !$general_failure) |
---|
360 | { |
---|
361 | $start_files = get_moment(); |
---|
362 | $start= $start_files; |
---|
363 | |
---|
364 | $fs = $site_reader->get_elements($basedir); |
---|
365 | //print_r($fs); echo "<br>"; |
---|
366 | echo '<!-- get_elements: '.get_elapsed_time($start, get_moment())." -->\n"; |
---|
367 | |
---|
368 | $cat_ids = array_diff(array_keys($db_categories), $to_delete); |
---|
369 | |
---|
370 | $db_elements = array(); |
---|
371 | $db_unvalidated = array(); |
---|
372 | |
---|
373 | if (count($cat_ids) > 0) |
---|
374 | { |
---|
375 | $query = ' |
---|
376 | SELECT id, path |
---|
377 | FROM '.IMAGES_TABLE.' |
---|
378 | WHERE storage_category_id IN (' |
---|
379 | .wordwrap( |
---|
380 | implode(', ', $cat_ids), |
---|
381 | 80, |
---|
382 | "\n" |
---|
383 | ).') |
---|
384 | ;'; |
---|
385 | $result = pwg_query($query); |
---|
386 | while ($row = mysql_fetch_array($result)) |
---|
387 | { |
---|
388 | $db_elements[$row['id']] = $row['path']; |
---|
389 | } |
---|
390 | |
---|
391 | // searching the unvalidated waiting elements (they must not be taken into |
---|
392 | // account) |
---|
393 | $query = ' |
---|
394 | SELECT file,storage_category_id |
---|
395 | FROM '.WAITING_TABLE.' |
---|
396 | WHERE storage_category_id IN ( |
---|
397 | '.wordwrap(implode(', ', $cat_ids), 80, "\n").') |
---|
398 | AND validated = \'false\' |
---|
399 | ;'; |
---|
400 | $result = pwg_query($query); |
---|
401 | while ($row = mysql_fetch_array($result)) |
---|
402 | { |
---|
403 | array_push( |
---|
404 | $db_unvalidated, |
---|
405 | array_search( |
---|
406 | $row['storage_category_id'], |
---|
407 | $db_fulldirs) |
---|
408 | .'/'.$row['file'] |
---|
409 | ); |
---|
410 | } |
---|
411 | } |
---|
412 | |
---|
413 | // next element id available |
---|
414 | $query = ' |
---|
415 | SELECT IF(MAX(id)+1 IS NULL, 1, MAX(id)+1) AS next_element_id |
---|
416 | FROM '.IMAGES_TABLE.' |
---|
417 | ;'; |
---|
418 | list($next_element_id) = mysql_fetch_array(pwg_query($query)); |
---|
419 | |
---|
420 | $start = get_moment(); |
---|
421 | |
---|
422 | $inserts = array(); |
---|
423 | $insert_links = array(); |
---|
424 | |
---|
425 | foreach (array_diff(array_keys($fs), $db_elements, $db_unvalidated) as $path) |
---|
426 | { |
---|
427 | $insert = array(); |
---|
428 | // storage category must exist |
---|
429 | $dirname = dirname($path); |
---|
430 | if (!isset($db_fulldirs[$dirname])) |
---|
431 | { |
---|
432 | continue; |
---|
433 | } |
---|
434 | $filename = basename($path); |
---|
435 | if (!preg_match('/^[a-zA-Z0-9-_.]+$/', $filename)) |
---|
436 | { |
---|
437 | array_push( |
---|
438 | $errors, |
---|
439 | array( |
---|
440 | 'path' => $path, |
---|
441 | 'type' => 'PWG-UPDATE-1' |
---|
442 | ) |
---|
443 | ); |
---|
444 | |
---|
445 | continue; |
---|
446 | } |
---|
447 | |
---|
448 | // 2 cases : the element is a picture or not. Indeed, for a picture |
---|
449 | // thumbnail is mandatory and for non picture element, thumbnail and |
---|
450 | // representative are optionnal |
---|
451 | if ( isset( $conf['flip_picture_ext'][get_extension($filename)] ) ) |
---|
452 | { |
---|
453 | // if we found a thumnbnail corresponding to our picture... |
---|
454 | if (isset($fs[$path]['tn_ext'])) |
---|
455 | { |
---|
456 | $insert = array( |
---|
457 | 'id' => $next_element_id++, |
---|
458 | 'file' => $filename, |
---|
459 | 'date_available' => CURRENT_DATE, |
---|
460 | 'tn_ext' => $fs[$path]['tn_ext'], |
---|
461 | 'path' => $path, |
---|
462 | 'storage_category_id' => $db_fulldirs[$dirname], |
---|
463 | ); |
---|
464 | |
---|
465 | array_push( |
---|
466 | $inserts, |
---|
467 | $insert |
---|
468 | ); |
---|
469 | |
---|
470 | array_push( |
---|
471 | $insert_links, |
---|
472 | array( |
---|
473 | 'image_id' => $insert{'id'}, |
---|
474 | 'category_id' => $insert['storage_category_id'], |
---|
475 | ) |
---|
476 | ); |
---|
477 | array_push( |
---|
478 | $infos, |
---|
479 | array( |
---|
480 | 'path' => $insert{'path'}, |
---|
481 | 'info' => l10n('update_research_added') |
---|
482 | ) |
---|
483 | ); |
---|
484 | } |
---|
485 | else |
---|
486 | { |
---|
487 | array_push( |
---|
488 | $errors, |
---|
489 | array( |
---|
490 | 'path' => $path, |
---|
491 | 'type' => 'PWG-UPDATE-2' |
---|
492 | ) |
---|
493 | ); |
---|
494 | } |
---|
495 | } |
---|
496 | else |
---|
497 | { |
---|
498 | $insert = array( |
---|
499 | 'id' => $next_element_id++, |
---|
500 | 'file' => $filename, |
---|
501 | 'date_available' => CURRENT_DATE, |
---|
502 | 'path' => $path, |
---|
503 | 'tn_ext' => isset($fs[$path]['tn_ext']) |
---|
504 | ? $fs[$path]['tn_ext'] |
---|
505 | : null, |
---|
506 | 'storage_category_id' => $db_fulldirs[$dirname], |
---|
507 | ); |
---|
508 | |
---|
509 | array_push( |
---|
510 | $inserts, |
---|
511 | $insert |
---|
512 | ); |
---|
513 | |
---|
514 | array_push( |
---|
515 | $insert_links, |
---|
516 | array( |
---|
517 | 'image_id' => $insert{'id'}, |
---|
518 | 'category_id' => $insert['storage_category_id'], |
---|
519 | ) |
---|
520 | ); |
---|
521 | |
---|
522 | array_push( |
---|
523 | $infos, |
---|
524 | array( |
---|
525 | 'path' => $insert{'path'}, |
---|
526 | 'info' => l10n('update_research_added') |
---|
527 | ) |
---|
528 | ); |
---|
529 | } |
---|
530 | } |
---|
531 | |
---|
532 | if (count($inserts) > 0) |
---|
533 | { |
---|
534 | if (!$simulate) |
---|
535 | { |
---|
536 | // inserts all new elements |
---|
537 | mass_inserts( |
---|
538 | IMAGES_TABLE, |
---|
539 | array_keys($inserts[0]), |
---|
540 | $inserts |
---|
541 | ); |
---|
542 | |
---|
543 | // inserts all links between new elements and their storage category |
---|
544 | mass_inserts( |
---|
545 | IMAGE_CATEGORY_TABLE, |
---|
546 | array_keys($insert_links[0]), |
---|
547 | $insert_links |
---|
548 | ); |
---|
549 | } |
---|
550 | $counts['new_elements'] = count($inserts); |
---|
551 | } |
---|
552 | |
---|
553 | // delete elements that are in database but not in the filesystem |
---|
554 | $to_delete_elements = array(); |
---|
555 | foreach (array_diff($db_elements, array_keys($fs)) as $path) |
---|
556 | { |
---|
557 | array_push($to_delete_elements, array_search($path, $db_elements)); |
---|
558 | array_push($infos, array('path' => $path, |
---|
559 | 'info' => l10n('update_research_deleted'))); |
---|
560 | } |
---|
561 | if (count($to_delete_elements) > 0) |
---|
562 | { |
---|
563 | if (!$simulate) |
---|
564 | { |
---|
565 | delete_elements($to_delete_elements); |
---|
566 | } |
---|
567 | $counts['del_elements'] = count($to_delete_elements); |
---|
568 | } |
---|
569 | |
---|
570 | echo '<!-- scanning files : '; |
---|
571 | echo get_elapsed_time($start_files, get_moment()); |
---|
572 | echo ' -->'."\n"; |
---|
573 | |
---|
574 | // retrieving informations given by uploaders |
---|
575 | if (!$simulate and count($cat_ids) > 0) |
---|
576 | { |
---|
577 | $query = ' |
---|
578 | SELECT id,file,storage_category_id,infos |
---|
579 | FROM '.WAITING_TABLE.' |
---|
580 | WHERE storage_category_id IN ( |
---|
581 | '.wordwrap(implode(', ', $cat_ids), 80, "\n").') |
---|
582 | AND validated = \'true\' |
---|
583 | ;'; |
---|
584 | $result = pwg_query($query); |
---|
585 | |
---|
586 | $datas = array(); |
---|
587 | $fields = |
---|
588 | array( |
---|
589 | 'primary' => array('id'), |
---|
590 | 'update' => array('date_creation', 'author', 'name', 'comment') |
---|
591 | ); |
---|
592 | |
---|
593 | $waiting_to_delete = array(); |
---|
594 | |
---|
595 | while ($row = mysql_fetch_array($result)) |
---|
596 | { |
---|
597 | $data = array(); |
---|
598 | |
---|
599 | $query = ' |
---|
600 | SELECT id |
---|
601 | FROM '.IMAGES_TABLE.' |
---|
602 | WHERE storage_category_id = '.$row['storage_category_id'].' |
---|
603 | AND file = \''.$row['file'].'\' |
---|
604 | ;'; |
---|
605 | list($data['id']) = mysql_fetch_array(pwg_query($query)); |
---|
606 | |
---|
607 | foreach ($fields['update'] as $field) |
---|
608 | { |
---|
609 | $data[$field] = addslashes( getAttribute($row['infos'], $field) ); |
---|
610 | } |
---|
611 | |
---|
612 | array_push($datas, $data); |
---|
613 | array_push($waiting_to_delete, $row['id']); |
---|
614 | } |
---|
615 | |
---|
616 | if (count($datas) > 0) |
---|
617 | { |
---|
618 | mass_updates(IMAGES_TABLE, $fields, $datas); |
---|
619 | |
---|
620 | // delete now useless waiting elements |
---|
621 | $query = ' |
---|
622 | DELETE |
---|
623 | FROM '.WAITING_TABLE.' |
---|
624 | WHERE id IN ('.implode(',', $waiting_to_delete).') |
---|
625 | ;'; |
---|
626 | pwg_query($query); |
---|
627 | } |
---|
628 | } |
---|
629 | } |
---|
630 | |
---|
631 | // +-----------------------------------------------------------------------+ |
---|
632 | // | synchronize files | |
---|
633 | // +-----------------------------------------------------------------------+ |
---|
634 | if (isset($_POST['submit']) |
---|
635 | and ($_POST['sync'] == 'dirs' or $_POST['sync'] == 'files') |
---|
636 | and !$general_failure ) |
---|
637 | { |
---|
638 | if (!$simulate) |
---|
639 | { |
---|
640 | $start = get_moment(); |
---|
641 | update_category('all'); |
---|
642 | echo '<!-- update_category(all) : '; |
---|
643 | echo get_elapsed_time($start,get_moment()); |
---|
644 | echo ' -->'."\n"; |
---|
645 | $start = get_moment(); |
---|
646 | ordering(); |
---|
647 | update_global_rank(); |
---|
648 | echo '<!-- ordering categories : '; |
---|
649 | echo get_elapsed_time($start, get_moment()); |
---|
650 | echo ' -->'."\n"; |
---|
651 | } |
---|
652 | |
---|
653 | if ($_POST['sync'] == 'files') |
---|
654 | { |
---|
655 | $start = get_moment(); |
---|
656 | $opts['category_id'] = ''; |
---|
657 | $opts['recursive'] = true; |
---|
658 | if (isset($_POST['cat'])) |
---|
659 | { |
---|
660 | $opts['category_id'] = $_POST['cat']; |
---|
661 | if (!isset($_POST['subcats-included']) or $_POST['subcats-included'] != 1) |
---|
662 | { |
---|
663 | $opts['recursive'] = false; |
---|
664 | } |
---|
665 | } |
---|
666 | $files = get_filelist($opts['category_id'], $site_id, |
---|
667 | $opts['recursive'], |
---|
668 | false); |
---|
669 | echo '<!-- get_filelist : '; |
---|
670 | echo get_elapsed_time($start, get_moment()); |
---|
671 | echo ' -->'."\n"; |
---|
672 | $start = get_moment(); |
---|
673 | |
---|
674 | $datas = array(); |
---|
675 | foreach ( $files as $id=>$file ) |
---|
676 | { |
---|
677 | $data = $site_reader->get_element_update_attributes($file); |
---|
678 | if ( !is_array($data) ) |
---|
679 | { |
---|
680 | continue; |
---|
681 | } |
---|
682 | $extension = get_extension($file); |
---|
683 | if ( isset($conf['flip_picture_ext'][$extension]) ) |
---|
684 | { |
---|
685 | if ( !isset($data['tn_ext']) ) |
---|
686 | { |
---|
687 | array_push( |
---|
688 | $errors, |
---|
689 | array( |
---|
690 | 'path' => $file, |
---|
691 | 'type' => 'PWG-UPDATE-2' |
---|
692 | ) |
---|
693 | ); |
---|
694 | continue; |
---|
695 | } |
---|
696 | } |
---|
697 | |
---|
698 | $data['id']=$id; |
---|
699 | array_push($datas, $data); |
---|
700 | } // end foreach file |
---|
701 | |
---|
702 | $counts['upd_elements'] = count($datas); |
---|
703 | if (!$simulate and count($datas)>0 ) |
---|
704 | { |
---|
705 | mass_updates( |
---|
706 | IMAGES_TABLE, |
---|
707 | // fields |
---|
708 | array( |
---|
709 | 'primary' => array('id'), |
---|
710 | 'update' => $site_reader->get_update_attributes(), |
---|
711 | ), |
---|
712 | $datas |
---|
713 | ); |
---|
714 | } |
---|
715 | echo '<!-- update files : '; |
---|
716 | echo get_elapsed_time($start,get_moment()); |
---|
717 | echo ' -->'."\n"; |
---|
718 | }// end if sync files |
---|
719 | } |
---|
720 | |
---|
721 | // +-----------------------------------------------------------------------+ |
---|
722 | // | synchronize files | |
---|
723 | // +-----------------------------------------------------------------------+ |
---|
724 | if (isset($_POST['submit']) |
---|
725 | and ($_POST['sync'] == 'dirs' or $_POST['sync'] == 'files')) |
---|
726 | { |
---|
727 | $template->assign_block_vars( |
---|
728 | 'update_result', |
---|
729 | array( |
---|
730 | 'NB_NEW_CATEGORIES'=>$counts['new_categories'], |
---|
731 | 'NB_DEL_CATEGORIES'=>$counts['del_categories'], |
---|
732 | 'NB_NEW_ELEMENTS'=>$counts['new_elements'], |
---|
733 | 'NB_DEL_ELEMENTS'=>$counts['del_elements'], |
---|
734 | 'NB_UPD_ELEMENTS'=>$counts['upd_elements'], |
---|
735 | 'NB_ERRORS'=>count($errors), |
---|
736 | )); |
---|
737 | } |
---|
738 | |
---|
739 | // +-----------------------------------------------------------------------+ |
---|
740 | // | synchronize metadata | |
---|
741 | // +-----------------------------------------------------------------------+ |
---|
742 | if (isset($_POST['submit']) and preg_match('/^metadata/', $_POST['sync']) |
---|
743 | and !$general_failure) |
---|
744 | { |
---|
745 | // sync only never synchronized files ? |
---|
746 | if ($_POST['sync'] == 'metadata_new') |
---|
747 | { |
---|
748 | $opts['only_new'] = true; |
---|
749 | } |
---|
750 | else |
---|
751 | { |
---|
752 | $opts['only_new'] = false; |
---|
753 | } |
---|
754 | $opts['category_id'] = ''; |
---|
755 | $opts['recursive'] = true; |
---|
756 | |
---|
757 | if (isset($_POST['cat'])) |
---|
758 | { |
---|
759 | $opts['category_id'] = $_POST['cat']; |
---|
760 | // recursive ? |
---|
761 | if (!isset($_POST['subcats-included']) or $_POST['subcats-included'] != 1) |
---|
762 | { |
---|
763 | $opts['recursive'] = false; |
---|
764 | } |
---|
765 | } |
---|
766 | $start = get_moment(); |
---|
767 | $files = get_filelist($opts['category_id'], $site_id, |
---|
768 | $opts['recursive'], |
---|
769 | $opts['only_new']); |
---|
770 | |
---|
771 | echo '<!-- get_filelist : '; |
---|
772 | echo get_elapsed_time($start, get_moment()); |
---|
773 | echo ' -->'."\n"; |
---|
774 | |
---|
775 | $start = get_moment(); |
---|
776 | $datas = array(); |
---|
777 | $tags_of = array(); |
---|
778 | foreach ( $files as $id=>$file ) |
---|
779 | { |
---|
780 | $data = $site_reader->get_element_metadata($file); |
---|
781 | if ( is_array($data) ) |
---|
782 | { |
---|
783 | $data['date_metadata_update'] = CURRENT_DATE; |
---|
784 | $data['id']=$id; |
---|
785 | array_push($datas, $data); |
---|
786 | |
---|
787 | foreach (array('keywords', 'tags') as $key) |
---|
788 | { |
---|
789 | if (isset($data[$key])) |
---|
790 | { |
---|
791 | if (!isset($tags_of[$id])) |
---|
792 | { |
---|
793 | $tags_of[$id] = array(); |
---|
794 | } |
---|
795 | |
---|
796 | foreach (explode(',', $data[$key]) as $tag_name) |
---|
797 | { |
---|
798 | array_push( |
---|
799 | $tags_of[$id], |
---|
800 | tag_id_from_tag_name($tag_name) |
---|
801 | ); |
---|
802 | } |
---|
803 | } |
---|
804 | } |
---|
805 | } |
---|
806 | else |
---|
807 | { |
---|
808 | array_push($errors, array('path' => $file, 'type' => 'PWG-ERROR-NO-FS')); |
---|
809 | } |
---|
810 | } |
---|
811 | |
---|
812 | if (!$simulate) |
---|
813 | { |
---|
814 | if (count($datas) > 0) |
---|
815 | { |
---|
816 | mass_updates( |
---|
817 | IMAGES_TABLE, |
---|
818 | // fields |
---|
819 | array( |
---|
820 | 'primary' => array('id'), |
---|
821 | 'update' => array_unique( |
---|
822 | array_merge( |
---|
823 | array_diff( |
---|
824 | $site_reader->get_metadata_attributes(), |
---|
825 | // keywords and tags fields are managed separately |
---|
826 | array('keywords', 'tags') |
---|
827 | ), |
---|
828 | array('date_metadata_update')) |
---|
829 | ) |
---|
830 | ), |
---|
831 | $datas |
---|
832 | ); |
---|
833 | } |
---|
834 | set_tags_of($tags_of); |
---|
835 | } |
---|
836 | |
---|
837 | echo '<!-- metadata update : '; |
---|
838 | echo get_elapsed_time($start, get_moment()); |
---|
839 | echo ' -->'."\n"; |
---|
840 | |
---|
841 | $template->assign_block_vars( |
---|
842 | 'metadata_result', |
---|
843 | array( |
---|
844 | 'NB_ELEMENTS_DONE' => count($datas), |
---|
845 | 'NB_ELEMENTS_CANDIDATES' => count($files), |
---|
846 | 'NB_ERRORS' => count($errors), |
---|
847 | )); |
---|
848 | } |
---|
849 | |
---|
850 | // +-----------------------------------------------------------------------+ |
---|
851 | // | template initialization | |
---|
852 | // +-----------------------------------------------------------------------+ |
---|
853 | $template->set_filenames(array('update'=>'admin/site_update.tpl')); |
---|
854 | $result_title = ''; |
---|
855 | if (isset($simulate) and $simulate) |
---|
856 | { |
---|
857 | $result_title.= l10n('update_simulation_title').' '; |
---|
858 | } |
---|
859 | |
---|
860 | // used_metadata string is displayed to inform admin which metadata will be |
---|
861 | // used from files for synchronization |
---|
862 | $used_metadata = implode( ', ', $site_reader->get_metadata_attributes()); |
---|
863 | if ($site_is_remote and !isset($_POST['submit']) ) |
---|
864 | { |
---|
865 | $used_metadata.= ' + ...'; |
---|
866 | } |
---|
867 | |
---|
868 | $template->assign_vars( |
---|
869 | array( |
---|
870 | 'SITE_URL'=>$site_url, |
---|
871 | 'U_SITE_MANAGER'=> PHPWG_ROOT_PATH.'admin.php?page=site_manager', |
---|
872 | 'L_RESULT_UPDATE'=>$result_title.l10n('update_part_research'), |
---|
873 | 'L_RESULT_METADATA'=>$result_title.l10n('update_result_metadata'), |
---|
874 | 'METADATA_LIST' => $used_metadata |
---|
875 | )); |
---|
876 | |
---|
877 | $template->assign_vars( |
---|
878 | array( |
---|
879 | 'U_HELP' => PHPWG_ROOT_PATH.'popuphelp.php?page=synchronize' |
---|
880 | ) |
---|
881 | ); |
---|
882 | // +-----------------------------------------------------------------------+ |
---|
883 | // | introduction : choices | |
---|
884 | // +-----------------------------------------------------------------------+ |
---|
885 | if (!isset($_POST['submit']) or (isset($simulate) and $simulate)) |
---|
886 | { |
---|
887 | $template->assign_block_vars('introduction', array()); |
---|
888 | |
---|
889 | if (isset($simulate) and $simulate) |
---|
890 | { |
---|
891 | switch ($_POST['sync']) |
---|
892 | { |
---|
893 | case 'dirs' : |
---|
894 | { |
---|
895 | $template->assign_vars( |
---|
896 | array('SYNC_DIRS_CHECKED'=>'checked="checked"')); |
---|
897 | break; |
---|
898 | } |
---|
899 | case 'files' : |
---|
900 | { |
---|
901 | $template->assign_vars( |
---|
902 | array('SYNC_ALL_CHECKED'=>'checked="checked"')); |
---|
903 | break; |
---|
904 | } |
---|
905 | case 'metadata_new' : |
---|
906 | { |
---|
907 | $template->assign_vars( |
---|
908 | array('SYNC_META_NEW_CHECKED'=>'checked="checked"')); |
---|
909 | break; |
---|
910 | } |
---|
911 | case 'metadata_all' : |
---|
912 | { |
---|
913 | $template->assign_vars( |
---|
914 | array('SYNC_META_ALL_CHECKED'=>'checked="checked"')); |
---|
915 | break; |
---|
916 | } |
---|
917 | } |
---|
918 | |
---|
919 | if (isset($_POST['display_info']) and $_POST['display_info'] == 1) |
---|
920 | { |
---|
921 | $template->assign_vars( |
---|
922 | array('DISPLAY_INFO_CHECKED'=>'checked="checked"')); |
---|
923 | } |
---|
924 | |
---|
925 | if (isset($_POST['subcats-included']) and $_POST['subcats-included'] == 1) |
---|
926 | { |
---|
927 | $template->assign_vars( |
---|
928 | array('SUBCATS_INCLUDED_CHECKED'=>'checked="checked"')); |
---|
929 | } |
---|
930 | |
---|
931 | if (isset($_POST['cat']) and is_numeric($_POST['cat'])) |
---|
932 | { |
---|
933 | $cat_selected = array($_POST['cat']); |
---|
934 | } |
---|
935 | else |
---|
936 | { |
---|
937 | $cat_selected = array(); |
---|
938 | } |
---|
939 | } |
---|
940 | else |
---|
941 | { |
---|
942 | $template->assign_vars( |
---|
943 | array('SYNC_DIRS_CHECKED' => 'checked="checked"', |
---|
944 | 'SUBCATS_INCLUDED_CHECKED'=>'checked="checked"')); |
---|
945 | |
---|
946 | $cat_selected = array(); |
---|
947 | } |
---|
948 | |
---|
949 | $query = ' |
---|
950 | SELECT id,name,uppercats,global_rank |
---|
951 | FROM '.CATEGORIES_TABLE.' |
---|
952 | WHERE site_id = '.$site_id.' |
---|
953 | ;'; |
---|
954 | display_select_cat_wrapper($query, |
---|
955 | $cat_selected, |
---|
956 | 'introduction.category_option', |
---|
957 | false); |
---|
958 | } |
---|
959 | |
---|
960 | if (count($errors) > 0) |
---|
961 | { |
---|
962 | $template->assign_block_vars('sync_errors', array()); |
---|
963 | foreach ($errors as $error) |
---|
964 | { |
---|
965 | $template->assign_block_vars( |
---|
966 | 'sync_errors.error', |
---|
967 | array( |
---|
968 | 'ELEMENT' => $error['path'], |
---|
969 | 'LABEL' => $error['type'].' ('.$error_labels[$error['type']][0].')' |
---|
970 | )); |
---|
971 | } |
---|
972 | |
---|
973 | foreach ($error_labels as $error_type=>$error_description) |
---|
974 | { |
---|
975 | $template->assign_block_vars( |
---|
976 | 'sync_errors.error_caption', |
---|
977 | array( |
---|
978 | 'TYPE' => $error_type, |
---|
979 | 'LABEL' => $error_description[1] |
---|
980 | )); |
---|
981 | } |
---|
982 | |
---|
983 | } |
---|
984 | if (count($infos) > 0 |
---|
985 | and isset($_POST['display_info']) |
---|
986 | and $_POST['display_info'] == 1) |
---|
987 | { |
---|
988 | $template->assign_block_vars('sync_infos', array()); |
---|
989 | foreach ($infos as $info) |
---|
990 | { |
---|
991 | $template->assign_block_vars( |
---|
992 | 'sync_infos.info', |
---|
993 | array( |
---|
994 | 'ELEMENT' => $info['path'], |
---|
995 | 'LABEL' => $info['info'] |
---|
996 | )); |
---|
997 | } |
---|
998 | } |
---|
999 | |
---|
1000 | // +-----------------------------------------------------------------------+ |
---|
1001 | // | sending html code | |
---|
1002 | // +-----------------------------------------------------------------------+ |
---|
1003 | $template->assign_var_from_handle('ADMIN_CONTENT', 'update'); |
---|
1004 | ?> |
---|