Rev Author Line No. Line
250 kaklik 1 <?php
2 /* $Id: db_operations.php,v 2.29 2006/01/17 17:02:28 cybot_tm Exp $ */
3 // vim: expandtab sw=4 ts=4 sts=4:
4  
5 /**
6 * handles miscellaneous db operations:
7 * - move/rename
8 * - copy
9 * - changing collation
10 * - changing comment
11 * - adding tables
12 * - viewing PDF schemas
13 */
14  
15 /**
16 * requirements
17 */
18 require_once('./libraries/common.lib.php');
19 require_once('./libraries/mysql_charsets.lib.php');
20  
21 /**
22 * Rename/move or copy database
23 */
24 if (isset($db) &&
25 ((isset($db_rename) && $db_rename == 'true') ||
26 (isset($db_copy) && $db_copy == 'true'))) {
27  
28 require_once('./libraries/tbl_move_copy.php');
29  
30 if (isset($db_rename) && $db_rename == 'true') {
31 $move = TRUE;
32 } else {
33 $move = FALSE;
34 }
35  
36 if (!isset($newname) || !strlen($newname)) {
37 $message = $strDatabaseEmpty;
38 } else {
39 if ($move ||
40 (isset($create_database_before_copying) && $create_database_before_copying)) {
41 $local_query = 'CREATE DATABASE ' . PMA_backquote($newname);
42 if (isset($db_collation)) {
43 $local_query .= ' DEFAULT' . PMA_generateCharsetQueryPart($db_collation);
44 }
45 $local_query .= ';';
46 $sql_query = $local_query;
47 PMA_DBI_query($local_query);
48 }
49  
50 $tables_full = PMA_DBI_get_tables_full($db);
51 foreach ($tables_full as $table => $tmp) {
52 $back = $sql_query;
53 $sql_query = '';
54  
55 // value of $what for this table only
56 $this_what = $what;
57  
58 if (!isset($tables_full[$table]['Engine'])) {
59 $tables_full[$table]['Engine'] = $tables_full[$table]['Type'];
60 }
61 // do not copy the data from a Merge table
62 // note: on the calling FORM, 'data' means 'structure and data'
63 if ($tables_full[$table]['Engine'] == 'MRG_MyISAM') {
64 if ($this_what == 'data') {
65 $this_what = 'structure';
66 }
67 if ($this_what == 'dataonly') {
68 $this_what = 'nocopy';
69 }
70 }
71  
72 if ($this_what != 'nocopy') {
73 PMA_table_move_copy($db, $table, $newname, $table,
74 isset($this_what) ? $this_what : 'data', $move);
75 }
76  
77 $sql_query = $back . $sql_query;
78 }
79 unset($table);
80  
81 // Duplicate the bookmarks for this db (done once for each db)
82 if ($db != $newname) {
83 $get_fields = array('user', 'label', 'query');
84 $where_fields = array('dbase' => $db);
85 $new_fields = array('dbase' => $newname);
86 PMA_duplicate_table_info('bookmarkwork', 'bookmark', $get_fields,
87 $where_fields, $new_fields);
88 }
89  
90 if ($move) {
91 // cleanup pmadb stuff for this db
92 require_once('./libraries/relation_cleanup.lib.php');
93 PMA_relationsCleanupDatabase($db);
94  
95 $local_query = 'DROP DATABASE ' . PMA_backquote($db) . ';';
96 $sql_query .= "\n" . $local_query;
97 PMA_DBI_query($local_query);
98 $message = sprintf($strRenameDatabaseOK, htmlspecialchars($db),
99 htmlspecialchars($newname));
100 } else {
101 $message = sprintf($strCopyDatabaseOK, htmlspecialchars($db),
102 htmlspecialchars($newname));
103 }
104 $reload = TRUE;
105  
106 /* Change database to be used */
107 if ($move) {
108 $db = $newname;
109 } else {
110 if (isset($switch_to_new) && $switch_to_new == 'true') {
111 PMA_setCookie( 'pma_switch_to_new', 'true' );
112 $db = $newname;
113 } else {
114 PMA_setCookie( 'pma_switch_to_new', '' );
115 }
116 }
117 }
118 }
119 /**
120 * Settings for relations stuff
121 */
122  
123 require_once('./libraries/relation.lib.php');
124 $cfgRelation = PMA_getRelationsParam();
125  
126 /**
127 * Check if comments were updated
128 * (must be done before displaying the menu tabs)
129 */
130 if ($cfgRelation['commwork'] && isset($db_comment) && $db_comment == 'true') {
131 PMA_SetComment($db, '', '(db_comment)', $comment);
132 }
133  
134 /**
135 * Prepares the tables list if the user where not redirected to this script
136 * because there is no table in the database ($is_info is TRUE)
137 */
138 if (empty($is_info)) {
139 require('./libraries/db_details_common.inc.php');
140 $url_query .= '&amp;goto=db_operations.php';
141  
142 // Gets the database structure
143 $sub_part = '_structure';
144 require('./libraries/db_details_db_info.inc.php');
145 echo "\n";
146 }
147  
148 if (PMA_MYSQL_INT_VERSION >= 40101) {
149 $db_collation = PMA_getDbCollation($db);
150 }
151 if (PMA_MYSQL_INT_VERSION < 50002
152 || (PMA_MYSQL_INT_VERSION >= 50002 && $db != 'information_schema')) {
153 $is_information_schema = FALSE;
154 } else {
155 $is_information_schema = TRUE;
156 }
157  
158 if (!$is_information_schema) {
159  
160 require('./libraries/display_create_table.lib.php');
161  
162 if ($cfgRelation['commwork']) {
163 /**
164 * database comment
165 */
166 ?>
167 <form method="post" action="db_operations.php">
168 <?php echo PMA_generate_common_hidden_inputs($db); ?>
169 <input type="hidden" name="db_comment" value="true" />
170 <fieldset>
171 <legend>
172 <?php
173 if ($cfg['PropertiesIconic']) {
174 echo '<img class="icon" src="' . $pmaThemeImage . 'b_comment.png"'
175 .' alt="" border="0" width="16" height="16" hspace="2" align="middle" />';
176 }
177 echo $strDBComment;
178 $comment = PMA_getComments($db);
179 ?>
180 </legend>
181 <input type="text" name="comment" class="textfield" size="30"
182 value="<?php
183 echo (isset($comment) && is_array($comment)
184 ? htmlspecialchars(implode(' ', $comment))
185 : ''); ?>" />
186 <input type="submit" value="<?php echo $strGo; ?>" />
187 </fieldset>
188 </form>
189 <?php
190 }
191 /**
192 * rename database
193 */
194 ?>
195 <form method="post" action="db_operations.php"
196 onsubmit="return emptyFormElements(this, 'newname')">
197 <input type="hidden" name="what" value="data" />
198 <input type="hidden" name="db_rename" value="true" />
199 <?php echo PMA_generate_common_hidden_inputs($db); ?>
200 <fieldset>
201 <legend>
202 <?php
203 if ($cfg['PropertiesIconic']) {
204 echo '<img class="icon" src="' . $pmaThemeImage . 'b_edit.png"'
205 .' alt="" width="16" height="16" />';
206 }
207 echo $strDBRename . ':';
208 ?>
209 </legend>
210 <input type="text" name="newname" size="30" class="textfield" value="" />
211 <input type="submit" value="<?php echo $strGo; ?>" />
212 </fieldset>
213 </form>
214  
215 <?php
216 /**
217 * Copy database
218 */
219 ?>
220 <form method="post" action="db_operations.php"
221 onsubmit="return emptyFormElements(this, 'newname')">
222 <?php
223 if (isset($db_collation)) {
224 echo '<input type="hidden" name="db_collation" value="' . $db_collation
225 .'" />' . "\n";
226 }
227 echo '<input type="hidden" name="db_copy" value="true" />' . "\n";
228 echo PMA_generate_common_hidden_inputs($db);
229 ?>
230 <fieldset>
231 <legend>
232 <?php
233 if ($cfg['PropertiesIconic']) {
234 echo '<img class="icon" src="' . $pmaThemeImage . 'b_edit.png"'
235 .' alt="" width="16" height="16" />';
236 }
237 echo $strDBCopy . ':';
238 ?>
239 </legend>
240 <input type="text" name="newname" size="30" class="textfield" value="" /><br />
241 <input type="radio" name="what" value="structure"
242 id="radio_copy_structure" style="vertical-align: middle" />
243 <label for="radio_copy_structure"><?php echo $strStrucOnly; ?></label><br />
244 <input type="radio" name="what" value="data" id="radio_copy_data"
245 checked="checked" style="vertical-align: middle" />
246 <label for="radio_copy_data"><?php echo $strStrucData; ?></label><br />
247 <input type="radio" name="what" value="dataonly"
248 id="radio_copy_dataonly" style="vertical-align: middle" />
249 <label for="radio_copy_dataonly"><?php echo $strDataOnly; ?></label><br />
250  
251 <input type="checkbox" name="create_database_before_copying" value="1"
252 id="checkbox_create_database_before_copying"
253 style="vertical-align: middle" checked="checked" />
254 <label for="checkbox_create_database_before_copying">
255 <?php echo $strCreateDatabaseBeforeCopying; ?></label><br />
256 <input type="checkbox" name="drop_if_exists" value="true"
257 id="checkbox_drop" style="vertical-align: middle" />
258 <label for="checkbox_drop"><?php echo $strStrucDrop; ?></label><br />
259 <input type="checkbox" name="sql_auto_increment" value="1"
260 id="checkbox_auto_increment" style="vertical-align: middle" />
261 <label for="checkbox_auto_increment">
262 <?php echo $strAddAutoIncrement; ?></label><br />
263 <input type="checkbox" name="constraints" value="1"
264 id="checkbox_constraints" style="vertical-align: middle" />
265 <label for="checkbox_constraints">
266 <?php echo $strAddConstraints; ?></label><br />
267 <?php
268 if (isset($_COOKIE) && isset($_COOKIE['pma_switch_to_new'])
269 && $_COOKIE['pma_switch_to_new'] == 'true') {
270 $pma_switch_to_new = 'true';
271 }
272 ?>
273 <input type="checkbox" name="switch_to_new" value="true"
274 id="checkbox_switch"
275 <?php echo ((isset($pma_switch_to_new) && $pma_switch_to_new == 'true') ? ' checked="checked"' : ''); ?>
276 style="vertical-align: middle" />
277 <label for="checkbox_switch"><?php echo $strSwitchToDatabase; ?></label>
278 </fieldset>
279 <fieldset class="tblFooters">
280 <input type="submit" name="submit_copy" value="<?php echo $strGo; ?>" />
281 </fieldset>
282 </form>
283  
284 <?php
285 /**
286 * Change database charset
287 */
288 if (PMA_MYSQL_INT_VERSION >= 40101) {
289 // MySQL supports setting default charsets / collations for databases since
290 // version 4.1.1.
291 echo '<form method="post" action="./db_operations.php">' . "\n"
292 . PMA_generate_common_hidden_inputs($db, $table)
293 . '<fieldset>' . "\n"
294 . ' <legend>';
295 if ($cfg['PropertiesIconic']) {
296 echo '<img class="icon" src="' . $pmaThemeImage . 's_asci.png"'
297 .' alt="" width="16" height="16" />';
298 }
299 echo ' <label for="select_db_collation">' . $strCollation . ':</label>' . "\n"
300 . ' </legend>' . "\n"
301 . PMA_generateCharsetDropdownBox(PMA_CSDROPDOWN_COLLATION,
302 'db_collation', 'select_db_collation', $db_collation, FALSE, 3)
303 . ' <input type="submit" name="submitcollation"'
304 . ' value="' . $strGo . '" style="vertical-align: middle" />' . "\n"
305 . '</fieldset>' . "\n"
306 . '</form>' . "\n";
307 }
308  
309 if ( $num_tables > 0
310 && !$cfgRelation['allworks'] && $cfg['PmaNoRelation_DisableWarning'] == FALSE) {
311 echo '<div class="error"><h1>' . $strError . '</h1>'
312 . sprintf( $strRelationNotWorking,
313 '<a href="' . $cfg['PmaAbsoluteUri'] . 'chk_rel.php?' . $url_query . '">',
314 '</a>')
315 . '</div>';
316 } // end if
317 } // end if (!$is_information_schema)
318  
319  
320 // not sure about leaving the PDF dialog for information_schema
321 if ($num_tables > 0) {
322 $takeaway = $url_query . '&amp;table=' . urlencode($table);
323 }
324  
325 if ($cfgRelation['pdfwork'] && $num_tables > 0) { ?>
326 <!-- Work on PDF Pages -->
327  
328 <?php
329 // We only show this if we find something in the new pdf_pages table
330  
331 $test_query = '
332 SELECT *
333 FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['pdf_pages']) . '
334 WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\'';
335 $test_rs = PMA_query_as_cu($test_query, null, PMA_DBI_QUERY_STORE);
336  
337 if ($test_rs && PMA_DBI_num_rows($test_rs) > 0) { ?>
338 <!-- PDF schema -->
339 <form method="post" action="pdf_schema.php">
340 <fieldset>
341 <legend>
342 <?php
343 echo PMA_generate_common_hidden_inputs($db);
344 if ($cfg['PropertiesIconic']) {
345 echo '<img class="icon" src="' . $pmaThemeImage . 'b_view.png"'
346 .' alt="" width="16" height="16" />';
347 }
348 echo $strDisplayPDF;
349 ?>:
350 </legend>
351 <label for="pdf_page_number_opt"><?php echo $strPageNumber; ?></label>
352 <select name="pdf_page_number" id="pdf_page_number_opt">
353 <?php
354 while ($pages = @PMA_DBI_fetch_assoc($test_rs)) {
355 echo ' <option value="' . $pages['page_nr'] . '">'
356 . $pages['page_nr'] . ': ' . $pages['page_descr'] . '</option>' . "\n";
357 } // end while
358 PMA_DBI_free_result($test_rs);
359 unset($test_rs);
360 ?>
361 </select><br />
362  
363 <input type="checkbox" name="show_grid" id="show_grid_opt" />
364 <label for="show_grid_opt"><?php echo $strShowGrid; ?></label><br />
365 <input type="checkbox" name="show_color" id="show_color_opt"
366 checked="checked" />
367 <label for="show_color_opt"><?php echo $strShowColor; ?></label><br />
368 <input type="checkbox" name="show_table_dimension" id="show_table_dim_opt" />
369 <label for="show_table_dim_opt"><?php echo $strShowTableDimension; ?>
370 </label><br />
371 <input type="checkbox" name="all_tab_same_wide" id="all_tab_same_wide" />
372 <label for="all_tab_same_wide"><?php echo $strAllTableSameWidth; ?>
373 </label><br />
374 <input type="checkbox" name="with_doc" id="with_doc" checked="checked" />
375 <label for="with_doc"><?php echo $strDataDict; ?></label><br />
376  
377 <label for="orientation_opt"><?php echo $strShowDatadictAs; ?></label>
378 <select name="orientation" id="orientation_opt">
379 <option value="L"><?php echo $strLandscape;?></option>
380 <option value="P"><?php echo $strPortrait;?></option>
381 </select><br />
382  
383 <label for="paper_opt"><?php echo $strPaperSize; ?></label>
384 <select name="paper" id="paper_opt">
385 <?php
386 foreach ($cfg['PDFPageSizes'] AS $key => $val) {
387 echo '<option value="' . $val . '"';
388 if ($val == $cfg['PDFDefaultPageSize']) {
389 echo ' selected="selected"';
390 }
391 echo ' >' . $val . '</option>' . "\n";
392 }
393 ?>
394 </select>
395 </fieldset>
396 <fieldset class="tblFooters">
397 <input type="submit" value="<?php echo $strGo; ?>" />
398 </fieldset>
399 </form>
400 <?php
401 } // end if
402 ?>
403 <ul>
404 <li>
405 <?php
406 echo '<a href="pdf_pages.php?' . $takeaway . '">';
407 if ($cfg['PropertiesIconic']) {
408 echo '<img class="icon" src="' . $pmaThemeImage . 'b_edit.png"'
409 .' alt="" width="16" height="16" />';
410 }
411 echo $strEditPDFPages . '</a>';
412 ?>
413 </li>
414 </ul>
415 <?php
416 } // end if
417  
418 if ( $num_tables > 0
419 && $cfgRelation['relwork'] && $cfgRelation['commwork']
420 && isset($cfg['docSQLDir']) && !empty($cfg['docSQLDir']) ) {
421 /**
422 * import docSQL files
423 */
424 echo '<ul>' . "\n"
425 .'<li><a href="db_details_importdocsql.php?' . $takeaway . '">' . "\n";
426 if ($cfg['PropertiesIconic']) {
427 echo '<img class="icon" src="' . $pmaThemeImage . 'b_docsql.png"'
428 .' alt="" width="16" height="16" />';
429 }
430 echo $strImportDocSQL . '</a></li>' . "\n"
431 .'</ul>';
432 }
433  
434 /**
435 * Displays the footer
436 */
437 require_once('./libraries/footer.inc.php');
438 ?>