Changeset 269


Ignore:
Timestamp:
Jan 11, 2004, 12:17:24 AM (20 years ago)
Author:
z0rglub
Message:

improvement of insert_local_category function : less SQL queries, only one
short access to filesystem.

File:
1 edited

Legend:

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

    r259 r269  
    2020include_once( './include/isadmin.inc.php' );
    2121//------------------------------------------------------------------- functions
    22 function insert_local_category( $cat_id )
     22function insert_local_category( $id_uppercat )
    2323{
    2424  global $conf, $page, $user, $lang;
    25                
     25 
    2626  $uppercats = '';
    2727               
     
    2929  $cat_directory = '../galleries';
    3030               
    31   if ( is_numeric( $cat_id ) )
    32   {
    33     $cat_directory.= '/'.get_local_dir( $cat_id );
    34     $result = get_cat_info( $cat_id );
    35     $uppercats = $result['uppercats'];
     31  if ( is_numeric( $id_uppercat ) )
     32  {
     33    $query = 'SELECT name,uppercats,dir';
     34    $query.= ' FROM '.PREFIX_TABLE.'categories';
     35    $query.= ' WHERE id = '.$id_uppercat;
     36    $query.= ';';
     37    $row = mysql_fetch_array( mysql_query( $query ) );
     38    $uppercats = $row['uppercats'];
     39    $name      = $row['name'];
     40    $dir       = $row['dir'];
     41
     42    $upper_array = explode( ',', $uppercats );
     43
     44    $local_dir = '';
     45
     46    $database_dirs = array();
     47    $query = 'SELECT id,dir';
     48    $query.= ' FROM '.PREFIX_TABLE.'categories';
     49    $query.= ' WHERE id IN ('.$uppercats.')';
     50    $query.= ';';
     51    $result = mysql_query( $query );
     52    while( $row = mysql_fetch_array( $result ) )
     53    {
     54      $database_dirs[$row['id']] = $row['dir'];
     55    }
     56    foreach ( $upper_array as $id ) {
     57      $local_dir.= $database_dirs[$id].'/';
     58    }
     59
     60    $cat_directory.= '/'.$local_dir;
     61
    3662    // 1. display the category name to update
    3763    $src = '../template/'.$user['template'].'/admin/images/puce.gif';
    3864    $output = '<img src="'.$src.'" alt="&gt;" />';
    39     $output.= '<span style="font-weight:bold;">';
    40     $output.= $result['name'][count($result['name'])-1];
    41     $output.= '</span>';
    42     $output.= ' [ '.$result['dir'].' ]';
     65    $output.= '<span style="font-weight:bold;">'.$name.'</span>';
     66    $output.= ' [ '.$dir.' ]';
    4367    $output.= '<div class="retrait">';
    4468
     
    4771    if ( isset( $page['cat'] ) or $_GET['update'] == 'all' )
    4872    {
    49       $output.= insert_local_image( $cat_directory, $cat_id );
    50     }
    51   }
    52 
    53   // 3. we have to remove the categories of the database not present anymore
    54   $query = 'SELECT id';
     73      $output.= insert_local_image( $cat_directory, $id_uppercat );
     74    }
     75  }
     76
     77  $sub_dirs = get_category_directories( $cat_directory );
     78
     79  $sub_category_dirs = array();
     80  $query = 'SELECT id,dir';
    5581  $query.= ' FROM '.PREFIX_TABLE.'categories';
    5682  $query.= ' WHERE site_id = 1';
    57   if ( !is_numeric( $cat_id ) ) $query.= ' AND id_uppercat IS NULL';
    58   else                          $query.= ' AND id_uppercat = '.$cat_id;
     83  if (!is_numeric($id_uppercat)) $query.= ' AND id_uppercat IS NULL';
     84  else                           $query.= ' AND id_uppercat = '.$id_uppercat;
     85  $query.= ' AND dir IS NOT NULL'; // virtual categories not taken
    5986  $query.= ';';
    6087  $result = mysql_query( $query );
    6188  while ( $row = mysql_fetch_array( $result ) )
    6289  {
    63     // retrieving the directory
    64     $rep = '../galleries/'.get_local_dir( $row['id'] );
    65     // is the directory present ?
    66     if ( !is_dir( $rep ) ) delete_category( $row['id'] );
    67   }
    68   // 4. retrieving the sub-directories
    69   $subdirs = array();
    70   $dirs = '';
    71   if ( $opendir = opendir( $cat_directory ) )
    72   {
    73     while ( $file = readdir( $opendir ) )
    74     {
    75       if ( $file != '.'
    76            and $file != '..'
    77            and is_dir( $cat_directory.'/'.$file )
    78            and $file != 'thumbnail' )
    79       {
    80         if ( preg_match( '/^[a-zA-Z0-9-_.]+$/', $file ) )
    81           array_push( $subdirs, $file );
    82         else
    83         {
    84           $output.= '<span style="color:red;">"'.$file.'" : ';
    85           $output.= $lang['update_wrong_dirname'].'</span><br />';
    86           // if the category even exists (from a previous release of
    87           // PhpWebGallery), we keep it in our $subdirs array
    88           $query = 'SELECT id';
    89           $query.= ' FROM '.PREFIX_TABLE.'categories';
    90           $query.= ' WHERE site_id = 1';
    91           $query.= " AND dir = '".$file."'";
    92           $query.= ' AND id_uppercat';
    93           if ( !is_numeric( $cat_id ) ) $query.= ' IS NULL';
    94           else                          $query.= ' = '.$cat_id;
    95           $query.= ';';
    96           $result = mysql_query( $query );
    97           if ( mysql_num_rows( $result ) != 0 )
    98           {
    99             array_push( $subdirs, $file );
    100           }
    101         }
    102       }
    103     }
    104   }
    105   foreach ( $subdirs as $subdir ) {
     90    $id = intval($row['id']);
     91    $sub_category_dirs[$id] = $row['dir'];
     92  }
     93 
     94  // 3. we have to remove the categories of the database not present anymore
     95  foreach ( $sub_category_dirs as $id => $dir ) {
     96    if ( !in_array( $dir, $sub_dirs ) ) delete_category( $id );
     97  }
     98
     99  foreach ( $sub_dirs as $sub_dir ) {
    106100    // 5. Is the category already existing ? we create a subcat if not
    107101    //    existing
    108     $category_id = '';
    109     $query = 'SELECT id';
    110     $query.= ' FROM '.PREFIX_TABLE.'categories';
    111     $query.= ' WHERE site_id = 1';
    112     $query.= " AND dir = '".$subdir."'";
    113     $query.= ' AND id_uppercat';
    114     if ( !is_numeric( $cat_id ) ) $query.= ' IS NULL';
    115     else                          $query.= ' = '.$cat_id;
    116     $query.= ';';
    117     $result = mysql_query( $query );
    118     if ( mysql_num_rows( $result ) == 0 )
    119     {
    120       $name = str_replace( '_', ' ', $subdir );
    121       // we have to create the category
    122       $query = 'INSERT INTO '.PREFIX_TABLE.'categories';
    123       $query.= ' (dir,name,site_id,id_uppercat) VALUES';
    124       $query.= " ('".$subdir."','".$name."',1";
    125       if ( !is_numeric( $cat_id ) ) $query.= ',NULL';
    126       else                          $query.= ",'".$cat_id."'";
    127       $query.= ');';
    128       mysql_query( $query );
    129       $category_id = mysql_insert_id();
    130     }
    131     else
    132     {
    133       // we get the already registered id
    134       $row = mysql_fetch_array( $result );
    135       $category_id = $row['id'];
     102    $category_id = array_search( $sub_dir, $sub_category_dirs );
     103    if ( !is_numeric( $category_id ) )
     104    {
     105      if ( preg_match( '/^[a-zA-Z0-9-_.]+$/', $sub_dir ) )
     106      {
     107        $name = str_replace( '_', ' ', $sub_dir );
     108        // we have to create the category
     109        $query = 'INSERT INTO '.PREFIX_TABLE.'categories';
     110        $query.= ' (dir,name,site_id,id_uppercat,uppercats) VALUES';
     111        $query.= " ('".$sub_dir."','".$name."',1";
     112        if ( !is_numeric( $id_uppercat ) ) $query.= ',NULL';
     113        else                               $query.= ','.$id_uppercat;
     114        $query.= ",'undef'";
     115        $query.= ');';
     116        mysql_query( $query );
     117        $category_id = mysql_insert_id();
     118        // updating uppercats field
     119        $query = 'UPDATE '.PREFIX_TABLE.'categories';
     120        $query.= " SET uppercats = '".$uppercats;
     121        if ( $uppercats != '' ) $query.= ',';
     122        $query.= $category_id;
     123        $query.= "'";
     124        $query.= ';';
     125        mysql_query( $query );
     126      }
     127      else
     128      {
     129        $output.= '<span style="color:red;">"'.$sub_dir.'" : ';
     130        $output.= $lang['update_wrong_dirname'].'</span><br />';
     131      }
    136132    }
    137133    // 6. recursive call
    138     $output.= insert_local_category( $category_id );
     134    if ( is_numeric( $category_id ) )
     135    {
     136      $output.= insert_local_category( $category_id );
     137    }
    139138  }
    140139               
    141   if ( is_numeric( $cat_id ) )
     140  if ( is_numeric( $id_uppercat ) )
    142141  {
    143142    $output.= '</div>';
     
    585584else
    586585{
     586  $start = get_moment();
    587587  $count_new = 0;
    588588  $count_deleted = 0;
     
    596596    $categories = insert_local_category( 'NULL' );
    597597  }
     598  $end = get_moment();
     599  echo get_elapsed_time( $start, $end ).' for update <br />';
    598600  $vtp->setVar( $sub, 'local_update.categories', $categories );
    599601  $vtp->setVar( $sub, 'local_update.count_new', $count_new );
Note: See TracChangeset for help on using the changeset viewer.