From 185b2c2596620be193879eb1b425727274dba0b5 Mon Sep 17 00:00:00 2001 From: z0rglub Date: Sat, 10 Jan 2004 23:17:24 +0000 Subject: improvement of insert_local_category function : less SQL queries, only one short access to filesystem. git-svn-id: http://piwigo.org/svn/branches/release-1_3@269 68402e56-0260-453c-a942-63ccdbb3a9ee --- admin/update.php | 170 ++++++++++++++++++++++++++++--------------------------- 1 file changed, 86 insertions(+), 84 deletions(-) diff --git a/admin/update.php b/admin/update.php index 78cccf192..aaf0a212c 100644 --- a/admin/update.php +++ b/admin/update.php @@ -19,126 +19,125 @@ include_once( './include/isadmin.inc.php' ); //------------------------------------------------------------------- functions -function insert_local_category( $cat_id ) +function insert_local_category( $id_uppercat ) { global $conf, $page, $user, $lang; - + $uppercats = ''; // 0. retrieving informations on the category to display $cat_directory = '../galleries'; - if ( is_numeric( $cat_id ) ) + if ( is_numeric( $id_uppercat ) ) { - $cat_directory.= '/'.get_local_dir( $cat_id ); - $result = get_cat_info( $cat_id ); - $uppercats = $result['uppercats']; + $query = 'SELECT name,uppercats,dir'; + $query.= ' FROM '.PREFIX_TABLE.'categories'; + $query.= ' WHERE id = '.$id_uppercat; + $query.= ';'; + $row = mysql_fetch_array( mysql_query( $query ) ); + $uppercats = $row['uppercats']; + $name = $row['name']; + $dir = $row['dir']; + + $upper_array = explode( ',', $uppercats ); + + $local_dir = ''; + + $database_dirs = array(); + $query = 'SELECT id,dir'; + $query.= ' FROM '.PREFIX_TABLE.'categories'; + $query.= ' WHERE id IN ('.$uppercats.')'; + $query.= ';'; + $result = mysql_query( $query ); + while( $row = mysql_fetch_array( $result ) ) + { + $database_dirs[$row['id']] = $row['dir']; + } + foreach ( $upper_array as $id ) { + $local_dir.= $database_dirs[$id].'/'; + } + + $cat_directory.= '/'.$local_dir; + // 1. display the category name to update $src = '../template/'.$user['template'].'/admin/images/puce.gif'; $output = '>'; - $output.= ''; - $output.= $result['name'][count($result['name'])-1]; - $output.= ''; - $output.= ' [ '.$result['dir'].' ]'; + $output.= ''.$name.''; + $output.= ' [ '.$dir.' ]'; $output.= '
'; // 2. we search pictures of the category only if the update is for all // or a cat_id is specified if ( isset( $page['cat'] ) or $_GET['update'] == 'all' ) { - $output.= insert_local_image( $cat_directory, $cat_id ); + $output.= insert_local_image( $cat_directory, $id_uppercat ); } } - // 3. we have to remove the categories of the database not present anymore - $query = 'SELECT id'; + $sub_dirs = get_category_directories( $cat_directory ); + + $sub_category_dirs = array(); + $query = 'SELECT id,dir'; $query.= ' FROM '.PREFIX_TABLE.'categories'; $query.= ' WHERE site_id = 1'; - if ( !is_numeric( $cat_id ) ) $query.= ' AND id_uppercat IS NULL'; - else $query.= ' AND id_uppercat = '.$cat_id; + if (!is_numeric($id_uppercat)) $query.= ' AND id_uppercat IS NULL'; + else $query.= ' AND id_uppercat = '.$id_uppercat; + $query.= ' AND dir IS NOT NULL'; // virtual categories not taken $query.= ';'; $result = mysql_query( $query ); while ( $row = mysql_fetch_array( $result ) ) { - // retrieving the directory - $rep = '../galleries/'.get_local_dir( $row['id'] ); - // is the directory present ? - if ( !is_dir( $rep ) ) delete_category( $row['id'] ); + $id = intval($row['id']); + $sub_category_dirs[$id] = $row['dir']; } - // 4. retrieving the sub-directories - $subdirs = array(); - $dirs = ''; - if ( $opendir = opendir( $cat_directory ) ) - { - while ( $file = readdir( $opendir ) ) - { - if ( $file != '.' - and $file != '..' - and is_dir( $cat_directory.'/'.$file ) - and $file != 'thumbnail' ) - { - if ( preg_match( '/^[a-zA-Z0-9-_.]+$/', $file ) ) - array_push( $subdirs, $file ); - else - { - $output.= '"'.$file.'" : '; - $output.= $lang['update_wrong_dirname'].'
'; - // if the category even exists (from a previous release of - // PhpWebGallery), we keep it in our $subdirs array - $query = 'SELECT id'; - $query.= ' FROM '.PREFIX_TABLE.'categories'; - $query.= ' WHERE site_id = 1'; - $query.= " AND dir = '".$file."'"; - $query.= ' AND id_uppercat'; - if ( !is_numeric( $cat_id ) ) $query.= ' IS NULL'; - else $query.= ' = '.$cat_id; - $query.= ';'; - $result = mysql_query( $query ); - if ( mysql_num_rows( $result ) != 0 ) - { - array_push( $subdirs, $file ); - } - } - } - } + + // 3. we have to remove the categories of the database not present anymore + foreach ( $sub_category_dirs as $id => $dir ) { + if ( !in_array( $dir, $sub_dirs ) ) delete_category( $id ); } - foreach ( $subdirs as $subdir ) { + + foreach ( $sub_dirs as $sub_dir ) { // 5. Is the category already existing ? we create a subcat if not // existing - $category_id = ''; - $query = 'SELECT id'; - $query.= ' FROM '.PREFIX_TABLE.'categories'; - $query.= ' WHERE site_id = 1'; - $query.= " AND dir = '".$subdir."'"; - $query.= ' AND id_uppercat'; - if ( !is_numeric( $cat_id ) ) $query.= ' IS NULL'; - else $query.= ' = '.$cat_id; - $query.= ';'; - $result = mysql_query( $query ); - if ( mysql_num_rows( $result ) == 0 ) + $category_id = array_search( $sub_dir, $sub_category_dirs ); + if ( !is_numeric( $category_id ) ) { - $name = str_replace( '_', ' ', $subdir ); - // we have to create the category - $query = 'INSERT INTO '.PREFIX_TABLE.'categories'; - $query.= ' (dir,name,site_id,id_uppercat) VALUES'; - $query.= " ('".$subdir."','".$name."',1"; - if ( !is_numeric( $cat_id ) ) $query.= ',NULL'; - else $query.= ",'".$cat_id."'"; - $query.= ');'; - mysql_query( $query ); - $category_id = mysql_insert_id(); + if ( preg_match( '/^[a-zA-Z0-9-_.]+$/', $sub_dir ) ) + { + $name = str_replace( '_', ' ', $sub_dir ); + // we have to create the category + $query = 'INSERT INTO '.PREFIX_TABLE.'categories'; + $query.= ' (dir,name,site_id,id_uppercat,uppercats) VALUES'; + $query.= " ('".$sub_dir."','".$name."',1"; + if ( !is_numeric( $id_uppercat ) ) $query.= ',NULL'; + else $query.= ','.$id_uppercat; + $query.= ",'undef'"; + $query.= ');'; + mysql_query( $query ); + $category_id = mysql_insert_id(); + // updating uppercats field + $query = 'UPDATE '.PREFIX_TABLE.'categories'; + $query.= " SET uppercats = '".$uppercats; + if ( $uppercats != '' ) $query.= ','; + $query.= $category_id; + $query.= "'"; + $query.= ';'; + mysql_query( $query ); + } + else + { + $output.= '"'.$sub_dir.'" : '; + $output.= $lang['update_wrong_dirname'].'
'; + } } - else + // 6. recursive call + if ( is_numeric( $category_id ) ) { - // we get the already registered id - $row = mysql_fetch_array( $result ); - $category_id = $row['id']; + $output.= insert_local_category( $category_id ); } - // 6. recursive call - $output.= insert_local_category( $category_id ); } - if ( is_numeric( $cat_id ) ) + if ( is_numeric( $id_uppercat ) ) { $output.= '
'; } @@ -584,6 +583,7 @@ if ( !isset( $_GET['update'] ) //------------------------------------------------- local update : ../galleries else { + $start = get_moment(); $count_new = 0; $count_deleted = 0; $vtp->addSession( $sub, 'local_update' ); @@ -595,6 +595,8 @@ else { $categories = insert_local_category( 'NULL' ); } + $end = get_moment(); + echo get_elapsed_time( $start, $end ).' for update
'; $vtp->setVar( $sub, 'local_update.categories', $categories ); $vtp->setVar( $sub, 'local_update.count_new', $count_new ); $vtp->setVar( $sub, 'local_update.count_deleted', $count_deleted ); -- cgit v1.2.3