Documentation TYPO3 par Ameos |
00001 <?php 00002 /* $Id: tbl_move_copy.php,v 2.5 2003/11/26 22:52:24 rabus Exp $ */ 00003 // vim: expandtab sw=4 ts=4 sts=4: 00004 00005 // Check parameters 00006 00007 require_once('./libraries/grab_globals.lib.php'); 00008 require_once('./libraries/common.lib.php'); 00009 00010 PMA_checkParameters(array('db', 'table')); 00011 00022 function PMA_myHandler($sql_insert = '') 00023 { 00024 global $db, $table, $target; 00025 global $sql_insert_data; 00026 00027 $sql_insert = preg_replace('~INSERT INTO (`?)' . $table . '(`?)~i', 'INSERT INTO ' . $target, $sql_insert); 00028 $result = PMA_mysql_query($sql_insert) or PMA_mysqlDie('', $sql_insert, '', $GLOBALS['err_url']); 00029 00030 $sql_insert_data .= $sql_insert . ';' . "\n"; 00031 } // end of the 'PMA_myHandler()' function 00032 00051 function PMA_duplicate_table($work, $pma_table, $get_fields, $where_fields, $new_fields) { 00052 global $cfgRelation; 00053 00054 $last_id = -1; 00055 00056 if ($cfgRelation[$work]) { 00057 $select_parts = array(); 00058 $row_fields = array(); 00059 foreach($get_fields AS $nr => $get_field) { 00060 $select_parts[] = PMA_backquote($get_field); 00061 $row_fields[$get_field] = 'cc'; 00062 } 00063 00064 $where_parts = array(); 00065 foreach($where_fields AS $_where => $_value) { 00066 $where_parts[] = PMA_backquote($_where) . ' = \'' . PMA_sqlAddslashes($_value) . '\''; 00067 } 00068 00069 $new_parts = array(); 00070 $new_value_parts = array(); 00071 foreach($new_fields AS $_where => $_value) { 00072 $new_parts[] = PMA_backquote($_where); 00073 $new_value_parts[] = PMA_sqlAddslashes($_value); 00074 } 00075 00076 $table_copy_query = 'SELECT ' . implode(', ', $select_parts) 00077 . ' FROM ' . PMA_backquote($cfgRelation[$pma_table]) 00078 . ' WHERE ' . implode(' AND ', $where_parts); 00079 $table_copy_rs = PMA_query_as_cu($table_copy_query); 00080 00081 while ($table_copy_row = @PMA_mysql_fetch_array($table_copy_rs)) { 00082 $value_parts = array(); 00083 foreach($table_copy_row AS $_key => $_val) { 00084 if (isset($row_fields[$_key]) && $row_fields[$_key] == 'cc') { 00085 $value_parts[] = PMA_sqlAddslashes($_val); 00086 } 00087 } 00088 00089 $new_table_query = 'INSERT IGNORE INTO ' . PMA_backquote($cfgRelation[$pma_table]) 00090 . ' (' . implode(', ', $select_parts) . ', ' . implode(', ', $new_parts) . ')' 00091 . ' VALUES ' 00092 . ' (\'' . implode('\', \'', $value_parts) . '\', \'' . implode('\', \'', $new_value_parts) . '\')'; 00093 00094 $new_table_rs = PMA_query_as_cu($new_table_query); 00095 $last_id = (@function_exists('mysql_insert_id') ? @mysql_insert_id() : -1); 00096 } // end while 00097 00098 return $last_id; 00099 } 00100 00101 return true; 00102 } // end of 'PMA_duplicate_table()' function 00103 00107 require_once('./libraries/grab_globals.lib.php'); 00108 require_once('./libraries/common.lib.php'); 00109 00110 00114 $err_url = 'tbl_properties.php?' . PMA_generate_common_url($db, $table); 00115 00116 00120 PMA_mysql_select_db($db); 00121 00122 00126 if (isset($new_name) && trim($new_name) != '') { 00127 $use_backquotes = 1; 00128 $asfile = 1; 00129 00130 // Ensure the target is valid 00131 if (count($dblist) > 0 && 00132 (PMA_isInto($db, $dblist) == -1 || PMA_isInto($target_db, $dblist) == -1)) { 00133 exit(); 00134 } 00135 00136 if ($db == $target_db && $new_name == $table) { 00137 $message = (isset($submit_move) ? $strMoveTableSameNames : $strCopyTableSameNames); 00138 } else { 00139 $source = PMA_backquote($db) . '.' . PMA_backquote($table); 00140 if (empty($target_db)) $target_db = $db; 00141 00142 // This could avoid some problems with replicated databases, when 00143 // moving table from replicated one to not replicated one 00144 PMA_mysql_select_db($target_db); 00145 00146 $target = PMA_backquote($target_db) . '.' . PMA_backquote($new_name); 00147 00148 // do not create the table if dataonly 00149 if ($what != 'dataonly') { 00150 require('./libraries/export/sql.php'); 00151 00152 $no_constraints_comments = true; 00153 $sql_structure = PMA_getTableDef($db, $table, "\n", $err_url); 00154 unset($no_constraints_comments); 00155 00156 $parsed_sql = PMA_SQP_parse($sql_structure); 00157 00158 /* nijel: Find table name in query and replace it */ 00159 $i = 0; 00160 while ($parsed_sql[$i]['type'] != 'quote_backtick') $i++; 00161 00162 /* no need to PMA_backquote() */ 00163 $parsed_sql[$i]['data'] = $target; 00164 00165 /* Generate query back */ 00166 $sql_structure = PMA_SQP_formatHtml($parsed_sql, 'query_only'); 00167 00168 // If table exists, and 'add drop table' is selected: Drop it! 00169 $drop_query = ''; 00170 if (isset($drop_if_exists) && $drop_if_exists == 'true') { 00171 $drop_query = 'DROP TABLE IF EXISTS ' . PMA_backquote($target_db) . '.' . PMA_backquote($new_name); 00172 $result = @PMA_mysql_query($drop_query); 00173 if (PMA_mysql_error()) { 00174 require_once('./header.inc.php'); 00175 PMA_mysqlDie('', $sql_structure, '', $err_url); 00176 } 00177 00178 if (isset($sql_query)) { 00179 $sql_query .= "\n" . $drop_query . ';'; 00180 } else { 00181 $sql_query = $drop_query . ';'; 00182 } 00183 00184 // garvin: If an existing table gets deleted, maintain any entries 00185 // for the PMA_* tables 00186 $maintain_relations = true; 00187 } 00188 00189 $result = @PMA_mysql_query($sql_structure); 00190 if (PMA_mysql_error()) { 00191 require_once('./header.inc.php'); 00192 PMA_mysqlDie('', $sql_structure, '', $err_url); 00193 } else if (isset($sql_query)) { 00194 $sql_query .= "\n" . $sql_structure . ';'; 00195 } else { 00196 $sql_query = $sql_structure . ';'; 00197 } 00198 00199 if ((isset($submit_move) || isset($constraints)) && isset($sql_constraints)) { 00200 $parsed_sql = PMA_SQP_parse($sql_constraints); 00201 00202 $i = 0; 00203 while ($parsed_sql[$i]['type'] != 'quote_backtick') $i++; 00204 00205 /* no need to PMA_backquote() */ 00206 $parsed_sql[$i]['data'] = $target; 00207 00208 /* Generate query back */ 00209 $sql_constraints = PMA_SQP_formatHtml($parsed_sql, 'query_only'); 00210 $result = @PMA_mysql_query($sql_constraints); 00211 if (PMA_mysql_error()) { 00212 require_once('./header.inc.php'); 00213 PMA_mysqlDie('', $sql_structure, '', $err_url); 00214 } else if (isset($sql_query)) { 00215 $sql_query .= "\n" . $sql_constraints; 00216 } else { 00217 $sql_query = $sql_constraints; 00218 } 00219 } 00220 00221 } else { 00222 $sql_query=''; 00223 } 00224 00225 // Copy the data 00226 if ($result != FALSE && ($what == 'data' || $what == 'dataonly')) { 00227 $sql_insert_data = 'INSERT INTO ' . $target . ' SELECT * FROM ' . $source; 00228 $result = @PMA_mysql_query($sql_insert_data); 00229 if (PMA_mysql_error()) { 00230 require_once('./header.inc.php'); 00231 PMA_mysqlDie('', $sql_insert_data, '', $err_url); 00232 } 00233 $sql_query .= "\n\n" . $sql_insert_data . ';'; 00234 } 00235 00236 require_once('./libraries/relation.lib.php'); 00237 $cfgRelation = PMA_getRelationsParam(); 00238 00239 // Drops old table if the user has requested to move it 00240 if (isset($submit_move)) { 00241 00242 // This could avoid some problems with replicated databases, when 00243 // moving table from replicated one to not replicated one 00244 PMA_mysql_select_db($db); 00245 00246 $sql_drop_table = 'DROP TABLE ' . $source; 00247 $result = @PMA_mysql_query($sql_drop_table); 00248 if (PMA_mysql_error()) { 00249 require_once('./header.inc.php'); 00250 PMA_mysqlDie('', $sql_drop_table, '', $err_url); 00251 } 00252 00253 // garvin: Move old entries from PMA-DBs to new table 00254 if ($cfgRelation['commwork']) { 00255 $remove_query = 'UPDATE ' . PMA_backquote($cfgRelation['column_info']) 00256 . ' SET table_name = \'' . PMA_sqlAddslashes($new_name) . '\', ' 00257 . ' db_name = \'' . PMA_sqlAddslashes($target_db) . '\'' 00258 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\'' 00259 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\''; 00260 $rmv_rs = PMA_query_as_cu($remove_query); 00261 unset($rmv_query); 00262 } 00263 00264 // garvin: updating bookmarks is not possible since only a single table is moved, 00265 // and not the whole DB. 00266 // if ($cfgRelation['bookmarkwork']) { 00267 // $remove_query = 'UPDATE ' . PMA_backquote($cfgRelation['bookmark']) 00268 // . ' SET dbase = \'' . PMA_sqlAddslashes($target_db) . '\'' 00269 // . ' WHERE dbase = \'' . PMA_sqlAddslashes($db) . '\''; 00270 // $rmv_rs = PMA_query_as_cu($remove_query); 00271 // unset($rmv_query); 00272 // } 00273 00274 if ($cfgRelation['displaywork']) { 00275 $table_query = 'UPDATE ' . PMA_backquote($cfgRelation['table_info']) 00276 . ' SET db_name = \'' . PMA_sqlAddslashes($target_db) . '\', ' 00277 . ' table_name = \'' . PMA_sqlAddslashes($new_name) . '\'' 00278 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\'' 00279 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\''; 00280 $tb_rs = PMA_query_as_cu($table_query); 00281 unset($table_query); 00282 unset($tb_rs); 00283 } 00284 00285 if ($cfgRelation['relwork']) { 00286 $table_query = 'UPDATE ' . PMA_backquote($cfgRelation['relation']) 00287 . ' SET foreign_table = \'' . PMA_sqlAddslashes($new_name) . '\',' 00288 . ' foreign_db = \'' . PMA_sqlAddslashes($target_db) . '\'' 00289 . ' WHERE foreign_db = \'' . PMA_sqlAddslashes($db) . '\'' 00290 . ' AND foreign_table = \'' . PMA_sqlAddslashes($table) . '\''; 00291 $tb_rs = PMA_query_as_cu($table_query); 00292 unset($table_query); 00293 unset($tb_rs); 00294 00295 $table_query = 'UPDATE ' . PMA_backquote($cfgRelation['relation']) 00296 . ' SET master_table = \'' . PMA_sqlAddslashes($new_name) . '\',' 00297 . ' master_db = \'' . PMA_sqlAddslashes($target_db) . '\'' 00298 . ' WHERE master_db = \'' . PMA_sqlAddslashes($db) . '\'' 00299 . ' AND master_table = \'' . PMA_sqlAddslashes($table) . '\''; 00300 $tb_rs = PMA_query_as_cu($table_query); 00301 unset($table_query); 00302 unset($tb_rs); 00303 } 00304 00305 // garvin: [TODO] Can't get moving PDFs the right way. The page numbers always 00306 // get screwed up independently from duplication because the numbers do not 00307 // seem to be stored on a per-database basis. Would the author of pdf support 00308 // please have a look at it? 00309 00310 if ($cfgRelation['pdfwork']) { 00311 $table_query = 'UPDATE ' . PMA_backquote($cfgRelation['table_coords']) 00312 . ' SET table_name = \'' . PMA_sqlAddslashes($new_name) . '\',' 00313 . ' db_name = \'' . PMA_sqlAddslashes($target_db) . '\'' 00314 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\'' 00315 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\''; 00316 $tb_rs = PMA_query_as_cu($table_query); 00317 unset($table_query); 00318 unset($tb_rs); 00319 /* 00320 $pdf_query = 'SELECT pdf_page_number ' 00321 . ' FROM ' . PMA_backquote($cfgRelation['table_coords']) 00322 . ' WHERE db_name = \'' . PMA_sqlAddslashes($target_db) . '\'' 00323 . ' AND table_name = \'' . PMA_sqlAddslashes($new_name) . '\''; 00324 $pdf_rs = PMA_query_as_cu($pdf_query); 00325 00326 while ($pdf_copy_row = @PMA_mysql_fetch_array($pdf_rs)) { 00327 $table_query = 'UPDATE ' . PMA_backquote($cfgRelation['pdf_pages']) 00328 . ' SET db_name = \'' . PMA_sqlAddslashes($target_db) . '\'' 00329 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\'' 00330 . ' AND page_nr = \'' . PMA_sqlAddslashes($pdf_copy_row['pdf_page_number']) . '\''; 00331 $tb_rs = PMA_query_as_cu($table_query); 00332 unset($table_query); 00333 unset($tb_rs); 00334 } 00335 */ 00336 } 00337 00338 $sql_query .= "\n\n" . $sql_drop_table . ';'; 00339 } else { 00340 // garvin: Create new entries as duplicates from old PMA DBs 00341 if ($what != 'dataonly' && !isset($maintain_relations)) { 00342 if ($cfgRelation['commwork']) { 00343 // Get all comments and MIME-Types for current table 00344 $comments_copy_query = 'SELECT 00345 column_name, ' . PMA_backquote('comment') . ($cfgRelation['mimework'] ? ', mimetype, transformation, transformation_options' : '') . ' 00346 FROM ' . PMA_backquote($cfgRelation['column_info']) . ' 00347 WHERE 00348 db_name = \'' . PMA_sqlAddslashes($db) . '\' AND 00349 table_name = \'' . PMA_sqlAddslashes($table) . '\''; 00350 $comments_copy_rs = PMA_query_as_cu($comments_copy_query); 00351 00352 // Write every comment as new copied entry. [MIME] 00353 while ($comments_copy_row = @PMA_mysql_fetch_array($comments_copy_rs)) { 00354 $new_comment_query = 'REPLACE INTO ' . PMA_backquote($cfgRelation['column_info']) 00355 . ' (db_name, table_name, column_name, ' . PMA_backquote('comment') . ($cfgRelation['mimework'] ? ', mimetype, transformation, transformation_options' : '') . ') ' 00356 . ' VALUES(' 00357 . '\'' . PMA_sqlAddslashes($target_db) . '\',' 00358 . '\'' . PMA_sqlAddslashes($new_name) . '\',' 00359 . '\'' . PMA_sqlAddslashes($comments_copy_row['column_name']) . '\'' 00360 . ($cfgRelation['mimework'] ? ',\'' . PMA_sqlAddslashes($comments_copy_row['comment']) . '\',' 00361 . '\'' . PMA_sqlAddslashes($comments_copy_row['mimetype']) . '\',' 00362 . '\'' . PMA_sqlAddslashes($comments_copy_row['transformation']) . '\',' 00363 . '\'' . PMA_sqlAddslashes($comments_copy_row['transformation_options']) . '\'' : '') 00364 . ')'; 00365 $new_comment_rs = PMA_query_as_cu($new_comment_query); 00366 } // end while 00367 } 00368 00369 if ($db != $target_db) { 00370 $get_fields = array('user','label','query'); 00371 $where_fields = array('dbase' => $db); 00372 $new_fields = array('dbase' => $target_db); 00373 PMA_duplicate_table('bookmarkwork', 'bookmark', $get_fields, $where_fields, $new_fields); 00374 } 00375 00376 $get_fields = array('display_field'); 00377 $where_fields = array('db_name' => $db, 'table_name' => $table); 00378 $new_fields = array('db_name' => $target_db, 'table_name' => $new_name); 00379 PMA_duplicate_table('displaywork', 'table_info', $get_fields, $where_fields, $new_fields); 00380 00381 $get_fields = array('master_field', 'foreign_db', 'foreign_table', 'foreign_field'); 00382 $where_fields = array('master_db' => $db, 'master_table' => $table); 00383 $new_fields = array('master_db' => $target_db, 'master_table' => $new_name); 00384 PMA_duplicate_table('relwork', 'relation', $get_fields, $where_fields, $new_fields); 00385 00386 $get_fields = array('foreign_field', 'master_db', 'master_table', 'master_field'); 00387 $where_fields = array('foreign_db' => $db, 'foreign_table' => $table); 00388 $new_fields = array('foreign_db' => $target_db, 'foreign_table' => $new_name); 00389 PMA_duplicate_table('relwork', 'relation', $get_fields, $where_fields, $new_fields); 00390 00391 // garvin: [TODO] Can't get duplicating PDFs the right way. The page numbers always 00392 // get screwed up independently from duplication because the numbers do not 00393 // seem to be stored on a per-database basis. Would the author of pdf support 00394 // please have a look at it? 00395 /* 00396 $get_fields = array('page_descr'); 00397 $where_fields = array('db_name' => $db); 00398 $new_fields = array('db_name' => $target_db); 00399 $last_id = PMA_duplicate_table('pdfwork', 'pdf_pages', $get_fields, $where_fields, $new_fields); 00400 00401 if (isset($last_id) && $last_id >= 0) { 00402 $get_fields = array('x', 'y'); 00403 $where_fields = array('db_name' => $db, 'table_name' => $table); 00404 $new_fields = array('db_name' => $target_db, 'table_name' => $new_name, 'pdf_page_number' => $last_id); 00405 PMA_duplicate_table('pdfwork', 'table_coords', $get_fields, $where_fields, $new_fields); 00406 } 00407 */ 00408 } 00409 } 00410 00411 $message = (isset($submit_move) ? $strMoveTableOK : $strCopyTableOK); 00412 $message = sprintf($message, htmlspecialchars($source), htmlspecialchars($target)); 00413 $reload = 1; 00414 $js_to_run = 'functions.js'; 00415 /* Check: Work on new table or on old table? */ 00416 if (isset($submit_move)) { 00417 $db = $target_db; 00418 $table = $new_name; 00419 } else { 00420 $pma_uri_parts = parse_url($cfg['PmaAbsoluteUri']); 00421 if (isset($switch_to_new) && $switch_to_new == 'true') { 00422 setcookie('pma_switch_to_new', 'true', 0, substr($pma_uri_parts['path'], 0, strrpos($pma_uri_parts['path'], '/')), '', ($pma_uri_parts['scheme'] == 'https')); 00423 $db = $target_db; 00424 $table = $new_name; 00425 } else { 00426 setcookie('pma_switch_to_new', '', 0, substr($pma_uri_parts['path'], 0, strrpos($pma_uri_parts['path'], '/')), '', ($pma_uri_parts['scheme'] == 'https')); 00427 // garvin:Keep original table for work. 00428 } 00429 } 00430 } 00431 require_once('./header.inc.php'); 00432 } // end is target table name 00433 00434 00438 else { 00439 require_once('./header.inc.php'); 00440 PMA_mysqlDie($strTableEmpty, '', '', $err_url); 00441 } 00442 00443 00448 require('./tbl_properties.php'); 00449 ?>