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