[10] | 1 | <?php |
---|
[362] | 2 | // +-----------------------------------------------------------------------+ |
---|
[593] | 3 | // | PhpWebGallery - a PHP based picture gallery | |
---|
| 4 | // | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net | |
---|
[675] | 5 | // | Copyright (C) 2003-2005 PhpWebGallery Team - http://phpwebgallery.net | |
---|
[362] | 6 | // +-----------------------------------------------------------------------+ |
---|
[593] | 7 | // | branch : BSF (Best So Far) |
---|
[362] | 8 | // | file : $RCSfile$ |
---|
| 9 | // | last update : $Date: 2005-12-03 22:03:58 +0000 (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 | // +-----------------------------------------------------------------------+ |
---|
[10] | 27 | |
---|
[659] | 28 | if (!defined('PHPWG_ROOT_PATH')) |
---|
[520] | 29 | { |
---|
[659] | 30 | die ('Hacking attempt!'); |
---|
[520] | 31 | } |
---|
[466] | 32 | include_once( PHPWG_ROOT_PATH.'admin/include/isadmin.inc.php'); |
---|
[486] | 33 | |
---|
[801] | 34 | list($dbnow) = mysql_fetch_row(pwg_query('SELECT NOW();')); |
---|
| 35 | define('CURRENT_DATE', $dbnow); |
---|
| 36 | |
---|
[659] | 37 | $error_labels = array('PWG-UPDATE-1' => $lang['update_wrong_dirname_short'], |
---|
| 38 | 'PWG-UPDATE-2' => $lang['update_missing_tn_short']); |
---|
| 39 | $errors = array(); |
---|
| 40 | $infos = array(); |
---|
[589] | 41 | // +-----------------------------------------------------------------------+ |
---|
[659] | 42 | // | directories / categories | |
---|
[589] | 43 | // +-----------------------------------------------------------------------+ |
---|
[659] | 44 | if (isset($_POST['submit']) |
---|
| 45 | and ($_POST['sync'] == 'dirs' or $_POST['sync'] == 'files')) |
---|
[10] | 46 | { |
---|
[659] | 47 | $counts['new_categories'] = 0; |
---|
| 48 | $counts['del_categories'] = 0; |
---|
| 49 | $counts['del_elements'] = 0; |
---|
| 50 | $counts['new_elements'] = 0; |
---|
[228] | 51 | |
---|
[659] | 52 | // shall we simulate only |
---|
| 53 | if (isset($_POST['simulate']) and $_POST['simulate'] == 1) |
---|
[345] | 54 | { |
---|
[659] | 55 | $simulate = true; |
---|
| 56 | } |
---|
| 57 | else |
---|
| 58 | { |
---|
| 59 | $simulate = false; |
---|
| 60 | } |
---|
| 61 | |
---|
| 62 | $start = get_moment(); |
---|
| 63 | // which categories to update ? |
---|
| 64 | $cat_ids = array(); |
---|
[228] | 65 | |
---|
[659] | 66 | $query = ' |
---|
| 67 | SELECT id, uppercats, global_rank, status, visible |
---|
[642] | 68 | FROM '.CATEGORIES_TABLE.' |
---|
[659] | 69 | WHERE dir IS NOT NULL |
---|
| 70 | AND site_id = 1'; |
---|
| 71 | if (isset($_POST['cat']) and is_numeric($_POST['cat'])) |
---|
| 72 | { |
---|
| 73 | if (isset($_POST['subcats-included']) and $_POST['subcats-included'] == 1) |
---|
[345] | 74 | { |
---|
[659] | 75 | $query.= ' |
---|
| 76 | AND uppercats REGEXP \'(^|,)'.$_POST['cat'].'(,|$)\' |
---|
| 77 | '; |
---|
[345] | 78 | } |
---|
[659] | 79 | else |
---|
[466] | 80 | { |
---|
[659] | 81 | $query.= ' |
---|
| 82 | AND id = '.$_POST['cat'].' |
---|
| 83 | '; |
---|
[345] | 84 | } |
---|
[10] | 85 | } |
---|
[466] | 86 | $query.= ' |
---|
| 87 | ;'; |
---|
[587] | 88 | $result = pwg_query($query); |
---|
[659] | 89 | |
---|
| 90 | $db_categories = array(); |
---|
[466] | 91 | while ($row = mysql_fetch_array($result)) |
---|
| 92 | { |
---|
[659] | 93 | $db_categories[$row['id']] = $row; |
---|
[10] | 94 | } |
---|
[659] | 95 | |
---|
| 96 | // get categort full directories in an array for comparison with file |
---|
| 97 | // system directory tree |
---|
| 98 | $db_fulldirs = get_fulldirs(array_keys($db_categories)); |
---|
[345] | 99 | |
---|
[659] | 100 | // what is the base directory to search file system sub-directories ? |
---|
| 101 | if (isset($_POST['cat']) and is_numeric($_POST['cat'])) |
---|
[466] | 102 | { |
---|
[659] | 103 | $basedir = $db_fulldirs[$_POST['cat']]; |
---|
[345] | 104 | } |
---|
[659] | 105 | else |
---|
[486] | 106 | { |
---|
[659] | 107 | $query = ' |
---|
| 108 | SELECT galleries_url |
---|
| 109 | FROM '.SITES_TABLE.' |
---|
| 110 | WHERE id = 1 |
---|
| 111 | ;'; |
---|
| 112 | list($galleries_url) = mysql_fetch_array(pwg_query($query)); |
---|
| 113 | $basedir = preg_replace('#/*$#', '', $galleries_url); |
---|
[486] | 114 | } |
---|
[345] | 115 | |
---|
[659] | 116 | // we need to have fulldirs as keys to make efficient comparison |
---|
| 117 | $db_fulldirs = array_flip($db_fulldirs); |
---|
[642] | 118 | |
---|
[913] | 119 | // finding next rank for each id_uppercat. By default, each category id |
---|
| 120 | // has 1 for next rank on its sub-categories to create |
---|
[660] | 121 | $next_rank['NULL'] = 1; |
---|
| 122 | |
---|
[659] | 123 | $query = ' |
---|
[913] | 124 | SELECT id |
---|
| 125 | FROM '.CATEGORIES_TABLE.' |
---|
| 126 | ;'; |
---|
| 127 | $result = pwg_query($query); |
---|
| 128 | while ($row = mysql_fetch_array($result)) |
---|
| 129 | { |
---|
| 130 | $next_rank[$row['id']] = 1; |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | // let's see if some categories already have some sub-categories... |
---|
| 134 | $query = ' |
---|
[659] | 135 | SELECT id_uppercat, MAX(rank)+1 AS next_rank |
---|
| 136 | FROM '.CATEGORIES_TABLE.' |
---|
| 137 | GROUP BY id_uppercat |
---|
| 138 | ;'; |
---|
| 139 | $result = pwg_query($query); |
---|
| 140 | while ($row = mysql_fetch_array($result)) |
---|
[642] | 141 | { |
---|
[659] | 142 | // for the id_uppercat NULL, we write 'NULL' and not the empty string |
---|
| 143 | if (!isset($row['id_uppercat']) or $row['id_uppercat'] == '') |
---|
[642] | 144 | { |
---|
[659] | 145 | $row['id_uppercat'] = 'NULL'; |
---|
[642] | 146 | } |
---|
[659] | 147 | $next_rank[$row['id_uppercat']] = $row['next_rank']; |
---|
[642] | 148 | } |
---|
[345] | 149 | |
---|
[659] | 150 | // next category id available |
---|
| 151 | $query = ' |
---|
[660] | 152 | SELECT IF(MAX(id)+1 IS NULL, 1, MAX(id)+1) AS next_id |
---|
[659] | 153 | FROM '.CATEGORIES_TABLE.' |
---|
| 154 | ;'; |
---|
| 155 | list($next_id) = mysql_fetch_array(pwg_query($query)); |
---|
| 156 | |
---|
| 157 | // retrieve file system sub-directories fulldirs |
---|
| 158 | $fs_fulldirs = get_fs_directories($basedir); |
---|
[666] | 159 | // get_fs_directories doesn't include the base directory, so if it's a |
---|
| 160 | // category directory, we need to include it in our array |
---|
| 161 | if (isset($_POST['cat'])) |
---|
| 162 | { |
---|
| 163 | array_push($fs_fulldirs, $basedir); |
---|
| 164 | } |
---|
[659] | 165 | |
---|
| 166 | $inserts = array(); |
---|
| 167 | // new categories are the directories not present yet in the database |
---|
| 168 | foreach (array_diff($fs_fulldirs, array_keys($db_fulldirs)) as $fulldir) |
---|
[466] | 169 | { |
---|
[659] | 170 | $dir = basename($fulldir); |
---|
| 171 | if (preg_match('/^[a-zA-Z0-9-_.]+$/', $dir)) |
---|
[10] | 172 | { |
---|
[642] | 173 | $insert = array(); |
---|
| 174 | |
---|
[659] | 175 | $insert{'id'} = $next_id++; |
---|
| 176 | $insert{'dir'} = $dir; |
---|
| 177 | $insert{'name'} = str_replace('_', ' ', $dir); |
---|
| 178 | $insert{'site_id'} = 1; |
---|
| 179 | $insert{'commentable'} = $conf['newcat_default_commentable']; |
---|
| 180 | $insert{'uploadable'} = $conf['newcat_default_uploadable']; |
---|
| 181 | $insert{'status'} = $conf{'newcat_default_status'}; |
---|
| 182 | $insert{'visible'} = $conf{'newcat_default_visible'}; |
---|
| 183 | |
---|
| 184 | if (isset($db_fulldirs[dirname($fulldir)])) |
---|
[10] | 185 | { |
---|
[659] | 186 | $parent = $db_fulldirs[dirname($fulldir)]; |
---|
[345] | 187 | |
---|
[659] | 188 | $insert{'id_uppercat'} = $parent; |
---|
| 189 | $insert{'uppercats'} = |
---|
| 190 | $db_categories[$parent]['uppercats'].','.$insert{'id'}; |
---|
| 191 | $insert{'rank'} = $next_rank[$parent]++; |
---|
| 192 | $insert{'global_rank'} = |
---|
| 193 | $db_categories[$parent]['global_rank'].'.'.$insert{'rank'}; |
---|
| 194 | if ('private' == $db_categories[$parent]['status']) |
---|
[466] | 195 | { |
---|
[659] | 196 | $insert{'status'} = 'private'; |
---|
[466] | 197 | } |
---|
[659] | 198 | if ('false' == $db_categories[$parent]['visible']) |
---|
| 199 | { |
---|
| 200 | $insert{'visible'} = 'false'; |
---|
| 201 | } |
---|
[10] | 202 | } |
---|
[345] | 203 | else |
---|
| 204 | { |
---|
[659] | 205 | $insert{'uppercats'} = $insert{'id'}; |
---|
| 206 | $insert{'rank'} = $next_rank['NULL']++; |
---|
| 207 | $insert{'global_rank'} = $insert{'rank'}; |
---|
[345] | 208 | } |
---|
| 209 | |
---|
[659] | 210 | array_push($inserts, $insert); |
---|
| 211 | array_push($infos, array('path' => $fulldir, |
---|
| 212 | 'info' => $lang['update_research_added'])); |
---|
| 213 | |
---|
| 214 | // add the new category to $db_categories and $db_fulldirs array |
---|
| 215 | $db_categories[$insert{'id'}] = |
---|
| 216 | array( |
---|
| 217 | 'id' => $insert{'id'}, |
---|
| 218 | 'status' => $insert{'status'}, |
---|
| 219 | 'visible' => $insert{'visible'}, |
---|
| 220 | 'uppercats' => $insert{'uppercats'}, |
---|
| 221 | 'global_rank' => $insert{'global_rank'} |
---|
| 222 | ); |
---|
| 223 | $db_fulldirs[$fulldir] = $insert{'id'}; |
---|
| 224 | $next_rank[$insert{'id'}] = 1; |
---|
[466] | 225 | } |
---|
| 226 | else |
---|
| 227 | { |
---|
[659] | 228 | array_push($errors, array('path' => $fulldir, 'type' => 'PWG-UPDATE-1')); |
---|
[466] | 229 | } |
---|
[10] | 230 | } |
---|
[345] | 231 | |
---|
[659] | 232 | if (count($inserts) > 0) |
---|
[589] | 233 | { |
---|
[659] | 234 | if (!$simulate) |
---|
[589] | 235 | { |
---|
[659] | 236 | $dbfields = array( |
---|
| 237 | 'id','dir','name','site_id','id_uppercat','uppercats','commentable', |
---|
| 238 | 'uploadable','visible','status','rank','global_rank' |
---|
| 239 | ); |
---|
| 240 | mass_inserts(CATEGORIES_TABLE, $dbfields, $inserts); |
---|
[589] | 241 | } |
---|
[659] | 242 | |
---|
| 243 | $counts['new_categories'] = count($inserts); |
---|
[466] | 244 | } |
---|
[345] | 245 | |
---|
[659] | 246 | // to delete categories |
---|
| 247 | $to_delete = array(); |
---|
| 248 | foreach (array_diff(array_keys($db_fulldirs), $fs_fulldirs) as $fulldir) |
---|
[345] | 249 | { |
---|
[659] | 250 | array_push($to_delete, $db_fulldirs[$fulldir]); |
---|
| 251 | unset($db_fulldirs[$fulldir]); |
---|
| 252 | array_push($infos, array('path' => $fulldir, |
---|
| 253 | 'info' => $lang['update_research_deleted'])); |
---|
[10] | 254 | } |
---|
[659] | 255 | if (count($to_delete) > 0) |
---|
| 256 | { |
---|
| 257 | if (!$simulate) |
---|
| 258 | { |
---|
| 259 | delete_categories($to_delete); |
---|
| 260 | } |
---|
| 261 | $counts['del_categories'] = count($to_delete); |
---|
| 262 | } |
---|
| 263 | |
---|
[691] | 264 | echo '<!-- scanning dirs : '; |
---|
[659] | 265 | echo get_elapsed_time($start, get_moment()); |
---|
[691] | 266 | echo ' -->'."\n"; |
---|
[10] | 267 | } |
---|
[659] | 268 | // +-----------------------------------------------------------------------+ |
---|
| 269 | // | files / elements | |
---|
| 270 | // +-----------------------------------------------------------------------+ |
---|
| 271 | if (isset($_POST['submit']) and $_POST['sync'] == 'files') |
---|
| 272 | { |
---|
| 273 | $start_files = get_moment(); |
---|
| 274 | $start= $start_files; |
---|
[345] | 275 | |
---|
[659] | 276 | $fs = get_fs($basedir); |
---|
| 277 | |
---|
[691] | 278 | echo '<!-- get_fs : '.get_elapsed_time($start, get_moment()).' -->'."\n"; |
---|
[659] | 279 | |
---|
| 280 | $cat_ids = array_diff(array_keys($db_categories), $to_delete); |
---|
[10] | 281 | |
---|
[659] | 282 | $db_elements = array(); |
---|
| 283 | $db_unvalidated = array(); |
---|
| 284 | |
---|
| 285 | if (count($cat_ids) > 0) |
---|
| 286 | { |
---|
| 287 | $query = ' |
---|
| 288 | SELECT id, path |
---|
[466] | 289 | FROM '.IMAGES_TABLE.' |
---|
[659] | 290 | WHERE storage_category_id IN ( |
---|
| 291 | '.wordwrap(implode(', ', $cat_ids), 80, "\n").') |
---|
[466] | 292 | ;'; |
---|
[659] | 293 | $result = pwg_query($query); |
---|
| 294 | while ($row = mysql_fetch_array($result)) |
---|
[10] | 295 | { |
---|
[659] | 296 | $db_elements[$row['id']] = $row['path']; |
---|
[10] | 297 | } |
---|
[659] | 298 | |
---|
| 299 | // searching the unvalidated waiting elements (they must not be taken into |
---|
| 300 | // account) |
---|
| 301 | $query = ' |
---|
| 302 | SELECT file,storage_category_id |
---|
| 303 | FROM '.WAITING_TABLE.' |
---|
| 304 | WHERE storage_category_id IN ( |
---|
| 305 | '.wordwrap(implode(', ', $cat_ids), 80, "\n").') |
---|
| 306 | AND validated = \'false\' |
---|
[466] | 307 | ;'; |
---|
[659] | 308 | $result = pwg_query($query); |
---|
| 309 | while ($row = mysql_fetch_array($result)) |
---|
[345] | 310 | { |
---|
[659] | 311 | array_push( |
---|
| 312 | $db_unvalidated, |
---|
| 313 | array_search($row['storage_category_id'], |
---|
| 314 | $db_fulldirs).'/'.$row['file'] |
---|
| 315 | ); |
---|
[345] | 316 | } |
---|
[10] | 317 | } |
---|
[345] | 318 | |
---|
[659] | 319 | // next element id available |
---|
[466] | 320 | $query = ' |
---|
[660] | 321 | SELECT IF(MAX(id)+1 IS NULL, 1, MAX(id)+1) AS next_element_id |
---|
[659] | 322 | FROM '.IMAGES_TABLE.' |
---|
[466] | 323 | ;'; |
---|
[659] | 324 | list($next_element_id) = mysql_fetch_array(pwg_query($query)); |
---|
[345] | 325 | |
---|
[659] | 326 | $start = get_moment(); |
---|
| 327 | |
---|
| 328 | // because isset is one hundred time faster than in_array |
---|
| 329 | $fs['thumbnails'] = array_flip($fs['thumbnails']); |
---|
| 330 | $fs['representatives'] = array_flip($fs['representatives']); |
---|
[345] | 331 | |
---|
| 332 | $inserts = array(); |
---|
[659] | 333 | $insert_links = array(); |
---|
[345] | 334 | |
---|
[659] | 335 | foreach (array_diff($fs['elements'], $db_elements, $db_unvalidated) as $path) |
---|
[466] | 336 | { |
---|
[659] | 337 | $insert = array(); |
---|
| 338 | // storage category must exist |
---|
| 339 | $dirname = dirname($path); |
---|
| 340 | if (!isset($db_fulldirs[$dirname])) |
---|
[10] | 341 | { |
---|
[659] | 342 | continue; |
---|
| 343 | } |
---|
| 344 | $filename = basename($path); |
---|
| 345 | if (!preg_match('/^[a-zA-Z0-9-_.]+$/', $filename)) |
---|
| 346 | { |
---|
| 347 | array_push($errors, array('path' => $path, 'type' => 'PWG-UPDATE-1')); |
---|
| 348 | continue; |
---|
| 349 | } |
---|
| 350 | |
---|
| 351 | // searching the thumbnail |
---|
| 352 | $filename_wo_ext = get_filename_wo_extension($filename); |
---|
| 353 | $tn_ext = ''; |
---|
| 354 | $base_test = $dirname.'/thumbnail/'; |
---|
| 355 | $base_test.= $conf['prefix_thumbnail'].$filename_wo_ext.'.'; |
---|
| 356 | foreach ($conf['picture_ext'] as $ext) |
---|
| 357 | { |
---|
| 358 | $test = $base_test.$ext; |
---|
| 359 | if (isset($fs['thumbnails'][$test])) |
---|
[466] | 360 | { |
---|
[659] | 361 | $tn_ext = $ext; |
---|
| 362 | break; |
---|
[345] | 363 | } |
---|
[659] | 364 | } |
---|
[466] | 365 | |
---|
[659] | 366 | // 2 cases : the element is a picture or not. Indeed, for a picture |
---|
| 367 | // thumbnail is mandatory and for non picture element, thumbnail and |
---|
| 368 | // representative are optionnal |
---|
| 369 | if (in_array(get_extension($filename), $conf['picture_ext'])) |
---|
| 370 | { |
---|
| 371 | // if we found a thumnbnail corresponding to our picture... |
---|
| 372 | if ($tn_ext != '') |
---|
[10] | 373 | { |
---|
[659] | 374 | $insert{'id'} = $next_element_id++; |
---|
| 375 | $insert{'file'} = $filename; |
---|
| 376 | $insert{'storage_category_id'} = $db_fulldirs[$dirname]; |
---|
| 377 | $insert{'date_available'} = CURRENT_DATE; |
---|
| 378 | $insert{'tn_ext'} = $tn_ext; |
---|
| 379 | $insert{'path'} = $path; |
---|
[160] | 380 | |
---|
[659] | 381 | array_push($inserts, $insert); |
---|
| 382 | array_push($insert_links, |
---|
| 383 | array('image_id' => $insert{'id'}, |
---|
| 384 | 'category_id' => $insert{'storage_category_id'})); |
---|
| 385 | array_push($infos, array('path' => $insert{'path'}, |
---|
| 386 | 'info' => $lang['update_research_added'])); |
---|
[466] | 387 | } |
---|
| 388 | else |
---|
| 389 | { |
---|
[659] | 390 | array_push($errors, array('path' => $path, 'type' => 'PWG-UPDATE-2')); |
---|
| 391 | } |
---|
| 392 | } |
---|
| 393 | else |
---|
| 394 | { |
---|
| 395 | // searching a representative |
---|
| 396 | $representative_ext = ''; |
---|
| 397 | $base_test = $dirname.'/pwg_representative/'.$filename_wo_ext.'.'; |
---|
| 398 | foreach ($conf['picture_ext'] as $ext) |
---|
| 399 | { |
---|
| 400 | $test = $base_test.$ext; |
---|
| 401 | if (isset($fs['representatives'][$test])) |
---|
[466] | 402 | { |
---|
[659] | 403 | $representative_ext = $ext; |
---|
| 404 | break; |
---|
[466] | 405 | } |
---|
[659] | 406 | } |
---|
[466] | 407 | |
---|
[659] | 408 | $insert{'id'} = $next_element_id++; |
---|
| 409 | $insert{'file'} = $filename; |
---|
| 410 | $insert{'storage_category_id'} = $db_fulldirs[$dirname]; |
---|
| 411 | $insert{'date_available'} = CURRENT_DATE; |
---|
| 412 | $insert{'path'} = $path; |
---|
| 413 | |
---|
| 414 | if ($tn_ext != '') |
---|
| 415 | { |
---|
| 416 | $insert{'tn_ext'} = $tn_ext; |
---|
[10] | 417 | } |
---|
[659] | 418 | if ($representative_ext != '') |
---|
| 419 | { |
---|
| 420 | $insert{'representative_ext'} = $representative_ext; |
---|
| 421 | } |
---|
| 422 | |
---|
| 423 | array_push($inserts, $insert); |
---|
| 424 | array_push($insert_links, |
---|
| 425 | array('image_id' => $insert{'id'}, |
---|
| 426 | 'category_id' => $insert{'storage_category_id'})); |
---|
| 427 | array_push($infos, array('path' => $insert{'path'}, |
---|
| 428 | 'info' => $lang['update_research_added'])); |
---|
[10] | 429 | } |
---|
| 430 | } |
---|
[659] | 431 | |
---|
[466] | 432 | if (count($inserts) > 0) |
---|
[345] | 433 | { |
---|
[659] | 434 | if (!$simulate) |
---|
| 435 | { |
---|
| 436 | // inserts all new elements |
---|
| 437 | $dbfields = array( |
---|
| 438 | 'id','file','storage_category_id','date_available','tn_ext' |
---|
| 439 | ,'representative_ext','path' |
---|
| 440 | ); |
---|
| 441 | mass_inserts(IMAGES_TABLE, $dbfields, $inserts); |
---|
[486] | 442 | |
---|
[659] | 443 | // insert all links between new elements and their storage category |
---|
| 444 | $dbfields = array('image_id','category_id'); |
---|
| 445 | mass_inserts(IMAGE_CATEGORY_TABLE, $dbfields, $insert_links); |
---|
[345] | 446 | } |
---|
[659] | 447 | $counts['new_elements'] = count($inserts); |
---|
| 448 | } |
---|
[345] | 449 | |
---|
[659] | 450 | // delete elements that are in database but not in the filesystem |
---|
| 451 | $to_delete_elements = array(); |
---|
| 452 | foreach (array_diff($db_elements, $fs['elements']) as $path) |
---|
| 453 | { |
---|
| 454 | array_push($to_delete_elements, array_search($path, $db_elements)); |
---|
| 455 | array_push($infos, array('path' => $path, |
---|
| 456 | 'info' => $lang['update_research_deleted'])); |
---|
| 457 | } |
---|
| 458 | if (count($to_delete_elements) > 0) |
---|
| 459 | { |
---|
| 460 | if (!$simulate) |
---|
[345] | 461 | { |
---|
[659] | 462 | delete_elements($to_delete_elements); |
---|
[345] | 463 | } |
---|
[659] | 464 | $counts['del_elements'] = count($to_delete_elements); |
---|
[345] | 465 | } |
---|
[659] | 466 | |
---|
[691] | 467 | echo '<!-- scanning files : '; |
---|
[659] | 468 | echo get_elapsed_time($start_files, get_moment()); |
---|
[691] | 469 | echo ' -->'."\n"; |
---|
[791] | 470 | |
---|
| 471 | // retrieving informations given by uploaders |
---|
[967] | 472 | if (!$simulate and count($cat_ids) > 0) |
---|
[791] | 473 | { |
---|
| 474 | $query = ' |
---|
| 475 | SELECT id,file,storage_category_id,infos |
---|
| 476 | FROM '.WAITING_TABLE.' |
---|
| 477 | WHERE storage_category_id IN ( |
---|
| 478 | '.wordwrap(implode(', ', $cat_ids), 80, "\n").') |
---|
| 479 | AND validated = \'true\' |
---|
| 480 | ;'; |
---|
| 481 | $result = pwg_query($query); |
---|
| 482 | |
---|
| 483 | $datas = array(); |
---|
| 484 | $fields = |
---|
| 485 | array( |
---|
| 486 | 'primary' => array('id'), |
---|
| 487 | 'update' => array('date_creation', 'author', 'name', 'comment') |
---|
| 488 | ); |
---|
| 489 | |
---|
| 490 | $waiting_to_delete = array(); |
---|
| 491 | |
---|
| 492 | while ($row = mysql_fetch_array($result)) |
---|
| 493 | { |
---|
| 494 | $data = array(); |
---|
| 495 | |
---|
| 496 | $query = ' |
---|
| 497 | SELECT id |
---|
| 498 | FROM '.IMAGES_TABLE.' |
---|
| 499 | WHERE storage_category_id = \''.$row['storage_category_id'].'\' |
---|
| 500 | AND file = \''.$row['file'].'\' |
---|
| 501 | ;'; |
---|
| 502 | list($data['id']) = mysql_fetch_array(pwg_query($query)); |
---|
| 503 | |
---|
| 504 | foreach ($fields['update'] as $field) |
---|
| 505 | { |
---|
| 506 | $data[$field] = getAttribute($row['infos'], $field); |
---|
| 507 | } |
---|
| 508 | |
---|
| 509 | array_push($datas, $data); |
---|
| 510 | array_push($waiting_to_delete, $row['id']); |
---|
| 511 | } |
---|
| 512 | |
---|
| 513 | if (count($datas) > 0) |
---|
| 514 | { |
---|
| 515 | mass_updates(IMAGES_TABLE, $fields, $datas); |
---|
| 516 | |
---|
| 517 | // delete now useless waiting elements |
---|
| 518 | $query = ' |
---|
| 519 | DELETE |
---|
| 520 | FROM '.WAITING_TABLE.' |
---|
| 521 | WHERE id IN ('.implode(',', $waiting_to_delete).') |
---|
| 522 | ;'; |
---|
| 523 | pwg_query($query); |
---|
| 524 | } |
---|
| 525 | } |
---|
[10] | 526 | } |
---|
[589] | 527 | // +-----------------------------------------------------------------------+ |
---|
| 528 | // | template initialization | |
---|
| 529 | // +-----------------------------------------------------------------------+ |
---|
[466] | 530 | $template->set_filenames(array('update'=>'admin/update.tpl')); |
---|
[393] | 531 | |
---|
[659] | 532 | $result_title = ''; |
---|
| 533 | if (isset($simulate) and $simulate) |
---|
| 534 | { |
---|
| 535 | $result_title.= $lang['update_simulation_title'].' '; |
---|
| 536 | } |
---|
| 537 | $result_title.= $lang['update_part_research']; |
---|
[589] | 538 | |
---|
[723] | 539 | // used_metadata string is displayed to inform admin which metadata will be |
---|
| 540 | // used from files for synchronization |
---|
| 541 | $used_metadata = $lang['metadata_basic'].' (filesize, width, height)'; |
---|
| 542 | |
---|
| 543 | if ($conf['use_exif']) |
---|
| 544 | { |
---|
| 545 | $used_metadata.= ', '.$lang['metadata_exif'].' (date_creation)'; |
---|
| 546 | } |
---|
| 547 | |
---|
| 548 | if ($conf['use_iptc']) |
---|
| 549 | { |
---|
| 550 | $used_metadata.= ', '.$lang['metadata_iptc']; |
---|
| 551 | $used_metadata.= '('; |
---|
| 552 | $used_metadata.= implode(', ', array_keys($conf['use_iptc_mapping'])); |
---|
| 553 | $used_metadata.= ')'; |
---|
| 554 | } |
---|
| 555 | |
---|
[589] | 556 | $template->assign_vars( |
---|
| 557 | array( |
---|
| 558 | 'L_SUBMIT'=>$lang['submit'], |
---|
[691] | 559 | 'L_RESET'=>$lang['reset'], |
---|
[589] | 560 | 'L_UPDATE_TITLE'=>$lang['update_default_title'], |
---|
| 561 | 'L_UPDATE_SYNC_FILES'=>$lang['update_sync_files'], |
---|
| 562 | 'L_UPDATE_SYNC_DIRS'=>$lang['update_sync_dirs'], |
---|
| 563 | 'L_UPDATE_SYNC_ALL'=>$lang['update_sync_all'], |
---|
| 564 | 'L_UPDATE_SYNC_METADATA'=>$lang['update_sync_metadata'], |
---|
| 565 | 'L_UPDATE_SYNC_METADATA_NEW'=>$lang['update_sync_metadata_new'], |
---|
| 566 | 'L_UPDATE_SYNC_METADATA_ALL'=>$lang['update_sync_metadata_all'], |
---|
| 567 | 'L_UPDATE_CATS_SUBSET'=>$lang['update_cats_subset'], |
---|
[659] | 568 | 'L_RESULT_UPDATE'=>$result_title, |
---|
[589] | 569 | 'L_NB_NEW_ELEMENTS'=>$lang['update_nb_new_elements'], |
---|
| 570 | 'L_NB_NEW_CATEGORIES'=>$lang['update_nb_new_categories'], |
---|
| 571 | 'L_NB_DEL_ELEMENTS'=>$lang['update_nb_del_elements'], |
---|
| 572 | 'L_NB_DEL_CATEGORIES'=>$lang['update_nb_del_categories'], |
---|
[659] | 573 | 'L_UPDATE_NB_ERRORS'=>$lang['update_nb_errors'], |
---|
[589] | 574 | 'L_SEARCH_SUBCATS_INCLUDED'=>$lang['search_subcats_included'], |
---|
[659] | 575 | 'L_UPDATE_WRONG_DIRNAME_INFO'=>$lang['update_wrong_dirname_info'], |
---|
| 576 | 'L_UPDATE_MISSING_TN_INFO'=>$lang['update_missing_tn_info'], |
---|
| 577 | 'PICTURE_EXT_LIST'=>implode(',', $conf['picture_ext']), |
---|
| 578 | 'L_UPDATE_ERROR_LIST_TITLE'=>$lang['update_error_list_title'], |
---|
| 579 | 'L_UPDATE_ERRORS_CAPTION'=>$lang['update_errors_caption'], |
---|
| 580 | 'L_UPDATE_DISPLAY_INFO'=>$lang['update_display_info'], |
---|
| 581 | 'L_UPDATE_SIMULATE'=>$lang['update_simulate'], |
---|
[723] | 582 | 'L_UPDATE_INFOS_TITLE'=>$lang['update_infos_title'], |
---|
| 583 | 'L_RESULT_METADATA'=>$lang['update_result_metadata'], |
---|
| 584 | 'L_ELEMENTS_METADATA_SYNC'=>$lang['update_elements_metadata_sync'], |
---|
| 585 | 'L_USED_METADATA'=>$lang['update_used_metadata'], |
---|
| 586 | 'METADATA_LIST' => $used_metadata |
---|
[589] | 587 | )); |
---|
[858] | 588 | |
---|
| 589 | $template->assign_vars( |
---|
| 590 | array( |
---|
| 591 | 'U_HELP' => PHPWG_ROOT_PATH.'/popuphelp.php?page=synchronize' |
---|
| 592 | ) |
---|
| 593 | ); |
---|
[589] | 594 | // +-----------------------------------------------------------------------+ |
---|
| 595 | // | introduction : choices | |
---|
| 596 | // +-----------------------------------------------------------------------+ |
---|
[691] | 597 | if (!isset($_POST['submit']) or (isset($simulate) and $simulate)) |
---|
[10] | 598 | { |
---|
[589] | 599 | $template->assign_block_vars('introduction', array()); |
---|
| 600 | |
---|
[691] | 601 | if (isset($simulate) and $simulate) |
---|
| 602 | { |
---|
| 603 | switch ($_POST['sync']) |
---|
| 604 | { |
---|
| 605 | case 'dirs' : |
---|
| 606 | { |
---|
| 607 | $template->assign_vars( |
---|
| 608 | array('SYNC_DIRS_CHECKED'=>'checked="checked"')); |
---|
| 609 | break; |
---|
| 610 | } |
---|
| 611 | case 'files' : |
---|
| 612 | { |
---|
| 613 | $template->assign_vars( |
---|
| 614 | array('SYNC_ALL_CHECKED'=>'checked="checked"')); |
---|
| 615 | break; |
---|
| 616 | } |
---|
| 617 | } |
---|
| 618 | |
---|
| 619 | if (isset($_POST['display_info']) and $_POST['display_info'] == 1) |
---|
| 620 | { |
---|
| 621 | $template->assign_vars( |
---|
| 622 | array('DISPLAY_INFO_CHECKED'=>'checked="checked"')); |
---|
| 623 | } |
---|
| 624 | |
---|
| 625 | if (isset($_POST['subcats-included']) and $_POST['subcats-included'] == 1) |
---|
| 626 | { |
---|
| 627 | $template->assign_vars( |
---|
| 628 | array('SUBCATS_INCLUDED_CHECKED'=>'checked="checked"')); |
---|
| 629 | } |
---|
| 630 | |
---|
| 631 | if (isset($_POST['cat']) and is_numeric($_POST['cat'])) |
---|
| 632 | { |
---|
| 633 | $cat_selected = array($_POST['cat']); |
---|
| 634 | } |
---|
| 635 | else |
---|
| 636 | { |
---|
| 637 | $cat_selected = array(); |
---|
| 638 | } |
---|
| 639 | } |
---|
| 640 | else |
---|
| 641 | { |
---|
| 642 | $template->assign_vars( |
---|
| 643 | array('SYNC_DIRS_CHECKED' => 'checked="checked"', |
---|
| 644 | 'SUBCATS_INCLUDED_CHECKED'=>'checked="checked"')); |
---|
| 645 | |
---|
| 646 | $cat_selected = array(); |
---|
| 647 | } |
---|
| 648 | |
---|
[589] | 649 | $query = ' |
---|
[614] | 650 | SELECT id,name,uppercats,global_rank |
---|
[589] | 651 | FROM '.CATEGORIES_TABLE.' |
---|
[614] | 652 | WHERE site_id = 1 |
---|
[589] | 653 | ;'; |
---|
[614] | 654 | display_select_cat_wrapper($query, |
---|
[691] | 655 | $cat_selected, |
---|
[614] | 656 | 'introduction.category_option', |
---|
| 657 | false); |
---|
[10] | 658 | } |
---|
[589] | 659 | // +-----------------------------------------------------------------------+ |
---|
| 660 | // | synchronize files | |
---|
| 661 | // +-----------------------------------------------------------------------+ |
---|
[691] | 662 | if (isset($_POST['submit']) |
---|
| 663 | and ($_POST['sync'] == 'dirs' or $_POST['sync'] == 'files')) |
---|
[10] | 664 | { |
---|
[498] | 665 | $template->assign_block_vars( |
---|
| 666 | 'update', |
---|
| 667 | array( |
---|
| 668 | 'NB_NEW_CATEGORIES'=>$counts['new_categories'], |
---|
| 669 | 'NB_DEL_CATEGORIES'=>$counts['del_categories'], |
---|
| 670 | 'NB_NEW_ELEMENTS'=>$counts['new_elements'], |
---|
[659] | 671 | 'NB_DEL_ELEMENTS'=>$counts['del_elements'], |
---|
| 672 | 'NB_ERRORS'=>count($errors), |
---|
[498] | 673 | )); |
---|
[659] | 674 | |
---|
| 675 | if (count($errors) > 0) |
---|
| 676 | { |
---|
[792] | 677 | $template->assign_block_vars('update.update_errors', array()); |
---|
[659] | 678 | foreach ($errors as $error) |
---|
| 679 | { |
---|
| 680 | $template->assign_block_vars( |
---|
[792] | 681 | 'update.update_errors.update_error', |
---|
[659] | 682 | array( |
---|
| 683 | 'ELEMENT' => $error['path'], |
---|
| 684 | 'LABEL' => $error['type'].' ('.$error_labels[$error['type']].')' |
---|
| 685 | )); |
---|
| 686 | } |
---|
| 687 | } |
---|
| 688 | if (count($infos) > 0 |
---|
| 689 | and isset($_POST['display_info']) |
---|
| 690 | and $_POST['display_info'] == 1) |
---|
| 691 | { |
---|
[792] | 692 | $template->assign_block_vars('update.update_infos', array()); |
---|
[659] | 693 | foreach ($infos as $info) |
---|
| 694 | { |
---|
| 695 | $template->assign_block_vars( |
---|
[792] | 696 | 'update.update_infos.update_info', |
---|
[659] | 697 | array( |
---|
| 698 | 'ELEMENT' => $info['path'], |
---|
| 699 | 'LABEL' => $info['info'] |
---|
| 700 | )); |
---|
| 701 | } |
---|
| 702 | } |
---|
| 703 | |
---|
| 704 | if (!$simulate) |
---|
| 705 | { |
---|
| 706 | $start = get_moment(); |
---|
| 707 | update_category('all'); |
---|
[691] | 708 | echo '<!-- update_category(all) : '; |
---|
[659] | 709 | echo get_elapsed_time($start,get_moment()); |
---|
[691] | 710 | echo ' -->'."\n"; |
---|
[659] | 711 | $start = get_moment(); |
---|
| 712 | ordering(); |
---|
| 713 | update_global_rank(); |
---|
[691] | 714 | echo '<!-- ordering categories : '; |
---|
[659] | 715 | echo get_elapsed_time($start, get_moment()); |
---|
[691] | 716 | echo ' -->'."\n"; |
---|
[659] | 717 | } |
---|
[345] | 718 | } |
---|
[589] | 719 | // +-----------------------------------------------------------------------+ |
---|
| 720 | // | synchronize metadata | |
---|
| 721 | // +-----------------------------------------------------------------------+ |
---|
| 722 | else if (isset($_POST['submit']) and preg_match('/^metadata/', $_POST['sync'])) |
---|
[486] | 723 | { |
---|
[589] | 724 | // sync only never synchronized files ? |
---|
| 725 | if ($_POST['sync'] == 'metadata_new') |
---|
[486] | 726 | { |
---|
[589] | 727 | $opts['only_new'] = true; |
---|
[486] | 728 | } |
---|
[589] | 729 | else |
---|
[486] | 730 | { |
---|
[589] | 731 | $opts['only_new'] = false; |
---|
[486] | 732 | } |
---|
[589] | 733 | $opts['category_id'] = ''; |
---|
| 734 | $opts['recursive'] = true; |
---|
| 735 | |
---|
| 736 | if (isset($_POST['cat'])) |
---|
[486] | 737 | { |
---|
[589] | 738 | $opts['category_id'] = $_POST['cat']; |
---|
| 739 | // recursive ? |
---|
| 740 | if (!isset($_POST['subcats-included']) or $_POST['subcats-included'] != 1) |
---|
| 741 | { |
---|
| 742 | $opts['recursive'] = false; |
---|
| 743 | } |
---|
[486] | 744 | } |
---|
[589] | 745 | $start = get_moment(); |
---|
| 746 | $files = get_filelist($opts['category_id'], |
---|
| 747 | $opts['recursive'], |
---|
| 748 | $opts['only_new']); |
---|
[486] | 749 | |
---|
[691] | 750 | echo '<!-- get_filelist : '; |
---|
| 751 | echo get_elapsed_time($start, get_moment()); |
---|
| 752 | echo ' -->'."\n"; |
---|
| 753 | |
---|
[486] | 754 | $start = get_moment(); |
---|
| 755 | update_metadata($files); |
---|
[691] | 756 | echo '<!-- metadata update : '; |
---|
| 757 | echo get_elapsed_time($start, get_moment()); |
---|
| 758 | echo ' -->'."\n"; |
---|
[723] | 759 | |
---|
| 760 | $template->assign_block_vars( |
---|
| 761 | 'metadata_result', |
---|
| 762 | array( |
---|
| 763 | 'NB_ELEMENTS' => count($files), |
---|
| 764 | )); |
---|
[486] | 765 | } |
---|
[589] | 766 | // +-----------------------------------------------------------------------+ |
---|
| 767 | // | sending html code | |
---|
| 768 | // +-----------------------------------------------------------------------+ |
---|
[393] | 769 | $template->assign_var_from_handle('ADMIN_CONTENT', 'update'); |
---|
[486] | 770 | ?> |
---|