2.11.11-dev
[phpmyadmin/dkf.git] / pmd_common.php
blob23f2855123f2c58520885841c3935c6510382246
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * @author Ivan A Kirillov (Ivan.A.Kirillov@gmail.com)
5 * @version $Id$
6 * @package phpMyAdmin-Designer
7 */
9 /**
12 require_once './libraries/common.inc.php';
13 // not understand
14 require_once './libraries/header_http.inc.php';
16 $GLOBALS['PMD']['STYLE'] = 'default';
18 require_once './libraries/relation.lib.php';
19 $cfgRelation = PMA_getRelationsParam();
21 $GLOBALS['script_display_field'] =
22 '<script type="text/javascript">' . "\n" .
23 '// <![CDATA[' . "\n" .
24 'var display_field = new Array();' . "\n";
26 /**
27 * retrieves table info and stores it in $GLOBALS['PMD']
29 * @uses $GLOBALS['script_display_field']
30 * @uses $GLOBALS['PMD'] to fill it
31 * @uses $GLOBALS['db']
32 * @uses PMA_DBI_get_tables_full()
33 * @uses PMA_DBI_select_db()
34 * @uses PMA_getDisplayField()
35 * @uses urlencode()
36 * @uses htmlspecialchars()
37 * @uses strtoupper()
38 * @uses urlencode()
40 function get_tabs()
42 $GLOBALS['PMD']['TABLE_NAME'] = array();// that foreach no error
43 $GLOBALS['PMD']['OWNER'] = array();
44 $GLOBALS['PMD']['TABLE_NAME_SMALL'] = array();
46 $tables = PMA_DBI_get_tables_full($GLOBALS['db']);
47 // seems to be needed later
48 PMA_DBI_select_db($GLOBALS['db']);
49 $i = 0;
50 foreach ($tables as $one_table) {
51 $GLOBALS['PMD']['TABLE_NAME'][$i] = $GLOBALS['db'] . "." . $one_table['TABLE_NAME'];
52 $GLOBALS['PMD']['OWNER'][$i] = $GLOBALS['db'];
53 $GLOBALS['PMD']['TABLE_NAME_SMALL'][$i] = $one_table['TABLE_NAME'];
55 $GLOBALS['PMD_URL']['TABLE_NAME'][$i] = urlencode($GLOBALS['db'] . "." . $one_table['TABLE_NAME']);
56 $GLOBALS['PMD_URL']['OWNER'][$i] = urlencode($GLOBALS['db']);
57 $GLOBALS['PMD_URL']['TABLE_NAME_SMALL'][$i] = urlencode($one_table['TABLE_NAME']);
59 $GLOBALS['PMD_OUT']['TABLE_NAME'][$i] = htmlspecialchars($GLOBALS['db'] . "." . $one_table['TABLE_NAME'], ENT_QUOTES);
60 $GLOBALS['PMD_OUT']['OWNER'][$i] = htmlspecialchars($GLOBALS['db'], ENT_QUOTES);
61 $GLOBALS['PMD_OUT']['TABLE_NAME_SMALL'][$i] = htmlspecialchars($one_table['TABLE_NAME'], ENT_QUOTES);
63 $GLOBALS['PMD']['TABLE_TYPE'][$i] = strtoupper($one_table['ENGINE']);
65 $DF = PMA_getDisplayField($GLOBALS['db'], $one_table['TABLE_NAME']);
66 if ($DF != '') {
67 $GLOBALS['script_display_field'] .= " display_field['"
68 . $GLOBALS['PMD_URL']["TABLE_NAME_SMALL"][$i] . "'] = '"
69 . urlencode($DF) . "';\n";
72 $i++;
74 $GLOBALS['script_display_field'] .=
75 '// ]]>' . "\n" .
76 '</script>' . "\n";
77 // return $GLOBALS['PMD']; // many bases // not use ??????
80 /**
81 * retrieves table column info
83 * @uses $GLOBALS['db']
84 * @uses PMA_DBI_QUERY_STORE
85 * @uses PMA_DBI_select_db()
86 * @uses PMA_DBI_query()
87 * @uses PMA_DBI_num_rows()
88 * @uses PMA_backquote()
89 * @uses count()
90 * @return array table column nfo
92 function get_tab_info()
94 PMA_DBI_select_db($GLOBALS['db']);
95 $tab_column = array();
96 for ($i = 0; $i < count($GLOBALS['PMD']["TABLE_NAME"]); $i++) {
97 $fields_rs = PMA_DBI_query('SHOW FULL FIELDS FROM '.PMA_backquote($GLOBALS['PMD']["TABLE_NAME_SMALL"][$i]), NULL, PMA_DBI_QUERY_STORE);
98 $j = 0;
99 while ($row = PMA_DBI_fetch_assoc($fields_rs)) {
100 $tab_column[$GLOBALS['PMD']['TABLE_NAME'][$i]]['COLUMN_ID'][$j] = $j;
101 $tab_column[$GLOBALS['PMD']['TABLE_NAME'][$i]]['COLUMN_NAME'][$j] = $row['Field'];
102 $tab_column[$GLOBALS['PMD']['TABLE_NAME'][$i]]['TYPE'][$j] = $row['Type'];
103 $tab_column[$GLOBALS['PMD']['TABLE_NAME'][$i]]['NULLABLE'][$j] = $row['Null'];
104 $j++;
107 return $tab_column;
111 * returns JavaScript code for intializing vars
113 * @uses $GLOBALS['db']
114 * @uses PMA_DBI_QUERY_STORE
115 * @uses PMA_DBI_select_db()
116 * @uses PMA_DBI_query()
117 * @uses PMA_backquote()
118 * @uses PMA_DBI_fetch_row()
119 * @uses PMA_getForeigners()
120 * @uses urlencode()
121 * @uses count()
122 * @uses in_array()
123 * @return string JavaScript code
125 function get_script_contr()
127 PMA_DBI_select_db($GLOBALS['db']);
128 $con["C_NAME"] = array();
129 $i = 0;
130 $alltab_rs = PMA_DBI_query('SHOW TABLES FROM ' . PMA_backquote($GLOBALS['db']), NULL, PMA_DBI_QUERY_STORE);
131 while ($val = @PMA_DBI_fetch_row($alltab_rs)) {
132 $row = PMA_getForeigners($GLOBALS['db'], $val[0], '', 'internal');
133 //echo "<br> internal ".$GLOBALS['db']." - ".$val[0]." - ";
134 //print_r($row);
135 if ($row !== false) {
136 foreach ($row as $field => $value) {
137 $con['C_NAME'][$i] = '';
138 $con['DTN'][$i] = urlencode($GLOBALS['db'] . "." . $val[0]);
139 $con['DCN'][$i] = urlencode($field);
140 $con['STN'][$i] = urlencode($value['foreign_db'] . "." . $value['foreign_table']);
141 $con['SCN'][$i] = urlencode($value['foreign_field']);
142 $i++;
145 $row = PMA_getForeigners($GLOBALS['db'], $val[0], '', 'innodb');
146 //echo "<br> INNO ";
147 //print_r($row);
148 if ($row !== false) {
149 foreach ($row as $field => $value) {
150 $con['C_NAME'][$i] = '';
151 $con['DTN'][$i] = urlencode($GLOBALS['db'].".".$val[0]);
152 $con['DCN'][$i] = urlencode($field);
153 $con['STN'][$i] = urlencode($value['foreign_db'].".".$value['foreign_table']);
154 $con['SCN'][$i] = urlencode($value['foreign_field']);
155 $i++;
160 $ti = 0;
161 $script_contr =
162 '<script type="text/javascript">' . "\n" .
163 '// <![CDATA[' . "\n" .
164 'var contr = new Array();' . "\n";
165 for ($i = 0; $i < count($con["C_NAME"]); $i++) {
166 $js_var = ' contr[' . $ti . ']';
167 $script_contr .= $js_var . " = new Array();\n";
168 $js_var .= "['" . $con['C_NAME'][$i] . "']";
169 $script_contr .= $js_var . " = new Array();\n";
170 if (in_array($con['DTN'][$i], $GLOBALS['PMD_URL']["TABLE_NAME"])
171 && in_array($con['STN'][$i], $GLOBALS['PMD_URL']["TABLE_NAME"])) {
172 $js_var .= "['" . $con['DTN'][$i] . "']";
173 $script_contr .= $js_var . " = new Array();\n";
174 $m_col = array();//}
175 $js_var .= "['" . $con['DCN'][$i] . "']";
176 $script_contr .= $js_var . " = new Array();\n";//}
177 $script_contr .= $js_var . "[0] = '" . $con['STN'][$i] . "';\n"; //
178 $script_contr .= $js_var . "[1] = '" . $con['SCN'][$i] . "';\n"; //
180 $ti++;
182 $script_contr .=
183 '// ]]>' . "\n" .
184 '</script>' . "\n";
185 return $script_contr;
189 * @uses $GLOBALS['db']
190 * @uses $GLOBALS['PMD']
191 * @uses PMA_DBI_select_db()
192 * @uses PMA_get_indexes()
193 * @uses PMA_extract_indexes()
194 * @uses count()
195 * @return array unique or primary indizes
197 function get_pk_or_unique_keys()
199 require_once './libraries/tbl_indexes.lib.php';
201 PMA_DBI_select_db($GLOBALS['db']);
202 $tables_pk_or_unique_keys = array();
204 for ($I = 0; $I < count($GLOBALS['PMD']['TABLE_NAME_SMALL']); $I++) {
205 $ret_keys = PMA_get_indexes($GLOBALS['PMD']['TABLE_NAME_SMALL'][$I]);
206 if (! empty($ret_keys)) {
207 // reset those as the function uses them by reference
208 $indexes = $indexes_info = $indexes_data = array();
209 PMA_extract_indexes($ret_keys, $indexes, $indexes_info, $indexes_data);
210 // for now, take into account only the first index segment
211 foreach ($indexes_data as $key_name => $one_index) {
212 $column_name = $one_index[1]['Column_name'];
213 if (isset($indexes_info[$key_name])
214 && $indexes_info[$key_name]['Non_unique'] == 0) {
215 $tables_pk_or_unique_keys[$GLOBALS['PMD']['OWNER'][$I] . '.' .$GLOBALS['PMD']['TABLE_NAME_SMALL'][$I] . '.' . $column_name] = 1;
220 return $tables_pk_or_unique_keys;
224 * returns all indizes
226 * @uses $GLOBALS['db']
227 * @uses $GLOBALS['PMD']
228 * @uses PMA_DBI_select_db()
229 * @uses PMA_get_indexes()
230 * @uses PMA_extract_indexes()
231 * @uses count()
232 * @return array indizes
234 function get_all_keys()
236 require_once './libraries/tbl_indexes.lib.php';
238 PMA_DBI_select_db($GLOBALS['db']);
239 $tables_all_keys = array();
241 for ($I = 0; $I < count($GLOBALS['PMD']['TABLE_NAME_SMALL']); $I++) {
242 $ret_keys = PMA_get_indexes($GLOBALS['PMD']['TABLE_NAME_SMALL'][$I]);
243 if (! empty($ret_keys)) {
244 // reset those as the function uses them by reference
245 $indexes = $indexes_info = $indexes_data = array();
246 PMA_extract_indexes($ret_keys, $indexes, $indexes_info, $indexes_data);
247 // for now, take into account only the first index segment
248 foreach ($indexes_data as $one_index) {
249 $column_name = $one_index[1]['Column_name'];
250 $tables_all_keys[$GLOBALS['PMD']['OWNER'][$I] . '.' .$GLOBALS['PMD']['TABLE_NAME_SMALL'][$I] . '.' . $column_name] = 1;
254 return $tables_all_keys;
260 * @uses $GLOBALS['PMD']
261 * @uses count()
262 * @uses in_array()
263 * @return array ???
265 function get_script_tabs()
267 $script_tabs =
268 '<script type="text/javascript">' . "\n" .
269 '// <![CDATA[' . "\n" .
270 'var j_tabs = new Array();' . "\n";
271 for ($i = 0; $i < count($GLOBALS['PMD']['TABLE_NAME']); $i++) {
272 $script_tabs .= "j_tabs['" . $GLOBALS['PMD_URL']['TABLE_NAME'][$i] . "'] = '"
273 . $GLOBALS['PMD']['TABLE_TYPE'][$i] . "';\n";
275 $script_tabs .=
276 '// ]]>' . "\n" .
277 '</script>' . "\n";
278 return $script_tabs;
282 * @uses $GLOBALS['controllink']
283 * @uses $cfgRelation['designerwork']
284 * @uses $cfgRelation['db']
285 * @uses $cfgRelation['designer_coords']
286 * @uses PMA_DBI_QUERY_STORE
287 * @uses PMA_getRelationsParam()
288 * @uses PMA_backquote()
289 * @uses PMA_DBI_fetch_result()
290 * @uses count()
291 * @return array table positions and sizes
293 function get_tab_pos()
295 $cfgRelation = PMA_getRelationsParam();
297 if (! $cfgRelation['designerwork']) {
298 return null;
301 $query = "
302 SELECT CONCAT_WS('.', `db_name`, `table_name`) AS `name`,
303 `x` AS `X`,
304 `y` AS `Y`,
305 `v` AS `V`,
306 `h` AS `H`
307 FROM " . PMA_backquote($cfgRelation['db']) . "." . PMA_backquote($cfgRelation['designer_coords']);
308 $tab_pos = PMA_DBI_fetch_result($query, 'name', null, $GLOBALS['controllink'], PMA_DBI_QUERY_STORE);
309 return count($tab_pos) ? $tab_pos : null;
313 * returns distinct values from $GLOBALS['PMD']['OWNER']
315 * @uses array_values()
316 * @uses array_unique()
317 * @uses $GLOBALS['PMD']['OWNER']
318 * @return array owner
320 function get_owners()
322 return array_values(array_unique($GLOBALS['PMD']['OWNER']));
325 get_tabs();