Changeset 466 for trunk


Ignore:
Timestamp:
Aug 5, 2004, 7:33:14 PM (20 years ago)
Author:
z0rglub
Message:
  • non picture files management
  • all remote site update functions moved to nex file update_remote.php
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/admin/update.php

    r423 r466  
    2626// +-----------------------------------------------------------------------+
    2727
    28 include_once( PHPWG_ROOT_PATH.'admin/include/isadmin.inc.php' );
     28include_once( PHPWG_ROOT_PATH.'admin/include/isadmin.inc.php');
    2929//------------------------------------------------------------------- functions
    30 function ordering( $id_uppercat )
     30function ordering( $id_uppercat)
    3131{
    3232  $rank = 1;
     
    3434  $query = 'SELECT id';
    3535  $query.= ' FROM '.CATEGORIES_TABLE;
    36   if ( !is_numeric( $id_uppercat ) )
     36  if ( !is_numeric( $id_uppercat))
    3737  {
    3838    $query.= ' WHERE id_uppercat IS NULL';
     
    4444  $query.= ' ORDER BY rank ASC, dir ASC';
    4545  $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))
    4848  {
    4949    $query = 'UPDATE '.CATEGORIES_TABLE;
     
    5151    $query.= ' WHERE id = '.$row['id'];
    5252    $query.= ';';
    53     mysql_query( $query );
     53    mysql_query( $query);
    5454    $rank++;
    55     ordering( $row['id'] );
     55    ordering( $row['id']);
    5656  }
    5757}
    5858
    59 function insert_local_category( $id_uppercat )
     59function insert_local_category($id_uppercat)
    6060{
    6161  global $conf, $page, $user, $lang;
     
    6666  // 0. retrieving informations on the category to display
    6767  $cat_directory = PHPWG_ROOT_PATH.'galleries';
    68   if ( is_numeric( $id_uppercat ) )
     68  if (is_numeric($id_uppercat))
    6969  {
    7070    $query = 'SELECT name,uppercats,dir FROM '.CATEGORIES_TABLE;
    7171    $query.= ' WHERE id = '.$id_uppercat;
    7272    $query.= ';';
    73     $row = mysql_fetch_array( mysql_query( $query ) );
     73    $row = mysql_fetch_array( mysql_query( $query));
    7474    $uppercats = $row['uppercats'];
    7575    $name      = $row['name'];
    7676    $dir       = $row['dir'];
    7777
    78     $upper_array = explode( ',', $uppercats );
     78    $upper_array = explode( ',', $uppercats);
    7979
    8080    $local_dir = '';
    8181
    8282    $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 = '
     84SELECT id,dir FROM '.CATEGORIES_TABLE.'
     85  WHERE id IN ('.$uppercats.')
     86;';
     87    $result = mysql_query( $query);
     88    while ($row = mysql_fetch_array($result))
    8889    {
    8990      $database_dirs[$row['id']] = $row['dir'];
    9091    }
    91     foreach ( $upper_array as $id ) {
     92    foreach ($upper_array as $id)
     93    {
    9294      $local_dir.= $database_dirs[$id].'/';
    9395    }
     
    103105    // 2. we search pictures of the category only if the update is for all
    104106    //    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);
    112114
    113115  $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 = '
     117SELECT 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))
    122134  {
    123135    $sub_category_dirs[$row['id']] = $row['dir'];
     
    125137 
    126138  // 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    }
    129145  }
    130146
     
    132148  $inserts = array();
    133149 
    134   foreach ( $sub_dirs as $sub_dir ) {
     150  foreach ($sub_dirs as $sub_dir)
     151  {
    135152    // 5. Is the category already existing ? we create a subcat if not
    136153    //    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))
    141158      {
    142         $name = str_replace( '_', ' ', $sub_dir );
     159        $name = str_replace('_', ' ', $sub_dir);
    143160
    144161        $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        }
    147170        $value.= ",'undef'";
    148171        $value.= ')';
    149         array_push( $inserts, $value );
     172        array_push($inserts, $value);
    150173      }
    151174      else
     
    158181
    159182  // 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 = '
     186INSERT INTO '.CATEGORIES_TABLE.'
     187  (dir,name,site_id,id_uppercat,uppercats) VALUES
     188';
     189    $query.= implode(',', $inserts);
     190    $query.= '
     191;';
     192    mysql_query($query);
    167193    // 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 = '
     195UPDATE '.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);
    177218  }
    178219
    179220  // 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 = '
     222SELECT 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))
    194245  {
    195246    $output.= '</ul>';
     
    198249}
    199250
    200 function insert_local_image( $dir, $category_id )
     251function insert_local_element($dir, $category_id)
    201252{
    202   global $lang,$conf,$count_new;
     253  global $lang,$conf,$count_new, $count_deleted;
    203254
    204255  $output = '';
    205256
    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 = '
     269SELECT 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))
    223277    {
    224278      $output.= $row['file'];
    225279      $output.= ' <span style="font-weight:bold;">';
    226280      $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 = '
     287SELECT 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  {
    230298    $thumbnail = $conf['prefix_thumbnail'];
    231     $thumbnail.= get_filename_wo_extension( $row['file'] );
     299    $thumbnail.= get_filename_wo_extension($row['file']);
    232300    $thumbnail.= '.'.$row['tn_ext'];
    233     if ( !in_array( $thumbnail, $fs_thumbnails ) )
     301    if (!in_array($thumbnail, $fs_thumbnails))
    234302    {
    235303      $output.= $row['file'];
    236304      $output.= ' : <span style="font-weight:bold;">';
    237305      $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 = '
     319SELECT 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']);
    252326  }
    253327
     
    257331  $unvalidated_pictures  = array();
    258332 
    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 = '
     334SELECT 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    {
    267343      $validated_pictures[$row['file']] = $row['infos'];
     344    }
    268345    else
    269       array_push( $unvalidated_pictures, $row['file'] );
     346    {
     347      array_push($unvalidated_pictures, $row['file']);
     348    }
    270349  }
    271350
     
    273352  // present in the database yet. If we know that this picture is known as
    274353  // an uploaded one but not validated, it's not tested neither
    275   $unregistered_pictures = array_diff( $fs_pictures
    276                                        ,$registered_pictures
    277                                        ,$unvalidated_pictures );
     354  $unregistered_elements = array_diff($fs_files
     355                                      ,$registered_elements
     356                                      ,$unvalidated_pictures);
    278357
    279358  $inserts = array();
    280359 
    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);
    285365      $tn_ext = '';
    286       foreach ( $conf['picture_ext'] as $ext ) {
     366      foreach ($conf['picture_ext'] as $ext)
     367      {
    287368        $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        }
    290378      }
    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']))
    293384      {
    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            }
    313414         
    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 = '
     422DELETE 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);
    325442        }
    326443        else
    327444        {
    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        }
    339455      }
    340456      else
    341457      {
    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);
    351511      }
    352512    }
    353513    else
    354514    {
    355       $output.= '<span style="color:red;">"'.$unregistered_picture.'" : ';
     515      $output.= '<span style="color:red;">"'.$unregistered_element.'" : ';
    356516      $output.= $lang['update_wrong_dirname'].'</span><br />';
    357517    }
    358518  }
    359 
    360   if ( count( $inserts ) > 0 )
     519 
     520  if (count($inserts) > 0)
    361521  {
    362522    // 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 = '
     524INSERT 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);
    371531
    372532    // what are the ids of the pictures in the $category_id ?
    373533    $ids = array();
    374534
    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 = '
     536SELECT 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']);
    383544    }
    384545
    385546    // recreation of the links between this storage category pictures and
    386547    // 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 = '
     549DELETE 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 = '
     560INSERT INTO '.IMAGE_CATEGORY_TABLE.'
     561  (category_id,image_id) VALUES
     562  '.implode(',', $ids).'
     563;';
     564    mysql_query($query);
    401565  }
    402566  return $output;
    403567}
    404 
    405 // remote_images verifies if a file named "listing.xml" is present is the
    406 // admin directory. If it is the case, creation of a remote picture storage
    407 // site if it doesn't already exists. Then, the function calls
    408 // 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 database
    429     $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   else
    436   {
    437     // we get the already registered id
    438     $row = mysql_fetch_array( $result );
    439     $site_id = $row['id'];
    440   }
    441 
    442   // 3. available dirs in the file
    443   $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 and
    448 // insert the contained categories if the are not in the database yet. The
    449 // function also deletes the categories that are in the database and not in
    450 // 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 display
    458   $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 update
    471     $src = './template/'.$user['template'].'/admin/images/puce.gif';
    472     $output = '<img src="'.$src.'" alt="&gt;" />';
    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 all
    478     //    or a cat_id is specified
    479     $output.= insert_remote_image( $xml_content, $id_uppercat );
    480   }
    481 
    482   // $xml_dirs contains dir names contained in the xml file for this
    483   // id_uppercat
    484   $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 this
    491   // id_uppercat and site_id
    492   $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 taken
    498   $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 anymore
    506   foreach ( $database_dirs as $id => $dir ) {
    507     if ( !in_array( $dir, $xml_dirs ) ) delete_category( $id );
    508   }
    509 
    510   // array of new categories to insert
    511   $inserts = array();
    512  
    513   foreach ( $xml_dirs as $xml_dir ) {
    514     // 5. Is the category already existing ? we create a subcat if not
    515     //    existing
    516     $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 category
    531   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 field
    539     $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 taken
    557   $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 and
    576 // 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 the
    591   // 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 pictures
    653     $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 and
    674     // its storage category
    675     $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 
    694568//----------------------------------------------------- template initialization
    695 $template->set_filenames( array('update'=>'admin/update.tpl') );
     569$template->set_filenames(array('update'=>'admin/update.tpl'));
    696570
    697571$template->assign_vars(array(
     
    703577  'L_DEL_CATEGORY'=>$lang['update_deletion_conclusion'],
    704578 
    705   'U_CAT_UPDATE'=>add_session_id( PHPWG_ROOT_PATH.'admin.php?page=update&amp;update=cats' ),
    706   'U_ALL_UPDATE'=>add_session_id( PHPWG_ROOT_PATH.'admin.php?page=update&amp;update=all' )
    707   ));
     579  'U_CAT_UPDATE'=>add_session_id(PHPWG_ROOT_PATH.'admin.php?page=update&amp;update=cats'),
     580  'U_ALL_UPDATE'=>add_session_id(PHPWG_ROOT_PATH.'admin.php?page=update&amp;update=all')
     581 ));
    708582 
    709583//-------------------------------------------- introduction : choices of update
    710584// Display choice if "update" var is not specified
    711 if (!isset( $_GET['update'] ))
     585if (!isset($_GET['update']))
    712586{
    713587 $template->assign_block_vars('introduction',array());
     
    716590else
    717591{
    718   check_cat_id( $_GET['update'] );
     592  check_cat_id($_GET['update']);
    719593  $start = get_moment();
    720594  $count_new = 0;
    721595  $count_deleted = 0;
    722596 
    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']);
    726600  }
    727601  else
    728602  {
    729     $categories = insert_local_category( 'NULL' );
     603    $categories = insert_local_category('NULL');
    730604  }
    731605  $end = get_moment();
    732   //echo get_elapsed_time( $start, $end ).' for update <br />';
     606  //echo get_elapsed_time($start, $end).' for update <br />';
    733607  $template->assign_block_vars('update',array(
    734608    'CATEGORIES'=>$categories,
    735609        'NEW_CAT'=>$count_new,
    736610        '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   ));
    755612}
    756613//---------------------------------------- update informations about categories
    757 if ( isset( $_GET['update'] )
    758      or isset( $page['cat'] )
    759      or @is_file( './listing.xml' ) && DEBUG)
     614if (isset($_GET['update'])
     615     or isset($page['cat'])
     616     or @is_file('./listing.xml') && DEBUG)
    760617{
    761618  $start = get_moment();
    762   update_category( 'all' );
     619  update_category('all');
    763620  ordering('NULL');
    764621  $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 />';
    766623}
    767624//----------------------------------------------------------- sending html code
Note: See TracChangeset for help on using the changeset viewer.