Changeset 466 for trunk/admin/update.php
- Timestamp:
- Aug 5, 2004, 7:33:14 PM (20 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/admin/update.php
r423 r466 26 26 // +-----------------------------------------------------------------------+ 27 27 28 include_once( PHPWG_ROOT_PATH.'admin/include/isadmin.inc.php' 28 include_once( PHPWG_ROOT_PATH.'admin/include/isadmin.inc.php'); 29 29 //------------------------------------------------------------------- functions 30 function ordering( $id_uppercat 30 function ordering( $id_uppercat) 31 31 { 32 32 $rank = 1; … … 34 34 $query = 'SELECT id'; 35 35 $query.= ' FROM '.CATEGORIES_TABLE; 36 if ( !is_numeric( $id_uppercat ))36 if ( !is_numeric( $id_uppercat)) 37 37 { 38 38 $query.= ' WHERE id_uppercat IS NULL'; … … 44 44 $query.= ' ORDER BY rank ASC, dir ASC'; 45 45 $query.= ';'; 46 $result = mysql_query( $query 47 while ( $row = mysql_fetch_array( $result ))46 $result = mysql_query( $query); 47 while ( $row = mysql_fetch_array( $result)) 48 48 { 49 49 $query = 'UPDATE '.CATEGORIES_TABLE; … … 51 51 $query.= ' WHERE id = '.$row['id']; 52 52 $query.= ';'; 53 mysql_query( $query 53 mysql_query( $query); 54 54 $rank++; 55 ordering( $row['id'] 55 ordering( $row['id']); 56 56 } 57 57 } 58 58 59 function insert_local_category( $id_uppercat)59 function insert_local_category($id_uppercat) 60 60 { 61 61 global $conf, $page, $user, $lang; … … 66 66 // 0. retrieving informations on the category to display 67 67 $cat_directory = PHPWG_ROOT_PATH.'galleries'; 68 if ( is_numeric( $id_uppercat ))68 if (is_numeric($id_uppercat)) 69 69 { 70 70 $query = 'SELECT name,uppercats,dir FROM '.CATEGORIES_TABLE; 71 71 $query.= ' WHERE id = '.$id_uppercat; 72 72 $query.= ';'; 73 $row = mysql_fetch_array( mysql_query( $query ));73 $row = mysql_fetch_array( mysql_query( $query)); 74 74 $uppercats = $row['uppercats']; 75 75 $name = $row['name']; 76 76 $dir = $row['dir']; 77 77 78 $upper_array = explode( ',', $uppercats 78 $upper_array = explode( ',', $uppercats); 79 79 80 80 $local_dir = ''; 81 81 82 82 $database_dirs = array(); 83 $query = 'SELECT id,dir FROM '.CATEGORIES_TABLE; 84 $query.= ' WHERE id IN ('.$uppercats.')'; 85 $query.= ';'; 86 $result = mysql_query( $query ); 87 while( $row = mysql_fetch_array( $result ) ) 83 $query = ' 84 SELECT id,dir FROM '.CATEGORIES_TABLE.' 85 WHERE id IN ('.$uppercats.') 86 ;'; 87 $result = mysql_query( $query); 88 while ($row = mysql_fetch_array($result)) 88 89 { 89 90 $database_dirs[$row['id']] = $row['dir']; 90 91 } 91 foreach ( $upper_array as $id ) { 92 foreach ($upper_array as $id) 93 { 92 94 $local_dir.= $database_dirs[$id].'/'; 93 95 } … … 103 105 // 2. we search pictures of the category only if the update is for all 104 106 // or a cat_id is specified 105 if ( isset( $page['cat'] ) or $_GET['update'] == 'all')106 { 107 $output.= insert_local_ image( $cat_directory, $id_uppercat);108 } 109 } 110 111 $sub_dirs = get_category_directories( $cat_directory);107 if (isset($page['cat']) or $_GET['update'] == 'all') 108 { 109 $output.= insert_local_element($cat_directory, $id_uppercat); 110 } 111 } 112 113 $sub_dirs = get_category_directories($cat_directory); 112 114 113 115 $sub_category_dirs = array(); 114 $query = 'SELECT id,dir FROM '.CATEGORIES_TABLE; 115 $query.= ' WHERE site_id = 1'; 116 if (!is_numeric($id_uppercat)) $query.= ' AND id_uppercat IS NULL'; 117 else $query.= ' AND id_uppercat = '.$id_uppercat; 118 $query.= ' AND dir IS NOT NULL'; // virtual categories not taken 119 $query.= ';'; 120 $result = mysql_query( $query ); 121 while ( $row = mysql_fetch_array( $result ) ) 116 $query = ' 117 SELECT id,dir FROM '.CATEGORIES_TABLE.' 118 WHERE site_id = 1 119 '; 120 if (!is_numeric($id_uppercat)) 121 { 122 $query.= ' AND id_uppercat IS NULL'; 123 } 124 else 125 { 126 $query.= ' AND id_uppercat = '.$id_uppercat; 127 } 128 $query.= ' 129 AND dir IS NOT NULL'; // virtual categories not taken 130 $query.= ' 131 ;'; 132 $result = mysql_query($query); 133 while ($row = mysql_fetch_array($result)) 122 134 { 123 135 $sub_category_dirs[$row['id']] = $row['dir']; … … 125 137 126 138 // 3. we have to remove the categories of the database not present anymore 127 foreach ( $sub_category_dirs as $id => $dir ) { 128 if ( !in_array( $dir, $sub_dirs ) ) delete_category( $id ); 139 foreach ($sub_category_dirs as $id => $dir) 140 { 141 if (!in_array($dir, $sub_dirs)) 142 { 143 delete_category($id); 144 } 129 145 } 130 146 … … 132 148 $inserts = array(); 133 149 134 foreach ( $sub_dirs as $sub_dir ) { 150 foreach ($sub_dirs as $sub_dir) 151 { 135 152 // 5. Is the category already existing ? we create a subcat if not 136 153 // existing 137 $category_id = array_search( $sub_dir, $sub_category_dirs);138 if ( !is_numeric( $category_id ))139 { 140 if ( preg_match( '/^[a-zA-Z0-9-_.]+$/', $sub_dir ))154 $category_id = array_search($sub_dir, $sub_category_dirs); 155 if (!is_numeric($category_id)) 156 { 157 if (preg_match('/^[a-zA-Z0-9-_.]+$/', $sub_dir)) 141 158 { 142 $name = str_replace( '_', ' ', $sub_dir);159 $name = str_replace('_', ' ', $sub_dir); 143 160 144 161 $value = "('".$sub_dir."','".$name."',1"; 145 if ( !is_numeric( $id_uppercat ) ) $value.= ',NULL'; 146 else $value.= ','.$id_uppercat; 162 if (!is_numeric($id_uppercat)) 163 { 164 $value.= ',NULL'; 165 } 166 else 167 { 168 $value.= ','.$id_uppercat; 169 } 147 170 $value.= ",'undef'"; 148 171 $value.= ')'; 149 array_push( $inserts, $value);172 array_push($inserts, $value); 150 173 } 151 174 else … … 158 181 159 182 // we have to create the category 160 if ( count( $inserts ) > 0 ) 161 { 162 $query = 'INSERT INTO '.CATEGORIES_TABLE; 163 $query.= ' (dir,name,site_id,id_uppercat,uppercats) VALUES '; 164 $query.= implode( ',', $inserts ); 165 $query.= ';'; 166 mysql_query( $query ); 183 if (count($inserts) > 0) 184 { 185 $query = ' 186 INSERT INTO '.CATEGORIES_TABLE.' 187 (dir,name,site_id,id_uppercat,uppercats) VALUES 188 '; 189 $query.= implode(',', $inserts); 190 $query.= ' 191 ;'; 192 mysql_query($query); 167 193 // updating uppercats field 168 $query = 'UPDATE '.CATEGORIES_TABLE; 169 $query.= ' SET uppercats = '; 170 if ( $uppercats != '' ) $query.= "CONCAT('".$uppercats."',',',id)"; 171 else $query.= 'id'; 172 $query.= ' WHERE id_uppercat '; 173 if (!is_numeric($id_uppercat)) $query.= 'IS NULL'; 174 else $query.= '= '.$id_uppercat; 175 $query.= ';'; 176 mysql_query( $query ); 194 $query = ' 195 UPDATE '.CATEGORIES_TABLE.' 196 SET uppercats = '; 197 if ($uppercats != '') 198 { 199 $query.= "CONCAT('".$uppercats."',',',id)"; 200 } 201 else 202 { 203 $query.= 'id'; 204 } 205 $query.= ' 206 WHERE id_uppercat '; 207 if (!is_numeric($id_uppercat)) 208 { 209 $query.= 'IS NULL'; 210 } 211 else 212 { 213 $query.= '= '.$id_uppercat; 214 } 215 $query.= ' 216 ;'; 217 mysql_query($query); 177 218 } 178 219 179 220 // Recursive call on the sub-categories (not virtual ones) 180 $query = 'SELECT id'; 181 $query.= ' FROM '.CATEGORIES_TABLE; 182 $query.= ' WHERE site_id = 1'; 183 if (!is_numeric($id_uppercat)) $query.= ' AND id_uppercat IS NULL'; 184 else $query.= ' AND id_uppercat = '.$id_uppercat; 185 $query.= ' AND dir IS NOT NULL'; // virtual categories not taken 186 $query.= ';'; 187 $result = mysql_query( $query ); 188 while ( $row = mysql_fetch_array( $result ) ) 189 { 190 $output.= insert_local_category( $row['id'] ); 191 } 192 193 if ( is_numeric( $id_uppercat ) ) 221 $query = ' 222 SELECT id 223 FROM '.CATEGORIES_TABLE.' 224 WHERE site_id = 1 225 '; 226 if (!is_numeric($id_uppercat)) 227 { 228 $query.= ' AND id_uppercat IS NULL'; 229 } 230 else 231 { 232 $query.= ' AND id_uppercat = '.$id_uppercat; 233 } 234 $query.= ' 235 AND dir IS NOT NULL'; // virtual categories not taken 236 $query.= ' 237 ;'; 238 $result = mysql_query($query); 239 while ($row = mysql_fetch_array($result)) 240 { 241 $output.= insert_local_category($row['id']); 242 } 243 244 if (is_numeric($id_uppercat)) 194 245 { 195 246 $output.= '</ul>'; … … 198 249 } 199 250 200 function insert_local_ image( $dir, $category_id)251 function insert_local_element($dir, $category_id) 201 252 { 202 global $lang,$conf,$count_new ;253 global $lang,$conf,$count_new, $count_deleted; 203 254 204 255 $output = ''; 205 256 206 // fs means filesystem : $fs_pictures contains pictures in the filesystem 207 // found in $dir, $fs_thumbnails contains thumbnails... 208 $fs_pictures = get_picture_files( $dir ); 209 $fs_thumbnails = get_thumb_files( $dir.'thumbnail' ); 210 211 // we have to delete all the images from the database that : 212 // - are not in the directory anymore 213 // - don't have the associated thumbnail available anymore 214 $query = 'SELECT id,file,tn_ext'; 215 $query.= ' FROM '.IMAGES_TABLE; 216 $query.= ' WHERE storage_category_id = '.$category_id; 217 $query.= ';'; 218 $result = mysql_query( $query ); 219 while ( $row = mysql_fetch_array( $result ) ) 220 { 221 $pic_to_delete = false; 222 if ( !in_array( $row['file'], $fs_pictures ) ) 257 // fs means FileSystem : $fs_files contains files in the filesystem found 258 // in $dir that can be managed by PhpWebGallery (see get_pwg_files 259 // function), $fs_thumbnails contains thumbnails, $fs_representatives 260 // contains potentially representative pictures for non picture files 261 $fs_files = get_pwg_files($dir); 262 $fs_thumbnails = get_thumb_files($dir); 263 $fs_representatives = get_representative_files($dir); 264 265 // element deletion 266 $to_delete_elements = array(); 267 // deletion of element if the correspond file doesn't exist anymore 268 $query = ' 269 SELECT id,file 270 FROM '.IMAGES_TABLE.' 271 WHERE storage_category_id = '.$category_id.' 272 ;'; 273 $result = mysql_query($query); 274 while ($row = mysql_fetch_array($result)) 275 { 276 if (!in_array($row['file'], $fs_files)) 223 277 { 224 278 $output.= $row['file']; 225 279 $output.= ' <span style="font-weight:bold;">'; 226 280 $output.= $lang['update_disappeared'].'</span><br />'; 227 $pic_to_delete = true; 228 } 229 281 array_push($to_delete_elements, $row['id']); 282 } 283 } 284 // in picture case, we also delete the element if the thumbnail doesn't 285 // existe anymore 286 $query = ' 287 SELECT id,file,tn_ext 288 FROM '.IMAGES_TABLE.' 289 WHERE storage_category_id = '.$category_id.' 290 AND ('.implode(' OR ', 291 array_map( 292 create_function('$s', 'return "file LIKE \'%".$s."\'";') 293 , $conf['picture_ext'])).') 294 ;'; 295 $result = mysql_query($query); 296 while ($row = mysql_fetch_array($result)) 297 { 230 298 $thumbnail = $conf['prefix_thumbnail']; 231 $thumbnail.= get_filename_wo_extension( $row['file']);299 $thumbnail.= get_filename_wo_extension($row['file']); 232 300 $thumbnail.= '.'.$row['tn_ext']; 233 if ( !in_array( $thumbnail, $fs_thumbnails ))301 if (!in_array($thumbnail, $fs_thumbnails)) 234 302 { 235 303 $output.= $row['file']; 236 304 $output.= ' : <span style="font-weight:bold;">'; 237 305 $output.= $lang['update_disappeared_tn'].'</span><br />'; 238 $pic_to_delete = true; 239 } 240 241 if ( $pic_to_delete ) delete_image( $row['id'] ); 242 } 243 244 $registered_pictures = array(); 245 $query = 'SELECT file FROM '.IMAGES_TABLE; 246 $query.= ' WHERE storage_category_id = '.$category_id; 247 $query.= ';'; 248 $result = mysql_query( $query ); 249 while ( $row = mysql_fetch_array( $result ) ) 250 { 251 array_push( $registered_pictures, $row['file'] ); 306 array_push($to_delete_elements, $row['id']); 307 } 308 } 309 310 $to_delete_elements = array_unique($to_delete_elements); 311 $count_deleted+= count($to_delete_elements); 312 if ($count_deleted > 0) 313 { 314 delete_elements($to_delete_elements); 315 } 316 317 $registered_elements = array(); 318 $query = ' 319 SELECT file FROM '.IMAGES_TABLE.' 320 WHERE storage_category_id = '.$category_id.' 321 ;'; 322 $result = mysql_query($query); 323 while ($row = mysql_fetch_array($result)) 324 { 325 array_push($registered_elements, $row['file']); 252 326 } 253 327 … … 257 331 $unvalidated_pictures = array(); 258 332 259 $query = 'SELECT file,infos,validated'; 260 $query.= ' FROM '.WAITING_TABLE; 261 $query.= ' WHERE storage_category_id = '.$category_id; 262 $query.= ';'; 263 $result = mysql_query( $query ); 264 while ( $row = mysql_fetch_array( $result ) ) 265 { 266 if ( $row['validated'] == 'true' ) 333 $query = ' 334 SELECT file,infos,validated 335 FROM '.WAITING_TABLE.' 336 WHERE storage_category_id = '.$category_id.' 337 ;'; 338 $result = mysql_query($query); 339 while ($row = mysql_fetch_array($result)) 340 { 341 if ($row['validated'] == 'true') 342 { 267 343 $validated_pictures[$row['file']] = $row['infos']; 344 } 268 345 else 269 array_push( $unvalidated_pictures, $row['file'] ); 346 { 347 array_push($unvalidated_pictures, $row['file']); 348 } 270 349 } 271 350 … … 273 352 // present in the database yet. If we know that this picture is known as 274 353 // an uploaded one but not validated, it's not tested neither 275 $unregistered_ pictures = array_diff( $fs_pictures276 ,$registered_pictures277 ,$unvalidated_pictures);354 $unregistered_elements = array_diff($fs_files 355 ,$registered_elements 356 ,$unvalidated_pictures); 278 357 279 358 $inserts = array(); 280 359 281 foreach ( $unregistered_pictures as $unregistered_picture ) { 282 if ( preg_match( '/^[a-zA-Z0-9-_.]+$/', $unregistered_picture ) ) 283 { 284 $file_wo_ext = get_filename_wo_extension( $unregistered_picture ); 360 foreach ($unregistered_elements as $unregistered_element) 361 { 362 if (preg_match('/^[a-zA-Z0-9-_.]+$/', $unregistered_element)) 363 { 364 $file_wo_ext = get_filename_wo_extension($unregistered_element); 285 365 $tn_ext = ''; 286 foreach ( $conf['picture_ext'] as $ext ) { 366 foreach ($conf['picture_ext'] as $ext) 367 { 287 368 $test = $conf['prefix_thumbnail'].$file_wo_ext.'.'.$ext; 288 if ( !in_array( $test, $fs_thumbnails ) ) continue; 289 else { $tn_ext = $ext; break; } 369 if (!in_array($test, $fs_thumbnails)) 370 { 371 continue; 372 } 373 else 374 { 375 $tn_ext = $ext; 376 break; 377 } 290 378 } 291 // if we found a thumnbnail corresponding to our picture... 292 if ( $tn_ext != '' ) 379 380 // 2 cases : the element is a picture or not. Indeed, for a picture 381 // thumbnail is mandatory and for non picture element, thumbnail and 382 // representative is optionnal 383 if (in_array(get_extension($unregistered_element), $conf['picture_ext'])) 293 384 { 294 $image_size = @getimagesize( $dir.$unregistered_picture ); 295 // (file, storage_category_id, date_available, tn_ext, filesize, 296 // width, height, name, author, comment, date_creation)' 297 $value = '('; 298 $value.= "'".$unregistered_picture."'"; 299 $value.= ','.$category_id; 300 $value.= ",'".date( 'Y-m-d' )."'"; 301 $value.= ",'".$tn_ext."'"; 302 $value.= ','.floor( filesize( $dir.$unregistered_picture) / 1024 ); 303 $value.= ','.$image_size[0]; 304 $value.= ','.$image_size[1]; 305 if ( isset( $validated_pictures[$unregistered_picture] ) ) 306 { 307 // retrieving infos from the XML description from waiting table 308 $infos = nl2br( $validated_pictures[$unregistered_picture] ); 309 310 $unixtime = getAttribute( $infos, 'date_creation' ); 311 if ($unixtime != '') $date_creation ="'".date('Y-m-d',$unixtime)."'"; 312 else $date_creation = 'NULL'; 385 // if we found a thumnbnail corresponding to our picture... 386 if ($tn_ext != '') 387 { 388 $image_size = @getimagesize($dir.$unregistered_element); 389 // (file, storage_category_id, date_available, tn_ext, filesize, 390 // width, height, name, author, comment, date_creation, 391 // representative_ext)' 392 $value = '('; 393 $value.= "'".$unregistered_element."'"; 394 $value.= ','.$category_id; 395 $value.= ",'".date('Y-m-d')."'"; 396 $value.= ",'".$tn_ext."'"; 397 $value.= ','.floor(filesize($dir.$unregistered_element) / 1024); 398 $value.= ','.$image_size[0]; 399 $value.= ','.$image_size[1]; 400 if (isset($validated_pictures[$unregistered_element])) 401 { 402 // retrieving infos from the XML description from waiting table 403 $infos = nl2br($validated_pictures[$unregistered_element]); 404 405 $unixtime = getAttribute($infos, 'date_creation'); 406 if ($unixtime != '') 407 { 408 $date_creation ="'".date('Y-m-d',$unixtime)."'"; 409 } 410 else 411 { 412 $date_creation = 'NULL'; 413 } 313 414 314 $value.= ",'".getAttribute( $infos, 'name' )."'"; 315 $value.= ",'".getAttribute( $infos, 'author' )."'"; 316 $value.= ",'".getAttribute( $infos, 'comment')."'"; 317 $value.= ','.$date_creation; 318 319 // deleting the waiting element 320 $query = 'DELETE FROM '.WAITING_TABLE; 321 $query.= " WHERE file = '".$unregistered_picture."'"; 322 $query.= ' AND storage_category_id = '.$category_id; 323 $query.= ';'; 324 mysql_query( $query ); 415 $value.= ",'".getAttribute($infos, 'name')."'"; 416 $value.= ",'".getAttribute($infos, 'author')."'"; 417 $value.= ",'".getAttribute($infos, 'comment')."'"; 418 $value.= ','.$date_creation; 419 420 // deleting the waiting element 421 $query = ' 422 DELETE FROM '.WAITING_TABLE.' 423 WHERE file = \''.$unregistered_element.'\' 424 AND storage_category_id = '.$category_id.' 425 ;'; 426 mysql_query($query); 427 } 428 else 429 { 430 $value.= ",'','','',NULL"; 431 } 432 $value.= ',NULL'; // representative_ext 433 $value.= ')'; 434 435 $count_new++; 436 $output.= $unregistered_element; 437 $output.= ' <span style="font-weight:bold;">'; 438 $output.= $lang['update_research_added'].'</span>'; 439 $output.= ' ('.$lang['update_research_tn_ext'].' '.$tn_ext.')'; 440 $output.= '<br />'; 441 array_push($inserts, $value); 325 442 } 326 443 else 327 444 { 328 $value.= ",'','','',NULL"; 329 } 330 $value.= ')'; 331 332 $count_new++; 333 $output.= $unregistered_picture; 334 $output.= ' <span style="font-weight:bold;">'; 335 $output.= $lang['update_research_added'].'</span>'; 336 $output.= ' ('.$lang['update_research_tn_ext'].' '.$tn_ext.')'; 337 $output.= '<br />'; 338 array_push( $inserts, $value ); 445 $output.= '<span style="color:red;">'; 446 $output.= $lang['update_missing_tn'].' : '.$unregistered_element; 447 $output.= ' (<span style="font-weight:bold;">'; 448 $output.= $conf['prefix_thumbnail']; 449 $output.= get_filename_wo_extension($unregistered_element); 450 $output.= '.XXX</span>'; 451 $output.= ', XXX = '; 452 $output.= implode(', ', $conf['picture_ext']); 453 $output.= ')</span><br />'; 454 } 339 455 } 340 456 else 341 457 { 342 $output.= '<span style="color:red;">'; 343 $output.= $lang['update_missing_tn'].' : '.$unregistered_picture; 344 $output.= ' (<span style="font-weight:bold;">'; 345 $output.= $conf['prefix_thumbnail']; 346 $output.= get_filename_wo_extension( $unregistered_picture ); 347 $output.= '.XXX</span>'; 348 $output.= ', XXX = '; 349 $output.= implode( ', ', $conf['picture_ext'] ); 350 $output.= ')</span><br />'; 458 $representative_ext = ''; 459 foreach ($conf['picture_ext'] as $ext) 460 { 461 $test = $conf['prefix_thumbnail'].$file_wo_ext.'.'.$ext; 462 if (!in_array($test, $fs_thumbnails)) 463 { 464 continue; 465 } 466 else 467 { 468 $representative_ext = $ext; 469 break; 470 } 471 } 472 473 // (file, storage_category_id, date_available, tn_ext, filesize, 474 // width, height, name, author, comment, date_creation, 475 // representative_ext)' 476 $value = '('; 477 $value.= "'".$unregistered_element."'"; 478 $value.= ','.$category_id; 479 $value.= ",'".date('Y-m-d')."'"; 480 if ( $tn_ext != '' ) 481 { 482 $value.= ",'".$tn_ext."'"; 483 } 484 else 485 { 486 $value.= ',NULL'; 487 } 488 $value.= ','.floor(filesize($dir.$unregistered_element) / 1024); 489 $value.= ',NULL'; 490 $value.= ',NULL'; 491 $value.= ',NULL'; 492 $value.= ',NULL'; 493 $value.= ',NULL'; 494 $value.= ',NULL'; 495 if ( $representative_ext != '' ) 496 { 497 $value.= ",'".$representative_ext."'"; 498 } 499 else 500 { 501 $value.= ',NULL'; 502 } 503 $value.= ')'; 504 505 $count_new++; 506 $output.= $unregistered_element; 507 $output.= ' <span style="font-weight:bold;">'; 508 $output.= $lang['update_research_added'].'</span>'; 509 $output.= '<br />'; 510 array_push($inserts, $value); 351 511 } 352 512 } 353 513 else 354 514 { 355 $output.= '<span style="color:red;">"'.$unregistered_ picture.'" : ';515 $output.= '<span style="color:red;">"'.$unregistered_element.'" : '; 356 516 $output.= $lang['update_wrong_dirname'].'</span><br />'; 357 517 } 358 518 } 359 360 if ( count( $inserts ) > 0)519 520 if (count($inserts) > 0) 361 521 { 362 522 // inserts all found pictures 363 $query = ' INSERT INTO '.IMAGES_TABLE;364 $query.= ' (file,storage_category_id,date_available,tn_ext'; 365 $query.= ',filesize,width,height';366 $query.= ',name,author,comment,date_creation)';367 $query.= ' VALUES ';368 $query.= implode( ',', $inserts );369 $query.= ';';370 mysql_query( $query);523 $query = ' 524 INSERT INTO '.IMAGES_TABLE.' 525 (file,storage_category_id,date_available,tn_ext,filesize,width,height 526 ,name,author,comment,date_creation,representative_ext) 527 VALUES 528 '.implode(',', $inserts).' 529 ;'; 530 mysql_query($query); 371 531 372 532 // what are the ids of the pictures in the $category_id ? 373 533 $ids = array(); 374 534 375 $query = 'SELECT id'; 376 $query.= ' FROM '.IMAGES_TABLE; 377 $query.= ' WHERE storage_category_id = '.$category_id; 378 $query.= ';'; 379 $result = mysql_query( $query ); 380 while ( $row = mysql_fetch_array( $result ) ) 381 { 382 array_push( $ids, $row['id'] ); 535 $query = ' 536 SELECT id 537 FROM '.IMAGES_TABLE.' 538 WHERE storage_category_id = '.$category_id.' 539 ;'; 540 $result = mysql_query($query); 541 while ($row = mysql_fetch_array($result)) 542 { 543 array_push($ids, $row['id']); 383 544 } 384 545 385 546 // recreation of the links between this storage category pictures and 386 547 // its storage category 387 $query = 'DELETE FROM '.IMAGE_CATEGORY_TABLE; 388 $query.= ' WHERE category_id = '.$category_id; 389 $query.= ' AND image_id IN ('.implode( ',', $ids ).')'; 390 $query.= ';'; 391 mysql_query( $query ); 392 393 $query = 'INSERT INTO '.IMAGE_CATEGORY_TABLE; 394 $query.= '(category_id,image_id) VALUES '; 395 foreach ( $ids as $num => $image_id ) { 396 if ( $num > 0 ) $query.= ','; 397 $query.= '('.$category_id.','.$image_id.')'; 398 } 399 $query.= ';'; 400 mysql_query( $query ); 548 $query = ' 549 DELETE FROM '.IMAGE_CATEGORY_TABLE.' 550 WHERE category_id = '.$category_id.' 551 AND image_id IN ('.implode(',', $ids).') 552 ;'; 553 mysql_query($query); 554 555 foreach ($ids as $num => $image_id) 556 { 557 $ids[$num] = '('.$category_id.','.$image_id.')'; 558 } 559 $query = ' 560 INSERT INTO '.IMAGE_CATEGORY_TABLE.' 561 (category_id,image_id) VALUES 562 '.implode(',', $ids).' 563 ;'; 564 mysql_query($query); 401 565 } 402 566 return $output; 403 567 } 404 405 // remote_images verifies if a file named "listing.xml" is present is the406 // admin directory. If it is the case, creation of a remote picture storage407 // site if it doesn't already exists. Then, the function calls408 // insert_remote_category for this remote site on the root category.409 function remote_images()410 {411 global $conf, $lang, $vtp, $sub;412 413 // 1. is there a file listing.xml ?414 if ( !( $xml_content = getXmlCode( './admin/listing.xml' ) ) )415 {416 return false;417 }418 $url = getContent( getChild( $xml_content, 'url' ) );419 $vtp->setVar( $sub, 'remote_update.url', $url );420 421 // 2. is the site already existing ?422 $query = 'SELECT id FROM '.SITES_TABLE;423 $query.= " WHERE galleries_url = '".$url."'";424 $query.= ';';425 $result = mysql_query( $query );426 if ( mysql_num_rows($result ) == 0 )427 {428 // we have to register this site in the database429 $query = 'INSERT INTO '.SITES_TABLE;430 $query.= " (galleries_url) VALUES ('".$url."')";431 $query.= ';';432 mysql_query( $query );433 $site_id = mysql_insert_id();434 }435 else436 {437 // we get the already registered id438 $row = mysql_fetch_array( $result );439 $site_id = $row['id'];440 }441 442 // 3. available dirs in the file443 $categories = insert_remote_category( $xml_content, $site_id, 'NULL', 0 );444 $vtp->setVar( $sub, 'remote_update.categories', $categories );445 }446 447 // insert_remote_category searchs the "dir" node of the xml_dir given and448 // insert the contained categories if the are not in the database yet. The449 // function also deletes the categories that are in the database and not in450 // the xml_file.451 function insert_remote_category( $xml_content, $site_id, $id_uppercat, $level )452 {453 global $conf, $page, $user, $lang;454 455 $uppercats = '';456 $output = '';457 // 0. retrieving informations on the category to display458 $cat_directory = '../galleries';459 460 if ( is_numeric( $id_uppercat ) )461 {462 $query = 'SELECT name,uppercats,dir';463 $query.= ' FROM '.CATEGORIES_TABLE;464 $query.= ' WHERE id = '.$id_uppercat;465 $query.= ';';466 $row = mysql_fetch_array( mysql_query( $query ) );467 $uppercats = $row['uppercats'];468 $name = $row['name'];469 470 // 1. display the category name to update471 $src = './template/'.$user['template'].'/admin/images/puce.gif';472 $output = '<img src="'.$src.'" alt=">" />';473 $output.= '<span style="font-weight:bold;">'.$name.'</span>';474 $output.= ' [ '.$row['dir'].' ]';475 $output.= '<div class="retrait">';476 477 // 2. we search pictures of the category only if the update is for all478 // or a cat_id is specified479 $output.= insert_remote_image( $xml_content, $id_uppercat );480 }481 482 // $xml_dirs contains dir names contained in the xml file for this483 // id_uppercat484 $xml_dirs = array();485 $temp_dirs = getChildren( $xml_content, 'dir'.$level );486 foreach ( $temp_dirs as $temp_dir ) {487 array_push( $xml_dirs, getAttribute( $temp_dir, 'name' ) );488 }489 490 // $database_dirs contains dir names contained in the database for this491 // id_uppercat and site_id492 $database_dirs = array();493 $query = 'SELECT id,dir FROM '.CATEGORIES_TABLE;494 $query.= ' WHERE site_id = '.$site_id;495 if (!is_numeric($id_uppercat)) $query.= ' AND id_uppercat IS NULL';496 else $query.= ' AND id_uppercat = '.$id_uppercat;497 $query.= ' AND dir IS NOT NULL'; // virtual categories not taken498 $query.= ';';499 $result = mysql_query( $query );500 while ( $row = mysql_fetch_array( $result ) )501 {502 $database_dirs[$row['id']] = $row['dir'];503 }504 505 // 3. we have to remove the categories of the database not present anymore506 foreach ( $database_dirs as $id => $dir ) {507 if ( !in_array( $dir, $xml_dirs ) ) delete_category( $id );508 }509 510 // array of new categories to insert511 $inserts = array();512 513 foreach ( $xml_dirs as $xml_dir ) {514 // 5. Is the category already existing ? we create a subcat if not515 // existing516 $category_id = array_search( $xml_dir, $database_dirs );517 if ( !is_numeric( $category_id ) )518 {519 $name = str_replace( '_', ' ', $xml_dir );520 521 $value = "('".$xml_dir."','".$name."',".$site_id;522 if ( !is_numeric( $id_uppercat ) ) $value.= ',NULL';523 else $value.= ','.$id_uppercat;524 $value.= ",'undef'";525 $value.= ')';526 array_push( $inserts, $value );527 }528 }529 530 // we have to create the category531 if ( count( $inserts ) > 0 )532 {533 $query = 'INSERT INTO '.CATEGORIES_TABLE;534 $query.= ' (dir,name,site_id,id_uppercat,uppercats) VALUES ';535 $query.= implode( ',', $inserts );536 $query.= ';';537 mysql_query( $query );538 // updating uppercats field539 $query = 'UPDATE '.CATEGORIES_TABLE;540 $query.= ' SET uppercats = ';541 if ( $uppercats != '' ) $query.= "CONCAT('".$uppercats."',',',id)";542 else $query.= 'id';543 $query.= ' WHERE id_uppercat ';544 if (!is_numeric($id_uppercat)) $query.= 'IS NULL';545 else $query.= '= '.$id_uppercat;546 $query.= ';';547 mysql_query( $query );548 }549 550 // Recursive call on the sub-categories (not virtual ones)551 $query = 'SELECT id,dir';552 $query.= ' FROM '.CATEGORIES_TABLE;553 $query.= ' WHERE site_id = '.$site_id;554 if (!is_numeric($id_uppercat)) $query.= ' AND id_uppercat IS NULL';555 else $query.= ' AND id_uppercat = '.$id_uppercat;556 $query.= ' AND dir IS NOT NULL'; // virtual categories not taken557 $query.= ';';558 $result = mysql_query( $query );559 while ( $row = mysql_fetch_array( $result ) )560 {561 $database_dirs[$row['dir']] = $row['id'];562 }563 foreach ( $temp_dirs as $temp_dir ) {564 $dir = getAttribute( $temp_dir, 'name' );565 $id_uppercat = $database_dirs[$dir];566 $output.= insert_remote_category( $temp_dir, $site_id,567 $id_uppercat,$level+1 );568 }569 570 if ( is_numeric( $id_uppercat ) ) $output.= '</div>';571 572 return $output;573 }574 575 // insert_remote_image searchs the "root" node of the xml_dir given and576 // insert the contained pictures if the are not in the database yet.577 function insert_remote_image( $xml_dir, $category_id )578 {579 global $count_new,$lang;580 581 $output = '';582 $root = getChild( $xml_dir, 'root' );583 584 $fs_pictures = array();585 $xml_pictures = getChildren( $root, 'picture' );586 foreach ( $xml_pictures as $xml_picture ) {587 array_push( $fs_pictures, getAttribute( $xml_picture, 'file' ) );588 }589 590 // we have to delete all the images from the database that are not in the591 // directory anymore (not in the XML anymore)592 $query = 'SELECT id,file FROM '.IMAGES_TABLE;593 $query.= ' WHERE storage_category_id = '.$category_id;594 $query.= ';';595 $result = mysql_query( $query );596 while ( $row = mysql_fetch_array( $result ) )597 {598 if ( !in_array( $row['file'], $fs_pictures ) )599 {600 $output.= $row['file'];601 $output.= ' <span style="font-weight:bold;">';602 $output.= $lang['update_disappeared'].'</span><br />';603 delete_image( $row['id'] );604 }605 }606 607 $database_pictures = array();608 $query = 'SELECT file FROM '.IMAGES_TABLE;609 $query.= ' WHERE storage_category_id = '.$category_id;610 $query.= ';';611 $result = mysql_query( $query );612 while ( $row = mysql_fetch_array( $result ) )613 {614 array_push( $database_pictures, $row['file'] );615 }616 617 $inserts = array();618 $xml_pictures = getChildren( $root, 'picture' );619 foreach ( $xml_pictures as $xml_picture ) {620 // <picture file="albatros.jpg" tn_ext="png" filesize="35" width="640"621 // height="480" />622 $file = getAttribute( $xml_picture, 'file' );623 624 // is the picture already existing in the database ?625 if ( !in_array( $file, $database_pictures ) )626 {627 $tn_ext = getAttribute( $xml_picture, 'tn_ext' );628 // (file, storage_category_id, date_available, tn_ext, filesize,629 // width, height)630 $value = '(';631 $value.= "'".$file."'";632 $value.= ','.$category_id;633 $value.= ",'".date( 'Y-m-d' )."'";634 $value.= ",'".$tn_ext."'";635 $value.= ','.getAttribute( $xml_picture, 'filesize' );636 $value.= ','.getAttribute( $xml_picture, 'width' );637 $value.= ','.getAttribute( $xml_picture, 'height' );638 $value.= ')';639 640 $count_new++;641 $output.= $file;642 $output.= ' <span style="font-weight:bold;">';643 $output.= $lang['update_research_added'].'</span>';644 $output.= ' ('.$lang['update_research_tn_ext'].' '.$tn_ext.')';645 $output.= '<br />';646 array_push( $inserts, $value );647 }648 }649 650 if ( count( $inserts ) > 0 )651 {652 // inserts all found pictures653 $query = 'INSERT INTO '.IMAGES_TABLE;654 $query.= ' (file,storage_category_id,date_available,tn_ext';655 $query.= ',filesize,width,height)';656 $query.= ' VALUES ';657 $query.= implode( ',', $inserts );658 $query.= ';';659 mysql_query( $query );660 661 // what are the ids of the pictures in the $category_id ?662 $ids = array();663 664 $query = 'SELECT id FROM '.IMAGES_TABLE;665 $query.= ' WHERE storage_category_id = '.$category_id;666 $query.= ';';667 $result = mysql_query( $query );668 while ( $row = mysql_fetch_array( $result ) )669 {670 array_push( $ids, $row['id'] );671 }672 673 // recreation of the links between this storage category pictures and674 // its storage category675 $query = 'DELETE FROM '.IMAGE_CATEGORY_TABLE;676 $query.= ' WHERE category_id = '.$category_id;677 $query.= ' AND image_id IN ('.implode( ',', $ids ).')';678 $query.= ';';679 mysql_query( $query );680 681 $query = 'INSERT INTO '.IMAGE_CATEGORY_TABLE;682 $query.= '(category_id,image_id) VALUES ';683 foreach ( $ids as $num => $image_id ) {684 if ( $num > 0 ) $query.= ',';685 $query.= '('.$category_id.','.$image_id.')';686 }687 $query.= ';';688 mysql_query( $query );689 }690 691 return $output;692 }693 694 568 //----------------------------------------------------- template initialization 695 $template->set_filenames( array('update'=>'admin/update.tpl'));569 $template->set_filenames(array('update'=>'admin/update.tpl')); 696 570 697 571 $template->assign_vars(array( … … 703 577 'L_DEL_CATEGORY'=>$lang['update_deletion_conclusion'], 704 578 705 'U_CAT_UPDATE'=>add_session_id( PHPWG_ROOT_PATH.'admin.php?page=update&update=cats'),706 'U_ALL_UPDATE'=>add_session_id( PHPWG_ROOT_PATH.'admin.php?page=update&update=all')707 579 'U_CAT_UPDATE'=>add_session_id(PHPWG_ROOT_PATH.'admin.php?page=update&update=cats'), 580 'U_ALL_UPDATE'=>add_session_id(PHPWG_ROOT_PATH.'admin.php?page=update&update=all') 581 )); 708 582 709 583 //-------------------------------------------- introduction : choices of update 710 584 // Display choice if "update" var is not specified 711 if (!isset( $_GET['update']))585 if (!isset($_GET['update'])) 712 586 { 713 587 $template->assign_block_vars('introduction',array()); … … 716 590 else 717 591 { 718 check_cat_id( $_GET['update']);592 check_cat_id($_GET['update']); 719 593 $start = get_moment(); 720 594 $count_new = 0; 721 595 $count_deleted = 0; 722 596 723 if ( isset( $page['cat'] ))724 { 725 $categories = insert_local_category( $page['cat']);597 if (isset($page['cat'])) 598 { 599 $categories = insert_local_category($page['cat']); 726 600 } 727 601 else 728 602 { 729 $categories = insert_local_category( 'NULL');603 $categories = insert_local_category('NULL'); 730 604 } 731 605 $end = get_moment(); 732 //echo get_elapsed_time( $start, $end).' for update <br />';606 //echo get_elapsed_time($start, $end).' for update <br />'; 733 607 $template->assign_block_vars('update',array( 734 608 'CATEGORIES'=>$categories, 735 609 'NEW_CAT'=>$count_new, 736 610 'DEL_CAT'=>$count_deleted 737 )); 738 } 739 //------------------------------------------------- remote update : listing.xml 740 if ( @is_file( './admin/listing.xml' ) ) 741 { 742 $count_new = 0; 743 $count_deleted = 0; 744 $vtp->addSession( $sub, 'remote_update' ); 745 746 $start = get_moment(); 747 remote_images(); 748 $end = get_moment(); 749 echo get_elapsed_time( $start, $end ).' for remote_images<br />'; 750 751 $vtp->setVar( $sub, 'remote_update.count_new', $count_new ); 752 $vtp->setVar( $sub, 'remote_update.count_deleted', $count_deleted ); 753 754 $vtp->closeSession( $sub, 'remote_update' ); 611 )); 755 612 } 756 613 //---------------------------------------- update informations about categories 757 if ( isset( $_GET['update'])758 or isset( $page['cat'])759 or @is_file( './listing.xml') && DEBUG)614 if (isset($_GET['update']) 615 or isset($page['cat']) 616 or @is_file('./listing.xml') && DEBUG) 760 617 { 761 618 $start = get_moment(); 762 update_category( 'all');619 update_category('all'); 763 620 ordering('NULL'); 764 621 $end = get_moment(); 765 echo get_elapsed_time( $start, $end ).' for update_category( all)<br />';622 echo get_elapsed_time($start, $end).' for update_category(all)<br />'; 766 623 } 767 624 //----------------------------------------------------------- sending html code
Note: See TracChangeset
for help on using the changeset viewer.