Changeset 278


Ignore:
Timestamp:
Jan 13, 2004, 11:43:18 PM (20 years ago)
Author:
z0rglub
Message:

Improved function insert_local_image : about 80 to 90 % of update time saved
:-)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/release-1_3/admin/update.php

    r276 r278  
    108108      if ( preg_match( '/^[a-zA-Z0-9-_.]+$/', $sub_dir ) )
    109109      {
    110         $value = '';
    111110        $name = str_replace( '_', ' ', $sub_dir );
    112111
    113         $value.= "('".$sub_dir."','".$name."',1";
     112        $value = "('".$sub_dir."','".$name."',1";
    114113        if ( !is_numeric( $id_uppercat ) ) $value.= ',NULL';
    115114        else                               $value.= ','.$id_uppercat;
     
    167166}
    168167
    169 function insert_local_image( $rep, $category_id )
     168function insert_local_image( $dir, $category_id )
    170169{
    171170  global $lang,$conf,$count_new;
    172171
    173172  $output = '';
     173
     174  // fs means filesystem : $fs_pictures contains pictures in the filesystem
     175  // found in $dir, $fs_thumbnails contains thumbnails...
     176  $fs_pictures   = get_picture_files( $dir );
     177  $fs_thumbnails = get_thumb_files( $dir.'thumbnail' );
     178
    174179  // we have to delete all the images from the database that :
    175180  //     - are not in the directory anymore
     
    182187  while ( $row = mysql_fetch_array( $result ) )
    183188  {
    184     $lien_image = $rep.'/'.$row['file'];
    185     $lien_thumbnail = $rep.'/thumbnail/'.$conf['prefix_thumbnail'];
    186     $lien_thumbnail.= get_filename_wo_extension( $row['file'] );
    187     $lien_thumbnail.= '.'.$row['tn_ext'];
    188                
    189     if ( !is_file ( $lien_image ) or !is_file ( $lien_thumbnail ) )
    190     {
    191       if ( !is_file ( $lien_image ) )
     189    $pic_to_delete = false;
     190    if ( !in_array( $row['file'], $fs_pictures ) )
     191    {
     192      $output.= $row['file'];
     193      $output.= ' <span style="font-weight:bold;">';
     194      $output.= $lang['update_disappeared'].'</span><br />';
     195      $pic_to_delete = true;
     196    }
     197
     198    $thumbnail = $conf['prefix_thumbnail'];
     199    $thumbnail.= get_filename_wo_extension( $row['file'] );
     200    $thumbnail.= '.'.$row['tn_ext'];
     201    if ( !in_array( $thumbnail, $fs_thumbnails ) )
     202    {
     203      $output.= $row['file'];
     204      $output.= ' : <span style="font-weight:bold;">';
     205      $output.= $lang['update_disappeared_tn'].'</span><br />';
     206      $pic_to_delete = true;
     207    }
     208
     209    if ( $pic_to_delete ) delete_image( $row['id'] );
     210  }
     211
     212  $registered_pictures = array();
     213  $query = 'SELECT file';
     214  $query.= ' FROM '.PREFIX_TABLE.'images';
     215  $query.= ' WHERE storage_category_id = '.$category_id;
     216  $query.= ';';
     217  $result = mysql_query( $query );
     218  while ( $row = mysql_fetch_array( $result ) )
     219  {
     220    array_push( $registered_pictures, $row['file'] );
     221  }
     222
     223  // validated pictures are picture uploaded by users, validated by an admin
     224  // and not registered (visible) yet
     225  $validated_pictures    = array();
     226  $unvalidated_pictures  = array();
     227 
     228  $query = 'SELECT file,infos,validated';
     229  $query.= ' FROM '.PREFIX_TABLE.'waiting';
     230  $query.= ' WHERE storage_category_id = '.$category_id;
     231  $query.= ';';
     232  $result = mysql_query( $query );
     233  while ( $row = mysql_fetch_array( $result ) )
     234  {
     235    if ( $row['validated'] == 'true' )
     236      $validated_pictures[$row['file']] = $row['infos'];
     237    else
     238      array_push( $unvalidated_pictures, $row['file'] );
     239  }
     240
     241  // we only search among the picture present in the filesystem and not
     242  // present in the database yet. If we know that this picture is known as
     243  // an uploaded one but not validated, it's not tested neither
     244  $unregistered_pictures = array_diff( $fs_pictures
     245                                       ,$registered_pictures
     246                                       ,$unvalidated_pictures );
     247
     248  $inserts = array();
     249 
     250  foreach ( $unregistered_pictures as $unregistered_picture ) {
     251    if ( preg_match( '/^[a-zA-Z0-9-_.]+$/', $unregistered_picture ) )
     252    {
     253      $file_wo_ext = get_filename_wo_extension( $unregistered_picture );
     254      $tn_ext = '';
     255      foreach ( $conf['picture_ext'] as $ext ) {
     256        $test = $conf['prefix_thumbnail'].$file_wo_ext.'.'.$ext;
     257        if ( !in_array( $test, $fs_thumbnails ) ) continue;
     258        else { $tn_ext = $ext; break; }
     259      }
     260      // if we found a thumnbnail corresponding to our picture...
     261      if ( $tn_ext != '' )
    192262      {
    193         $output.= $row['file'];
     263        $image_size = @getimagesize( $dir.$unregistered_picture );
     264        // (file, storage_category_id, date_available, tn_ext, filesize,
     265        // width, height, name, author, comment, date_creation)'
     266        $value = '(';
     267        $value.= "'".$unregistered_picture."'";
     268        $value.= ','.$category_id;
     269        $value.= ",'".date( 'Y-m-d' )."'";
     270        $value.= ",'".$tn_ext."'";
     271        $value.= ','.floor( filesize( $dir.$unregistered_picture) / 1024 );
     272        $value.= ','.$image_size[0];
     273        $value.= ','.$image_size[1];
     274        if ( isset( $validated_pictures[$unregistered_picture] ) )
     275        {
     276          // retrieving infos from the XML description from waiting table
     277          $infos = nl2br( $validated_pictures[$unregistered_picture] );
     278
     279          $unixtime = getAttribute( $infos, 'date_creation' );
     280          if ($unixtime != '') $date_creation ="'".date('Y-m-d',$unixtime)."'";
     281          else                 $date_creation = 'NULL';
     282         
     283          $value.= ",'".getAttribute( $infos, 'name' )."'";
     284          $value.= ",'".getAttribute( $infos, 'author' )."'";
     285          $value.= ",'".getAttribute( $infos, 'comment')."'";
     286          $value.= ','.$date_creation;
     287
     288          // deleting the waiting element
     289          $query = 'DELETE FROM '.PREFIX_TABLE.'waiting';
     290          $query.= " WHERE file = '".$unregistered_picture."'";
     291          $query.= ' AND storage_category_id = '.$category_id;
     292          $query.= ';';
     293          mysql_query( $query );
     294        }
     295        else
     296        {
     297          $value.= ",'','','',NULL";
     298        }
     299        $value.= ')';
     300       
     301        $count_new++;
     302        $output.= $unregistered_picture;
    194303        $output.= ' <span style="font-weight:bold;">';
    195         $output.= $lang['update_disappeared'].'</span><br />';
    196       }
    197       if ( !is_file ( $lien_thumbnail ) )
     304        $output.= $lang['update_research_added'].'</span>';
     305        $output.= ' ('.$lang['update_research_tn_ext'].' '.$tn_ext.')';
     306        $output.= '<br />';
     307        array_push( $inserts, $value );
     308      }
     309      else
    198310      {
    199         $output.= $row['file'];
    200         $output.= ' : <span style="font-weight:bold;">';
    201         $output.= $lang['update_disappeared_tn'].'</span><br />';
    202       }
    203       // suppression de la base :
    204       delete_image( $row['id'] );
    205     }
    206   }
    207                
    208   // searching the new images in the directory
    209   $pictures = array();         
    210   $tn_ext = '';
    211   if ( $opendir = opendir( $rep ) )
    212   {
    213     while ( $file = readdir( $opendir ) )
    214     {
    215       if ( is_file( $rep.'/'.$file ) and is_image( $rep.'/'.$file ) )
    216       {
    217         // is the picture waiting for validation by an administrator ?
    218         $query = 'SELECT id,validated,infos';
    219         $query.= ' FROM '.PREFIX_TABLE.'waiting';
    220         $query.= ' WHERE storage_category_id = '.$category_id;
    221         $query.= " AND file = '".$file."'";
    222         $query.= ';';
    223         $result = mysql_query( $query );
    224         $waiting = mysql_fetch_array( $result );
    225         if (mysql_num_rows( $result ) == 0 or $waiting['validated'] == 'true')
    226         {
    227           if ( $tn_ext = TN_exists( $rep, $file ) )
    228           {
    229             // is the picture already in the database ?
    230             $query = 'SELECT id';
    231             $query.= ' FROM '.PREFIX_TABLE.'images';
    232             $query.= ' WHERE storage_category_id = '.$category_id;
    233             $query.= " AND file = '".$file."'";
    234             $query.= ';';
    235             $result = mysql_query( $query );
    236             if ( mysql_num_rows( $result ) == 0 )
    237             {
    238               // the name of the file must not use acentuated characters or
    239               // blank space..
    240               if ( preg_match( '/^[a-zA-Z0-9-_.]+$/', $file ) )
    241               {
    242                 $picture = array();
    243                 $picture['file']     = $file;
    244                 $picture['tn_ext']   = $tn_ext;
    245                 $picture['date'] = date( 'Y-m-d', filemtime($rep.'/'.$file) );
    246                 $picture['filesize'] = floor( filesize($rep.'/'.$file) / 1024);
    247                 $image_size = @getimagesize( $rep.'/'.$file );
    248                 $picture['width']    = $image_size[0];
    249                 $picture['height']   = $image_size[1];
    250                 if ( $waiting['validated'] == 'true' )
    251                 {
    252                   // retrieving infos from the XML description of
    253                   // $waiting['infos']
    254                   $infos = nl2br( $waiting['infos'] );
    255                   $picture['author']        = getAttribute( $infos, 'author' );
    256                   $picture['comment']       = getAttribute( $infos, 'comment');
    257                   $unixtime = getAttribute( $infos, 'date_creation' );
    258                   $picture['date_creation'] = '';
    259                   if ( $unixtime != '' )
    260                     $picture['date_creation'] = date( 'Y-m-d', $unixtime );
    261                   $picture['name']          = getAttribute( $infos, 'name' );
    262                   // deleting the waiting element
    263                   $query = 'DELETE FROM '.PREFIX_TABLE.'waiting';
    264                   $query.= ' WHERE id = '.$waiting['id'];
    265                   $query.= ';';
    266                   mysql_query( $query );
    267                 }
    268                 array_push( $pictures, $picture );
    269               }
    270               else
    271               {
    272                 $output.= '<span style="color:red;">"'.$file.'" : ';
    273                 $output.= $lang['update_wrong_dirname'].'</span><br />';
    274               }
    275 
    276             }
    277           }
    278           else
    279           {
    280             $output.= '<span style="color:red;">';
    281             $output.= $lang['update_missing_tn'].' : '.$file;
    282             $output.= ' (<span style="font-weight:bold;">';
    283             $output.= $conf['prefix_thumbnail'];
    284             $output.= get_filename_wo_extension( $file ).'.XXX</span>';
    285             $output.= ', XXX = ';
    286             $output.= implode( ', ', $conf['picture_ext'] );
    287             $output.= ')</span><br />';
    288           }
    289         }
    290       }
    291     }
    292   }
    293   // inserting the pictures found in the directory
    294   foreach ( $pictures as $picture ) {
    295         $name = '';
    296         $author = '';
    297         $comment = '';
    298         if (isset ($picture['name'])) $name = $picture['name'];
    299         if (isset ($picture['author'])) $author = $picture['author'];
    300         if (isset ($picture['comment'])) $comment = $picture['comment'];
    301  
     311        $output.= '<span style="color:red;">';
     312        $output.= $lang['update_missing_tn'].' : '.$unregistered_picture;
     313        $output.= ' (<span style="font-weight:bold;">';
     314        $output.= $conf['prefix_thumbnail'];
     315        $output.= get_filename_wo_extension( $unregistered_picture );
     316        $output.= '.XXX</span>';
     317        $output.= ', XXX = ';
     318        $output.= implode( ', ', $conf['picture_ext'] );
     319        $output.= ')</span><br />';
     320      }
     321    }
     322    else
     323    {
     324      $output.= '<span style="color:red;">"'.$file.'" : ';
     325      $output.= $lang['update_wrong_dirname'].'</span><br />';
     326    }
     327  }
     328
     329  if ( count( $inserts ) > 0 )
     330  {
     331    // inserts all found pictures
    302332    $query = 'INSERT INTO '.PREFIX_TABLE.'images';
    303333    $query.= ' (file,storage_category_id,date_available,tn_ext';
     
    305335    $query.= ',name,author,comment,date_creation)';
    306336    $query.= ' VALUES ';
    307     $query.= "('".$picture['file']."','".$category_id."'";
    308     $query.= ",'".$picture['date']."','".$picture['tn_ext']."'";
    309     $query.= ",'".$picture['filesize']."','".$picture['width']."'";
    310     $query.= ",'".$picture['height']."','$name', '$author', '$comment'";
    311     if ( isset ($picture['date_creation']))
    312     {
    313       $query.= ",'".$picture['date_creation']."'";
    314     }
    315     else
    316     {
    317       $query.= ',NULL';
    318     }
    319     $query.= ');';
     337    $query.= implode( ',', $inserts );
     338    $query.= ';';
    320339    mysql_query( $query );
    321     $count_new++;
    322     // retrieving the id of newly inserted picture
     340
     341    // what are the ids of the pictures in the $category_id ?
     342    $ids = array();
     343
    323344    $query = 'SELECT id';
    324345    $query.= ' FROM '.PREFIX_TABLE.'images';
    325346    $query.= ' WHERE storage_category_id = '.$category_id;
    326     $query.= " AND file = '".$picture['file']."'";
    327     $query.= ';';
    328     list( $image_id ) = mysql_fetch_array( mysql_query( $query ) );
    329     // adding the link between this picture and its storage category
     347    $query.= ';';
     348    $result = mysql_query( $query );
     349    while ( $row = mysql_fetch_array( $result ) )
     350    {
     351      array_push( $ids, $row['id'] );
     352    }
     353
     354    // recreation of the links between this storage category pictures and
     355    // its storage category
     356    $query = 'DELETE FROM '.PREFIX_TABLE.'image_category';
     357    $query.= ' WHERE category_id = '.$category_id;
     358    $query.= ' AND image_id IN ('.implode( ',', $ids ).')';
     359    $query.= ';';
     360    mysql_query( $query );
     361
    330362    $query = 'INSERT INTO '.PREFIX_TABLE.'image_category';
    331     $query.= ' (image_id,category_id) VALUES ';
    332     $query.= ' ('.$image_id.','.$category_id.')';
     363    $query.= '(category_id,image_id) VALUES ';
     364    foreach ( $ids as $num => $image_id ) {
     365      if ( $num > 0 ) $query.= ',';
     366      $query.= '('.$category_id.','.$image_id.')';
     367    }
    333368    $query.= ';';
    334369    mysql_query( $query );
    335 
    336     $output.= $picture['file'];
    337     $output.= ' <span style="font-weight:bold;">';
    338     $output.= $lang['update_research_added'].'</span>';
    339     $output.= ' ('.$lang['update_research_tn_ext'].' '.$picture['tn_ext'].')';
    340     $output.= '<br />';
    341370  }
    342371  return $output;
Note: See TracChangeset for help on using the changeset viewer.