Changeset 659 for trunk/admin/update.php


Ignore:
Timestamp:
Dec 27, 2004, 3:30:49 PM (19 years ago)
Author:
plg
Message:
  • admin/update : filesystem synchronization process completely rewritten. How to speed up sync ? by avoiding recursivity !
  • admin/update : option to display verbose information details
  • admin/update : option to simulate only. No database insert, delete or update will be made
  • bug fixed : in admin/cat_list, if you delete a virtual category, you may create a gap in the rank list. This gap will generate errors when trying to move a category on this gap. Fixed by calling ordering and update_global_rank at category deletion.
  • admin/cat_list, only one query to insert a new virtual category (no need of a second query to update uppercats and global_rank)
  • for a given category, even if empty, the representing element must not be the one of a forbidden category for the current user
  • generation time optionnaly displayed on the bottom of each page becomes more price : number of SQL queries and SQL time added.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/admin/update.php

    r642 r659  
    2626// +-----------------------------------------------------------------------+
    2727
    28 if( !defined("PHPWG_ROOT_PATH") )
     28if (!defined('PHPWG_ROOT_PATH'))
    2929{
    30   die ("Hacking attempt!");
     30  die ('Hacking attempt!');
    3131}
    3232include_once( PHPWG_ROOT_PATH.'admin/include/isadmin.inc.php');
    3333
    3434define('CURRENT_DATE', date('Y-m-d'));
    35 // +-----------------------------------------------------------------------+
    36 // |                              functions                                |
    37 // +-----------------------------------------------------------------------+
    38 
    39 function insert_local_category($id_uppercat)
     35$error_labels = array('PWG-UPDATE-1' => $lang['update_wrong_dirname_short'],
     36                      'PWG-UPDATE-2' => $lang['update_missing_tn_short']);
     37$errors = array();
     38$infos = array();
     39// +-----------------------------------------------------------------------+
     40// |                      directories / categories                         |
     41// +-----------------------------------------------------------------------+
     42if (isset($_POST['submit'])
     43    and ($_POST['sync'] == 'dirs' or $_POST['sync'] == 'files'))
    4044{
    41   global $conf, $page, $user, $lang, $counts;
    42  
    43   $uppercats = '';
    44   $output = '';
    45 
    46   // 0. retrieving informations on the category to display
    47   $cat_directory = PHPWG_ROOT_PATH.'galleries';
    48   if (is_numeric($id_uppercat))
     45  $counts['new_categories'] = 0;
     46  $counts['del_categories'] = 0;
     47  $counts['del_elements'] = 0;
     48  $counts['new_elements'] = 0;
     49
     50  // shall we simulate only
     51  if (isset($_POST['simulate']) and $_POST['simulate'] == 1)
     52  {
     53    $simulate = true;
     54  }
     55  else
     56  {
     57    $simulate = false;
     58  }
     59 
     60  $start = get_moment();
     61  // which categories to update ?
     62  $cat_ids = array();
     63
     64  $query = '
     65SELECT id, uppercats, global_rank, status, visible
     66  FROM '.CATEGORIES_TABLE.'
     67  WHERE dir IS NOT NULL
     68    AND site_id = 1';
     69  if (isset($_POST['cat']) and is_numeric($_POST['cat']))
     70  {
     71    if (isset($_POST['subcats-included']) and $_POST['subcats-included'] == 1)
     72    {
     73      $query.= '
     74    AND uppercats REGEXP \'(^|,)'.$_POST['cat'].'(,|$)\'
     75';
     76    }
     77    else
     78    {
     79      $query.= '
     80    AND id = '.$_POST['cat'].'
     81';
     82    }
     83  }
     84  $query.= '
     85;';
     86  $result = pwg_query($query);
     87
     88  $db_categories = array();
     89  while ($row = mysql_fetch_array($result))
     90  {
     91    $db_categories[$row['id']] = $row;
     92  }
     93
     94  // get categort full directories in an array for comparison with file
     95  // system directory tree
     96  $db_fulldirs = get_fulldirs(array_keys($db_categories));
     97 
     98  // what is the base directory to search file system sub-directories ?
     99  if (isset($_POST['cat']) and is_numeric($_POST['cat']))
     100  {
     101    $basedir = $db_fulldirs[$_POST['cat']];
     102  }
     103  else
    49104  {
    50105    $query = '
    51 SELECT id,name,uppercats,dir,visible,status
     106SELECT galleries_url
     107  FROM '.SITES_TABLE.'
     108  WHERE id = 1
     109;';
     110    list($galleries_url) = mysql_fetch_array(pwg_query($query));
     111    $basedir = preg_replace('#/*$#', '', $galleries_url);
     112  }
     113
     114  // we need to have fulldirs as keys to make efficient comparison
     115  $db_fulldirs = array_flip($db_fulldirs);
     116
     117  // finding next rank for each id_uppercat
     118  $query = '
     119SELECT id_uppercat, MAX(rank)+1 AS next_rank
    52120  FROM '.CATEGORIES_TABLE.'
    53   WHERE id = '.$id_uppercat.'
    54 ;';
    55     $row = mysql_fetch_array(pwg_query($query));
    56     $parent = array('id' => $row['id'],
    57                     'name' => $row['name'],
    58                     'dir' => $row['dir'],
    59                     'uppercats' => $row['uppercats'],
    60                     'visible' => $row['visible'],
    61                     'status' => $row['status']);
    62 
    63     $upper_array = explode( ',', $parent['uppercats']);
    64 
    65     $local_dir = '';
    66 
    67     $database_dirs = array();
     121  GROUP BY id_uppercat
     122;';
     123  $result = pwg_query($query);
     124  while ($row = mysql_fetch_array($result))
     125  {
     126    // for the id_uppercat NULL, we write 'NULL' and not the empty string
     127    if (!isset($row['id_uppercat']) or $row['id_uppercat'] == '')
     128    {
     129      $row['id_uppercat'] = 'NULL';
     130    }
     131    $next_rank[$row['id_uppercat']] = $row['next_rank'];
     132  }
     133 
     134  // next category id available
     135  $query = '
     136SELECT MAX(id)+1 AS next_id
     137  FROM '.CATEGORIES_TABLE.'
     138;';
     139  list($next_id) = mysql_fetch_array(pwg_query($query));
     140
     141  // retrieve file system sub-directories fulldirs
     142  $fs_fulldirs = get_fs_directories($basedir);
     143 
     144  $inserts = array();
     145  // new categories are the directories not present yet in the database
     146  foreach (array_diff($fs_fulldirs, array_keys($db_fulldirs)) as $fulldir)
     147  {
     148    $dir = basename($fulldir);
     149    if (preg_match('/^[a-zA-Z0-9-_.]+$/', $dir))
     150    {
     151      $insert = array();
     152     
     153      $insert{'id'} = $next_id++;
     154      $insert{'dir'} = $dir;
     155      $insert{'name'} = str_replace('_', ' ', $dir);
     156      $insert{'site_id'} = 1;
     157      $insert{'commentable'} = $conf['newcat_default_commentable'];
     158      $insert{'uploadable'} = $conf['newcat_default_uploadable'];
     159      $insert{'status'} = $conf{'newcat_default_status'};
     160      $insert{'visible'} = $conf{'newcat_default_visible'};
     161
     162      if (isset($db_fulldirs[dirname($fulldir)]))
     163      {
     164        $parent = $db_fulldirs[dirname($fulldir)];
     165
     166        $insert{'id_uppercat'} = $parent;
     167        $insert{'uppercats'} =
     168          $db_categories[$parent]['uppercats'].','.$insert{'id'};
     169        $insert{'rank'} = $next_rank[$parent]++;
     170        $insert{'global_rank'} =
     171          $db_categories[$parent]['global_rank'].'.'.$insert{'rank'};
     172        if ('private' == $db_categories[$parent]['status'])
     173        {
     174          $insert{'status'} = 'private';
     175        }
     176        if ('false' == $db_categories[$parent]['visible'])
     177        {
     178          $insert{'visible'} = 'false';
     179        }
     180      }
     181      else
     182      {
     183        $insert{'uppercats'} = $insert{'id'};
     184        $insert{'rank'} = $next_rank['NULL']++;
     185        $insert{'global_rank'} = $insert{'rank'};
     186      }
     187
     188      array_push($inserts, $insert);
     189      array_push($infos, array('path' => $fulldir,
     190                               'info' => $lang['update_research_added']));
     191
     192      // add the new category to $db_categories and $db_fulldirs array
     193      $db_categories[$insert{'id'}] =
     194        array(
     195          'id' => $insert{'id'},
     196          'status' => $insert{'status'},
     197          'visible' => $insert{'visible'},
     198          'uppercats' => $insert{'uppercats'},
     199          'global_rank' => $insert{'global_rank'}
     200          );
     201      $db_fulldirs[$fulldir] = $insert{'id'};
     202      $next_rank[$insert{'id'}] = 1;
     203    }
     204    else
     205    {
     206      array_push($errors, array('path' => $fulldir, 'type' => 'PWG-UPDATE-1'));
     207    }
     208  }
     209
     210  if (count($inserts) > 0)
     211  {
     212    if (!$simulate)
     213    {
     214      $dbfields = array(
     215        'id','dir','name','site_id','id_uppercat','uppercats','commentable',
     216        'uploadable','visible','status','rank','global_rank'
     217        );
     218      mass_inserts(CATEGORIES_TABLE, $dbfields, $inserts);
     219    }
     220   
     221    $counts['new_categories'] = count($inserts);
     222  }
     223
     224  // to delete categories
     225  $to_delete = array();
     226  foreach (array_diff(array_keys($db_fulldirs), $fs_fulldirs) as $fulldir)
     227  {
     228    array_push($to_delete, $db_fulldirs[$fulldir]);
     229    unset($db_fulldirs[$fulldir]);
     230    array_push($infos, array('path' => $fulldir,
     231                             'info' => $lang['update_research_deleted']));
     232  }
     233  if (count($to_delete) > 0)
     234  {
     235    if (!$simulate)
     236    {
     237      delete_categories($to_delete);
     238    }
     239    $counts['del_categories'] = count($to_delete);
     240  }
     241 
     242  echo get_elapsed_time($start, get_moment());
     243  echo ' for new method scanning directories<br />';
     244}
     245// +-----------------------------------------------------------------------+
     246// |                           files / elements                            |
     247// +-----------------------------------------------------------------------+
     248if (isset($_POST['submit']) and $_POST['sync'] == 'files')
     249
     250  $start_files = get_moment();
     251  $start= $start_files;
     252
     253  $fs = get_fs($basedir);
     254 
     255  echo get_elapsed_time($start, get_moment());
     256  echo ' for get_fs<br />';
     257 
     258  $cat_ids = array_diff(array_keys($db_categories), $to_delete);
     259
     260  $db_elements = array();
     261  $db_unvalidated = array();
     262 
     263  if (count($cat_ids) > 0)
     264  {
    68265    $query = '
    69 SELECT id,dir
    70   FROM '.CATEGORIES_TABLE.'
    71   WHERE id IN ('.$parent['uppercats'].')
     266SELECT id, path
     267  FROM '.IMAGES_TABLE.'
     268  WHERE storage_category_id IN (
     269'.wordwrap(implode(', ', $cat_ids), 80, "\n").')
    72270;';
    73271    $result = pwg_query($query);
    74272    while ($row = mysql_fetch_array($result))
    75273    {
    76       $database_dirs[$row['id']] = $row['dir'];
    77     }
    78     foreach ($upper_array as $id)
    79     {
    80       $local_dir.= $database_dirs[$id].'/';
    81     }
    82 
    83     $cat_directory.= '/'.$local_dir;
    84 
    85     // 1. display the category name to update
    86     $output = '<ul class="menu">';
    87     $output.= '<li><strong>'.$parent['name'].'</strong>';
    88     $output.= ' [ '.$parent['dir'].' ]';
    89     $output.= '</li>';
    90 
    91     // 2. we search pictures of the category only if the update is for all
    92     //    or a cat_id is specified
    93     if ($_POST['sync'] == 'files')
    94     {
    95       $output.= insert_local_element($cat_directory, $id_uppercat);
    96     }
    97   }
    98 
    99   $fs_subdirs = get_category_directories($cat_directory);
    100 
    101   $sub_category_dirs = array();
    102   $query = '
    103 SELECT id,dir
    104   FROM '.CATEGORIES_TABLE.'
    105   WHERE site_id = 1
    106 ';
    107   if (!is_numeric($id_uppercat))
    108   {
    109     $query.= ' AND id_uppercat IS NULL';
    110   }
    111   else
    112   {
    113     $query.= ' AND id_uppercat = '.$id_uppercat;
    114   }
    115   $query.= '
    116     AND dir IS NOT NULL'; // virtual categories not taken
    117   $query.= '
    118 ;';
    119   $result = pwg_query($query);
    120   while ($row = mysql_fetch_array($result))
    121   {
    122     $sub_category_dirs[$row['id']] = $row['dir'];
    123   }
    124  
    125   // 3. we have to remove the categories of the database not present anymore
    126   $to_delete_categories = array();
    127   foreach ($sub_category_dirs as $id => $dir)
    128   {
    129     if (!in_array($dir, $fs_subdirs))
    130     {
    131       array_push($to_delete_categories,$id);
    132     }
    133   }
    134   if (count($to_delete_categories) > 0)
    135   {
    136     delete_categories($to_delete_categories);
    137   }
    138 
    139   // array of new categories to insert
    140   $inserts = array();
    141 
    142   // calculate default value at category creation
    143   $create_values = array();
    144   if (isset($parent))
    145   {
    146     // at creation, must a category be visible or not ? Warning : if
    147     // the parent category is invisible, the category is automatically
    148     // create invisible. (invisible = locked)
    149     if ('false' == $parent['visible'])
    150     {
    151       $create_values{'visible'} = 'false';
    152     }
    153     else
    154     {
    155       $create_values{'visible'} = $conf['newcat_default_visible'];
    156     }
    157     // at creation, must a category be public or private ? Warning :
    158     // if the parent category is private, the category is
    159     // automatically create private.
    160     if ('private' == $parent['status'])
    161     {
    162       $create_values{'status'} = 'private';
    163     }
    164     else
    165     {
    166       $create_values{'status'} = $conf['newcat_default_status'];
    167     }
    168   }
    169   else
    170   {
    171     $create_values{'visible'} = $conf['newcat_default_visible'];
    172     $create_values{'status'} = $conf['newcat_default_status'];
    173   }
    174  
    175   foreach ($fs_subdirs as $fs_subdir)
    176   {
    177     // 5. Is the category already existing ? we create a subcat if not
    178     //    existing
    179     $category_id = array_search($fs_subdir, $sub_category_dirs);
    180     if (!is_numeric($category_id))
    181     {
    182       $insert = array();
    183      
    184       if (preg_match('/^[a-zA-Z0-9-_.]+$/', $fs_subdir))
    185       {
    186         $name = str_replace('_', ' ', $fs_subdir);
    187 
    188         $insert{'dir'} = $fs_subdir;
    189         $insert{'name'} = $name;
    190         $insert{'site_id'} = 1;
    191         $insert{'uppercats'} = 'undef';
    192         $insert{'commentable'} = $conf['newcat_default_commentable'];
    193         $insert{'uploadable'} = $conf['newcat_default_uploadable'];
    194         $insert{'status'} = $create_values{'status'};
    195         $insert{'visible'} = $create_values{'visible'};
    196 
    197         if (isset($parent))
    198         {
    199           $insert{'id_uppercat'} = $parent['id'];
    200         }
    201        
    202         array_push($inserts, $insert);
    203       }
    204       else
    205       {
    206         $output.= '<span class="update_category_error">"'.$fs_subdir.'" : ';
    207         $output.= $lang['update_wrong_dirname'].'</span><br />';
    208       }
    209     }
    210   }
    211 
    212   // we have to create the category
    213   if (count($inserts) > 0)
    214   {
    215     $dbfields = array(
    216       'dir','name','site_id','id_uppercat','uppercats','commentable',
    217       'uploadable','visible','status'
    218       );
    219     mass_inserts(CATEGORIES_TABLE, $dbfields, $inserts);
    220    
    221     $counts['new_categories']+= count($inserts);
    222     // updating uppercats field
     274      $db_elements[$row['id']] = $row['path'];
     275    }
     276
     277    // searching the unvalidated waiting elements (they must not be taken into
     278    // account)
    223279    $query = '
    224 UPDATE '.CATEGORIES_TABLE;
    225     if (isset($parent))
    226     {
    227       $query.= "
    228   SET uppercats = CONCAT('".$parent['uppercats']."',',',id)
    229   WHERE id_uppercat = ".$parent['id'];
    230     }
    231     else
    232     {
    233       $query.= '
    234   SET uppercats = id
    235   WHERE id_uppercat IS NULL';
    236     }
    237     $query.= '
    238 ;';
    239     pwg_query($query);
    240   }
    241 
    242   // Recursive call on the sub-categories (not virtual ones)
    243   if (!isset($_POST['cat'])
    244       or (isset($_POST['subcats-included'])
    245           and $_POST['subcats-included'] == 1))
    246   {
    247     $query = '
    248 SELECT id
    249   FROM '.CATEGORIES_TABLE.'
    250   WHERE site_id = 1
    251 ';
    252     if (!is_numeric($id_uppercat))
    253     {
    254       $query.= '    AND id_uppercat IS NULL';
    255     }
    256     else
    257     {
    258       $query.= '    AND id_uppercat = '.$id_uppercat;
    259     }
    260     $query.= '
    261     AND dir IS NOT NULL'; // virtual categories not taken
    262     $query.= '
     280SELECT file,storage_category_id
     281  FROM '.WAITING_TABLE.'
     282  WHERE storage_category_id IN (
     283'.wordwrap(implode(', ', $cat_ids), 80, "\n").')
     284    AND validated = \'false\'
    263285;';
    264286    $result = pwg_query($query);
    265287    while ($row = mysql_fetch_array($result))
    266288    {
    267       $output.= insert_local_category($row['id']);
    268     }
    269   }
    270 
    271   if (is_numeric($id_uppercat))
    272   {
    273     $output.= '</ul>';
    274   }
    275   return $output;
    276 }
    277 
    278 function insert_local_element($dir, $category_id)
    279 {
    280   global $lang,$conf,$counts;
    281 
    282   $output = '';
    283 
    284   // fs means FileSystem : $fs_files contains files in the filesystem found
    285   // in $dir that can be managed by PhpWebGallery (see get_pwg_files
    286   // function), $fs_thumbnails contains thumbnails, $fs_representatives
    287   // contains potentially representative pictures for non picture files
    288   $fs_files = get_pwg_files($dir);
    289   $fs_thumbnails = get_thumb_files($dir);
    290   $fs_representatives = get_representative_files($dir);
    291 
    292   // element deletion
    293   $to_delete_elements = array();
    294   // deletion of element if the correspond file doesn't exist anymore
     289      array_push(
     290        $db_unvalidated,
     291        array_search($row['storage_category_id'],
     292                     $db_fulldirs).'/'.$row['file']
     293        );
     294    }
     295  }
     296
     297  // next element id available
    295298  $query = '
    296 SELECT id,file
     299SELECT MAX(id)+1 AS next_element_id
    297300  FROM '.IMAGES_TABLE.'
    298   WHERE storage_category_id = '.$category_id.'
    299 ;';
    300   $result = pwg_query($query);
    301   while ($row = mysql_fetch_array($result))
    302   {
    303     if (!in_array($row['file'], $fs_files))
    304     {
    305       $output.= $row['file'];
    306       $output.= ' <span style="font-weight:bold;">';
    307       $output.= $lang['update_disappeared'].'</span><br />';
    308       array_push($to_delete_elements, $row['id']);
    309     }
    310   }
    311   // in picture case, we also delete the element if the thumbnail doesn't
    312   // existe anymore
    313   $query = '
    314 SELECT id,file,tn_ext
    315   FROM '.IMAGES_TABLE.'
    316   WHERE storage_category_id = '.$category_id.'
    317     AND ('.implode(' OR ',
    318                    array_map(
    319                      create_function('$s', 'return "file LIKE \'%".$s."\'";')
    320                      , $conf['picture_ext'])).')
    321 ;';
    322   $result = pwg_query($query);
    323   while ($row = mysql_fetch_array($result))
    324   {
    325     $thumbnail = $conf['prefix_thumbnail'];
    326     $thumbnail.= get_filename_wo_extension($row['file']);
    327     $thumbnail.= '.'.$row['tn_ext'];
    328     if (!in_array($thumbnail, $fs_thumbnails))
    329     {
    330       $output.= $row['file'];
    331       $output.= ' : <span style="font-weight:bold;">';
    332       $output.= $lang['update_disappeared_tn'].'</span><br />';
    333       array_push($to_delete_elements, $row['id']);
    334     }
    335   }
    336 
    337   $to_delete_elements = array_unique($to_delete_elements);
    338   if (count($to_delete_elements) > 0)
    339   {
    340     delete_elements($to_delete_elements);
    341   }
    342  
    343   $registered_elements = array();
    344   $query = '
    345 SELECT file FROM '.IMAGES_TABLE.'
    346    WHERE storage_category_id = '.$category_id.'
    347 ;';
    348   $result = pwg_query($query);
    349   while ($row = mysql_fetch_array($result))
    350   {
    351     array_push($registered_elements, $row['file']);
    352   }
    353 
    354   // unvalidated pictures are picture uploaded by users, but not validated
    355   // by an admin (so not registered truly visible yet)
    356   $unvalidated_pictures  = array();
    357  
    358   $query = '
    359 SELECT file
    360   FROM '.WAITING_TABLE.'
    361   WHERE storage_category_id = '.$category_id.'
    362     AND validated = \'false\'
    363 ;';
    364   $result = pwg_query($query);
    365   while ($row = mysql_fetch_array($result))
    366   {
    367     array_push($unvalidated_pictures, $row['file']);
    368   }
    369 
    370   // we only search among the picture present in the filesystem and not
    371   // present in the database yet. If we know that this picture is known as
    372   // an uploaded one but not validated, it's not tested neither
    373   $unregistered_elements = array_diff($fs_files
    374                                       ,$registered_elements
    375                                       ,$unvalidated_pictures);
    376 
     301;';
     302  list($next_element_id) = mysql_fetch_array(pwg_query($query));
     303
     304  $start = get_moment();
     305
     306  // because isset is one hundred time faster than in_array
     307  $fs['thumbnails'] = array_flip($fs['thumbnails']);
     308  $fs['representatives'] = array_flip($fs['representatives']);
     309 
    377310  $inserts = array();
    378  
    379   foreach ($unregistered_elements as $unregistered_element)
    380   {
    381     if (preg_match('/^[a-zA-Z0-9-_.]+$/', $unregistered_element))
    382     {
    383       $file_wo_ext = get_filename_wo_extension($unregistered_element);
    384       $tn_ext = '';
     311  $insert_links = array();
     312 
     313  foreach (array_diff($fs['elements'], $db_elements, $db_unvalidated) as $path)
     314  {
     315    $insert = array();
     316    // storage category must exist
     317    $dirname = dirname($path);
     318    if (!isset($db_fulldirs[$dirname]))
     319    {
     320      continue;
     321    }
     322    $filename = basename($path);
     323    if (!preg_match('/^[a-zA-Z0-9-_.]+$/', $filename))
     324    {
     325      array_push($errors, array('path' => $path, 'type' => 'PWG-UPDATE-1'));
     326      continue;
     327    }
     328
     329    // searching the thumbnail
     330    $filename_wo_ext = get_filename_wo_extension($filename);
     331    $tn_ext = '';
     332    $base_test = $dirname.'/thumbnail/';
     333    $base_test.= $conf['prefix_thumbnail'].$filename_wo_ext.'.';
     334    foreach ($conf['picture_ext'] as $ext)
     335    {
     336      $test = $base_test.$ext;
     337      if (isset($fs['thumbnails'][$test]))
     338      {
     339        $tn_ext = $ext;
     340        break;
     341      }
     342      else
     343      {
     344        continue;
     345      }
     346    }
     347
     348    // 2 cases : the element is a picture or not. Indeed, for a picture
     349    // thumbnail is mandatory and for non picture element, thumbnail and
     350    // representative are optionnal
     351    if (in_array(get_extension($filename), $conf['picture_ext']))
     352    {
     353      // if we found a thumnbnail corresponding to our picture...
     354      if ($tn_ext != '')
     355      {
     356        $insert{'id'} = $next_element_id++;
     357        $insert{'file'} = $filename;
     358        $insert{'storage_category_id'} = $db_fulldirs[$dirname];
     359        $insert{'date_available'} = CURRENT_DATE;
     360        $insert{'tn_ext'} = $tn_ext;
     361        $insert{'path'} = $path;
     362
     363        array_push($inserts, $insert);
     364        array_push($insert_links,
     365                   array('image_id' => $insert{'id'},
     366                         'category_id' => $insert{'storage_category_id'}));
     367        array_push($infos, array('path' => $insert{'path'},
     368                                 'info' => $lang['update_research_added']));
     369      }
     370      else
     371      {
     372        array_push($errors, array('path' => $path, 'type' => 'PWG-UPDATE-2'));
     373      }
     374    }
     375    else
     376    {
     377      // searching a representative
     378      $representative_ext = '';
     379      $base_test = $dirname.'/pwg_representative/'.$filename_wo_ext.'.';
    385380      foreach ($conf['picture_ext'] as $ext)
    386381      {
    387         $test = $conf['prefix_thumbnail'].$file_wo_ext.'.'.$ext;
    388         if (!in_array($test, $fs_thumbnails))
     382        $test = $base_test.$ext;
     383        if (isset($fs['representatives'][$test]))
     384        {
     385          $representative_ext = $ext;
     386          break;
     387        }
     388        else
    389389        {
    390390          continue;
    391391        }
    392         else
    393         {
    394           $tn_ext = $ext;
    395           break;
    396         }
    397       }
    398 
    399       // 2 cases : the element is a picture or not. Indeed, for a picture
    400       // thumbnail is mandatory and for non picture element, thumbnail and
    401       // representative is optionnal
    402       if (in_array(get_extension($unregistered_element), $conf['picture_ext']))
    403       {
    404         // if we found a thumnbnail corresponding to our picture...
    405         if ($tn_ext != '')
    406         {
    407           $insert = array();
    408           $insert['file'] = $unregistered_element;
    409           $insert['storage_category_id'] = $category_id;
    410           $insert['date_available'] = CURRENT_DATE;
    411           $insert['tn_ext'] = $tn_ext;
    412           $insert['path'] = $dir.$unregistered_element;
    413 
    414           $counts['new_elements']++;
    415           array_push($inserts, $insert);
    416         }
    417         else
    418         {
    419           $output.= '<span class="update_error_element">';
    420           $output.= $lang['update_missing_tn'].' : '.$unregistered_element;
    421           $output.= ' (<span style="font-weight:bold;">';
    422           $output.= $conf['prefix_thumbnail'];
    423           $output.= get_filename_wo_extension($unregistered_element);
    424           $output.= '.XXX</span>';
    425           $output.= ', XXX = ';
    426           $output.= implode(', ', $conf['picture_ext']);
    427           $output.= ')</span><br />';
    428         }
    429       }
    430       else
    431       {
    432         $representative_ext = '';
    433         foreach ($conf['picture_ext'] as $ext)
    434         {
    435           $candidate = $file_wo_ext.'.'.$ext;
    436           if (!in_array($candidate, $fs_representatives))
    437           {
    438             continue;
    439           }
    440           else
    441           {
    442             $representative_ext = $ext;
    443             break;
    444           }
    445         }
    446 
    447         $insert = array();
    448         $insert['file'] = $unregistered_element;
    449         $insert['path'] = $dir.$unregistered_element;
    450         $insert['storage_category_id'] = $category_id;
    451         $insert['date_available'] = CURRENT_DATE;
    452         if ( $tn_ext != '' )
    453         {
    454           $insert['tn_ext'] = $tn_ext;
    455         }
    456         if ( $representative_ext != '' )
    457         {
    458           $insert['representative_ext'] = $representative_ext;
    459         }
    460 
    461         $counts['new_elements']++;
    462         array_push($inserts, $insert);
    463       }
    464     }
    465     else
    466     {
    467       $output.= '<span class="update_error_element">"';
    468       $output.= $unregistered_element.'" : ';
    469       $output.= $lang['update_wrong_dirname'].'</span><br />';
    470     }
    471   }
    472  
     392      }
     393
     394      $insert{'id'} = $next_element_id++;
     395      $insert{'file'} = $filename;
     396      $insert{'storage_category_id'} = $db_fulldirs[$dirname];
     397      $insert{'date_available'} = CURRENT_DATE;
     398      $insert{'path'} = $path;
     399       
     400      if ($tn_ext != '')
     401      {
     402        $insert{'tn_ext'} = $tn_ext;
     403      }
     404      if ($representative_ext != '')
     405      {
     406        $insert{'representative_ext'} = $representative_ext;
     407      }
     408     
     409      array_push($inserts, $insert);
     410      array_push($insert_links,
     411                 array('image_id' => $insert{'id'},
     412                       'category_id' => $insert{'storage_category_id'}));
     413      array_push($infos, array('path' => $insert{'path'},
     414                               'info' => $lang['update_research_added']));
     415    }
     416  }
     417
    473418  if (count($inserts) > 0)
    474419  {
    475     // inserts all found pictures
    476     $dbfields = array(
    477       'file','storage_category_id','date_available','tn_ext'
    478       ,'representative_ext','path'
    479       );
    480     mass_inserts(IMAGES_TABLE, $dbfields, $inserts);
    481 
    482     // what are the ids of the pictures in the $category_id ?
    483     $ids = array();
    484 
    485     $query = '
    486 SELECT id
    487   FROM '.IMAGES_TABLE.'
    488   WHERE storage_category_id = '.$category_id.'
    489 ;';
    490     $result = pwg_query($query);
    491     while ($row = mysql_fetch_array($result))
    492     {
    493       array_push($ids, $row['id']);
    494     }
    495 
    496     // recreation of the links between this storage category pictures and
    497     // its storage category
    498     $query = '
    499 DELETE FROM '.IMAGE_CATEGORY_TABLE.'
    500   WHERE category_id = '.$category_id.'
    501     AND image_id IN ('.implode(',', $ids).')
    502 ;';
    503     pwg_query($query);
    504 
    505     foreach ($ids as $num => $image_id)
    506     {
    507       $ids[$num] =  '('.$category_id.','.$image_id.')';
    508     }
    509     $query = '
    510 INSERT INTO '.IMAGE_CATEGORY_TABLE.'
    511   (category_id,image_id) VALUES
    512   '.implode(',', $ids).'
    513 ;';
    514     pwg_query($query);
    515 
    516     set_random_representant(array($category_id));
    517   }
    518   return $output;
     420    if (!$simulate)
     421    {
     422      // inserts all new elements
     423      $dbfields = array(
     424        'id','file','storage_category_id','date_available','tn_ext'
     425        ,'representative_ext','path'
     426        );
     427      mass_inserts(IMAGES_TABLE, $dbfields, $inserts);
     428
     429      // insert all links between new elements and their storage category
     430      $dbfields = array('image_id','category_id');
     431      mass_inserts(IMAGE_CATEGORY_TABLE, $dbfields, $insert_links);
     432    }
     433    $counts['new_elements'] = count($inserts);
     434  }
     435
     436  // delete elements that are in database but not in the filesystem
     437  $to_delete_elements = array();
     438  foreach (array_diff($db_elements, $fs['elements']) as $path)
     439  {
     440    array_push($to_delete_elements, array_search($path, $db_elements));
     441    array_push($infos, array('path' => $path,
     442                             'info' => $lang['update_research_deleted']));
     443  }
     444  if (count($to_delete_elements) > 0)
     445  {
     446    if (!$simulate)
     447    {
     448      delete_elements($to_delete_elements);
     449    }
     450    $counts['del_elements'] = count($to_delete_elements);
     451  }
     452 
     453  echo get_elapsed_time($start_files, get_moment());
     454  echo ' for new method scanning files<br />';
    519455}
    520456// +-----------------------------------------------------------------------+
     
    523459$template->set_filenames(array('update'=>'admin/update.tpl'));
    524460
    525 $base_url = PHPWG_ROOT_PATH.'admin.php?page=update';
     461$result_title = '';
     462if (isset($simulate) and $simulate)
     463{
     464  $result_title.= $lang['update_simulation_title'].' ';
     465}
     466$result_title.= $lang['update_part_research'];
    526467
    527468$template->assign_vars(
     
    536477    'L_UPDATE_SYNC_METADATA_ALL'=>$lang['update_sync_metadata_all'],
    537478    'L_UPDATE_CATS_SUBSET'=>$lang['update_cats_subset'],
    538     'L_RESULT_UPDATE'=>$lang['update_part_research'],
     479    'L_RESULT_UPDATE'=>$result_title,
    539480    'L_NB_NEW_ELEMENTS'=>$lang['update_nb_new_elements'],
    540481    'L_NB_NEW_CATEGORIES'=>$lang['update_nb_new_categories'],
    541482    'L_NB_DEL_ELEMENTS'=>$lang['update_nb_del_elements'],
    542483    'L_NB_DEL_CATEGORIES'=>$lang['update_nb_del_categories'],
     484    'L_UPDATE_NB_ERRORS'=>$lang['update_nb_errors'],
    543485    'L_SEARCH_SUBCATS_INCLUDED'=>$lang['search_subcats_included'],
    544    
    545     'U_SYNC_DIRS'=>add_session_id($base_url.'&amp;update=dirs'),
    546     'U_SYNC_ALL'=>add_session_id($base_url.'&amp;update=all'),
    547     'U_SYNC_METADATA_NEW'=>add_session_id($base_url.'&amp;metadata=all:new'),
    548     'U_SYNC_METADATA_ALL'=>add_session_id($base_url.'&amp;metadata=all')
     486    'L_UPDATE_WRONG_DIRNAME_INFO'=>$lang['update_wrong_dirname_info'],
     487    'L_UPDATE_MISSING_TN_INFO'=>$lang['update_missing_tn_info'],
     488    'PICTURE_EXT_LIST'=>implode(',', $conf['picture_ext']),
     489    'L_UPDATE_ERROR_LIST_TITLE'=>$lang['update_error_list_title'],
     490    'L_UPDATE_ERRORS_CAPTION'=>$lang['update_errors_caption'],
     491    'L_UPDATE_DISPLAY_INFO'=>$lang['update_display_info'],
     492    'L_UPDATE_SIMULATE'=>$lang['update_simulate'],
     493    'L_UPDATE_INFOS_TITLE'=>$lang['update_infos_title']
    549494    ));
    550495// +-----------------------------------------------------------------------+
     
    571516         and ($_POST['sync'] == 'dirs' or $_POST['sync'] == 'files'))
    572517{
    573   $counts = array(
    574     'new_elements' => 0,
    575     'new_categories' => 0,
    576     'del_elements' => 0,
    577     'del_categories' => 0
    578     );
    579 
    580   if (isset($_POST['cat']))
    581   {
    582     $opts['category_id'] = $_POST['cat'];
    583   }
    584   else
    585   {
    586     $opts['category_id'] = 'NULL';
    587   }
    588  
    589   $start = get_moment();
    590   $categories = insert_local_category($opts['category_id']);
    591   echo get_elapsed_time($start,get_moment()).' for scanning directories<br />';
    592  
    593518  $template->assign_block_vars(
    594519    'update',
    595520    array(
    596       'CATEGORIES'=>$categories,
    597521      'NB_NEW_CATEGORIES'=>$counts['new_categories'],
    598522      'NB_DEL_CATEGORIES'=>$counts['del_categories'],
    599523      'NB_NEW_ELEMENTS'=>$counts['new_elements'],
    600       'NB_DEL_ELEMENTS'=>$counts['del_elements']
     524      'NB_DEL_ELEMENTS'=>$counts['del_elements'],
     525      'NB_ERRORS'=>count($errors),
    601526      ));
    602   $start = get_moment();
    603   update_category('all');
    604   echo get_elapsed_time($start,get_moment()).' for update_category(all)<br />';
    605   $start = get_moment();
    606   ordering();
    607   update_global_rank();
    608   echo get_elapsed_time($start, get_moment()).' for ordering categories<br />';
     527 
     528  if (count($errors) > 0)
     529  {
     530    $template->assign_block_vars('update.errors', array());
     531    foreach ($errors as $error)
     532    {
     533      $template->assign_block_vars(
     534        'update.errors.error',
     535        array(
     536          'ELEMENT' => $error['path'],
     537          'LABEL' => $error['type'].' ('.$error_labels[$error['type']].')'
     538          ));
     539    }
     540  }
     541  if (count($infos) > 0
     542      and isset($_POST['display_info'])
     543      and $_POST['display_info'] == 1)
     544  {
     545    $template->assign_block_vars('update.infos', array());
     546    foreach ($infos as $info)
     547    {
     548      $template->assign_block_vars(
     549        'update.infos.info',
     550        array(
     551          'ELEMENT' => $info['path'],
     552          'LABEL' => $info['info']
     553          ));
     554    }
     555  }
     556
     557  if (!$simulate)
     558  {
     559    $start = get_moment();
     560    update_category('all');
     561    echo get_elapsed_time($start,get_moment());
     562    echo ' for update_category(all)<br />';
     563    $start = get_moment();
     564    ordering();
     565    update_global_rank();
     566    echo get_elapsed_time($start, get_moment());
     567    echo ' for ordering categories<br />';
     568  }
    609569}
    610570// +-----------------------------------------------------------------------+
Note: See TracChangeset for help on using the changeset viewer.