Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 108 lines (104 sloc) 4.921 kb
f8d378f Prepare schema
jakubvrana authored
1 <?php
3022dcb Define DB
jakubvrana authored
2 page_header(lang('Database schema'), "", array(), DB);
f8d378f Prepare schema
jakubvrana authored
3
6636a61 Draggable tables in schema
jakubvrana authored
4 $table_pos = array();
5 $table_pos_js = array();
64ba924 Comments
jakubvrana authored
6 // saved in one cookie because there is a limit of 20 cookies per domain
3eba731 Jakub Vrána Fix saving schema to cookie in Opera and Firefox 4
authored
7 $name = "adminer_schema";
989d758 Jakub Vrána Schema permanent link before mouse move
authored
8 $SCHEMA = ($_GET["schema"] ? $_GET["schema"] : $_COOKIE[($_COOKIE["$name-" . DB] ? "$name-" . DB : $name)]); // $_COOKIE["adminer_schema"] was used before 3.2.0 //! ':' in table name
9 preg_match_all('~([^:]+):([-0-9.]+)x([-0-9.]+)(_|$)~', $SCHEMA, $matches, PREG_SET_ORDER);
6636a61 Draggable tables in schema
jakubvrana authored
10 foreach ($matches as $i => $match) {
7c22767 Position lines
jakubvrana authored
11 $table_pos[$match[1]] = array($match[2], $match[3]);
28f21aa Jakub Vrána Escape JavaScript strings (bug #3093243)
authored
12 $table_pos_js[] = "\n\t'" . js_escape($match[1]) . "': [ $match[2], $match[3] ]";
6636a61 Draggable tables in schema
jakubvrana authored
13 }
14
15 $top = 0;
1eee0a1 Test $_COOKIE instead of SID (PHP bug #42135)
jakubvrana authored
16 $base_left = -1;
64ba924 Comments
jakubvrana authored
17 $schema = array(); // table => array("fields" => array(name => field), "pos" => array(top, left), "references" => array(table => array(left => array(source, target))))
18 $referenced = array(); // target_table => array(table => array(left => target_column))
19 $lefts = array(); // float => bool
caeb30f Separate table_status
jakubvrana authored
20 foreach (table_status() as $row) {
f8d378f Prepare schema
jakubvrana authored
21 if (!isset($row["Engine"])) { // view
22 continue;
23 }
6636a61 Draggable tables in schema
jakubvrana authored
24 $pos = 0;
25 $schema[$row["Name"]]["fields"] = array();
26 foreach (fields($row["Name"]) as $name => $field) {
27 $pos += 1.25;
28 $field["pos"] = $pos;
29 $schema[$row["Name"]]["fields"][$name] = $field;
30 }
7c22767 Position lines
jakubvrana authored
31 $schema[$row["Name"]]["pos"] = ($table_pos[$row["Name"]] ? $table_pos[$row["Name"]] : array($top, 0));
753909e Jakub Vrána Support for virtual foreign keys
authored
32 foreach ($adminer->foreignKeys($row["Name"]) as $val) {
33 if (!$val["db"]) {
34 $left = $base_left;
35 if ($table_pos[$row["Name"]][1] || $table_pos[$val["table"]][1]) {
36 $left = min(floatval($table_pos[$row["Name"]][1]), floatval($table_pos[$val["table"]][1])) - 1;
37 } else {
38 $base_left -= .1;
f8d378f Prepare schema
jakubvrana authored
39 }
753909e Jakub Vrána Support for virtual foreign keys
authored
40 while ($lefts[(string) $left]) {
41 // find free $left
42 $left -= .0001;
43 }
44 $schema[$row["Name"]]["references"][$val["table"]][(string) $left] = array($val["source"], $val["target"]);
45 $referenced[$val["table"]][$row["Name"]][(string) $left] = $val["target"];
46 $lefts[(string) $left] = true;
f8d378f Prepare schema
jakubvrana authored
47 }
48 }
7c22767 Position lines
jakubvrana authored
49 $top = max($top, $schema[$row["Name"]]["pos"][0] + 2.5 + $pos);
f8d378f Prepare schema
jakubvrana authored
50 }
6636a61 Draggable tables in schema
jakubvrana authored
51
f8d378f Prepare schema
jakubvrana authored
52 ?>
56e884f JSLint
jakubvrana authored
53 <div id="schema" style="height: <?php echo $top; ?>em;">
6636a61 Draggable tables in schema
jakubvrana authored
54 <script type="text/javascript">
0477032 JavaScript comments
jakubvrana authored
55 tablePos = {<?php echo implode(",", $table_pos_js) . "\n"; ?>};
56e884f JSLint
jakubvrana authored
56 em = document.getElementById('schema').offsetHeight / <?php echo $top; ?>;
3c5c0f0 Use camelCase in JavaScript
jakubvrana authored
57 document.onmousemove = schemaMousemove;
3eba731 Jakub Vrána Fix saving schema to cookie in Opera and Firefox 4
authored
58 document.onmouseup = function (ev) {
59 schemaMouseup(ev, '<?php echo js_escape(DB); ?>');
60 };
6636a61 Draggable tables in schema
jakubvrana authored
61 </script>
f8d378f Prepare schema
jakubvrana authored
62 <?php
38cb78a Show references in schema
jakubvrana authored
63 foreach ($schema as $name => $table) {
3c5c0f0 Use camelCase in JavaScript
jakubvrana authored
64 echo "<div class='table' style='top: " . $table["pos"][0] . "em; left: " . $table["pos"][1] . "em;' onmousedown='schemaMousedown(this, event);'>";
687b3fd Jakub Vrána Replace <strong> by <b> and <em> by <i>
authored
65 echo '<a href="' . h(ME) . 'table=' . urlencode($name) . '"><b>' . h($name) . "</b></a><br>\n";
6636a61 Draggable tables in schema
jakubvrana authored
66 foreach ($table["fields"] as $field) {
b349612 Remove useless translations
jakubvrana authored
67 $val = '<span' . type_class($field["type"]) . ' title="' . h($field["full_type"] . ($field["null"] ? " NULL" : '')) . '">' . h($field["field"]) . '</span>';
687b3fd Jakub Vrána Replace <strong> by <b> and <em> by <i>
authored
68 echo ($field["primary"] ? "<i>$val</i>" : $val) . "<br>\n";
6636a61 Draggable tables in schema
jakubvrana authored
69 }
70 foreach ((array) $table["references"] as $target_name => $refs) {
a055d87 Don't use PHP 5 only function (bug #2214423)
jakubvrana authored
71 foreach ($refs as $left => $ref) {
256438e Set vertical lines only once
jakubvrana authored
72 $left1 = $left - $table_pos[$name][1];
81dbdda Move references lines
jakubvrana authored
73 $i = 0;
a055d87 Don't use PHP 5 only function (bug #2214423)
jakubvrana authored
74 foreach ($ref[0] as $source) {
689699a Shortcut for htmlspecialchars
jakubvrana authored
75 echo "<div class='references' title='" . h($target_name) . "' id='refs$left-" . ($i++) . "' style='left: $left1" . "em; top: " . $table["fields"][$source]["pos"] . "em; padding-top: .5em;'><div style='border-top: 1px solid Gray; width: " . (-$left1) . "em;'></div></div>\n";
6636a61 Draggable tables in schema
jakubvrana authored
76 }
77 }
78 }
1eee0a1 Test $_COOKIE instead of SID (PHP bug #42135)
jakubvrana authored
79 foreach ((array) $referenced[$name] as $target_name => $refs) {
80 foreach ($refs as $left => $columns) {
256438e Set vertical lines only once
jakubvrana authored
81 $left1 = $left - $table_pos[$name][1];
81dbdda Move references lines
jakubvrana authored
82 $i = 0;
1eee0a1 Test $_COOKIE instead of SID (PHP bug #42135)
jakubvrana authored
83 foreach ($columns as $target) {
ab3c366 Separate static files
jakubvrana authored
84 echo "<div class='references' title='" . h($target_name) . "' id='refd$left-" . ($i++) . "' style='left: $left1" . "em; top: " . $table["fields"][$target]["pos"] . "em; height: 1.25em; background: url(../adminer/static/arrow.gif) no-repeat right center;'><div style='height: .5em; border-bottom: 1px solid Gray; width: " . (-$left1) . "em;'></div></div>\n";
1eee0a1 Test $_COOKIE instead of SID (PHP bug #42135)
jakubvrana authored
85 }
6636a61 Draggable tables in schema
jakubvrana authored
86 }
f8d378f Prepare schema
jakubvrana authored
87 }
88 echo "</div>\n";
89 }
90 foreach ($schema as $name => $table) {
d7d9c59 Change referenced to references
jakubvrana authored
91 foreach ((array) $table["references"] as $target_name => $refs) {
6636a61 Draggable tables in schema
jakubvrana authored
92 foreach ($refs as $left => $ref) {
38cb78a Show references in schema
jakubvrana authored
93 $min_pos = $top;
6636a61 Draggable tables in schema
jakubvrana authored
94 $max_pos = -10;
a055d87 Don't use PHP 5 only function (bug #2214423)
jakubvrana authored
95 foreach ($ref[0] as $key => $source) {
7c22767 Position lines
jakubvrana authored
96 $pos1 = $table["pos"][0] + $table["fields"][$source]["pos"];
a055d87 Don't use PHP 5 only function (bug #2214423)
jakubvrana authored
97 $pos2 = $schema[$target_name]["pos"][0] + $schema[$target_name]["fields"][$ref[1][$key]]["pos"];
38cb78a Show references in schema
jakubvrana authored
98 $min_pos = min($min_pos, $pos1, $pos2);
99 $max_pos = max($max_pos, $pos1, $pos2);
100 }
f203092 Typo
jakubvrana authored
101 echo "<div class='references' id='refl$left' style='left: $left" . "em; top: $min_pos" . "em; padding: .5em 0;'><div style='border-right: 1px solid Gray; margin-top: 1px; height: " . ($max_pos - $min_pos) . "em;'></div></div>\n";
38cb78a Show references in schema
jakubvrana authored
102 }
103 }
f8d378f Prepare schema
jakubvrana authored
104 }
105 ?>
106 </div>
989d758 Jakub Vrána Schema permanent link before mouse move
authored
107 <p><a href="<?php echo h(ME . "schema=" . urlencode($SCHEMA)); ?>" id="schema-link"><?php echo lang('Permanent link'); ?></a>
Something went wrong with that request. Please try again.