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