tbl_indexes.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Displays index edit/creation form and handles it
  5. *
  6. * @package PhpMyAdmin
  7. */
  8. /**
  9. * Gets some core libraries
  10. */
  11. require_once 'libraries/common.inc.php';
  12. require_once 'libraries/Index.class.php';
  13. require_once 'libraries/tbl_common.inc.php';
  14. // Get fields and stores their name/type
  15. $fields = array();
  16. foreach (PMA_DBI_get_columns_full($db, $table) as $row) {
  17. if (preg_match('@^(set|enum)\((.+)\)$@i', $row['Type'], $tmp)) {
  18. $tmp[2] = substr(
  19. preg_replace('@([^,])\'\'@', '\\1\\\'', ',' . $tmp[2]), 1
  20. );
  21. $fields[$row['Field']] = $tmp[1] . '('
  22. . str_replace(',', ', ', $tmp[2]) . ')';
  23. } else {
  24. $fields[$row['Field']] = $row['Type'];
  25. }
  26. } // end while
  27. // Prepares the form values
  28. if (isset($_REQUEST['index'])) {
  29. if (is_array($_REQUEST['index'])) {
  30. // coming already from form
  31. $index = new PMA_Index($_REQUEST['index']);
  32. } else {
  33. $index = PMA_Index::singleton($db, $table, $_REQUEST['index']);
  34. }
  35. } else {
  36. $index = new PMA_Index;
  37. }
  38. /**
  39. * Process the data from the edit/create index form,
  40. * run the query to build the new index
  41. * and moves back to "tbl_sql.php"
  42. */
  43. if (isset($_REQUEST['do_save_data'])) {
  44. $error = false;
  45. // $sql_query is the one displayed in the query box
  46. $sql_query = 'ALTER TABLE ' . PMA_Util::backquote($db)
  47. . '.' . PMA_Util::backquote($table);
  48. // Drops the old index
  49. if (! empty($_REQUEST['old_index'])) {
  50. if ($_REQUEST['old_index'] == 'PRIMARY') {
  51. $sql_query .= ' DROP PRIMARY KEY,';
  52. } else {
  53. $sql_query .= ' DROP INDEX '
  54. . PMA_Util::backquote($_REQUEST['old_index']) . ',';
  55. }
  56. } // end if
  57. // Builds the new one
  58. switch ($index->getType()) {
  59. case 'PRIMARY':
  60. if ($index->getName() == '') {
  61. $index->setName('PRIMARY');
  62. } elseif ($index->getName() != 'PRIMARY') {
  63. $error = PMA_Message::error(
  64. __('The name of the primary key must be "PRIMARY"!')
  65. );
  66. }
  67. $sql_query .= ' ADD PRIMARY KEY';
  68. break;
  69. case 'FULLTEXT':
  70. case 'UNIQUE':
  71. case 'INDEX':
  72. case 'SPATIAL':
  73. if ($index->getName() == 'PRIMARY') {
  74. $error = PMA_Message::error(__('Can\'t rename index to PRIMARY!'));
  75. }
  76. $sql_query .= ' ADD ' . $index->getType() . ' '
  77. . ($index->getName() ? PMA_Util::backquote($index->getName()) : '');
  78. break;
  79. } // end switch
  80. $index_fields = array();
  81. foreach ($index->getColumns() as $key => $column) {
  82. $index_fields[$key] = PMA_Util::backquote($column->getName());
  83. if ($column->getSubPart()) {
  84. $index_fields[$key] .= '(' . $column->getSubPart() . ')';
  85. }
  86. } // end while
  87. if (empty($index_fields)) {
  88. $error = PMA_Message::error(__('No index parts defined!'));
  89. } else {
  90. $sql_query .= ' (' . implode(', ', $index_fields) . ')';
  91. }
  92. if (PMA_MYSQL_INT_VERSION > 50500) {
  93. $sql_query .= "COMMENT '"
  94. . PMA_Util::sqlAddSlashes($index->getComment())
  95. . "'";
  96. }
  97. $sql_query .= ';';
  98. if (! $error) {
  99. PMA_DBI_query($sql_query);
  100. $message = PMA_Message::success(
  101. __('Table %1$s has been altered successfully')
  102. );
  103. $message->addParam($table);
  104. if ($GLOBALS['is_ajax_request'] == true) {
  105. $response = PMA_Response::getInstance();
  106. $response->addJSON('message', $message);
  107. $response->addJSON('index_table', PMA_Index::getView($table, $db));
  108. $response->addJSON(
  109. 'sql_query',
  110. PMA_Util::getMessage(null, $sql_query)
  111. );
  112. } else {
  113. $active_page = 'tbl_structure.php';
  114. include 'tbl_structure.php';
  115. }
  116. exit;
  117. } else {
  118. if ($GLOBALS['is_ajax_request'] == true) {
  119. $response = PMA_Response::getInstance();
  120. $response->isSuccess(false);
  121. $response->addJSON('message', $error);
  122. exit;
  123. } else {
  124. $error->display();
  125. }
  126. }
  127. } // end builds the new index
  128. /**
  129. * Display the form to edit/create an index
  130. */
  131. // Displays headers (if needed)
  132. $response = PMA_Response::getInstance();
  133. $header = $response->getHeader();
  134. $scripts = $header->getScripts();
  135. $scripts->addFile('indexes.js');
  136. require_once 'libraries/tbl_info.inc.php';
  137. if (isset($_REQUEST['index']) && is_array($_REQUEST['index'])) {
  138. // coming already from form
  139. $add_fields
  140. = count($_REQUEST['index']['columns']['names']) - $index->getColumnCount();
  141. if (isset($_REQUEST['add_fields'])) {
  142. $add_fields += $_REQUEST['added_fields'];
  143. }
  144. } elseif (isset($_REQUEST['create_index'])) {
  145. $add_fields = $_REQUEST['added_fields'];
  146. } else {
  147. $add_fields = 1;
  148. }
  149. // end preparing form values
  150. ?>
  151. <form action="tbl_indexes.php" method="post" name="index_frm" id="index_frm" class="ajax"
  152. onsubmit="if (typeof(this.elements['index[Key_name]'].disabled) != 'undefined') {
  153. this.elements['index[Key_name]'].disabled = false}">
  154. <?php
  155. $form_params = array(
  156. 'db' => $db,
  157. 'table' => $table,
  158. );
  159. if (isset($_REQUEST['create_index'])) {
  160. $form_params['create_index'] = 1;
  161. } elseif (isset($_REQUEST['old_index'])) {
  162. $form_params['old_index'] = $_REQUEST['old_index'];
  163. } elseif (isset($_REQUEST['index'])) {
  164. $form_params['old_index'] = $_REQUEST['index'];
  165. }
  166. echo PMA_generate_common_hidden_inputs($form_params);
  167. ?>
  168. <fieldset id="index_edit_fields">
  169. <?php
  170. if ($GLOBALS['is_ajax_request'] != true) {
  171. ?>
  172. <legend>
  173. <?php
  174. if (isset($_REQUEST['create_index'])) {
  175. echo __('Add index');
  176. } else {
  177. echo __('Edit index');
  178. }
  179. ?>
  180. </legend>
  181. <?php
  182. }
  183. ?>
  184. <div class='index_info'>
  185. <div>
  186. <div class="label">
  187. <strong>
  188. <label for="input_index_name">
  189. <?php echo __('Index name:'); ?>
  190. <?php
  191. echo PMA_Util::showHint(
  192. PMA_Message::notice(
  193. __(
  194. '("PRIMARY" <b>must</b> be the name of'
  195. . ' and <b>only of</b> a primary key!)'
  196. )
  197. )
  198. );
  199. ?>
  200. </label>
  201. </strong>
  202. </div>
  203. <input type="text" name="index[Key_name]" id="input_index_name" size="25"
  204. value="<?php echo htmlspecialchars($index->getName()); ?>"
  205. onfocus="this.select()" />
  206. </div>
  207. <?php
  208. if (PMA_MYSQL_INT_VERSION > 50500) {
  209. ?>
  210. <div>
  211. <div class="label">
  212. <strong>
  213. <label for="input_index_comment">
  214. <?php echo __('Comment:'); ?>
  215. </label>
  216. </strong>
  217. </div>
  218. <input type="text" name="index[Index_comment]" id="input_index_comment" size="30"
  219. value="<?php echo htmlspecialchars($index->getComment()); ?>"
  220. onfocus="this.select()" />
  221. </div>
  222. <?php
  223. }
  224. ?>
  225. <div>
  226. <div class="label">
  227. <strong>
  228. <label for="select_index_type">
  229. <?php echo __('Index type:'); ?>
  230. <?php echo PMA_Util::showMySQLDocu('SQL-Syntax', 'ALTER_TABLE'); ?>
  231. </label>
  232. </strong>
  233. </div>
  234. <select name="index[Index_type]" id="select_index_type" >
  235. <?php echo $index->generateIndexSelector(); ?>
  236. </select>
  237. </div>
  238. <div class="clearfloat"></div>
  239. </div>
  240. <table id="index_columns">
  241. <thead>
  242. <tr><th><?php echo __('Column'); ?></th>
  243. <th><?php echo __('Size'); ?></th>
  244. </tr>
  245. </thead>
  246. <tbody>
  247. <?php
  248. $odd_row = true;
  249. $spatial_types = array(
  250. 'geometry', 'point', 'linestring', 'polygon', 'multipoint',
  251. 'multilinestring', 'multipolygon', 'geomtrycollection'
  252. );
  253. foreach ($index->getColumns() as $column) {
  254. ?>
  255. <tr class="<?php echo $odd_row ? 'odd' : 'even'; ?> noclick">
  256. <td>
  257. <select name="index[columns][names][]">
  258. <option value="">-- <?php echo __('Ignore'); ?> --</option>
  259. <?php
  260. foreach ($fields as $field_name => $field_type) {
  261. if (($index->getType() != 'FULLTEXT'
  262. || preg_match('/(char|text)/i', $field_type))
  263. && ($index->getType() != 'SPATIAL'
  264. || in_array($field_type, $spatial_types))
  265. ) {
  266. echo '<option value="' . htmlspecialchars($field_name) . '"'
  267. . (($field_name == $column->getName())
  268. ? ' selected="selected"'
  269. : '') . '>'
  270. . htmlspecialchars($field_name) . ' ['
  271. . htmlspecialchars($field_type) . ']'
  272. . '</option>' . "\n";
  273. }
  274. } // end foreach $fields
  275. ?>
  276. </select>
  277. </td>
  278. <td>
  279. <input type="text" size="5" onfocus="this.select()"
  280. name="index[columns][sub_parts][]"
  281. value="<?php
  282. if ($index->getType() != 'SPATIAL') {
  283. echo $column->getSubPart();
  284. }
  285. ?>"/>
  286. </td>
  287. </tr>
  288. <?php
  289. $odd_row = !$odd_row;
  290. } // end foreach $edited_index_info['Sequences']
  291. for ($i = 0; $i < $add_fields; $i++) {
  292. ?>
  293. <tr class="<?php echo $odd_row ? 'odd' : 'even'; ?> noclick">
  294. <td>
  295. <select name="index[columns][names][]">
  296. <option value="">-- <?php echo __('Ignore'); ?> --</option>
  297. <?php
  298. foreach ($fields as $field_name => $field_type) {
  299. echo '<option value="' . htmlspecialchars($field_name) . '">'
  300. . htmlspecialchars($field_name) . ' ['
  301. . htmlspecialchars($field_type) . ']'
  302. . '</option>' . "\n";
  303. } // end foreach $fields
  304. ?>
  305. </select>
  306. </td>
  307. <td>
  308. <input type="text" size="5" onfocus="this.select()"
  309. name="index[columns][sub_parts][]" value="" />
  310. </td>
  311. </tr>
  312. <?php
  313. $odd_row = !$odd_row;
  314. } // end foreach $edited_index_info['Sequences']
  315. ?>
  316. </tbody>
  317. </table>
  318. </fieldset>
  319. <fieldset class="tblFooters">
  320. <?php
  321. if ($GLOBALS['is_ajax_request'] != true || ! empty($_REQUEST['ajax_page_request'])) {
  322. ?>
  323. <input type="submit" name="do_save_data" value="<?php echo __('Save'); ?>" />
  324. <span id="addMoreColumns">
  325. <?php
  326. echo __('Or') . ' ';
  327. printf(
  328. __('Add %s column(s) to index') . "\n",
  329. '<input type="text" name="added_fields" size="2" value="1" />'
  330. );
  331. echo '<input type="submit" name="add_fields" value="' . __('Go') . '" />' . "\n";
  332. ?>
  333. </span>
  334. <?php
  335. } else {
  336. $btn_value = sprintf(__('Add %s column(s) to index'), 1);
  337. echo '<div class="slider"></div>';
  338. echo '<div class="add_fields">';
  339. echo '<input type="submit" value="' . $btn_value . '" />';
  340. echo '</div>';
  341. }
  342. ?>
  343. </fieldset>
  344. </form>