Translated using Weblate (Ukrainian)
[phpmyadmin.git] / tbl_operations.php
blob54a6630bc85ca04267fe1062f32da1abd16329e0
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Various table operations
6 * @package PhpMyAdmin
7 */
8 use PhpMyAdmin\Index;
9 use PhpMyAdmin\Message;
10 use PhpMyAdmin\Partition;
11 use PhpMyAdmin\Operations;
12 use PhpMyAdmin\Relation;
13 use PhpMyAdmin\Response;
14 use PhpMyAdmin\Table;
15 use PhpMyAdmin\Util;
17 /**
20 require_once 'libraries/common.inc.php';
22 /**
23 * functions implementation for this script
25 require_once 'libraries/check_user_privileges.inc.php';
27 $pma_table = new Table($GLOBALS['table'], $GLOBALS['db']);
29 /**
30 * Load JavaScript files
32 $response = Response::getInstance();
33 $header = $response->getHeader();
34 $scripts = $header->getScripts();
35 $scripts->addFile('tbl_operations.js');
37 /**
38 * Runs common work
40 require 'libraries/tbl_common.inc.php';
41 $url_query .= '&amp;goto=tbl_operations.php&amp;back=tbl_operations.php';
42 $url_params['goto'] = $url_params['back'] = 'tbl_operations.php';
44 /**
45 * Gets relation settings
47 $cfgRelation = Relation::getRelationsParam();
49 // reselect current db (needed in some cases probably due to
50 // the calling of PhpMyAdmin\Relation)
51 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
53 /**
54 * Gets tables information
56 $pma_table = $GLOBALS['dbi']->getTable(
57 $GLOBALS['db'],
58 $GLOBALS['table']
60 $reread_info = $pma_table->getStatusInfo(null, false);
61 $GLOBALS['showtable'] = $pma_table->getStatusInfo(null, (isset($reread_info) && $reread_info ? true : false));
62 if ($pma_table->isView()) {
63 $tbl_is_view = true;
64 $tbl_storage_engine = __('View');
65 $show_comment = null;
66 } else {
67 $tbl_is_view = false;
68 $tbl_storage_engine = $pma_table->getStorageEngine();
69 $show_comment = $pma_table->getComment();
71 $tbl_collation = $pma_table->getCollation();
72 $table_info_num_rows = $pma_table->getNumRows();
73 $row_format = $pma_table->getRowFormat();
74 $auto_increment = $pma_table->getAutoIncrement();
75 $create_options = $pma_table->getCreateOptions();
77 // set initial value of these variables, based on the current table engine
78 if ($pma_table->isEngine('ARIA')) {
79 // the value for transactional can be implicit
80 // (no create option found, in this case it means 1)
81 // or explicit (option found with a value of 0 or 1)
82 // ($create_options['transactional'] may have been set by Table class,
83 // from the $create_options)
84 $create_options['transactional'] = (isset($create_options['transactional']) && $create_options['transactional'] == '0')
85 ? '0'
86 : '1';
87 $create_options['page_checksum'] = (isset($create_options['page_checksum'])) ? $create_options['page_checksum'] : '';
90 $pma_table = $GLOBALS['dbi']->getTable(
91 $GLOBALS['db'],
92 $GLOBALS['table']
94 $reread_info = false;
95 $table_alters = array();
97 /**
98 * If the table has to be moved to some other database
100 if (isset($_REQUEST['submit_move']) || isset($_REQUEST['submit_copy'])) {
101 //$_message = '';
102 Operations::moveOrCopyTable($db, $table);
103 // This was ended in an Ajax call
104 exit;
107 * If the table has to be maintained
109 if (isset($_REQUEST['table_maintenance'])) {
110 include_once 'sql.php';
111 unset($result);
114 * Updates table comment, type and options if required
116 if (isset($_REQUEST['submitoptions'])) {
117 $_message = '';
118 $warning_messages = array();
120 if (isset($_REQUEST['new_name'])) {
121 // Get original names before rename operation
122 $oldTable = $pma_table->getName();
123 $oldDb = $pma_table->getDbName();
125 if ($pma_table->rename($_REQUEST['new_name'])) {
126 if (isset($_REQUEST['adjust_privileges'])
127 && ! empty($_REQUEST['adjust_privileges'])
129 Operations::adjustPrivilegesRenameOrMoveTable(
130 $oldDb, $oldTable, $_REQUEST['db'], $_REQUEST['new_name']
134 // Reselect the original DB
135 $GLOBALS['db'] = $oldDb;
136 $GLOBALS['dbi']->selectDb($oldDb);
137 $_message .= $pma_table->getLastMessage();
138 $result = true;
139 $GLOBALS['table'] = $pma_table->getName();
140 $reread_info = true;
141 $reload = true;
142 } else {
143 $_message .= $pma_table->getLastError();
144 $result = false;
148 if (! empty($_REQUEST['new_tbl_storage_engine'])
149 && mb_strtoupper($_REQUEST['new_tbl_storage_engine']) !== $tbl_storage_engine
151 $new_tbl_storage_engine = mb_strtoupper($_REQUEST['new_tbl_storage_engine']);
153 if ($pma_table->isEngine('ARIA')) {
154 $create_options['transactional'] = (isset($create_options['transactional']) && $create_options['transactional'] == '0')
155 ? '0'
156 : '1';
157 $create_options['page_checksum'] = (isset($create_options['page_checksum'])) ? $create_options['page_checksum'] : '';
159 } else {
160 $new_tbl_storage_engine = '';
163 $row_format = (isset($create_options['row_format']))
164 ? $create_options['row_format']
165 : $pma_table->getRowFormat();
167 $table_alters = Operations::getTableAltersArray(
168 $pma_table,
169 $create_options['pack_keys'],
170 (empty($create_options['checksum']) ? '0' : '1'),
171 ((isset($create_options['page_checksum'])) ? $create_options['page_checksum'] : ''),
172 (empty($create_options['delay_key_write']) ? '0' : '1'),
173 $row_format,
174 $new_tbl_storage_engine,
175 ((isset($create_options['transactional']) && $create_options['transactional'] == '0') ? '0' : '1'),
176 $tbl_collation
179 if (count($table_alters) > 0) {
180 $sql_query = 'ALTER TABLE '
181 . Util::backquote($GLOBALS['table']);
182 $sql_query .= "\r\n" . implode("\r\n", $table_alters);
183 $sql_query .= ';';
184 $result .= $GLOBALS['dbi']->query($sql_query) ? true : false;
185 $reread_info = true;
186 unset($table_alters);
187 $warning_messages = Operations::getWarningMessagesArray();
190 if (isset($_REQUEST['tbl_collation'])
191 && ! empty($_REQUEST['tbl_collation'])
192 && isset($_REQUEST['change_all_collations'])
193 && ! empty($_REQUEST['change_all_collations'])
195 Operations::changeAllColumnsCollation(
196 $GLOBALS['db'], $GLOBALS['table'], $_REQUEST['tbl_collation']
201 * Reordering the table has been requested by the user
203 if (isset($_REQUEST['submitorderby']) && ! empty($_REQUEST['order_field'])) {
204 list($sql_query, $result) = Operations::getQueryAndResultForReorderingTable();
205 } // end if
208 * A partition operation has been requested by the user
210 if (isset($_REQUEST['submit_partition'])
211 && ! empty($_REQUEST['partition_operation'])
213 list($sql_query, $result) = Operations::getQueryAndResultForPartition();
214 } // end if
216 if ($reread_info) {
217 // to avoid showing the old value (for example the AUTO_INCREMENT) after
218 // a change, clear the cache
219 $GLOBALS['dbi']->clearTableCache();
220 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
221 $GLOBALS['showtable'] = $pma_table->getStatusInfo(null, true);
222 if ($pma_table->isView()) {
223 $tbl_is_view = true;
224 $tbl_storage_engine = __('View');
225 $show_comment = null;
226 } else {
227 $tbl_is_view = false;
228 $tbl_storage_engine = $pma_table->getStorageEngine();
229 $show_comment = $pma_table->getComment();
231 $tbl_collation = $pma_table->getCollation();
232 $table_info_num_rows = $pma_table->getNumRows();
233 $row_format = $pma_table->getRowFormat();
234 $auto_increment = $pma_table->getAutoIncrement();
235 $create_options = $pma_table->getCreateOptions();
237 unset($reread_info);
239 if (isset($result) && empty($message_to_show)) {
240 if (empty($_message)) {
241 if (empty($sql_query)) {
242 $_message = Message::success(__('No change'));
243 } else {
244 $_message = $result
245 ? Message::success()
246 : Message::error();
249 if ($response->isAjax()) {
250 $response->setRequestStatus($_message->isSuccess());
251 $response->addJSON('message', $_message);
252 if (!empty($sql_query)) {
253 $response->addJSON(
254 'sql_query', Util::getMessage(null, $sql_query)
257 exit;
259 } else {
260 $_message = $result
261 ? Message::success($_message)
262 : Message::error($_message);
265 if (! empty($warning_messages)) {
266 $_message = new Message;
267 $_message->addMessagesString($warning_messages);
268 $_message->isError(true);
269 if ($response->isAjax()) {
270 $response->setRequestStatus(false);
271 $response->addJSON('message', $_message);
272 if (!empty($sql_query)) {
273 $response->addJSON(
274 'sql_query', Util::getMessage(null, $sql_query)
277 exit;
279 unset($warning_messages);
282 if (empty($sql_query)) {
283 $response->addHTML(
284 $_message->getDisplay()
286 } else {
287 $response->addHTML(
288 Util::getMessage($_message, $sql_query)
291 unset($_message);
294 $url_params['goto']
295 = $url_params['back']
296 = 'tbl_operations.php';
299 * Get columns names
301 $columns = $GLOBALS['dbi']->getColumns($GLOBALS['db'], $GLOBALS['table']);
304 * Displays the page
308 * Order the table
310 $hideOrderTable = false;
311 // `ALTER TABLE ORDER BY` does not make sense for InnoDB tables that contain
312 // a user-defined clustered index (PRIMARY KEY or NOT NULL UNIQUE index).
313 // InnoDB always orders table rows according to such an index if one is present.
314 if ($tbl_storage_engine == 'INNODB') {
315 $indexes = Index::getFromTable($GLOBALS['table'], $GLOBALS['db']);
316 foreach ($indexes as $name => $idx) {
317 if ($name == 'PRIMARY') {
318 $hideOrderTable = true;
319 break;
320 } elseif (! $idx->getNonUnique()) {
321 $notNull = true;
322 foreach ($idx->getColumns() as $column) {
323 if ($column->getNull()) {
324 $notNull = false;
325 break;
328 if ($notNull) {
329 $hideOrderTable = true;
330 break;
335 if (! $hideOrderTable) {
336 $response->addHTML(Operations::getHtmlForOrderTheTable($columns));
340 * Move table
342 $response->addHTML(Operations::getHtmlForMoveTable());
344 if (mb_strstr($show_comment, '; InnoDB free') === false) {
345 if (mb_strstr($show_comment, 'InnoDB free') === false) {
346 // only user entered comment
347 $comment = $show_comment;
348 } else {
349 // here we have just InnoDB generated part
350 $comment = '';
352 } else {
353 // remove InnoDB comment from end, just the minimal part (*? is non greedy)
354 $comment = preg_replace('@; InnoDB free:.*?$@', '', $show_comment);
357 // PACK_KEYS: MyISAM or ISAM
358 // DELAY_KEY_WRITE, CHECKSUM, : MyISAM only
359 // AUTO_INCREMENT: MyISAM and InnoDB since 5.0.3, PBXT
361 // Here should be version check for InnoDB, however it is supported
362 // in >5.0.4, >4.1.12 and >4.0.11, so I decided not to
363 // check for version
365 $response->addHTML(
366 Operations::getTableOptionDiv(
367 $pma_table, $comment, $tbl_collation, $tbl_storage_engine,
368 $create_options['pack_keys'],
369 $auto_increment,
370 (empty($create_options['delay_key_write']) ? '0' : '1'),
371 ((isset($create_options['transactional']) && $create_options['transactional'] == '0') ? '0' : '1'),
372 ((isset($create_options['page_checksum'])) ? $create_options['page_checksum'] : ''),
373 (empty($create_options['checksum']) ? '0' : '1')
378 * Copy table
380 $response->addHTML(Operations::getHtmlForCopytable());
383 * Table maintenance
385 $response->addHTML(
386 Operations::getHtmlForTableMaintenance($pma_table, $url_params)
389 if (! (isset($db_is_system_schema) && $db_is_system_schema)) {
390 $truncate_table_url_params = array();
391 $drop_table_url_params = array();
393 if (! $tbl_is_view
394 && ! (isset($db_is_system_schema) && $db_is_system_schema)
396 $this_sql_query = 'TRUNCATE TABLE '
397 . Util::backquote($GLOBALS['table']);
398 $truncate_table_url_params = array_merge(
399 $url_params,
400 array(
401 'sql_query' => $this_sql_query,
402 'goto' => 'tbl_structure.php',
403 'reload' => '1',
404 'message_to_show' => sprintf(
405 __('Table %s has been emptied.'),
406 htmlspecialchars($table)
411 if (! (isset($db_is_system_schema) && $db_is_system_schema)) {
412 $this_sql_query = 'DROP TABLE '
413 . Util::backquote($GLOBALS['table']);
414 $drop_table_url_params = array_merge(
415 $url_params,
416 array(
417 'sql_query' => $this_sql_query,
418 'goto' => 'db_operations.php',
419 'reload' => '1',
420 'purge' => '1',
421 'message_to_show' => sprintf(
422 ($tbl_is_view
423 ? __('View %s has been dropped.')
424 : __('Table %s has been dropped.')
426 htmlspecialchars($table)
428 // table name is needed to avoid running
429 // PhpMyAdmin\RelationCleanup::database() on the whole db later
430 'table' => $GLOBALS['table'],
434 $response->addHTML(
435 Operations::getHtmlForDeleteDataOrTable(
436 $truncate_table_url_params,
437 $drop_table_url_params
442 if (Partition::havePartitioning()) {
443 $partition_names = Partition::getPartitionNames($db, $table);
444 // show the Partition maintenance section only if we detect a partition
445 if (! is_null($partition_names[0])) {
446 $response->addHTML(
447 Operations::getHtmlForPartitionMaintenance($partition_names, $url_params)
449 } // end if
450 } // end if
451 unset($partition_names);
453 // Referential integrity check
454 // The Referential integrity check was intended for the non-InnoDB
455 // tables for which the relations are defined in pmadb
456 // so I assume that if the current table is InnoDB, I don't display
457 // this choice (InnoDB maintains integrity by itself)
459 if ($cfgRelation['relwork'] && ! $pma_table->isEngine("INNODB")) {
460 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
461 $foreign = Relation::getForeigners($GLOBALS['db'], $GLOBALS['table'], '', 'internal');
463 if (! empty($foreign)) {
464 $response->addHTML(
465 Operations::getHtmlForReferentialIntegrityCheck($foreign, $url_params)
467 } // end if ($foreign)
469 } // end if (!empty($cfg['Server']['relation']))