Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 92 lines (88 sloc) 3.243 kb
88e1daf Prepare for index edit
jakubvrana authored
1 <?php
79d1902 Substitute table name to $TABLE
jakubvrana authored
2 $TABLE = $_GET["indexes"];
88e1daf Prepare for index edit
jakubvrana authored
3 $index_types = array("PRIMARY", "UNIQUE", "INDEX", "FULLTEXT");
79d1902 Substitute table name to $TABLE
jakubvrana authored
4 $indexes = indexes($TABLE);
430034c Cottage homework
jakubvrana authored
5 if ($_POST && !$error && !$_POST["add"]) {
0b6ab61 Alter indexes
jakubvrana authored
6 $alter = array();
7 foreach ($_POST["indexes"] as $index) {
8 if (in_array($index["type"], $index_types)) {
9 $columns = array();
e78ec76 Index length
jakubvrana authored
10 $lengths = array();
11 $set = array();
0b6ab61 Alter indexes
jakubvrana authored
12 ksort($index["columns"]);
e78ec76 Index length
jakubvrana authored
13 foreach ($index["columns"] as $key => $column) {
7352c28 Replace strlen() by != ""
jakubvrana authored
14 if ($column != "") {
e78ec76 Index length
jakubvrana authored
15 $length = $index["lengths"][$key];
16 $set[] = idf_escape($column) . ($length ? "(" . intval($length) . ")" : "");
0b6ab61 Alter indexes
jakubvrana authored
17 $columns[count($columns) + 1] = $column;
e78ec76 Index length
jakubvrana authored
18 $lengths[count($lengths) + 1] = ($length ? $length : null);
0b6ab61 Alter indexes
jakubvrana authored
19 }
20 }
21 if ($columns) {
22 foreach ($indexes as $name => $existing) {
8983582 Sort existing for ===
jakubvrana authored
23 ksort($existing["columns"]);
24 ksort($existing["lengths"]);
e78ec76 Index length
jakubvrana authored
25 if ($index["type"] == $existing["type"] && $existing["columns"] === $columns && $existing["lengths"] === $lengths) {
f83f204 Comments
jakubvrana authored
26 // skip existing index
0b6ab61 Alter indexes
jakubvrana authored
27 unset($indexes[$name]);
28 continue 2;
29 }
30 }
20679d8 Use \n in SQL commands
jakubvrana authored
31 $alter[] = "\nADD $index[type]" . ($index["type"] == "PRIMARY" ? " KEY" : "") . " (" . implode(", ", $set) . ")";
0b6ab61 Alter indexes
jakubvrana authored
32 }
33 }
34 }
f83f204 Comments
jakubvrana authored
35 // drop removed indexes
0b6ab61 Alter indexes
jakubvrana authored
36 foreach ($indexes as $name => $existing) {
20679d8 Use \n in SQL commands
jakubvrana authored
37 $alter[] = "\nDROP INDEX " . idf_escape($name);
0b6ab61 Alter indexes
jakubvrana authored
38 }
f91b2c4 Print SQL query by message
jakubvrana authored
39 if (!$alter) {
79d1902 Substitute table name to $TABLE
jakubvrana authored
40 redirect(ME . "table=" . urlencode($TABLE));
0b6ab61 Alter indexes
jakubvrana authored
41 }
79d1902 Substitute table name to $TABLE
jakubvrana authored
42 query_redirect("ALTER TABLE " . idf_escape($TABLE) . implode(",", $alter), ME . "table=" . urlencode($TABLE), lang('Indexes have been altered.'));
88e1daf Prepare for index edit
jakubvrana authored
43 }
36d6864 Whitespace
jakubvrana authored
44
79d1902 Substitute table name to $TABLE
jakubvrana authored
45 page_header(lang('Indexes'), $error, array("table" => $TABLE), $TABLE);
88e1daf Prepare for index edit
jakubvrana authored
46
79d1902 Substitute table name to $TABLE
jakubvrana authored
47 $fields = array_keys(fields($TABLE));
d8df520 Initialize variables outside blocks
jakubvrana authored
48 $row = array("indexes" => $indexes);
88e1daf Prepare for index edit
jakubvrana authored
49 if ($_POST) {
e78ec76 Index length
jakubvrana authored
50 $row = $_POST;
4d38c7d Protection against big POST data
jakubvrana authored
51 if ($_POST["add"]) {
e78ec76 Index length
jakubvrana authored
52 foreach ($row["indexes"] as $key => $index) {
7352c28 Replace strlen() by != ""
jakubvrana authored
53 if ($index["columns"][count($index["columns"])] != "") {
e78ec76 Index length
jakubvrana authored
54 $row["indexes"][$key]["columns"][] = "";
55 }
56 }
69673ae Use end() for last item
jakubvrana authored
57 $index = end($row["indexes"]);
8983582 Sort existing for ===
jakubvrana authored
58 if ($index["type"] || array_filter($index["columns"], 'strlen') || array_filter($index["lengths"], 'strlen')) {
e78ec76 Index length
jakubvrana authored
59 $row["indexes"][] = array("columns" => array(1 => ""));
60 }
aaa22e0 Add next
jakubvrana authored
61 }
88e1daf Prepare for index edit
jakubvrana authored
62 } else {
e78ec76 Index length
jakubvrana authored
63 foreach ($row["indexes"] as $key => $index) {
64 $row["indexes"][$key]["columns"][] = "";
65 }
66 $row["indexes"][] = array("columns" => array(1 => ""));
88e1daf Prepare for index edit
jakubvrana authored
67 }
68 ?>
f508fca Empty line
jakubvrana authored
69
88e1daf Prepare for index edit
jakubvrana authored
70 <form action="" method="post">
d30face Remove useless table attributes (thanks to Juraj Krivda)
jakubvrana authored
71 <table cellspacing="0">
ace55ed HTML instead of XHTML
jakubvrana authored
72 <thead><tr><th><?php echo lang('Index Type'); ?><th><?php echo lang('Column (length)'); ?></thead>
88e1daf Prepare for index edit
jakubvrana authored
73 <?php
74 $j = 0;
720f5fc Restructure indexes
jakubvrana authored
75 foreach ($row["indexes"] as $index) {
3c5c0f0 Use camelCase in JavaScript
jakubvrana authored
76 echo "<tr><td>" . html_select("indexes[$j][type]", array(-1 => "") + $index_types, $index["type"], ($j == count($row["indexes"]) - 1 ? "indexesAddRow(this);" : 1)) . "<td>\n";
e78ec76 Index length
jakubvrana authored
77 ksort($index["columns"]);
78 foreach ($index["columns"] as $i => $column) {
3c5c0f0 Use camelCase in JavaScript
jakubvrana authored
79 echo "<span>" . html_select("indexes[$j][columns][$i]", array(-1 => "") + $fields, $column, ($i == count($index["columns"]) ? "indexesAddColumn(this);" : 1));
689699a Shortcut for htmlspecialchars
jakubvrana authored
80 echo "<input name='indexes[$j][lengths][$i]' size='2' value='" . h($index["lengths"][$i]) . "'> </span>\n";
88e1daf Prepare for index edit
jakubvrana authored
81 }
ace55ed HTML instead of XHTML
jakubvrana authored
82 echo "\n";
e78ec76 Index length
jakubvrana authored
83 $j++;
88e1daf Prepare for index edit
jakubvrana authored
84 }
85 ?>
86 </table>
58178ab Move buttons
jakubvrana authored
87 <p>
ace55ed HTML instead of XHTML
jakubvrana authored
88 <input type="hidden" name="token" value="<?php echo $token; ?>">
89 <input type="submit" value="<?php echo lang('Save'); ?>">
90 <noscript><p><input type="submit" name="add" value="<?php echo lang('Add next'); ?>"></noscript>
88e1daf Prepare for index edit
jakubvrana authored
91 </form>
Something went wrong with that request. Please try again.