TableRelationController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Holds the PhpMyAdmin\Controllers\Table\TableRelationController
  5. *
  6. * @package PhpMyAdmin\Controllers
  7. */
  8. namespace PhpMyAdmin\Controllers\Table;
  9. use PhpMyAdmin\Controllers\TableController;
  10. use PhpMyAdmin\Core;
  11. use PhpMyAdmin\DatabaseInterface;
  12. use PhpMyAdmin\Index;
  13. use PhpMyAdmin\Relation;
  14. use PhpMyAdmin\Table;
  15. use PhpMyAdmin\Template;
  16. use PhpMyAdmin\Util;
  17. /**
  18. * Handles table relation logic
  19. *
  20. * @package PhpMyAdmin\Controllers
  21. */
  22. class TableRelationController extends TableController
  23. {
  24. /**
  25. * @var array $options_array
  26. */
  27. protected $options_array;
  28. /**
  29. * @var array $cfgRelation
  30. */
  31. protected $cfgRelation;
  32. /**
  33. * @var array $existrel
  34. */
  35. protected $existrel;
  36. /**
  37. * @var string $tbl_storage_engine
  38. */
  39. protected $tbl_storage_engine;
  40. /**
  41. * @var array $existrel_foreign
  42. */
  43. protected $existrel_foreign;
  44. /**
  45. * @var Table $udp_query
  46. */
  47. protected $upd_query;
  48. /**
  49. * @var Relation $relation
  50. */
  51. private $relation;
  52. /**
  53. * Constructor
  54. *
  55. * @param array|null $options_array Options
  56. * @param array|null $cfgRelation Config relation
  57. * @param string $tbl_storage_engine Table storage engine
  58. * @param array|null $existrel Relations
  59. * @param array|null $existrel_foreign External relations
  60. * @param string $upd_query Update query
  61. */
  62. public function __construct(
  63. $response,
  64. $dbi,
  65. $db,
  66. $table,
  67. $options_array,
  68. $cfgRelation,
  69. $tbl_storage_engine,
  70. $existrel,
  71. $existrel_foreign,
  72. $upd_query
  73. ) {
  74. parent::__construct($response, $dbi, $db, $table);
  75. $this->options_array = $options_array;
  76. $this->cfgRelation = $cfgRelation;
  77. $this->tbl_storage_engine = $tbl_storage_engine;
  78. $this->existrel = $existrel;
  79. $this->existrel_foreign = $existrel_foreign;
  80. $this->upd_query = $upd_query;
  81. $this->relation = new Relation();
  82. }
  83. /**
  84. * Index
  85. *
  86. * @return void
  87. */
  88. public function indexAction()
  89. {
  90. // Send table of column names to populate corresponding dropdowns depending
  91. // on the current selection
  92. if (isset($_POST['getDropdownValues'])
  93. && $_POST['getDropdownValues'] === 'true'
  94. ) {
  95. // if both db and table are selected
  96. if (isset($_POST['foreignTable'])) {
  97. $this->getDropdownValueForTableAction();
  98. } else { // if only the db is selected
  99. $this->getDropdownValueForDbAction();
  100. }
  101. return;
  102. }
  103. $this->response->getHeader()->getScripts()->addFiles(
  104. array(
  105. 'tbl_relation.js',
  106. 'indexes.js'
  107. )
  108. );
  109. // Set the database
  110. $this->dbi->selectDb($this->db);
  111. // updates for Internal relations
  112. if (isset($_POST['destination_db']) && $this->cfgRelation['relwork']) {
  113. $this->updateForInternalRelationAction();
  114. }
  115. // updates for foreign keys
  116. if (isset($_POST['destination_foreign_db'])) {
  117. $this->updateForForeignKeysAction();
  118. }
  119. // Updates for display field
  120. if ($this->cfgRelation['displaywork'] && isset($_POST['display_field'])) {
  121. $this->updateForDisplayField();
  122. }
  123. // If we did an update, refresh our data
  124. if (isset($_POST['destination_db']) && $this->cfgRelation['relwork']) {
  125. $this->existrel = $this->relation->getForeigners(
  126. $this->db, $this->table, '', 'internal'
  127. );
  128. }
  129. if (isset($_POST['destination_foreign_db'])
  130. && Util::isForeignKeySupported($this->tbl_storage_engine)
  131. ) {
  132. $this->existrel_foreign = $this->relation->getForeigners(
  133. $this->db, $this->table, '', 'foreign'
  134. );
  135. }
  136. // display secondary level tabs if necessary
  137. $engine = $this->dbi->getTable($this->db, $this->table)->getStorageEngine();
  138. $this->response->addHTML(
  139. Template::get('table/secondary_tabs')->render(
  140. array(
  141. 'url_params' => array(
  142. 'db' => $GLOBALS['db'],
  143. 'table' => $GLOBALS['table']
  144. ),
  145. 'is_foreign_key_supported' => Util::isForeignKeySupported($engine),
  146. 'cfg_relation' => $this->relation->getRelationsParam(),
  147. )
  148. )
  149. );
  150. $this->response->addHTML('<div id="structure_content">');
  151. /**
  152. * Dialog
  153. */
  154. // Now find out the columns of our $table
  155. // need to use DatabaseInterface::QUERY_STORE with $this->dbi->numRows()
  156. // in mysqli
  157. $columns = $this->dbi->getColumns($this->db, $this->table);
  158. $column_array = array();
  159. $column_array[''] = '';
  160. foreach ($columns as $column) {
  161. if (strtoupper($this->tbl_storage_engine) == 'INNODB'
  162. || ! empty($column['Key'])
  163. ) {
  164. $column_array[$column['Field']] = $column['Field'];
  165. }
  166. }
  167. if ($GLOBALS['cfg']['NaturalOrder']) {
  168. uksort($column_array, 'strnatcasecmp');
  169. }
  170. // common form
  171. $this->response->addHTML(
  172. Template::get('table/relation/common_form')->render([
  173. 'db' => $this->db,
  174. 'table' => $this->table,
  175. 'cfg_relation' => $this->cfgRelation,
  176. 'tbl_storage_engine' => $this->tbl_storage_engine,
  177. 'existrel' => isset($this->existrel) ? $this->existrel : array(),
  178. 'existrel_foreign' => isset($this->existrel_foreign)
  179. ? $this->existrel_foreign['foreign_keys_data'] : array(),
  180. 'options_array' => $this->options_array,
  181. 'column_array' => $column_array,
  182. 'save_row' => array_values($columns),
  183. 'url_params' => $GLOBALS['url_params'],
  184. 'databases' => $GLOBALS['dblist']->databases,
  185. 'dbi' => $GLOBALS['dbi'],
  186. ])
  187. );
  188. if (Util::isForeignKeySupported($this->tbl_storage_engine)) {
  189. $this->response->addHTML(Index::getHtmlForDisplayIndexes());
  190. }
  191. $this->response->addHTML('</div>');
  192. }
  193. /**
  194. * Update for display field
  195. *
  196. * @return void
  197. */
  198. public function updateForDisplayField()
  199. {
  200. if ($this->upd_query->updateDisplayField(
  201. $_POST['display_field'], $this->cfgRelation
  202. )
  203. ) {
  204. $this->response->addHTML(
  205. Util::getMessage(
  206. __('Display column was successfully updated.'),
  207. '', 'success'
  208. )
  209. );
  210. }
  211. }
  212. /**
  213. * Update for FK
  214. *
  215. * @return void
  216. */
  217. public function updateForForeignKeysAction()
  218. {
  219. $multi_edit_columns_name = isset($_POST['foreign_key_fields_name'])
  220. ? $_POST['foreign_key_fields_name']
  221. : null;
  222. // (for now, one index name only; we keep the definitions if the
  223. // foreign db is not the same)
  224. list($html, $preview_sql_data, $display_query, $seen_error)
  225. = $this->upd_query->updateForeignKeys(
  226. $_POST['destination_foreign_db'],
  227. $multi_edit_columns_name, $_POST['destination_foreign_table'],
  228. $_POST['destination_foreign_column'], $this->options_array,
  229. $this->table,
  230. isset($this->existrel_foreign)
  231. ? $this->existrel_foreign['foreign_keys_data']
  232. : null
  233. );
  234. $this->response->addHTML($html);
  235. // If there is a request for SQL previewing.
  236. if (isset($_POST['preview_sql'])) {
  237. Core::previewSQL($preview_sql_data);
  238. }
  239. if (!empty($display_query) && !$seen_error) {
  240. $GLOBALS['display_query'] = $display_query;
  241. $this->response->addHTML(
  242. Util::getMessage(
  243. __('Your SQL query has been executed successfully.'),
  244. null, 'success'
  245. )
  246. );
  247. }
  248. }
  249. /**
  250. * Update for internal relation
  251. *
  252. * @return void
  253. */
  254. public function updateForInternalRelationAction()
  255. {
  256. $multi_edit_columns_name = isset($_POST['fields_name'])
  257. ? $_POST['fields_name']
  258. : null;
  259. if ($this->upd_query->updateInternalRelations(
  260. $multi_edit_columns_name,
  261. $_POST['destination_db'],
  262. $_POST['destination_table'],
  263. $_POST['destination_column'],
  264. $this->cfgRelation,
  265. isset($this->existrel) ? $this->existrel : null
  266. )
  267. ) {
  268. $this->response->addHTML(
  269. Util::getMessage(
  270. __('Internal relationships were successfully updated.'),
  271. '', 'success'
  272. )
  273. );
  274. }
  275. }
  276. /**
  277. * Send table columns for foreign table dropdown
  278. *
  279. * @return void
  280. *
  281. */
  282. public function getDropdownValueForTableAction()
  283. {
  284. $foreignTable = $_POST['foreignTable'];
  285. $table_obj = $this->dbi->getTable($_POST['foreignDb'], $foreignTable);
  286. // Since views do not have keys defined on them provide the full list of
  287. // columns
  288. if ($table_obj->isView()) {
  289. $columnList = $table_obj->getColumns(false, false);
  290. } else {
  291. $columnList = $table_obj->getIndexedColumns(false, false);
  292. }
  293. $columns = array();
  294. foreach ($columnList as $column) {
  295. $columns[] = htmlspecialchars($column);
  296. }
  297. if ($GLOBALS['cfg']['NaturalOrder']) {
  298. usort($columns, 'strnatcasecmp');
  299. }
  300. $this->response->addJSON('columns', $columns);
  301. // @todo should be: $server->db($db)->table($table)->primary()
  302. $primary = Index::getPrimary($foreignTable, $_POST['foreignDb']);
  303. if (false === $primary) {
  304. return;
  305. }
  306. $this->response->addJSON('primary', array_keys($primary->getColumns()));
  307. }
  308. /**
  309. * Send database selection values for dropdown
  310. *
  311. * @return void
  312. *
  313. */
  314. public function getDropdownValueForDbAction()
  315. {
  316. $tables = array();
  317. $foreign = isset($_POST['foreign']) && $_POST['foreign'] === 'true';
  318. if ($foreign) {
  319. $query = 'SHOW TABLE STATUS FROM '
  320. . Util::backquote($_POST['foreignDb']);
  321. $tables_rs = $this->dbi->query(
  322. $query,
  323. DatabaseInterface::CONNECT_USER,
  324. DatabaseInterface::QUERY_STORE
  325. );
  326. while ($row = $this->dbi->fetchArray($tables_rs)) {
  327. if (isset($row['Engine'])
  328. && mb_strtoupper($row['Engine']) == $this->tbl_storage_engine
  329. ) {
  330. $tables[] = htmlspecialchars($row['Name']);
  331. }
  332. }
  333. } else {
  334. $query = 'SHOW TABLES FROM '
  335. . Util::backquote($_POST['foreignDb']);
  336. $tables_rs = $this->dbi->query(
  337. $query,
  338. DatabaseInterface::CONNECT_USER,
  339. DatabaseInterface::QUERY_STORE
  340. );
  341. while ($row = $this->dbi->fetchArray($tables_rs)) {
  342. $tables[] = htmlspecialchars($row[0]);
  343. }
  344. }
  345. if ($GLOBALS['cfg']['NaturalOrder']) {
  346. usort($tables, 'strnatcasecmp');
  347. }
  348. $this->response->addJSON('tables', $tables);
  349. }
  350. }