From 7254f665493ff99059b26daac04f6823626733fc Mon Sep 17 00:00:00 2001 From: z0rglub Date: Sun, 11 Jan 2004 00:05:10 +0000 Subject: improved again function insert_local_category : only one multiple insert query, one update for uppercats field git-svn-id: http://piwigo.org/svn/branches/release-1_3@272 68402e56-0260-453c-a942-63ccdbb3a9ee --- admin/update.php | 70 +++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 46 insertions(+), 24 deletions(-) diff --git a/admin/update.php b/admin/update.php index aaf0a212c..b4f46b958 100644 --- a/admin/update.php +++ b/admin/update.php @@ -96,6 +96,9 @@ function insert_local_category( $id_uppercat ) if ( !in_array( $dir, $sub_dirs ) ) delete_category( $id ); } + // array of new categories to insert + $inserts = array(); + foreach ( $sub_dirs as $sub_dir ) { // 5. Is the category already existing ? we create a subcat if not // existing @@ -104,25 +107,15 @@ function insert_local_category( $id_uppercat ) { if ( preg_match( '/^[a-zA-Z0-9-_.]+$/', $sub_dir ) ) { + $value = ''; $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 ); + + $value.= "('".$sub_dir."','".$name."',1"; + if ( !is_numeric( $id_uppercat ) ) $value.= ',NULL'; + else $value.= ','.$id_uppercat; + $value.= ",'undef'"; + $value.= ')'; + array_push( $inserts, $value ); } else { @@ -130,13 +123,42 @@ function insert_local_category( $id_uppercat ) $output.= $lang['update_wrong_dirname'].'
'; } } - // 6. recursive call - if ( is_numeric( $category_id ) ) - { - $output.= insert_local_category( $category_id ); - } } - + + // we have to create the category + if ( count( $inserts ) > 0 ) + { + $query = 'INSERT INTO '.PREFIX_TABLE.'categories'; + $query.= ' (dir,name,site_id,id_uppercat,uppercats) VALUES '; + $query.= implode( ',', $inserts ); + $query.= ';'; + mysql_query( $query ); + // updating uppercats field + $query = 'UPDATE '.PREFIX_TABLE.'categories'; + $query.= ' SET uppercats = '; + if ( $uppercats != '' ) $query.= "CONCAT('".$uppercats."',',',id)"; + else $query.= 'id'; + $query.= ' WHERE id_uppercat '; + if (!is_numeric($id_uppercat)) $query.= 'IS NULL'; + else $query.= '= '.$id_uppercat; + $query.= ';'; + mysql_query( $query ); + } + + // Recursive call on the sub-categories (not virtual ones) + $query = 'SELECT id'; + $query.= ' FROM '.PREFIX_TABLE.'categories'; + $query.= ' WHERE site_id = 1'; + 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 ) ) + { + $output.= insert_local_category( $row['id'] ); + } + if ( is_numeric( $id_uppercat ) ) { $output.= ''; -- cgit v1.2.3