PageRenderTime 35ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/var/www/vhcs2/tools/pma/libraries/tbl_move_copy.php

https://bitbucket.org/wmark/gentoo_vhcs_mods
PHP | 370 lines | 210 code | 58 blank | 102 comment | 51 complexity | 3c0be3b16c6f20b9340f91f90e1b7888 MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. /* $Id: tbl_move_copy.php,v 1.5 2005/04/30 12:01:48 lem9 Exp $ */
  3. // vim: expandtab sw=4 ts=4 sts=4:
  4. /**
  5. * Inserts existing entries in a PMA_* table by reading a value from an old entry
  6. *
  7. * @param string The array index, which Relation feature to check
  8. * ('relwork', 'commwork', ...)
  9. * @param string The array index, which PMA-table to update
  10. * ('bookmark', 'relation', ...)
  11. * @param array Which fields will be SELECT'ed from the old entry
  12. * @param array Which fields will be used for the WHERE query
  13. * (array('FIELDNAME' => 'FIELDVALUE'))
  14. * @param array Which fields will be used as new VALUES. These are the important
  15. * keys which differ from the old entry.
  16. * (array('FIELDNAME' => 'NEW FIELDVALUE'))
  17. * @global string relation variable
  18. *
  19. * @author Garvin Hicking <me@supergarv.de>
  20. */
  21. function PMA_duplicate_table_info($work, $pma_table, $get_fields, $where_fields, $new_fields) {
  22. global $cfgRelation;
  23. $last_id = -1;
  24. if ($cfgRelation[$work]) {
  25. $select_parts = array();
  26. $row_fields = array();
  27. foreach ($get_fields AS $nr => $get_field) {
  28. $select_parts[] = PMA_backquote($get_field);
  29. $row_fields[$get_field] = 'cc';
  30. }
  31. $where_parts = array();
  32. foreach ($where_fields AS $_where => $_value) {
  33. $where_parts[] = PMA_backquote($_where) . ' = \'' . PMA_sqlAddslashes($_value) . '\'';
  34. }
  35. $new_parts = array();
  36. $new_value_parts = array();
  37. foreach ($new_fields AS $_where => $_value) {
  38. $new_parts[] = PMA_backquote($_where);
  39. $new_value_parts[] = PMA_sqlAddslashes($_value);
  40. }
  41. $table_copy_query = 'SELECT ' . implode(', ', $select_parts)
  42. . ' FROM ' . PMA_backquote($cfgRelation[$pma_table])
  43. . ' WHERE ' . implode(' AND ', $where_parts);
  44. // must use PMA_DBI_QUERY_STORE here, since we execute another
  45. // query inside the loop
  46. $table_copy_rs = PMA_query_as_cu($table_copy_query, TRUE, PMA_DBI_QUERY_STORE);
  47. while ($table_copy_row = @PMA_DBI_fetch_assoc($table_copy_rs)) {
  48. $value_parts = array();
  49. foreach ($table_copy_row AS $_key => $_val) {
  50. if (isset($row_fields[$_key]) && $row_fields[$_key] == 'cc') {
  51. $value_parts[] = PMA_sqlAddslashes($_val);
  52. }
  53. }
  54. $new_table_query = 'INSERT IGNORE INTO ' . PMA_backquote($cfgRelation[$pma_table])
  55. . ' (' . implode(', ', $select_parts) . ', ' . implode(', ', $new_parts) . ')'
  56. . ' VALUES '
  57. . ' (\'' . implode('\', \'', $value_parts) . '\', \'' . implode('\', \'', $new_value_parts) . '\')';
  58. $new_table_rs = PMA_query_as_cu($new_table_query);
  59. $last_id = PMA_DBI_insert_id();
  60. } // end while
  61. return $last_id;
  62. }
  63. return true;
  64. } // end of 'PMA_duplicate_table_info()' function
  65. /**
  66. * Copies or renames table
  67. * FIXME: use RENAME
  68. *
  69. * @author Michal Čihař <michal@cihar.com>
  70. */
  71. function PMA_table_move_copy($source_db, $source_table, $target_db, $target_table, $what, $move) {
  72. global $cfgRelation, $dblist, $err_url, $sql_query;
  73. // set export settings we need
  74. $GLOBALS['use_backquotes'] = 1;
  75. $GLOBALS['asfile'] = 1;
  76. // Ensure the target is valid
  77. if (count($dblist) > 0 &&
  78. (PMA_isInto($source_db, $dblist) == -1 || PMA_isInto($target_db, $dblist) == -1)) {
  79. exit();
  80. }
  81. $source = PMA_backquote($source_db) . '.' . PMA_backquote($source_table);
  82. if (empty($target_db)) $target_db = $source_db;
  83. // Doing a select_db could avoid some problems with replicated databases,
  84. // when moving table from replicated one to not replicated one
  85. PMA_DBI_select_db($target_db);
  86. $target = PMA_backquote($target_db) . '.' . PMA_backquote($target_table);
  87. // do not create the table if dataonly
  88. if ($what != 'dataonly') {
  89. require_once('./libraries/export/sql.php');
  90. $no_constraints_comments = true;
  91. $sql_structure = PMA_getTableDef($source_db, $source_table, "\n", $err_url);
  92. unset($no_constraints_comments);
  93. $parsed_sql = PMA_SQP_parse($sql_structure);
  94. /* nijel: Find table name in query and replace it */
  95. $i = 0;
  96. while ($parsed_sql[$i]['type'] != 'quote_backtick') $i++;
  97. /* no need to PMA_backquote() */
  98. $parsed_sql[$i]['data'] = $target;
  99. /* Generate query back */
  100. $sql_structure = PMA_SQP_formatHtml($parsed_sql, 'query_only');
  101. // If table exists, and 'add drop table' is selected: Drop it!
  102. $drop_query = '';
  103. if (isset($GLOBALS['drop_if_exists']) && $GLOBALS['drop_if_exists'] == 'true') {
  104. $drop_query = 'DROP TABLE IF EXISTS ' . PMA_backquote($target_db) . '.' . PMA_backquote($target_table);
  105. $result = PMA_DBI_query($drop_query);
  106. if (isset($sql_query)) {
  107. $sql_query .= "\n" . $drop_query . ';';
  108. } else {
  109. $sql_query = $drop_query . ';';
  110. }
  111. // garvin: If an existing table gets deleted, maintain any entries
  112. // for the PMA_* tables
  113. $maintain_relations = TRUE;
  114. }
  115. $result = @PMA_DBI_query($sql_structure);
  116. if (isset($sql_query)) {
  117. $sql_query .= "\n" . $sql_structure . ';';
  118. } else {
  119. $sql_query = $sql_structure . ';';
  120. }
  121. if (($move || isset($GLOBALS['constraints'])) && isset($GLOBALS['sql_constraints'])) {
  122. $parsed_sql = PMA_SQP_parse($GLOBALS['sql_constraints']);
  123. $i = 0;
  124. // find the first quote_backtick, it must be the source table name
  125. while ($parsed_sql[$i]['type'] != 'quote_backtick') {
  126. $i++;
  127. }
  128. // replace it by the target table name, no need to PMA_backquote()
  129. $parsed_sql[$i]['data'] = $target;
  130. // now we must remove all quote_backtick that follow a CONSTRAINT
  131. // keyword, because a constraint name must be unique in a db
  132. $cnt = $parsed_sql['len'] - 1;
  133. for ($j = $i; $j < $cnt; $j++) {
  134. if ($parsed_sql[$j]['type'] == 'alpha_reservedWord'
  135. && strtoupper($parsed_sql[$j]['data']) == 'CONSTRAINT') {
  136. if ($parsed_sql[$j+1]['type'] == 'quote_backtick') {
  137. $parsed_sql[$j+1]['data'] = '';
  138. }
  139. }
  140. }
  141. // Generate query back
  142. $GLOBALS['sql_constraints'] = PMA_SQP_formatHtml($parsed_sql, 'query_only');
  143. $result = PMA_DBI_query($GLOBALS['sql_constraints']);
  144. if (isset($sql_query)) {
  145. $sql_query .= "\n" . $GLOBALS['sql_constraints'];
  146. } else {
  147. $sql_query = $GLOBALS['sql_constraints'];
  148. }
  149. unset($GLOBALS['sql_constraints']);
  150. }
  151. } else {
  152. $sql_query='';
  153. }
  154. // Copy the data
  155. //if ($result != FALSE && ($what == 'data' || $what == 'dataonly')) {
  156. if ($what == 'data' || $what == 'dataonly') {
  157. $sql_insert_data = 'INSERT INTO ' . $target . ' SELECT * FROM ' . $source;
  158. PMA_DBI_query($sql_insert_data);
  159. $sql_query .= "\n\n" . $sql_insert_data . ';';
  160. }
  161. require_once('./libraries/relation.lib.php');
  162. $cfgRelation = PMA_getRelationsParam();
  163. // Drops old table if the user has requested to move it
  164. if ($move) {
  165. // This could avoid some problems with replicated databases, when
  166. // moving table from replicated one to not replicated one
  167. PMA_DBI_select_db($source_db);
  168. $sql_drop_table = 'DROP TABLE ' . $source;
  169. PMA_DBI_query($sql_drop_table);
  170. // garvin: Move old entries from PMA-DBs to new table
  171. if ($cfgRelation['commwork']) {
  172. $remove_query = 'UPDATE ' . PMA_backquote($cfgRelation['column_info'])
  173. . ' SET table_name = \'' . PMA_sqlAddslashes($target_table) . '\', '
  174. . ' db_name = \'' . PMA_sqlAddslashes($target_db) . '\''
  175. . ' WHERE db_name = \'' . PMA_sqlAddslashes($source_db) . '\''
  176. . ' AND table_name = \'' . PMA_sqlAddslashes($source_table) . '\'';
  177. $rmv_rs = PMA_query_as_cu($remove_query);
  178. unset($rmv_query);
  179. }
  180. // garvin: updating bookmarks is not possible since only a single table is moved,
  181. // and not the whole DB.
  182. // if ($cfgRelation['bookmarkwork']) {
  183. // $remove_query = 'UPDATE ' . PMA_backquote($cfgRelation['bookmark'])
  184. // . ' SET dbase = \'' . PMA_sqlAddslashes($target_db) . '\''
  185. // . ' WHERE dbase = \'' . PMA_sqlAddslashes($source_db) . '\'';
  186. // $rmv_rs = PMA_query_as_cu($remove_query);
  187. // unset($rmv_query);
  188. // }
  189. if ($cfgRelation['displaywork']) {
  190. $table_query = 'UPDATE ' . PMA_backquote($cfgRelation['table_info'])
  191. . ' SET db_name = \'' . PMA_sqlAddslashes($target_db) . '\', '
  192. . ' table_name = \'' . PMA_sqlAddslashes($target_table) . '\''
  193. . ' WHERE db_name = \'' . PMA_sqlAddslashes($source_db) . '\''
  194. . ' AND table_name = \'' . PMA_sqlAddslashes($source_table) . '\'';
  195. $tb_rs = PMA_query_as_cu($table_query);
  196. unset($table_query);
  197. unset($tb_rs);
  198. }
  199. if ($cfgRelation['relwork']) {
  200. $table_query = 'UPDATE ' . PMA_backquote($cfgRelation['relation'])
  201. . ' SET foreign_table = \'' . PMA_sqlAddslashes($target_table) . '\','
  202. . ' foreign_db = \'' . PMA_sqlAddslashes($target_db) . '\''
  203. . ' WHERE foreign_db = \'' . PMA_sqlAddslashes($source_db) . '\''
  204. . ' AND foreign_table = \'' . PMA_sqlAddslashes($source_table) . '\'';
  205. $tb_rs = PMA_query_as_cu($table_query);
  206. unset($table_query);
  207. unset($tb_rs);
  208. $table_query = 'UPDATE ' . PMA_backquote($cfgRelation['relation'])
  209. . ' SET master_table = \'' . PMA_sqlAddslashes($target_table) . '\','
  210. . ' master_db = \'' . PMA_sqlAddslashes($target_db) . '\''
  211. . ' WHERE master_db = \'' . PMA_sqlAddslashes($source_db) . '\''
  212. . ' AND master_table = \'' . PMA_sqlAddslashes($source_table) . '\'';
  213. $tb_rs = PMA_query_as_cu($table_query);
  214. unset($table_query);
  215. unset($tb_rs);
  216. }
  217. // garvin: [TODO] Can't get moving PDFs the right way. The page numbers always
  218. // get screwed up independently from duplication because the numbers do not
  219. // seem to be stored on a per-database basis. Would the author of pdf support
  220. // please have a look at it?
  221. if ($cfgRelation['pdfwork']) {
  222. $table_query = 'UPDATE ' . PMA_backquote($cfgRelation['table_coords'])
  223. . ' SET table_name = \'' . PMA_sqlAddslashes($target_table) . '\','
  224. . ' db_name = \'' . PMA_sqlAddslashes($target_db) . '\''
  225. . ' WHERE db_name = \'' . PMA_sqlAddslashes($source_db) . '\''
  226. . ' AND table_name = \'' . PMA_sqlAddslashes($source_table) . '\'';
  227. $tb_rs = PMA_query_as_cu($table_query);
  228. unset($table_query);
  229. unset($tb_rs);
  230. /*
  231. $pdf_query = 'SELECT pdf_page_number '
  232. . ' FROM ' . PMA_backquote($cfgRelation['table_coords'])
  233. . ' WHERE db_name = \'' . PMA_sqlAddslashes($target_db) . '\''
  234. . ' AND table_name = \'' . PMA_sqlAddslashes($target_table) . '\'';
  235. $pdf_rs = PMA_query_as_cu($pdf_query);
  236. while ($pdf_copy_row = PMA_DBI_fetch_assoc($pdf_rs)) {
  237. $table_query = 'UPDATE ' . PMA_backquote($cfgRelation['pdf_pages'])
  238. . ' SET db_name = \'' . PMA_sqlAddslashes($target_db) . '\''
  239. . ' WHERE db_name = \'' . PMA_sqlAddslashes($source_db) . '\''
  240. . ' AND page_nr = \'' . PMA_sqlAddslashes($pdf_copy_row['pdf_page_number']) . '\'';
  241. $tb_rs = PMA_query_as_cu($table_query);
  242. unset($table_query);
  243. unset($tb_rs);
  244. }
  245. */
  246. }
  247. $sql_query .= "\n\n" . $sql_drop_table . ';';
  248. } else {
  249. // garvin: Create new entries as duplicates from old PMA DBs
  250. if ($what != 'dataonly' && !isset($maintain_relations)) {
  251. if ($cfgRelation['commwork']) {
  252. // Get all comments and MIME-Types for current table
  253. $comments_copy_query = 'SELECT
  254. column_name, ' . PMA_backquote('comment') . ($cfgRelation['mimework'] ? ', mimetype, transformation, transformation_options' : '') . '
  255. FROM ' . PMA_backquote($cfgRelation['column_info']) . '
  256. WHERE
  257. db_name = \'' . PMA_sqlAddslashes($source_db) . '\' AND
  258. table_name = \'' . PMA_sqlAddslashes($source_table) . '\'';
  259. $comments_copy_rs = PMA_query_as_cu($comments_copy_query);
  260. // Write every comment as new copied entry. [MIME]
  261. while ($comments_copy_row = PMA_DBI_fetch_assoc($comments_copy_rs)) {
  262. $new_comment_query = 'REPLACE INTO ' . PMA_backquote($cfgRelation['column_info'])
  263. . ' (db_name, table_name, column_name, ' . PMA_backquote('comment') . ($cfgRelation['mimework'] ? ', mimetype, transformation, transformation_options' : '') . ') '
  264. . ' VALUES('
  265. . '\'' . PMA_sqlAddslashes($target_db) . '\','
  266. . '\'' . PMA_sqlAddslashes($target_table) . '\','
  267. . '\'' . PMA_sqlAddslashes($comments_copy_row['column_name']) . '\''
  268. . ($cfgRelation['mimework'] ? ',\'' . PMA_sqlAddslashes($comments_copy_row['comment']) . '\','
  269. . '\'' . PMA_sqlAddslashes($comments_copy_row['mimetype']) . '\','
  270. . '\'' . PMA_sqlAddslashes($comments_copy_row['transformation']) . '\','
  271. . '\'' . PMA_sqlAddslashes($comments_copy_row['transformation_options']) . '\'' : '')
  272. . ')';
  273. $new_comment_rs = PMA_query_as_cu($new_comment_query);
  274. } // end while
  275. }
  276. // duplicating the bookmarks must not be done here, but
  277. // just once per db
  278. $get_fields = array('display_field');
  279. $where_fields = array('db_name' => $source_db, 'table_name' => $source_table);
  280. $new_fields = array('db_name' => $target_db, 'table_name' => $target_table);
  281. PMA_duplicate_table_info('displaywork', 'table_info', $get_fields, $where_fields, $new_fields);
  282. $get_fields = array('master_field', 'foreign_db', 'foreign_table', 'foreign_field');
  283. $where_fields = array('master_db' => $source_db, 'master_table' => $source_table);
  284. $new_fields = array('master_db' => $target_db, 'master_table' => $target_table);
  285. PMA_duplicate_table_info('relwork', 'relation', $get_fields, $where_fields, $new_fields);
  286. $get_fields = array('foreign_field', 'master_db', 'master_table', 'master_field');
  287. $where_fields = array('foreign_db' => $source_db, 'foreign_table' => $source_table);
  288. $new_fields = array('foreign_db' => $target_db, 'foreign_table' => $target_table);
  289. PMA_duplicate_table_info('relwork', 'relation', $get_fields, $where_fields, $new_fields);
  290. // garvin: [TODO] Can't get duplicating PDFs the right way. The page numbers always
  291. // get screwed up independently from duplication because the numbers do not
  292. // seem to be stored on a per-database basis. Would the author of pdf support
  293. // please have a look at it?
  294. /*
  295. $get_fields = array('page_descr');
  296. $where_fields = array('db_name' => $source_db);
  297. $new_fields = array('db_name' => $target_db);
  298. $last_id = PMA_duplicate_table_info('pdfwork', 'pdf_pages', $get_fields, $where_fields, $new_fields);
  299. if (isset($last_id) && $last_id >= 0) {
  300. $get_fields = array('x', 'y');
  301. $where_fields = array('db_name' => $source_db, 'table_name' => $source_table);
  302. $new_fields = array('db_name' => $target_db, 'table_name' => $target_table, 'pdf_page_number' => $last_id);
  303. PMA_duplicate_table_info('pdfwork', 'table_coords', $get_fields, $where_fields, $new_fields);
  304. }
  305. */
  306. }
  307. }
  308. }
  309. ?>