pmd_common.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Common functions for Designer
  5. *
  6. * @package PhpMyAdmin-Designer
  7. */
  8. /**
  9. * Block attempts to directly run this script
  10. */
  11. if (! defined('PHPMYADMIN')) {
  12. exit;
  13. }
  14. $GLOBALS['PMD']['STYLE'] = 'default';
  15. $cfgRelation = PMA_getRelationsParam();
  16. /**
  17. * Retrieves table info and stores it in $GLOBALS['PMD']
  18. *
  19. * @return array with table info
  20. */
  21. function PMA_getTablesInfo()
  22. {
  23. $retval = array();
  24. $GLOBALS['PMD']['TABLE_NAME'] = array();// that foreach no error
  25. $GLOBALS['PMD']['OWNER'] = array();
  26. $GLOBALS['PMD']['TABLE_NAME_SMALL'] = array();
  27. $tables = $GLOBALS['dbi']->getTablesFull($GLOBALS['db']);
  28. // seems to be needed later
  29. $GLOBALS['dbi']->selectDb($GLOBALS['db']);
  30. $i = 0;
  31. foreach ($tables as $one_table) {
  32. $GLOBALS['PMD']['TABLE_NAME'][$i]
  33. = $GLOBALS['db'] . "." . $one_table['TABLE_NAME'];
  34. $GLOBALS['PMD']['OWNER'][$i] = $GLOBALS['db'];
  35. $GLOBALS['PMD']['TABLE_NAME_SMALL'][$i] = $one_table['TABLE_NAME'];
  36. $GLOBALS['PMD_URL']['TABLE_NAME'][$i]
  37. = urlencode($GLOBALS['db'] . "." . $one_table['TABLE_NAME']);
  38. $GLOBALS['PMD_URL']['OWNER'][$i] = urlencode($GLOBALS['db']);
  39. $GLOBALS['PMD_URL']['TABLE_NAME_SMALL'][$i]
  40. = urlencode($one_table['TABLE_NAME']);
  41. $GLOBALS['PMD_OUT']['TABLE_NAME'][$i] = htmlspecialchars(
  42. $GLOBALS['db'] . "." . $one_table['TABLE_NAME'], ENT_QUOTES
  43. );
  44. $GLOBALS['PMD_OUT']['OWNER'][$i] = htmlspecialchars(
  45. $GLOBALS['db'], ENT_QUOTES
  46. );
  47. $GLOBALS['PMD_OUT']['TABLE_NAME_SMALL'][$i] = htmlspecialchars(
  48. $one_table['TABLE_NAME'], ENT_QUOTES
  49. );
  50. $GLOBALS['PMD']['TABLE_TYPE'][$i] = strtoupper($one_table['ENGINE']);
  51. $DF = PMA_getDisplayField($GLOBALS['db'], $one_table['TABLE_NAME']);
  52. if ($DF != '') {
  53. $retval[$GLOBALS['PMD_URL']["TABLE_NAME_SMALL"][$i]] = urlencode($DF);
  54. }
  55. $i++;
  56. }
  57. return $retval;
  58. }
  59. /**
  60. * Retrieves table column info
  61. *
  62. * @return array table column nfo
  63. */
  64. function PMA_getColumnsInfo()
  65. {
  66. $GLOBALS['dbi']->selectDb($GLOBALS['db']);
  67. $tab_column = array();
  68. for ($i = 0, $cnt = count($GLOBALS['PMD']["TABLE_NAME"]); $i < $cnt; $i++) {
  69. $fields_rs = $GLOBALS['dbi']->query(
  70. $GLOBALS['dbi']->getColumnsSql(
  71. $GLOBALS['db'],
  72. $GLOBALS['PMD']["TABLE_NAME_SMALL"][$i],
  73. null,
  74. true
  75. ),
  76. null,
  77. PMA_DatabaseInterface::QUERY_STORE
  78. );
  79. $tbl_name_i = $GLOBALS['PMD']['TABLE_NAME'][$i];
  80. $j = 0;
  81. while ($row = $GLOBALS['dbi']->fetchAssoc($fields_rs)) {
  82. $tab_column[$tbl_name_i]['COLUMN_ID'][$j] = $j;
  83. $tab_column[$tbl_name_i]['COLUMN_NAME'][$j] = $row['Field'];
  84. $tab_column[$tbl_name_i]['TYPE'][$j] = $row['Type'];
  85. $tab_column[$tbl_name_i]['NULLABLE'][$j] = $row['Null'];
  86. $j++;
  87. }
  88. }
  89. return $tab_column;
  90. }
  91. /**
  92. * Returns JavaScript code for initializing vars
  93. *
  94. * @return string JavaScript code
  95. */
  96. function PMA_getScriptContr()
  97. {
  98. $GLOBALS['dbi']->selectDb($GLOBALS['db']);
  99. $con = array();
  100. $con["C_NAME"] = array();
  101. $i = 0;
  102. $alltab_rs = $GLOBALS['dbi']->query(
  103. 'SHOW TABLES FROM ' . PMA_Util::backquote($GLOBALS['db']),
  104. null,
  105. PMA_DatabaseInterface::QUERY_STORE
  106. );
  107. while ($val = @$GLOBALS['dbi']->fetchRow($alltab_rs)) {
  108. $row = PMA_getForeigners($GLOBALS['db'], $val[0], '', 'internal');
  109. //echo "<br> internal ".$GLOBALS['db']." - ".$val[0]." - ";
  110. //print_r($row);
  111. if ($row !== false) {
  112. foreach ($row as $field => $value) {
  113. $con['C_NAME'][$i] = '';
  114. $con['DTN'][$i] = urlencode($GLOBALS['db'] . "." . $val[0]);
  115. $con['DCN'][$i] = urlencode($field);
  116. $con['STN'][$i] = urlencode(
  117. $value['foreign_db'] . "." . $value['foreign_table']
  118. );
  119. $con['SCN'][$i] = urlencode($value['foreign_field']);
  120. $i++;
  121. }
  122. }
  123. $row = PMA_getForeigners($GLOBALS['db'], $val[0], '', 'foreign');
  124. //echo "<br> INNO ";
  125. //print_r($row);
  126. if ($row !== false) {
  127. foreach ($row as $field => $value) {
  128. $con['C_NAME'][$i] = '';
  129. $con['DTN'][$i] = urlencode($GLOBALS['db'] . "." . $val[0]);
  130. $con['DCN'][$i] = urlencode($field);
  131. $con['STN'][$i] = urlencode(
  132. $value['foreign_db'] . "." . $value['foreign_table']
  133. );
  134. $con['SCN'][$i] = urlencode($value['foreign_field']);
  135. $i++;
  136. }
  137. }
  138. }
  139. $ti = 0;
  140. $retval = array();
  141. for ($i = 0, $cnt = count($con["C_NAME"]); $i < $cnt; $i++) {
  142. $c_name_i = $con['C_NAME'][$i];
  143. $dtn_i = $con['DTN'][$i];
  144. $retval[$ti] = array();
  145. $retval[$ti][$c_name_i] = array();
  146. if (in_array($dtn_i, $GLOBALS['PMD_URL']["TABLE_NAME"])
  147. && in_array($con['STN'][$i], $GLOBALS['PMD_URL']["TABLE_NAME"])
  148. ) {
  149. $retval[$ti][$c_name_i][$dtn_i] = array();
  150. $retval[$ti][$c_name_i][$dtn_i][$con['DCN'][$i]] = array(
  151. 0 => $con['STN'][$i],
  152. 1 => $con['SCN'][$i]
  153. );
  154. }
  155. $ti++;
  156. }
  157. return $retval;
  158. }
  159. /**
  160. * Returns UNIQUE and PRIMARY indices
  161. *
  162. * @return array unique or primary indices
  163. */
  164. function PMA_getPKOrUniqueKeys()
  165. {
  166. return PMA_getAllKeys(true);
  167. }
  168. /**
  169. * Returns all indices
  170. *
  171. * @param bool $unique_only whether to include only unique ones
  172. *
  173. * @return array indices
  174. */
  175. function PMA_getAllKeys($unique_only = false)
  176. {
  177. include_once './libraries/Index.class.php';
  178. $keys = array();
  179. foreach ($GLOBALS['PMD']['TABLE_NAME_SMALL'] as $I => $table) {
  180. $schema = $GLOBALS['PMD']['OWNER'][$I];
  181. // for now, take into account only the first index segment
  182. foreach (PMA_Index::getFromTable($table, $schema) as $index) {
  183. if ($unique_only && ! $index->isUnique()) {
  184. continue;
  185. }
  186. $columns = $index->getColumns();
  187. foreach ($columns as $column_name => $dummy) {
  188. $keys[$schema . '.' . $table . '.' . $column_name] = 1;
  189. }
  190. }
  191. }
  192. return $keys;
  193. }
  194. /**
  195. * Return script to create j_tab and h_tab arrays
  196. *
  197. * @return string
  198. */
  199. function PMA_getScriptTabs()
  200. {
  201. $retval = array(
  202. 'j_tabs' => array(),
  203. 'h_tabs' => array()
  204. );
  205. for ($i = 0, $cnt = count($GLOBALS['PMD']['TABLE_NAME']); $i < $cnt; $i++) {
  206. $j = 0;
  207. if (PMA_Util::isForeignKeySupported($GLOBALS['PMD']['TABLE_TYPE'][$i])) {
  208. $j = 1;
  209. }
  210. $retval['j_tabs'][$GLOBALS['PMD_URL']['TABLE_NAME'][$i]] = $j;
  211. $retval['h_tabs'][$GLOBALS['PMD_URL']['TABLE_NAME'][$i]] = 1;
  212. }
  213. return $retval;
  214. }
  215. /**
  216. * Returns table position
  217. *
  218. * @return array table positions and sizes
  219. */
  220. function PMA_getTabPos()
  221. {
  222. $cfgRelation = PMA_getRelationsParam();
  223. if (! $cfgRelation['designerwork']) {
  224. return null;
  225. }
  226. $query = "
  227. SELECT CONCAT_WS('.', `db_name`, `table_name`) AS `name`,
  228. `x` AS `X`,
  229. `y` AS `Y`,
  230. `v` AS `V`,
  231. `h` AS `H`
  232. FROM " . PMA_Util::backquote($cfgRelation['db'])
  233. . "." . PMA_Util::backquote($cfgRelation['designer_coords']);
  234. $tab_pos = $GLOBALS['dbi']->fetchResult(
  235. $query,
  236. 'name',
  237. null,
  238. $GLOBALS['controllink'],
  239. PMA_DatabaseInterface::QUERY_STORE
  240. );
  241. return count($tab_pos) ? $tab_pos : null;
  242. }
  243. /**
  244. * Prepares XML output for js/pmd/ajax.js to display a message
  245. *
  246. * @param string $b b attribute value
  247. * @param string $ret Return attribute value
  248. *
  249. * @return void
  250. */
  251. function PMA_returnUpd($b, $ret)
  252. {
  253. // not sure where this was defined...
  254. global $K;
  255. header("Content-Type: text/xml; charset=utf-8");
  256. header("Cache-Control: no-cache");
  257. die(
  258. '<root act="relation_upd" return="' . $ret . '" b="'
  259. . $b . '" K="' . $K . '"></root>'
  260. );
  261. }
  262. ?>