Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 107 lines (103 sloc) 5.042 kB
f8d378f Prepare schema
jakubvrana authored
1 <?php
c99c116 @vrana Display schema in title
authored
2 page_header(lang('Database schema'), "", array(), DB . ($_GET["ns"] ? ".$_GET[ns]" : ""));
f8d378f Prepare schema
jakubvrana authored
3
6636a61 Draggable tables in schema
jakubvrana authored
4 $table_pos = array();
5 $table_pos_js = array();
3eba731 @vrana Fix saving schema to cookie in Opera and Firefox 4
authored
6 $name = "adminer_schema";
989d758 @vrana Schema permanent link before mouse move
authored
7 $SCHEMA = ($_GET["schema"] ? $_GET["schema"] : $_COOKIE[($_COOKIE["$name-" . DB] ? "$name-" . DB : $name)]); // $_COOKIE["adminer_schema"] was used before 3.2.0 //! ':' in table name
8 preg_match_all('~([^:]+):([-0-9.]+)x([-0-9.]+)(_|$)~', $SCHEMA, $matches, PREG_SET_ORDER);
6636a61 Draggable tables in schema
jakubvrana authored
9 foreach ($matches as $i => $match) {
7c22767 Position lines
jakubvrana authored
10 $table_pos[$match[1]] = array($match[2], $match[3]);
28f21aa @vrana Escape JavaScript strings (bug #3093243)
authored
11 $table_pos_js[] = "\n\t'" . js_escape($match[1]) . "': [ $match[2], $match[3] ]";
6636a61 Draggable tables in schema
jakubvrana authored
12 }
13
14 $top = 0;
1eee0a1 Test $_COOKIE instead of SID (PHP bug #42135)
jakubvrana authored
15 $base_left = -1;
64ba924 Comments
jakubvrana authored
16 $schema = array(); // table => array("fields" => array(name => field), "pos" => array(top, left), "references" => array(table => array(left => array(source, target))))
17 $referenced = array(); // target_table => array(table => array(left => target_column))
18 $lefts = array(); // float => bool
5275ed8 @vrana Rename variable
authored
19 foreach (table_status() as $table_status) {
20 if (!isset($table_status["Engine"])) { // view
f8d378f Prepare schema
jakubvrana authored
21 continue;
22 }
6636a61 Draggable tables in schema
jakubvrana authored
23 $pos = 0;
5275ed8 @vrana Rename variable
authored
24 $schema[$table_status["Name"]]["fields"] = array();
25 foreach (fields($table_status["Name"]) as $name => $field) {
6636a61 Draggable tables in schema
jakubvrana authored
26 $pos += 1.25;
27 $field["pos"] = $pos;
5275ed8 @vrana Rename variable
authored
28 $schema[$table_status["Name"]]["fields"][$name] = $field;
6636a61 Draggable tables in schema
jakubvrana authored
29 }
5275ed8 @vrana Rename variable
authored
30 $schema[$table_status["Name"]]["pos"] = ($table_pos[$table_status["Name"]] ? $table_pos[$table_status["Name"]] : array($top, 0));
31 foreach ($adminer->foreignKeys($table_status["Name"]) as $val) {
753909e @vrana Support for virtual foreign keys
authored
32 if (!$val["db"]) {
33 $left = $base_left;
5275ed8 @vrana Rename variable
authored
34 if ($table_pos[$table_status["Name"]][1] || $table_pos[$val["table"]][1]) {
35 $left = min(floatval($table_pos[$table_status["Name"]][1]), floatval($table_pos[$val["table"]][1])) - 1;
753909e @vrana Support for virtual foreign keys
authored
36 } else {
37 $base_left -= .1;
f8d378f Prepare schema
jakubvrana authored
38 }
753909e @vrana Support for virtual foreign keys
authored
39 while ($lefts[(string) $left]) {
40 // find free $left
41 $left -= .0001;
42 }
5275ed8 @vrana Rename variable
authored
43 $schema[$table_status["Name"]]["references"][$val["table"]][(string) $left] = array($val["source"], $val["target"]);
44 $referenced[$val["table"]][$table_status["Name"]][(string) $left] = $val["target"];
753909e @vrana Support for virtual foreign keys
authored
45 $lefts[(string) $left] = true;
f8d378f Prepare schema
jakubvrana authored
46 }
47 }
5275ed8 @vrana Rename variable
authored
48 $top = max($top, $schema[$table_status["Name"]]["pos"][0] + 2.5 + $pos);
f8d378f Prepare schema
jakubvrana authored
49 }
6636a61 Draggable tables in schema
jakubvrana authored
50
f8d378f Prepare schema
jakubvrana authored
51 ?>
f539836 @vrana Disable selecting text on Database schema
authored
52 <div id="schema" style="height: <?php echo $top; ?>em;" onselectstart="return false;">
6636a61 Draggable tables in schema
jakubvrana authored
53 <script type="text/javascript">
b959ba4 @vrana Save bytes
authored
54 var tablePos = {<?php echo implode(",", $table_pos_js) . "\n"; ?>};
55 var em = document.getElementById('schema').offsetHeight / <?php echo $top; ?>;
3c5c0f0 Use camelCase in JavaScript
jakubvrana authored
56 document.onmousemove = schemaMousemove;
3eba731 @vrana Fix saving schema to cookie in Opera and Firefox 4
authored
57 document.onmouseup = function (ev) {
58 schemaMouseup(ev, '<?php echo js_escape(DB); ?>');
59 };
6636a61 Draggable tables in schema
jakubvrana authored
60 </script>
f8d378f Prepare schema
jakubvrana authored
61 <?php
38cb78a Show references in schema
jakubvrana authored
62 foreach ($schema as $name => $table) {
3c5c0f0 Use camelCase in JavaScript
jakubvrana authored
63 echo "<div class='table' style='top: " . $table["pos"][0] . "em; left: " . $table["pos"][1] . "em;' onmousedown='schemaMousedown(this, event);'>";
2212579 @vrana Improve readability of HTML
authored
64 echo '<a href="' . h(ME) . 'table=' . urlencode($name) . '"><b>' . h($name) . "</b></a>";
6636a61 Draggable tables in schema
jakubvrana authored
65 foreach ($table["fields"] as $field) {
b349612 Remove useless translations
jakubvrana authored
66 $val = '<span' . type_class($field["type"]) . ' title="' . h($field["full_type"] . ($field["null"] ? " NULL" : '')) . '">' . h($field["field"]) . '</span>';
2212579 @vrana Improve readability of HTML
authored
67 echo "<br>" . ($field["primary"] ? "<i>$val</i>" : $val);
6636a61 Draggable tables in schema
jakubvrana authored
68 }
69 foreach ((array) $table["references"] as $target_name => $refs) {
a055d87 Don't use PHP 5 only function (bug #2214423)
jakubvrana authored
70 foreach ($refs as $left => $ref) {
256438e Set vertical lines only once
jakubvrana authored
71 $left1 = $left - $table_pos[$name][1];
81dbdda Move references lines
jakubvrana authored
72 $i = 0;
a055d87 Don't use PHP 5 only function (bug #2214423)
jakubvrana authored
73 foreach ($ref[0] as $source) {
2212579 @vrana Improve readability of HTML
authored
74 echo "\n<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>";
6636a61 Draggable tables in schema
jakubvrana authored
75 }
76 }
77 }
1eee0a1 Test $_COOKIE instead of SID (PHP bug #42135)
jakubvrana authored
78 foreach ((array) $referenced[$name] as $target_name => $refs) {
79 foreach ($refs as $left => $columns) {
256438e Set vertical lines only once
jakubvrana authored
80 $left1 = $left - $table_pos[$name][1];
81dbdda Move references lines
jakubvrana authored
81 $i = 0;
1eee0a1 Test $_COOKIE instead of SID (PHP bug #42135)
jakubvrana authored
82 foreach ($columns as $target) {
2212579 @vrana Improve readability of HTML
authored
83 echo "\n<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>";
1eee0a1 Test $_COOKIE instead of SID (PHP bug #42135)
jakubvrana authored
84 }
6636a61 Draggable tables in schema
jakubvrana authored
85 }
f8d378f Prepare schema
jakubvrana authored
86 }
2212579 @vrana Improve readability of HTML
authored
87 echo "\n</div>\n";
f8d378f Prepare schema
jakubvrana authored
88 }
89 foreach ($schema as $name => $table) {
d7d9c59 Change referenced to references
jakubvrana authored
90 foreach ((array) $table["references"] as $target_name => $refs) {
6636a61 Draggable tables in schema
jakubvrana authored
91 foreach ($refs as $left => $ref) {
38cb78a Show references in schema
jakubvrana authored
92 $min_pos = $top;
6636a61 Draggable tables in schema
jakubvrana authored
93 $max_pos = -10;
a055d87 Don't use PHP 5 only function (bug #2214423)
jakubvrana authored
94 foreach ($ref[0] as $key => $source) {
7c22767 Position lines
jakubvrana authored
95 $pos1 = $table["pos"][0] + $table["fields"][$source]["pos"];
a055d87 Don't use PHP 5 only function (bug #2214423)
jakubvrana authored
96 $pos2 = $schema[$target_name]["pos"][0] + $schema[$target_name]["fields"][$ref[1][$key]]["pos"];
38cb78a Show references in schema
jakubvrana authored
97 $min_pos = min($min_pos, $pos1, $pos2);
98 $max_pos = max($max_pos, $pos1, $pos2);
99 }
f203092 Typo
jakubvrana authored
100 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
101 }
102 }
f8d378f Prepare schema
jakubvrana authored
103 }
104 ?>
105 </div>
989d758 @vrana Schema permanent link before mouse move
authored
106 <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.