Translated using Weblate (Turkish)
[phpmyadmin.git] / tbl_operations.php
blobd0702987536a664509970b5c06c1543274c2d26d
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Various table operations
6 * @package PhpMyAdmin
7 */
9 /**
12 require_once 'libraries/common.inc.php';
14 /**
15 * functions implementation for this script
17 require_once 'libraries/operations.lib.php';
19 $pma_table = new PMA_Table($GLOBALS['table'], $GLOBALS['db']);
21 /**
22 * Load JavaScript files
24 $response = PMA_Response::getInstance();
25 $header = $response->getHeader();
26 $scripts = $header->getScripts();
27 $scripts->addFile('functions.js');
28 $scripts->addFile('tbl_operations.js');
30 /**
31 * Runs common work
33 require 'libraries/tbl_common.inc.php';
34 $url_query .= '&amp;goto=tbl_operations.php&amp;back=tbl_operations.php';
35 $url_params['goto'] = $url_params['back'] = 'tbl_operations.php';
37 /**
38 * Gets relation settings
40 $cfgRelation = PMA_getRelationsParam();
42 /**
43 * Gets available MySQL charsets and storage engines
45 require_once 'libraries/mysql_charsets.inc.php';
46 require_once 'libraries/StorageEngine.class.php';
48 /**
49 * Class for partition management
51 require_once 'libraries/Partition.class.php';
53 // reselect current db (needed in some cases probably due to
54 // the calling of relation.lib.php)
55 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
57 /**
58 * Gets tables information
60 require 'libraries/tbl_info.inc.php';
62 // define some variables here, for improved syntax in the conditionals
63 $is_myisam_or_aria = $is_isam = $is_innodb = $is_berkeleydb = false;
64 $is_aria = $is_pbxt = false;
65 // set initial value of these variables, based on the current table engine
66 list($is_myisam_or_aria, $is_innodb, $is_isam,
67 $is_berkeleydb, $is_aria, $is_pbxt
68 ) = PMA_setGlobalVariablesForEngine($tbl_storage_engine);
70 if ($is_aria) {
71 // the value for transactional can be implicit
72 // (no create option found, in this case it means 1)
73 // or explicit (option found with a value of 0 or 1)
74 // ($transactional may have been set by libraries/tbl_info.inc.php,
75 // from the $create_options)
76 $transactional = (isset($transactional) && $transactional == '0')
77 ? '0'
78 : '1';
79 $page_checksum = (isset($page_checksum)) ? $page_checksum : '';
82 $reread_info = false;
83 $table_alters = array();
85 /** @var PMA_String $pmaString */
86 $pmaString = $GLOBALS['PMA_String'];
88 /**
89 * If the table has to be moved to some other database
91 if (isset($_REQUEST['submit_move']) || isset($_REQUEST['submit_copy'])) {
92 //$_message = '';
93 PMA_moveOrCopyTable($db, $table);
94 // This was ended in an Ajax call
95 exit;
97 /**
98 * If the table has to be maintained
100 if (isset($_REQUEST['table_maintenance'])) {
101 include_once 'sql.php';
102 unset($result);
105 * Updates table comment, type and options if required
107 if (isset($_REQUEST['submitoptions'])) {
108 $_message = '';
109 $warning_messages = array();
111 if (isset($_REQUEST['new_name'])) {
112 // Get original names before rename operation
113 $oldTable = $pma_table->getName();
114 $oldDb = $pma_table->getDbName();
116 if ($pma_table->rename($_REQUEST['new_name'])) {
117 if (isset($_REQUEST['adjust_privileges'])
118 && ! empty($_REQUEST['adjust_privileges'])
120 PMA_AdjustPrivileges_renameOrMoveTable(
121 $oldDb, $oldTable, $_REQUEST['db'], $_REQUEST['new_name']
125 // Reselect the original DB
126 $GLOBALS['db'] = $oldDb;
127 $GLOBALS['dbi']->selectDb($oldDb);
129 $_message .= $pma_table->getLastMessage();
130 $result = true;
131 $GLOBALS['table'] = $pma_table->getName();
132 $reread_info = true;
133 $reload = true;
134 } else {
135 $_message .= $pma_table->getLastError();
136 $result = false;
140 if (! empty($_REQUEST['new_tbl_storage_engine'])
141 && /*overload*/mb_strtolower($_REQUEST['new_tbl_storage_engine']) !== /*overload*/mb_strtolower($tbl_storage_engine)
143 $new_tbl_storage_engine = $_REQUEST['new_tbl_storage_engine'];
144 // reset the globals for the new engine
145 list($is_myisam_or_aria, $is_innodb, $is_isam,
146 $is_berkeleydb, $is_aria, $is_pbxt
147 ) = PMA_setGlobalVariablesForEngine($new_tbl_storage_engine);
149 if ($is_aria) {
150 $transactional = (isset($transactional) && $transactional == '0')
151 ? '0'
152 : '1';
153 $page_checksum = (isset($page_checksum)) ? $page_checksum : '';
155 } else {
156 $new_tbl_storage_engine = '';
159 $table_alters = PMA_getTableAltersArray(
160 $is_myisam_or_aria, $is_isam, $pack_keys,
161 (empty($checksum) ? '0' : '1'),
162 $is_aria,
163 ((isset($page_checksum)) ? $page_checksum : ''),
164 (empty($delay_key_write) ? '0' : '1'),
165 $is_innodb, $is_pbxt, $row_format,
166 $new_tbl_storage_engine,
167 ((isset($transactional) && $transactional == '0') ? '0' : '1'),
168 $tbl_collation
171 if (count($table_alters) > 0) {
172 $sql_query = 'ALTER TABLE '
173 . PMA_Util::backquote($GLOBALS['table']);
174 $sql_query .= "\r\n" . implode("\r\n", $table_alters);
175 $sql_query .= ';';
176 $result .= $GLOBALS['dbi']->query($sql_query) ? true : false;
177 $reread_info = true;
178 unset($table_alters);
179 $warning_messages = PMA_getWarningMessagesArray();
182 if (isset($_REQUEST['tbl_collation'])
183 && ! empty($_REQUEST['tbl_collation'])
184 && isset($_REQUEST['change_all_collations'])
185 && ! empty($_REQUEST['change_all_collations'])
187 PMA_changeAllColumnsCollation(
188 $GLOBALS['db'], $GLOBALS['table'], $_REQUEST['tbl_collation']
193 * Reordering the table has been requested by the user
195 if (isset($_REQUEST['submitorderby']) && ! empty($_REQUEST['order_field'])) {
196 list($sql_query, $result) = PMA_getQueryAndResultForReorderingTable();
197 } // end if
200 * A partition operation has been requested by the user
202 $sql_query = '';
204 if (isset($_REQUEST['submit_partition'])
205 && ! empty($_REQUEST['partition_operation'])
207 list($sql_query, $result) = PMA_getQueryAndResultForPartition();
208 } // end if
210 if ($reread_info) {
211 // to avoid showing the old value (for example the AUTO_INCREMENT) after
212 // a change, clear the cache
213 PMA_Table::$cache = array();
214 $page_checksum = $checksum = $delay_key_write = 0;
215 include 'libraries/tbl_info.inc.php';
217 unset($reread_info);
219 if (isset($result) && empty($message_to_show)) {
220 // set to success by default, because result set could be empty
221 // (for example, a table rename)
222 $_type = 'success';
223 if (empty($_message)) {
224 $_message = $result
225 ? PMA_Message::success(
226 __('Your SQL query has been executed successfully.')
228 : PMA_Message::error(__('Error'));
229 // $result should exist, regardless of $_message
230 $_type = $result ? 'success' : 'error';
232 if (isset($GLOBALS['ajax_request'])
233 && $GLOBALS['ajax_request'] == true
235 $response = PMA_Response::getInstance();
236 $response->isSuccess($_message->isSuccess());
237 $response->addJSON('message', $_message);
238 $response->addJSON(
239 'sql_query', PMA_Util::getMessage(null, $sql_query)
241 exit;
244 if (! empty($warning_messages)) {
245 $_message = new PMA_Message;
246 $_message->addMessages($warning_messages);
247 $_message->isError(true);
248 if ($GLOBALS['ajax_request'] == true) {
249 $response = PMA_Response::getInstance();
250 $response->isSuccess(false);
251 $response->addJSON('message', $_message);
252 exit;
254 unset($warning_messages);
257 $response->addHTML(
258 PMA_Util::getMessage($_message, $sql_query, $_type)
260 unset($_message, $_type);
263 $url_params['goto']
264 = $url_params['back']
265 = 'tbl_operations.php';
268 * Get columns names
270 $columns = $GLOBALS['dbi']->getColumns($GLOBALS['db'], $GLOBALS['table']);
273 * Displays the page
275 $response->addHTML('<div id="boxContainer" data-box-width="300">');
278 * Order the table
280 $hideOrderTable = false;
281 // `ALTER TABLE ORDER BY` does not make sense for InnoDB tables that contain
282 // a user-defined clustered index (PRIMARY KEY or NOT NULL UNIQUE index).
283 // InnoDB always orders table rows according to such an index if one is present.
284 if ($tbl_storage_engine == 'INNODB') {
285 include_once 'libraries/Index.class.php';
286 $indexes = PMA_Index::getFromTable($GLOBALS['table'], $GLOBALS['db']);
287 foreach ($indexes as $name => $idx) {
288 if ($name == 'PRIMARY') {
289 $hideOrderTable = true;
290 break;
291 } elseif (! $idx->getNonUnique()) {
292 $notNull = true;
293 foreach ($idx->getColumns() as $column) {
294 if ($column->getNull()) {
295 $notNull = false;
296 break;
299 if ($notNull) {
300 $hideOrderTable = true;
301 break;
306 if (! $hideOrderTable) {
307 $response->addHTML(PMA_getHtmlForOrderTheTable($columns));
311 * Move table
313 $response->addHTML(PMA_getHtmlForMoveTable());
315 if (/*overload*/mb_strstr($show_comment, '; InnoDB free') === false) {
316 if (/*overload*/mb_strstr($show_comment, 'InnoDB free') === false) {
317 // only user entered comment
318 $comment = $show_comment;
319 } else {
320 // here we have just InnoDB generated part
321 $comment = '';
323 } else {
324 // remove InnoDB comment from end, just the minimal part (*? is non greedy)
325 $comment = preg_replace('@; InnoDB free:.*?$@', '', $show_comment);
328 // PACK_KEYS: MyISAM or ISAM
329 // DELAY_KEY_WRITE, CHECKSUM, : MyISAM only
330 // AUTO_INCREMENT: MyISAM and InnoDB since 5.0.3, PBXT
332 // Here should be version check for InnoDB, however it is supported
333 // in >5.0.4, >4.1.12 and >4.0.11, so I decided not to
334 // check for version
336 $response->addHTML(
337 PMA_getTableOptionDiv(
338 $comment, $tbl_collation, $tbl_storage_engine,
339 $is_myisam_or_aria, $is_isam, $pack_keys,
340 $auto_increment,
341 (empty($delay_key_write) ? '0' : '1'),
342 ((isset($transactional) && $transactional == '0') ? '0' : '1'),
343 ((isset($page_checksum)) ? $page_checksum : ''),
344 $is_innodb, $is_pbxt, $is_aria, (empty($checksum) ? '0' : '1')
349 * Copy table
351 $response->addHTML(PMA_getHtmlForCopytable());
354 * Table maintenance
356 $response->addHTML(
357 PMA_getHtmlForTableMaintenance(
358 $is_myisam_or_aria,
359 $is_innodb,
360 $is_berkeleydb,
361 $url_params
365 if (! (isset($db_is_system_schema) && $db_is_system_schema)) {
366 $truncate_table_url_params = array();
367 $drop_table_url_params = array();
369 if (! $tbl_is_view
370 && ! (isset($db_is_system_schema) && $db_is_system_schema)
372 $this_sql_query = 'TRUNCATE TABLE '
373 . PMA_Util::backquote($GLOBALS['table']);
374 $truncate_table_url_params = array_merge(
375 $url_params,
376 array(
377 'sql_query' => $this_sql_query,
378 'goto' => 'tbl_structure.php',
379 'reload' => '1',
380 'message_to_show' => sprintf(
381 __('Table %s has been emptied.'),
382 htmlspecialchars($table)
387 if (! (isset($db_is_system_schema) && $db_is_system_schema)) {
388 $this_sql_query = 'DROP TABLE '
389 . PMA_Util::backquote($GLOBALS['table']);
390 $drop_table_url_params = array_merge(
391 $url_params,
392 array(
393 'sql_query' => $this_sql_query,
394 'goto' => 'db_operations.php',
395 'reload' => '1',
396 'purge' => '1',
397 'message_to_show' => sprintf(
398 ($tbl_is_view
399 ? __('View %s has been dropped.')
400 : __('Table %s has been dropped.')
402 htmlspecialchars($table)
404 // table name is needed to avoid running
405 // PMA_relationsCleanupDatabase() on the whole db later
406 'table' => $GLOBALS['table'],
410 $response->addHTML(
411 PMA_getHtmlForDeleteDataOrTable(
412 $truncate_table_url_params,
413 $drop_table_url_params
418 if (PMA_Partition::havePartitioning()) {
419 $partition_names = PMA_Partition::getPartitionNames($db, $table);
420 // show the Partition maintenance section only if we detect a partition
421 if (! is_null($partition_names[0])) {
422 $response->addHTML(
423 PMA_getHtmlForPartitionMaintenance($partition_names, $url_params)
425 } // end if
426 } // end if
427 unset($partition_names);
429 // Referential integrity check
430 // The Referential integrity check was intended for the non-InnoDB
431 // tables for which the relations are defined in pmadb
432 // so I assume that if the current table is InnoDB, I don't display
433 // this choice (InnoDB maintains integrity by itself)
435 if ($cfgRelation['relwork'] && ! $is_innodb) {
436 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
437 $foreign = PMA_getForeigners($GLOBALS['db'], $GLOBALS['table'], '', 'internal');
439 if (! empty($foreign)) {
440 $response->addHTML(
441 PMA_getHtmlForReferentialIntegrityCheck($foreign, $url_params)
443 } // end if ($foreign)
445 } // end if (!empty($cfg['Server']['relation']))
447 $response->addHTML('</div>');