Translated using Weblate (Turkish)
[phpmyadmin.git] / tbl_operations.php
blobf9a560a1caf9337cea59aaa6f2710aecffc6bfed
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/check_user_privileges.lib.php';
18 require_once 'libraries/operations.lib.php';
20 $pma_table = new PMA_Table($GLOBALS['table'], $GLOBALS['db']);
22 /**
23 * Load JavaScript files
25 $response = PMA_Response::getInstance();
26 $header = $response->getHeader();
27 $scripts = $header->getScripts();
28 $scripts->addFile('functions.js');
29 $scripts->addFile('tbl_operations.js');
31 /**
32 * Runs common work
34 require 'libraries/tbl_common.inc.php';
35 $url_query .= '&amp;goto=tbl_operations.php&amp;back=tbl_operations.php';
36 $url_params['goto'] = $url_params['back'] = 'tbl_operations.php';
38 /**
39 * Gets relation settings
41 $cfgRelation = PMA_getRelationsParam();
43 /**
44 * Gets available MySQL charsets and storage engines
46 require_once 'libraries/mysql_charsets.inc.php';
47 require_once 'libraries/StorageEngine.class.php';
49 /**
50 * Class for partition management
52 require_once 'libraries/Partition.class.php';
54 // reselect current db (needed in some cases probably due to
55 // the calling of relation.lib.php)
56 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
58 /**
59 * Gets tables information
61 require 'libraries/tbl_info.inc.php';
63 // define some variables here, for improved syntax in the conditionals
64 $is_myisam_or_aria = $is_isam = $is_innodb = $is_berkeleydb = false;
65 $is_aria = $is_pbxt = false;
66 // set initial value of these variables, based on the current table engine
67 list($is_myisam_or_aria, $is_innodb, $is_isam,
68 $is_berkeleydb, $is_aria, $is_pbxt
69 ) = PMA_setGlobalVariablesForEngine($tbl_storage_engine);
71 if ($is_aria) {
72 // the value for transactional can be implicit
73 // (no create option found, in this case it means 1)
74 // or explicit (option found with a value of 0 or 1)
75 // ($transactional may have been set by libraries/tbl_info.inc.php,
76 // from the $create_options)
77 $transactional = (isset($transactional) && $transactional == '0')
78 ? '0'
79 : '1';
80 $page_checksum = (isset($page_checksum)) ? $page_checksum : '';
83 $reread_info = false;
84 $table_alters = array();
86 /** @var PMA_String $pmaString */
87 $pmaString = $GLOBALS['PMA_String'];
89 /**
90 * If the table has to be moved to some other database
92 if (isset($_REQUEST['submit_move']) || isset($_REQUEST['submit_copy'])) {
93 //$_message = '';
94 PMA_moveOrCopyTable($db, $table);
95 // This was ended in an Ajax call
96 exit;
98 /**
99 * If the table has to be maintained
101 if (isset($_REQUEST['table_maintenance'])) {
102 include_once 'sql.php';
103 unset($result);
106 * Updates table comment, type and options if required
108 if (isset($_REQUEST['submitoptions'])) {
109 $_message = '';
110 $warning_messages = array();
112 if (isset($_REQUEST['new_name'])) {
113 // Get original names before rename operation
114 $oldTable = $pma_table->getName();
115 $oldDb = $pma_table->getDbName();
117 if ($pma_table->rename($_REQUEST['new_name'])) {
118 if (isset($_REQUEST['adjust_privileges'])
119 && ! empty($_REQUEST['adjust_privileges'])
121 PMA_AdjustPrivileges_renameOrMoveTable(
122 $oldDb, $oldTable, $_REQUEST['db'], $_REQUEST['new_name']
126 // Reselect the original DB
127 $GLOBALS['db'] = $oldDb;
128 $GLOBALS['dbi']->selectDb($oldDb);
130 $_message .= $pma_table->getLastMessage();
131 $result = true;
132 $GLOBALS['table'] = $pma_table->getName();
133 $reread_info = true;
134 $reload = true;
135 } else {
136 $_message .= $pma_table->getLastError();
137 $result = false;
141 if (! empty($_REQUEST['new_tbl_storage_engine'])
142 && /*overload*/mb_strtolower($_REQUEST['new_tbl_storage_engine']) !== /*overload*/mb_strtolower($tbl_storage_engine)
144 $new_tbl_storage_engine = $_REQUEST['new_tbl_storage_engine'];
145 // reset the globals for the new engine
146 list($is_myisam_or_aria, $is_innodb, $is_isam,
147 $is_berkeleydb, $is_aria, $is_pbxt
148 ) = PMA_setGlobalVariablesForEngine($new_tbl_storage_engine);
150 if ($is_aria) {
151 $transactional = (isset($transactional) && $transactional == '0')
152 ? '0'
153 : '1';
154 $page_checksum = (isset($page_checksum)) ? $page_checksum : '';
156 } else {
157 $new_tbl_storage_engine = '';
160 $table_alters = PMA_getTableAltersArray(
161 $is_myisam_or_aria, $is_isam, $pack_keys,
162 (empty($checksum) ? '0' : '1'),
163 $is_aria,
164 ((isset($page_checksum)) ? $page_checksum : ''),
165 (empty($delay_key_write) ? '0' : '1'),
166 $is_innodb, $is_pbxt, $row_format,
167 $new_tbl_storage_engine,
168 ((isset($transactional) && $transactional == '0') ? '0' : '1'),
169 $tbl_collation
172 if (count($table_alters) > 0) {
173 $sql_query = 'ALTER TABLE '
174 . PMA_Util::backquote($GLOBALS['table']);
175 $sql_query .= "\r\n" . implode("\r\n", $table_alters);
176 $sql_query .= ';';
177 $result .= $GLOBALS['dbi']->query($sql_query) ? true : false;
178 $reread_info = true;
179 unset($table_alters);
180 $warning_messages = PMA_getWarningMessagesArray();
183 if (isset($_REQUEST['tbl_collation'])
184 && ! empty($_REQUEST['tbl_collation'])
185 && isset($_REQUEST['change_all_collations'])
186 && ! empty($_REQUEST['change_all_collations'])
188 PMA_changeAllColumnsCollation(
189 $GLOBALS['db'], $GLOBALS['table'], $_REQUEST['tbl_collation']
194 * Reordering the table has been requested by the user
196 if (isset($_REQUEST['submitorderby']) && ! empty($_REQUEST['order_field'])) {
197 list($sql_query, $result) = PMA_getQueryAndResultForReorderingTable();
198 } // end if
201 * A partition operation has been requested by the user
203 $sql_query = '';
205 if (isset($_REQUEST['submit_partition'])
206 && ! empty($_REQUEST['partition_operation'])
208 list($sql_query, $result) = PMA_getQueryAndResultForPartition();
209 } // end if
211 if ($reread_info) {
212 // to avoid showing the old value (for example the AUTO_INCREMENT) after
213 // a change, clear the cache
214 $GLOBALS['dbi']->clearTableCache();
215 $page_checksum = $checksum = $delay_key_write = 0;
216 include 'libraries/tbl_info.inc.php';
218 unset($reread_info);
220 if (isset($result) && empty($message_to_show)) {
221 // set to success by default, because result set could be empty
222 // (for example, a table rename)
223 $_type = 'success';
224 if (empty($_message)) {
225 $_message = $result
226 ? PMA_Message::success(
227 __('Your SQL query has been executed successfully.')
229 : PMA_Message::error(__('Error'));
230 // $result should exist, regardless of $_message
231 $_type = $result ? 'success' : 'error';
233 if (isset($GLOBALS['ajax_request'])
234 && $GLOBALS['ajax_request'] == true
236 $response = PMA_Response::getInstance();
237 $response->isSuccess($_message->isSuccess());
238 $response->addJSON('message', $_message);
239 $response->addJSON(
240 'sql_query', PMA_Util::getMessage(null, $sql_query)
242 exit;
245 if (! empty($warning_messages)) {
246 $_message = new PMA_Message;
247 $_message->addMessages($warning_messages);
248 $_message->isError(true);
249 if ($GLOBALS['ajax_request'] == true) {
250 $response = PMA_Response::getInstance();
251 $response->isSuccess(false);
252 $response->addJSON('message', $_message);
253 exit;
255 unset($warning_messages);
258 $response->addHTML(
259 PMA_Util::getMessage($_message, $sql_query, $_type)
261 unset($_message, $_type);
264 $url_params['goto']
265 = $url_params['back']
266 = 'tbl_operations.php';
269 * Get columns names
271 $columns = $GLOBALS['dbi']->getColumns($GLOBALS['db'], $GLOBALS['table']);
274 * Displays the page
276 $response->addHTML('<div id="boxContainer" data-box-width="300">');
279 * Order the table
281 $hideOrderTable = false;
282 // `ALTER TABLE ORDER BY` does not make sense for InnoDB tables that contain
283 // a user-defined clustered index (PRIMARY KEY or NOT NULL UNIQUE index).
284 // InnoDB always orders table rows according to such an index if one is present.
285 if ($tbl_storage_engine == 'INNODB') {
286 include_once 'libraries/Index.class.php';
287 $indexes = PMA_Index::getFromTable($GLOBALS['table'], $GLOBALS['db']);
288 foreach ($indexes as $name => $idx) {
289 if ($name == 'PRIMARY') {
290 $hideOrderTable = true;
291 break;
292 } elseif (! $idx->getNonUnique()) {
293 $notNull = true;
294 foreach ($idx->getColumns() as $column) {
295 if ($column->getNull()) {
296 $notNull = false;
297 break;
300 if ($notNull) {
301 $hideOrderTable = true;
302 break;
307 if (! $hideOrderTable) {
308 $response->addHTML(PMA_getHtmlForOrderTheTable($columns));
312 * Move table
314 $response->addHTML(PMA_getHtmlForMoveTable());
316 if (/*overload*/mb_strstr($show_comment, '; InnoDB free') === false) {
317 if (/*overload*/mb_strstr($show_comment, 'InnoDB free') === false) {
318 // only user entered comment
319 $comment = $show_comment;
320 } else {
321 // here we have just InnoDB generated part
322 $comment = '';
324 } else {
325 // remove InnoDB comment from end, just the minimal part (*? is non greedy)
326 $comment = preg_replace('@; InnoDB free:.*?$@', '', $show_comment);
329 // PACK_KEYS: MyISAM or ISAM
330 // DELAY_KEY_WRITE, CHECKSUM, : MyISAM only
331 // AUTO_INCREMENT: MyISAM and InnoDB since 5.0.3, PBXT
333 // Here should be version check for InnoDB, however it is supported
334 // in >5.0.4, >4.1.12 and >4.0.11, so I decided not to
335 // check for version
337 $response->addHTML(
338 PMA_getTableOptionDiv(
339 $comment, $tbl_collation, $tbl_storage_engine,
340 $is_myisam_or_aria, $is_isam, $pack_keys,
341 $auto_increment,
342 (empty($delay_key_write) ? '0' : '1'),
343 ((isset($transactional) && $transactional == '0') ? '0' : '1'),
344 ((isset($page_checksum)) ? $page_checksum : ''),
345 $is_innodb, $is_pbxt, $is_aria, (empty($checksum) ? '0' : '1')
350 * Copy table
352 $response->addHTML(PMA_getHtmlForCopytable());
355 * Table maintenance
357 $response->addHTML(
358 PMA_getHtmlForTableMaintenance(
359 $is_myisam_or_aria,
360 $is_innodb,
361 $is_berkeleydb,
362 $url_params
366 if (! (isset($db_is_system_schema) && $db_is_system_schema)) {
367 $truncate_table_url_params = array();
368 $drop_table_url_params = array();
370 if (! $tbl_is_view
371 && ! (isset($db_is_system_schema) && $db_is_system_schema)
373 $this_sql_query = 'TRUNCATE TABLE '
374 . PMA_Util::backquote($GLOBALS['table']);
375 $truncate_table_url_params = array_merge(
376 $url_params,
377 array(
378 'sql_query' => $this_sql_query,
379 'goto' => 'tbl_structure.php',
380 'reload' => '1',
381 'message_to_show' => sprintf(
382 __('Table %s has been emptied.'),
383 htmlspecialchars($table)
388 if (! (isset($db_is_system_schema) && $db_is_system_schema)) {
389 $this_sql_query = 'DROP TABLE '
390 . PMA_Util::backquote($GLOBALS['table']);
391 $drop_table_url_params = array_merge(
392 $url_params,
393 array(
394 'sql_query' => $this_sql_query,
395 'goto' => 'db_operations.php',
396 'reload' => '1',
397 'purge' => '1',
398 'message_to_show' => sprintf(
399 ($tbl_is_view
400 ? __('View %s has been dropped.')
401 : __('Table %s has been dropped.')
403 htmlspecialchars($table)
405 // table name is needed to avoid running
406 // PMA_relationsCleanupDatabase() on the whole db later
407 'table' => $GLOBALS['table'],
411 $response->addHTML(
412 PMA_getHtmlForDeleteDataOrTable(
413 $truncate_table_url_params,
414 $drop_table_url_params
419 if (PMA_Partition::havePartitioning()) {
420 $partition_names = PMA_Partition::getPartitionNames($db, $table);
421 // show the Partition maintenance section only if we detect a partition
422 if (! is_null($partition_names[0])) {
423 $response->addHTML(
424 PMA_getHtmlForPartitionMaintenance($partition_names, $url_params)
426 } // end if
427 } // end if
428 unset($partition_names);
430 // Referential integrity check
431 // The Referential integrity check was intended for the non-InnoDB
432 // tables for which the relations are defined in pmadb
433 // so I assume that if the current table is InnoDB, I don't display
434 // this choice (InnoDB maintains integrity by itself)
436 if ($cfgRelation['relwork'] && ! $is_innodb) {
437 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
438 $foreign = PMA_getForeigners($GLOBALS['db'], $GLOBALS['table'], '', 'internal');
440 if (! empty($foreign)) {
441 $response->addHTML(
442 PMA_getHtmlForReferentialIntegrityCheck($foreign, $url_params)
444 } // end if ($foreign)
446 } // end if (!empty($cfg['Server']['relation']))
448 $response->addHTML('</div>');