Add IF NOT EXISTS to CREATE DATABASE query (RFE #1608372), reload navigation after...
[phpmyadmin/crack.git] / pmd_common.php
blob33249f4ae8e926a06df1c66d7d81dcdb2f6f1ecf
1 <?php
2 /* $Id$ */
3 // vim: expandtab sw=4 ts=4 sts=4:
5 /*
6 @author Ivan A Kirillov (develop.php@gmail.com)
7 */
9 require_once './libraries/common.lib.php';
10 $GLOBALS['PMD']['STYLE'] = 'default';
12 require_once './libraries/relation.lib.php';
13 $cfgRelation = PMA_getRelationsParam();
15 function get_tabs() // PMA_DBI
17 global $db;
18 $GLOBALS['PMD']['TABLE_NAME'] = array();// that foreach no error
19 $GLOBALS['PMD']['OWNER'] = array();
20 $GLOBALS['PMD']['TABLE_NAME_SMALL'] = array();
22 $tables = PMA_DBI_get_tables_full($db);
23 // seems to be needed later
24 PMA_DBI_select_db($db);
25 $i = 0;
26 foreach ($tables as $one_table) {
27 $GLOBALS['PMD']['TABLE_NAME'][$i] = $db . "." . $one_table['TABLE_NAME'];
28 $GLOBALS['PMD']['OWNER'][$i] = $db;
29 $GLOBALS['PMD']['TABLE_NAME_SMALL'][$i] = $one_table['TABLE_NAME'];
30 $GLOBALS['PMD']['TABLE_TYPE'][$i] = strtoupper($one_table['ENGINE']);
31 $i++;
33 // return $GLOBALS['PMD']; // many bases // not use ??????
36 function get_tab_info() // PMA_DBI //PMA_backquote
38 global $db;
39 PMA_DBI_select_db($db);
40 $tab_column = array();
41 for ( $i=0; $i < sizeof( $GLOBALS['PMD']["TABLE_NAME"] ); $i++ ) {
42 PMA_DBI_select_db($db);
43 $fields_rs = PMA_DBI_query('SHOW FULL FIELDS FROM '.PMA_backquote($GLOBALS['PMD']["TABLE_NAME_SMALL"][$i]), NULL, PMA_DBI_QUERY_STORE);
44 $fields_cnt = PMA_DBI_num_rows($fields_rs);
45 $j=0;
46 while ($row = PMA_DBI_fetch_assoc($fields_rs)) {
47 $tab_column[$GLOBALS['PMD']['TABLE_NAME'][$i]]['COLUMN_ID'][$j] = $j;
48 $tab_column[$GLOBALS['PMD']['TABLE_NAME'][$i]]['COLUMN_NAME'][$j] = $row['Field'];
49 $tab_column[$GLOBALS['PMD']['TABLE_NAME'][$i]]['TYPE'][$j] = $row['Type'];
50 $tab_column[$GLOBALS['PMD']['TABLE_NAME'][$i]]['NULLABLE'][$j] = $row['Null'];
51 $j++;
54 return $tab_column;
56 //-------------------------------------CONTR-----------------------------------------------
57 function get_script_contr() {
58 global $db;
59 PMA_DBI_select_db($db);
60 $con["C_NAME"] = array();
61 $i = 0;
62 $alltab_rs = PMA_DBI_query('SHOW TABLES FROM ' . PMA_backquote($db), NULL, PMA_DBI_QUERY_STORE);
63 while ($val = @PMA_DBI_fetch_row($alltab_rs)) {
64 $row = PMA_getForeigners($db,$val[0],'','internal');
65 if ($row !== false) {
66 foreach ($row as $field => $value) {
67 $con['C_NAME'][$i] = '';
68 $con['DTN'][$i] = $db . "." . $val[0];
69 $con['DCN'][$i] = $field;
70 $con['STN'][$i] = $value['foreign_db'] . "." . $value['foreign_table'];
71 $con['SCN'][$i] = $value['foreign_field'];
72 $i++;
75 $row = PMA_getForeigners($db,$val[0],'','innodb');
76 if ($row !== false) {
77 foreach ($row as $field => $value) {
78 $con['C_NAME'][$i] = '';
79 $con['DTN'][$i] = $db.".".$val[0];
80 $con['DCN'][$i] = $field;
81 $con['STN'][$i] = $value['foreign_db'].".".$value['foreign_table'];
82 $con['SCN'][$i] = $value['foreign_field'];
83 $i++;
88 $ti = 0;
89 $script_contr = "<script> var contr = new Array();";
90 for ( $i=0; $i < sizeof( $con["C_NAME"] ); $i++ ) {
91 $script_contr .= " contr[$ti] = new Array();\n";
92 $script_contr .= " contr[$ti]['".$con['C_NAME'][$i]."'] = new Array();\n";
93 if (in_array($con['DTN'][$i],$GLOBALS['PMD']["TABLE_NAME"]) && in_array($con['STN'][$i],$GLOBALS['PMD']["TABLE_NAME"])) {
94 $script_contr .= " contr[$ti]['".$con['C_NAME'][$i]."']['".$con['DTN'][$i]."'] = new Array();\n";$m_col = array();//}
95 $script_contr .= " contr[$ti]['".$con['C_NAME'][$i]."']['".$con['DTN'][$i]."']['".$con['DCN'][$i]."'] = new Array();\n";//}
96 $script_contr .= " contr[$ti]['".$con['C_NAME'][$i]."']['".$con['DTN'][$i]."']['".$con['DCN'][$i]."'][0] = '".$con['STN'][$i]."';\n"; //
97 $script_contr .= " contr[$ti]['".$con['C_NAME'][$i]."']['".$con['DTN'][$i]."']['".$con['DCN'][$i]."'][1] = '".$con['SCN'][$i]."';\n"; //
99 $ti++;
101 $script_contr .= "</script>";
102 return $script_contr;
105 function get_pk_or_unique_keys() {
106 global $db;
107 require_once('./libraries/tbl_indexes.lib.php');
109 PMA_DBI_select_db($db);
110 $tables_pk_or_unique_keys = array();
112 for( $I=0; $I<sizeof($GLOBALS['PMD']['TABLE_NAME_SMALL']); $I++) {
113 $ret_keys = PMA_get_indexes($GLOBALS['PMD']['TABLE_NAME_SMALL'][$I]);
114 if (! empty($ret_keys)) {
115 // reset those as the function uses them by reference
116 $indexes = $indexes_info = $indexes_data = array();
117 PMA_extract_indexes($ret_keys, $indexes, $indexes_info, $indexes_data);
118 // for now, take into account only the first index segment
119 foreach ($indexes_data as $key_name => $one_index) {
120 $column_name = $one_index[1]['Column_name'];
121 if (isset($indexes_info[$key_name]) && $indexes_info[$key_name]['Non_unique'] == 0) {
122 $tables_pk_or_unique_keys[$GLOBALS['PMD']['OWNER'][$I] . '.' .$GLOBALS['PMD']['TABLE_NAME_SMALL'][$I] . '.' . $column_name] = 1;
127 return $tables_pk_or_unique_keys;
130 function get_all_keys() {
131 global $db;
132 require_once('./libraries/tbl_indexes.lib.php');
134 PMA_DBI_select_db($db);
135 $tables_all_keys = array();
137 for( $I=0; $I<sizeof($GLOBALS['PMD']['TABLE_NAME_SMALL']); $I++) {
138 $ret_keys = PMA_get_indexes($GLOBALS['PMD']['TABLE_NAME_SMALL'][$I]);
139 if (! empty($ret_keys)) {
140 // reset those as the function uses them by reference
141 $indexes = $indexes_info = $indexes_data = array();
142 PMA_extract_indexes($ret_keys, $indexes, $indexes_info, $indexes_data);
143 // for now, take into account only the first index segment
144 foreach ($indexes_data as $one_index) {
145 $column_name = $one_index[1]['Column_name'];
146 $tables_all_keys[$GLOBALS['PMD']['OWNER'][$I] . '.' .$GLOBALS['PMD']['TABLE_NAME_SMALL'][$I] . '.' . $column_name] = 1;
150 return $tables_all_keys;
153 function get_script_tabs() {
154 $script_tabs = "<script> var j_tabs = new Array();\n";
155 for ( $i=0; $i < sizeof( $GLOBALS['PMD']['TABLE_NAME'] ); $i++ ) {
156 $script_tabs .= "j_tabs['".$GLOBALS['PMD']['TABLE_NAME'][$i]."'] = '".$GLOBALS['PMD']['TABLE_TYPE'][$i]."';\n";
158 $script_tabs .= "</script>";
159 return $script_tabs;
162 function get_tab_pos() {
163 $stmt = PMA_query_as_cu("SELECT * FROM " . PMA_backquote($GLOBALS['cfgRelation']['designer_coords']), FALSE, PMA_DBI_QUERY_STORE);
164 if ( $stmt ) // exist table repository
166 while ($t_p = PMA_DBI_fetch_array($stmt, MYSQL_ASSOC)) {
167 $t_name = $t_p['db_name'] . '.' . $t_p['table_name'];
168 $tab_pos[ $t_name ]['X'] = $t_p['x'];
169 $tab_pos[ $t_name ]['Y'] = $t_p['y'];
170 $tab_pos[ $t_name ]['V'] = $t_p['v'];
171 $tab_pos[ $t_name ]['H'] = $t_p['h'];
174 return isset($tab_pos) ? $tab_pos : NULL;
177 function get_owners() {
178 $m = array();
179 $j = 0;
180 for ( $i=0; $i < sizeof( $GLOBALS['PMD']["OWNER"] ); $i++ ) {
181 if( ! in_array($GLOBALS['PMD']["OWNER"][$i],$m)) {
182 $m[$j++] = $GLOBALS['PMD']["OWNER"][$i];
185 return $m;
188 get_tabs();