diff options
author | rvelices <rv-github@modusoptimus.com> | 2008-08-29 12:35:16 +0000 |
---|---|---|
committer | rvelices <rv-github@modusoptimus.com> | 2008-08-29 12:35:16 +0000 |
commit | bd833ee9bcd52d7e5c1cc52c220ead224beb1225 (patch) | |
tree | d7fb01c6cb0ce046f6ddaebf61a8c0b74bfb774c /admin | |
parent | 844799d0401321704c1ef390a1014118438fe7bd (diff) |
synchro improvements:
- able to sync metadata at the same time as the files/dirs
- by default empty metadata does not overwrite database infos (checkbox can switch to previous behaviour) (bug 132)
- the form is shown again even after a successfull non simulated run
git-svn-id: http://piwigo.org/svn/trunk@2491 68402e56-0260-453c-a942-63ccdbb3a9ee
Diffstat (limited to 'admin')
-rw-r--r-- | admin/include/functions.php | 215 | ||||
-rw-r--r-- | admin/site_update.php | 152 | ||||
-rw-r--r-- | admin/template/yoga/admin/site_update.tpl | 45 |
3 files changed, 188 insertions, 224 deletions
diff --git a/admin/include/functions.php b/admin/include/functions.php index db95ab383..e700d662e 100644 --- a/admin/include/functions.php +++ b/admin/include/functions.php @@ -377,7 +377,7 @@ function mass_inserts($table_name, $dbfields, $datas) { $first = true; - $query = 'SHOW VARIABLES LIKE \'max_allowed_packet\';'; + $query = 'SHOW VARIABLES LIKE \'max_allowed_packet\''; list(, $packet_size) = mysql_fetch_row(pwg_query($query)); $packet_size = $packet_size - 2000; // The last list of values MUST not exceed 2000 character*/ $query = ''; @@ -386,8 +386,6 @@ function mass_inserts($table_name, $dbfields, $datas) { if (strlen($query) >= $packet_size) { - $query .= ' -;'; pwg_query($query); $first = true; } @@ -425,57 +423,53 @@ INSERT INTO '.$table_name.' } $query .= ')'; } - - $query .= ' -;'; pwg_query($query); } } +define('MASS_UPDATES_SKIP_EMPTY', 1); /** * updates multiple lines in a table * * @param string table_name * @param array dbfields * @param array datas + * @param int flags - if MASS_UPDATES_SKIP_EMPTY - empty values do not overwrite existing ones * @return void */ -function mass_updates($tablename, $dbfields, $datas) +function mass_updates($tablename, $dbfields, $datas, $flags=0) { - if (count($datas) != 0) - { - // depending on the MySQL version, we use the multi table update or N - // update queries - if (count($datas) < 10 or version_compare(mysql_get_server_info(), '4.0.4') < 0) + if (count($datas) == 0) + return; + // depending on the MySQL version, we use the multi table update or N update queries + if (count($datas) < 10 or version_compare(mysql_get_server_info(), '4.0.4') < 0) + { // MySQL is prior to version 4.0.4, multi table update feature is not available + foreach ($datas as $data) { - // MySQL is prior to version 4.0.4, multi table update feature is not - // available - foreach ($datas as $data) - { - $query = ' + $query = ' UPDATE '.$tablename.' SET '; - $is_first = true; - foreach ($dbfields['update'] as $key) + $is_first = true; + foreach ($dbfields['update'] as $key) + { + $separator = $is_first ? '' : ",\n "; + + if (isset($data[$key]) and $data[$key] != '') { - if (!$is_first) - { - $query.= ",\n "; - } - $query.= $key.' = '; - if (isset($data[$key]) and $data[$key] != '') - { - $query.= '\''.$data[$key].'\''; - } - else - { - $query.= 'NULL'; - } - $is_first = false; + $query.= $separator.$key.' = \''.$data[$key].'\''; + } + else + { + if ($flags & MASS_UPDATES_SKIP_EMPTY ) + continue; // next field + $query.= "$separator$key = NULL"; } + $is_first = false; + } + if (!$is_first) + {// only if one field at least updated $query.= ' WHERE '; - $is_first = true; foreach ($dbfields['primary'] as $key) { @@ -493,87 +487,83 @@ UPDATE '.$tablename.' } $is_first = false; } - $query.= ' -;'; pwg_query($query); } - } - else + } // foreach update + } // if mysql_ver or count<X + else + { + // creation of the temporary table + $query = ' +SHOW FULL COLUMNS FROM '.$tablename; + $result = pwg_query($query); + $columns = array(); + $all_fields = array_merge($dbfields['primary'], $dbfields['update']); + while ($row = mysql_fetch_array($result)) { - // creation of the temporary table - $query = ' -SHOW FULL COLUMNS FROM '.$tablename.' -;'; - $result = pwg_query($query); - $columns = array(); - $all_fields = array_merge($dbfields['primary'], $dbfields['update']); - while ($row = mysql_fetch_array($result)) + if (in_array($row['Field'], $all_fields)) { - if (in_array($row['Field'], $all_fields)) - { - $column = $row['Field']; - $column.= ' '.$row['Type']; + $column = $row['Field']; + $column.= ' '.$row['Type']; - $nullable = true; - if (!isset($row['Null']) or $row['Null'] == '' or $row['Null']=='NO') - { - $column.= ' NOT NULL'; - $nullable = false; - } - if (isset($row['Default'])) - { - $column.= " default '".$row['Default']."'"; - } - elseif ($nullable) - { - $column.= " default NULL"; - } - if (isset($row['Collation']) and $row['Collation'] != 'NULL') - { - $column.= " collate '".$row['Collation']."'"; - } - array_push($columns, $column); + $nullable = true; + if (!isset($row['Null']) or $row['Null'] == '' or $row['Null']=='NO') + { + $column.= ' NOT NULL'; + $nullable = false; } + if (isset($row['Default'])) + { + $column.= " default '".$row['Default']."'"; + } + elseif ($nullable) + { + $column.= " default NULL"; + } + if (isset($row['Collation']) and $row['Collation'] != 'NULL') + { + $column.= " collate '".$row['Collation']."'"; + } + array_push($columns, $column); } + } - $temporary_tablename = $tablename.'_'.micro_seconds(); + $temporary_tablename = $tablename.'_'.micro_seconds(); - $query = ' - CREATE TABLE '.$temporary_tablename.' - ( - '.implode(",\n", $columns).', + $query = ' +CREATE TABLE '.$temporary_tablename.' +( + '.implode(",\n ", $columns).', UNIQUE KEY the_key ('.implode(',', $dbfields['primary']).') - ) -;'; +)'; - pwg_query($query); - mass_inserts($temporary_tablename, $all_fields, $datas); - // update of images table by joining with temporary table - $query = ' + pwg_query($query); + mass_inserts($temporary_tablename, $all_fields, $datas); + if ( $flags & MASS_UPDATES_SKIP_EMPTY ) + $func_set = create_function('$s', 'return "t1.$s = IFNULL(t2.$s, t1.$s)";'); + else + $func_set = create_function('$s', 'return "t1.$s = t2.$s";'); + + // update of images table by joining with temporary table + $query = ' UPDATE '.$tablename.' AS t1, '.$temporary_tablename.' AS t2 SET '. - implode( - "\n , ", - array_map( - create_function('$s', 'return "t1.$s = t2.$s";'), - $dbfields['update'] - ) - ).' + implode( + "\n , ", + array_map($func_set,$dbfields['update']) + ).' WHERE '. - implode( - "\n AND ", - array_map( - create_function('$s', 'return "t1.$s = t2.$s";'), - $dbfields['primary'] - ) - ).' - ;'; - pwg_query($query); - $query = ' -DROP TABLE '.$temporary_tablename.' -;'; - pwg_query($query); - } + implode( + "\n AND ", + array_map( + create_function('$s', 'return "t1.$s = t2.$s";'), + $dbfields['primary'] + ) + ); + pwg_query($query); + $query = ' +DROP TABLE '.$temporary_tablename; + pwg_query($query); } } @@ -587,8 +577,7 @@ function update_global_rank() $query = ' SELECT id, if(id_uppercat is null,\'\',id_uppercat) AS id_uppercat, uppercats, rank, global_rank FROM '.CATEGORIES_TABLE.' - ORDER BY id_uppercat,rank,name -;'; + ORDER BY id_uppercat,rank,name'; $cat_map = array(); @@ -657,6 +646,7 @@ function set_cat_visible($categories, $value) { if (!in_array($value, array('true', 'false'))) { + trigger_error("set_cat_visible invalid param $value", E_USER_WARNING); return false; } @@ -667,8 +657,7 @@ function set_cat_visible($categories, $value) $query = ' UPDATE '.CATEGORIES_TABLE.' SET visible = \'true\' - WHERE id IN ('.implode(',', $uppercats).') -;'; + WHERE id IN ('.implode(',', $uppercats).')'; pwg_query($query); } // locking a category => all its child categories become locked @@ -678,8 +667,7 @@ UPDATE '.CATEGORIES_TABLE.' $query = ' UPDATE '.CATEGORIES_TABLE.' SET visible = \'false\' - WHERE id IN ('.implode(',', $subcats).') -;'; + WHERE id IN ('.implode(',', $subcats).')'; pwg_query($query); } } @@ -695,6 +683,7 @@ function set_cat_status($categories, $value) { if (!in_array($value, array('public', 'private'))) { + trigger_error("set_cat_status invalid param $value", E_USER_WARNING); return false; } @@ -716,8 +705,7 @@ UPDATE '.CATEGORIES_TABLE.' $query = ' UPDATE '.CATEGORIES_TABLE.' SET status = \'private\' - WHERE id IN ('.implode(',', $subcats).') -;'; + WHERE id IN ('.implode(',', $subcats).')'; pwg_query($query); } } @@ -1582,7 +1570,7 @@ function do_maintenance_all_tables() $all_tables = array(); // List all tables - $query = 'SHOW TABLES LIKE \''.$prefixeTable.'%\';'; + $query = 'SHOW TABLES LIKE \''.$prefixeTable.'%\''; $result = pwg_query($query); while ($row = mysql_fetch_array($result)) { @@ -1590,7 +1578,7 @@ function do_maintenance_all_tables() } // Repair all tables - $query = 'REPAIR TABLE '.implode(', ', $all_tables).';'; + $query = 'REPAIR TABLE '.implode(', ', $all_tables); $mysql_rc = pwg_query($query); // Re-Order all tables @@ -1616,7 +1604,7 @@ function do_maintenance_all_tables() } // Optimize all tables - $query = 'OPTIMIZE TABLE '.implode(', ', $all_tables).';'; + $query = 'OPTIMIZE TABLE '.implode(', ', $all_tables); $mysql_rc = $mysql_rc && pwg_query($query); if ($mysql_rc) { @@ -1832,8 +1820,7 @@ function invalidate_user_cache() { $query = ' UPDATE '.USER_CACHE_TABLE.' - SET need_update = \'true\' -;'; + SET need_update = \'true\''; pwg_query($query); trigger_action('invalidate_user_cache'); } diff --git a/admin/site_update.php b/admin/site_update.php index e6fb6d4ef..ba406afe4 100644 --- a/admin/site_update.php +++ b/admin/site_update.php @@ -42,8 +42,7 @@ $site_id = $_GET['site']; $query=' SELECT galleries_url FROM '.SITES_TABLE.' - WHERE id = '.$site_id.' -;'; + WHERE id = '.$site_id; list($site_url) = mysql_fetch_row(pwg_query($query)); if (!isset($site_url)) { @@ -139,8 +138,6 @@ if (isset($_POST['submit']) { $start = get_moment(); // which categories to update ? - $cat_ids = array(); - $query = ' SELECT id, uppercats, global_rank, status, visible FROM '.CATEGORIES_TABLE.' @@ -161,15 +158,7 @@ SELECT id, uppercats, global_rank, status, visible '; } } - $query.= ' -;'; - $result = pwg_query($query); - - $db_categories = array(); - while ($row = mysql_fetch_array($result)) - { - $db_categories[$row['id']] = $row; - } + $db_categories = hash_from_query($query, 'id'); // get categort full directories in an array for comparison with file // system directory tree @@ -194,8 +183,7 @@ SELECT id, uppercats, global_rank, status, visible $query = ' SELECT id - FROM '.CATEGORIES_TABLE.' -;'; + FROM '.CATEGORIES_TABLE; $result = pwg_query($query); while ($row = mysql_fetch_array($result)) { @@ -206,8 +194,7 @@ SELECT id $query = ' SELECT id_uppercat, MAX(rank)+1 AS next_rank FROM '.CATEGORIES_TABLE.' - GROUP BY id_uppercat -;'; + GROUP BY id_uppercat'; $result = pwg_query($query); while ($row = mysql_fetch_array($result)) { @@ -222,8 +209,7 @@ SELECT id_uppercat, MAX(rank)+1 AS next_rank // next category id available $query = ' SELECT IF(MAX(id)+1 IS NULL, 1, MAX(id)+1) AS next_id - FROM '.CATEGORIES_TABLE.' -;'; + FROM '.CATEGORIES_TABLE; list($next_id) = mysql_fetch_array(pwg_query($query)); // retrieve sub-directories fulldirs from the site reader @@ -388,13 +374,8 @@ SELECT id, path implode(', ', $cat_ids), 80, "\n" - ).') -;'; - $result = pwg_query($query); - while ($row = mysql_fetch_array($result)) - { - $db_elements[$row['id']] = $row['path']; - } + ).')'; + $db_elements = simple_hash_from_query($query, 'id', 'path'); // searching the unvalidated waiting elements (they must not be taken into // account) @@ -403,8 +384,7 @@ SELECT file,storage_category_id FROM '.WAITING_TABLE.' WHERE storage_category_id IN ( '.wordwrap(implode(', ', $cat_ids), 80, "\n").') - AND validated = \'false\' -;'; + AND validated = \'false\''; $result = pwg_query($query); while ($row = mysql_fetch_array($result)) { @@ -421,8 +401,7 @@ SELECT file,storage_category_id // next element id available $query = ' SELECT IF(MAX(id)+1 IS NULL, 1, MAX(id)+1) AS next_element_id - FROM '.IMAGES_TABLE.' -;'; + FROM '.IMAGES_TABLE; list($next_element_id) = mysql_fetch_array(pwg_query($query)); $start = get_moment(); @@ -564,8 +543,7 @@ SELECT id,file,storage_category_id,infos FROM '.WAITING_TABLE.' WHERE storage_category_id IN ( '.wordwrap(implode(', ', $cat_ids), 80, "\n").') - AND validated = \'true\' -;'; + AND validated = \'true\''; $result = pwg_query($query); $datas = array(); @@ -585,8 +563,7 @@ SELECT id,file,storage_category_id,infos SELECT id FROM '.IMAGES_TABLE.' WHERE storage_category_id = '.$row['storage_category_id'].' - AND file = \''.$row['file'].'\' -;'; + AND file = \''.$row['file'].'\''; list($data['id']) = mysql_fetch_array(pwg_query($query)); foreach ($fields['update'] as $field) @@ -723,18 +700,11 @@ if (isset($_POST['submit']) // +-----------------------------------------------------------------------+ // | synchronize metadata | // +-----------------------------------------------------------------------+ -if (isset($_POST['submit']) and preg_match('/^metadata/', $_POST['sync']) +if (isset($_POST['submit']) and isset($_POST['sync_meta']) and !$general_failure) { // sync only never synchronized files ? - if ($_POST['sync'] == 'metadata_new') - { - $opts['only_new'] = true; - } - else - { - $opts['only_new'] = false; - } + $opts['only_new'] = isset($_POST['meta_all']) ? false : true; $opts['category_id'] = ''; $opts['recursive'] = true; @@ -776,14 +746,8 @@ SELECT id WHERE has_high = \'true\' AND id IN ( '.wordwrap(implode(', ', $image_ids), 80, "\n").' -) -;'; - - $result = pwg_query($query); - while ($row = mysql_fetch_array($result)) - { - array_push($has_high_images, $row['id']); - } +)'; + $has_high_images = array_from_query($query, 'id' ); } foreach ( $files as $id=>$file ) @@ -843,7 +807,8 @@ SELECT id array('date_metadata_update')) ) ), - $datas + $datas, + isset($_POST['meta_empty_overrides']) ? 0 : MASS_UPDATES_SKIP_EMPTY ); } set_tags_of($tags_of); @@ -893,58 +858,61 @@ $template->assign( // +-----------------------------------------------------------------------+ // | introduction : choices | // +-----------------------------------------------------------------------+ -if (!isset($_POST['submit']) or (isset($simulate) and $simulate)) +if (isset($_POST['submit'])) { - if (isset($simulate) and $simulate) - { - $tpl_introduction = array( - 'sync' => $_POST['sync'], - 'display_info' => isset($_POST['display_info']) and $_POST['display_info']==1, - 'add_to_caddie' => isset($_POST['add_to_caddie']) and $_POST['add_to_caddie']==1, - 'subcats_included' => isset($_POST['subcats-included']) and $_POST['subcats-included']==1, - 'privacy_level_selected' => (int)@$_POST['privacy_level'], - ); + $tpl_introduction = array( + 'sync' => $_POST['sync'], + 'sync_meta' => isset($_POST['sync_meta']) ? true : false, + 'display_info' => isset($_POST['display_info']) and $_POST['display_info']==1, + 'add_to_caddie' => isset($_POST['add_to_caddie']) and $_POST['add_to_caddie']==1, + 'subcats_included' => isset($_POST['subcats-included']) and $_POST['subcats-included']==1, + 'privacy_level_selected' => (int)@$_POST['privacy_level'], + 'meta_all' => isset($_POST['meta_all']) ? true : false, + 'meta_empty_overrides' => isset($_POST['meta_empty_overrides']) ? true : false, + ); - if (isset($_POST['cat']) and is_numeric($_POST['cat'])) - { - $cat_selected = array($_POST['cat']); - } - else - { - $cat_selected = array(); - } + if (isset($_POST['cat']) and is_numeric($_POST['cat'])) + { + $cat_selected = array($_POST['cat']); } else { - $tpl_introduction = array( - 'sync' => 'dirs', - 'display_info' => false, - 'add_to_caddie' => false, - 'subcats_included' => true, - 'privacy_level_selected' => 0, - ); - $cat_selected = array(); } +} +else +{ + $tpl_introduction = array( + 'sync' => 'dirs', + 'sync_meta' => true, + 'display_info' => false, + 'add_to_caddie' => false, + 'subcats_included' => true, + 'privacy_level_selected' => 0, + 'meta_all' => false, + 'meta_empty_overrides' => false, + ); + + $cat_selected = array(); +} - $tpl_introduction['privacy_level_options']=array(); - foreach ($conf['available_permission_levels'] as $level) - { - $tpl_introduction['privacy_level_options'][$level] = l10n( sprintf('Level %d', $level) ); - } +$tpl_introduction['privacy_level_options']=array(); +foreach ($conf['available_permission_levels'] as $level) +{ + $tpl_introduction['privacy_level_options'][$level] = l10n( sprintf('Level %d', $level) ); +} - $template->assign('introduction', $tpl_introduction); +$template->assign('introduction', $tpl_introduction); - $query = ' +$query = ' SELECT id,name,uppercats,global_rank FROM '.CATEGORIES_TABLE.' - WHERE site_id = '.$site_id.' -;'; - display_select_cat_wrapper($query, - $cat_selected, - 'category_options', - false); -} + WHERE site_id = '.$site_id; +display_select_cat_wrapper($query, + $cat_selected, + 'category_options', + false); + if (count($errors) > 0) { diff --git a/admin/template/yoga/admin/site_update.tpl b/admin/template/yoga/admin/site_update.tpl index e3016c68b..ad4f99e0f 100644 --- a/admin/template/yoga/admin/site_update.tpl +++ b/admin/template/yoga/admin/site_update.tpl @@ -58,25 +58,34 @@ <h3>{'update_default_title'|@translate}</h3> <form action="" method="post" id="update"> - <fieldset id="syncFiles"> - <legend>{'update_sync_files'|@translate}</legend> - <ul> - <li><label><input type="radio" name="sync" value="dirs" {if 'dirs'==$introduction.sync}checked="checked"{/if}/> {'update_sync_dirs'|@translate}</label></li> - <li><label><input type="radio" name="sync" value="files" {if 'files'==$introduction.sync}checked="checked"{/if}/> {'update_sync_all'|@translate}</label></li> - <li><label><input type="checkbox" name="display_info" value="1" {if $introduction.display_info}checked="checked"{/if}/> {'update_display_info'|@translate}</label></li> - <li><label><input type="checkbox" name="add_to_caddie" value="1" {if $introduction.add_to_caddie}checked="checked"{/if}/> {'add new elements to caddie'|@translate}</label></li> - <li><label>{'Minimum privacy level'|@translate} <select name="privacy_level">{html_options options=$introduction.privacy_level_options selected=$introduction.privacy_level_selected}</select></label></li> - </ul> - </fieldset> + <fieldset id="syncFiles"> + <legend>{'update_sync_files'|@translate}</legend> + <ul> + <li><label><input type="radio" name="sync" value="" {if empty($introduction.sync)}checked="checked"{/if}/> {'nothing'|@translate}</label></li> + <li><label><input type="radio" name="sync" value="dirs" {if 'dirs'==$introduction.sync}checked="checked"{/if}/> {'update_sync_dirs'|@translate}</label></li> - <fieldset id="syncMetadata"> - <legend>{'update_sync_metadata'|@translate}</legend> - {'update_used_metadata'|@translate} : {$METADATA_LIST}.<br/> - <ul> - <li><label><input type="radio" name="sync" value="metadata_new" {if 'metadata_new'==$introduction.sync}checked="checked"{/if}/> {'update_sync_metadata_new'|@translate}</label></li> - <li><label><input type="radio" name="sync" value="metadata_all" {if 'metadata_all'==$introduction.sync}checked="checked"{/if}/> {'update_sync_metadata_all'|@translate}</label></li> - </ul> - </fieldset> + <li><label><input type="radio" name="sync" value="files" {if 'files'==$introduction.sync}checked="checked"{/if}/> {'update_sync_all'|@translate}</label> + <ul style="padding-left:3em"> + <li><label><input type="checkbox" name="display_info" value="1" {if $introduction.display_info}checked="checked"{/if}/> {'update_display_info'|@translate}</label></li> + <li><label><input type="checkbox" name="add_to_caddie" value="1" {if $introduction.add_to_caddie}checked="checked"{/if}/> {'add new elements to caddie'|@translate}</label></li> + <li><label>{'Minimum privacy level'|@translate} <select name="privacy_level">{html_options options=$introduction.privacy_level_options selected=$introduction.privacy_level_selected}</select></label></li> + </ul> + </li> + </ul> + </fieldset> + + <fieldset id="syncMetadata"> + <legend>{'update_sync_metadata'|@translate}</legend> + <label><input type="checkbox" name="sync_meta" {if $introduction.sync_meta}checked="checked"{/if}/> {'synchronize metadata'|@translate} ({$METADATA_LIST})</label></li> + <ul style="padding-left:3em"> + <li> + <label><input type="checkbox" name="meta_all" {if $introduction.meta_all}checked="checked"{/if}/> {'update_sync_metadata_all'|@translate}</label> + </li> + <li> + <label><input type="checkbox" name="meta_empty_overrides" {if $introduction.meta_empty_overrides}checked="checked"{/if}/> {'overrides existing values with empty ones'|@translate}</label> + </li> + </ul> + </fieldset> <fieldset id="syncSimulate"> <legend></legend> |