Fix data sync in replication scenario
[phpmyadmin.git] / pmd_common.php
blob9205349aaf8e2f3b9886071e45beccf9dff2613e
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++;
75 $GLOBALS['script_display_field'] .=
76 '// ]]>' . "\n" .
77 '</script>' . "\n";
78 // return $GLOBALS['PMD']; // many bases // not use ??????
81 /**
82 * retrieves table column info
84 * @uses $GLOBALS['db']
85 * @uses PMA_DBI_QUERY_STORE
86 * @uses PMA_DBI_select_db()
87 * @uses PMA_DBI_query()
88 * @uses PMA_DBI_num_rows()
89 * @uses PMA_backquote()
90 * @uses count()
91 * @return array table column nfo
93 function get_tab_info()
95 PMA_DBI_select_db($GLOBALS['db']);
96 $tab_column = array();
97 for ($i = 0, $cnt = count($GLOBALS['PMD']["TABLE_NAME"]); $i < $cnt; $i++) {
98 $fields_rs = PMA_DBI_query('SHOW FULL FIELDS FROM '.PMA_backquote($GLOBALS['PMD']["TABLE_NAME_SMALL"][$i]), NULL, PMA_DBI_QUERY_STORE);
99 $j = 0;
100 while ($row = PMA_DBI_fetch_assoc($fields_rs)) {
101 $tab_column[$GLOBALS['PMD']['TABLE_NAME'][$i]]['COLUMN_ID'][$j] = $j;
102 $tab_column[$GLOBALS['PMD']['TABLE_NAME'][$i]]['COLUMN_NAME'][$j] = $row['Field'];
103 $tab_column[$GLOBALS['PMD']['TABLE_NAME'][$i]]['TYPE'][$j] = $row['Type'];
104 $tab_column[$GLOBALS['PMD']['TABLE_NAME'][$i]]['NULLABLE'][$j] = $row['Null'];
105 $j++;
108 return $tab_column;
112 * returns JavaScript code for intializing vars
114 * @uses $GLOBALS['db']
115 * @uses PMA_DBI_QUERY_STORE
116 * @uses PMA_DBI_select_db()
117 * @uses PMA_DBI_query()
118 * @uses PMA_backquote()
119 * @uses PMA_DBI_fetch_row()
120 * @uses PMA_getForeigners()
121 * @uses urlencode()
122 * @uses count()
123 * @uses in_array()
124 * @return string JavaScript code
126 function get_script_contr()
128 PMA_DBI_select_db($GLOBALS['db']);
129 $con["C_NAME"] = array();
130 $i = 0;
131 $alltab_rs = PMA_DBI_query('SHOW TABLES FROM ' . PMA_backquote($GLOBALS['db']), NULL, PMA_DBI_QUERY_STORE);
132 while ($val = @PMA_DBI_fetch_row($alltab_rs)) {
133 $row = PMA_getForeigners($GLOBALS['db'], $val[0], '', 'internal');
134 //echo "<br> internal ".$GLOBALS['db']." - ".$val[0]." - ";
135 //print_r($row);
136 if ($row !== false) {
137 foreach ($row as $field => $value) {
138 $con['C_NAME'][$i] = '';
139 $con['DTN'][$i] = urlencode($GLOBALS['db'] . "." . $val[0]);
140 $con['DCN'][$i] = urlencode($field);
141 $con['STN'][$i] = urlencode($value['foreign_db'] . "." . $value['foreign_table']);
142 $con['SCN'][$i] = urlencode($value['foreign_field']);
143 $i++;
146 $row = PMA_getForeigners($GLOBALS['db'], $val[0], '', 'foreign');
147 //echo "<br> INNO ";
148 //print_r($row);
149 if ($row !== false) {
150 foreach ($row as $field => $value) {
151 $con['C_NAME'][$i] = '';
152 $con['DTN'][$i] = urlencode($GLOBALS['db'].".".$val[0]);
153 $con['DCN'][$i] = urlencode($field);
154 $con['STN'][$i] = urlencode($value['foreign_db'].".".$value['foreign_table']);
155 $con['SCN'][$i] = urlencode($value['foreign_field']);
156 $i++;
161 $ti = 0;
162 $script_contr =
163 '<script type="text/javascript">' . "\n" .
164 '// <![CDATA[' . "\n" .
165 'var contr = new Array();' . "\n";
166 for ($i = 0, $cnt = count($con["C_NAME"]); $i < $cnt; $i++) {
167 $js_var = ' contr[' . $ti . ']';
168 $script_contr .= $js_var . " = new Array();\n";
169 $js_var .= "['" . $con['C_NAME'][$i] . "']";
170 $script_contr .= $js_var . " = new Array();\n";
171 if (in_array($con['DTN'][$i], $GLOBALS['PMD_URL']["TABLE_NAME"])
172 && in_array($con['STN'][$i], $GLOBALS['PMD_URL']["TABLE_NAME"])) {
173 $js_var .= "['" . $con['DTN'][$i] . "']";
174 $script_contr .= $js_var . " = new Array();\n";
175 $m_col = array();//}
176 $js_var .= "['" . $con['DCN'][$i] . "']";
177 $script_contr .= $js_var . " = new Array();\n";//}
178 $script_contr .= $js_var . "[0] = '" . $con['STN'][$i] . "';\n"; //
179 $script_contr .= $js_var . "[1] = '" . $con['SCN'][$i] . "';\n"; //
181 $ti++;
183 $script_contr .=
184 '// ]]>' . "\n" .
185 '</script>' . "\n";
186 return $script_contr;
190 * @uses get_all_keys()
191 * @return array unique or primary indizes
193 function get_pk_or_unique_keys()
195 return get_all_keys(true);
199 * returns all indizes
201 * @uses $GLOBALS['PMD']
202 * @uses PMA_Index::getFromTable()
203 * @uses PMA_Index->isUnique()
204 * @uses PMA_Index->getColumns()
205 * @param boolean whether to include ony unique ones
206 * @return array indizes
208 function get_all_keys($unique_only = false)
210 require_once './libraries/Index.class.php';
212 $keys = array();
214 foreach ($GLOBALS['PMD']['TABLE_NAME_SMALL'] as $I => $table) {
215 $schema = $GLOBALS['PMD']['OWNER'][$I];
216 // for now, take into account only the first index segment
217 foreach (PMA_Index::getFromTable($table, $schema) as $index) {
218 if ($unique_only && ! $index->isUnique()) {
219 continue;
221 $columns = $index->getColumns();
222 foreach ($columns as $column_name => $dummy) {
223 $keys[$schema . '.' .$table . '.' . $column_name] = 1;
227 return $keys;
233 * @uses $GLOBALS['PMD']
234 * @uses count()
235 * @uses in_array()
236 * @return array ???
238 function get_script_tabs()
240 $script_tabs =
241 '<script type="text/javascript">' . "\n" .
242 '// <![CDATA[' . "\n" .
243 'var j_tabs = new Array();' . "\n";
244 for ($i = 0, $cnt = count($GLOBALS['PMD']['TABLE_NAME']); $i < $cnt; $i++) {
245 $script_tabs .= "j_tabs['" . $GLOBALS['PMD_URL']['TABLE_NAME'][$i] . "'] = '"
246 . (PMA_foreignkey_supported($GLOBALS['PMD']['TABLE_TYPE'][$i]) ? '1' : '0') . "';\n";
248 $script_tabs .=
249 '// ]]>' . "\n" .
250 '</script>' . "\n";
251 return $script_tabs;
255 * @uses $GLOBALS['controllink']
256 * @uses $cfgRelation['designerwork']
257 * @uses $cfgRelation['db']
258 * @uses $cfgRelation['designer_coords']
259 * @uses PMA_DBI_QUERY_STORE
260 * @uses PMA_getRelationsParam()
261 * @uses PMA_backquote()
262 * @uses PMA_DBI_fetch_result()
263 * @uses count()
264 * @return array table positions and sizes
266 function get_tab_pos()
268 $cfgRelation = PMA_getRelationsParam();
270 if (! $cfgRelation['designerwork']) {
271 return null;
274 $query = "
275 SELECT CONCAT_WS('.', `db_name`, `table_name`) AS `name`,
276 `x` AS `X`,
277 `y` AS `Y`,
278 `v` AS `V`,
279 `h` AS `H`
280 FROM " . PMA_backquote($cfgRelation['db']) . "." . PMA_backquote($cfgRelation['designer_coords']);
281 $tab_pos = PMA_DBI_fetch_result($query, 'name', null, $GLOBALS['controllink'], PMA_DBI_QUERY_STORE);
282 return count($tab_pos) ? $tab_pos : null;
286 * returns distinct values from $GLOBALS['PMD']['OWNER']
288 * @uses array_values()
289 * @uses array_unique()
290 * @uses $GLOBALS['PMD']['OWNER']
291 * @return array owner
293 function get_owners()
295 return array_values(array_unique($GLOBALS['PMD']['OWNER']));
298 get_tabs();