TableRelationController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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. $this->updateForForeignKeysAction();
  117. // Updates for display field
  118. if ($this->cfgRelation['displaywork'] && isset($_POST['display_field'])) {
  119. $this->updateForDisplayField();
  120. }
  121. // If we did an update, refresh our data
  122. if (isset($_POST['destination_db']) && $this->cfgRelation['relwork']) {
  123. $this->existrel = $this->relation->getForeigners(
  124. $this->db, $this->table, '', 'internal'
  125. );
  126. }
  127. if (isset($_POST['destination_foreign_db'])
  128. && Util::isForeignKeySupported($this->tbl_storage_engine)
  129. ) {
  130. $this->existrel_foreign = $this->relation->getForeigners(
  131. $this->db, $this->table, '', 'foreign'
  132. );
  133. }
  134. // display secondary level tabs if necessary
  135. $engine = $this->dbi->getTable($this->db, $this->table)->getStorageEngine();
  136. $this->response->addHTML(
  137. Template::get('table/secondary_tabs')->render(
  138. array(
  139. 'url_params' => array(
  140. 'db' => $GLOBALS['db'],
  141. 'table' => $GLOBALS['table']
  142. ),
  143. 'is_foreign_key_supported' => Util::isForeignKeySupported($engine),
  144. 'cfg_relation' => $this->relation->getRelationsParam(),
  145. )
  146. )
  147. );
  148. $this->response->addHTML('<div id="structure_content">');
  149. /**
  150. * Dialog
  151. */
  152. // Now find out the columns of our $table
  153. // need to use DatabaseInterface::QUERY_STORE with $this->dbi->numRows()
  154. // in mysqli
  155. $columns = $this->dbi->getColumns($this->db, $this->table);
  156. $column_array = array();
  157. $column_array[''] = '';
  158. foreach ($columns as $column) {
  159. if (strtoupper($this->tbl_storage_engine) == 'INNODB'
  160. || ! empty($column['Key'])
  161. ) {
  162. $column_array[$column['Field']] = $column['Field'];
  163. }
  164. }
  165. if ($GLOBALS['cfg']['NaturalOrder']) {
  166. uksort($column_array, 'strnatcasecmp');
  167. }
  168. // common form
  169. $this->response->addHTML(
  170. Template::get('table/relation/common_form')->render([
  171. 'db' => $this->db,
  172. 'table' => $this->table,
  173. 'cfg_relation' => $this->cfgRelation,
  174. 'tbl_storage_engine' => $this->tbl_storage_engine,
  175. 'existrel' => isset($this->existrel) ? $this->existrel : array(),
  176. 'existrel_foreign' => is_array($this->existrel_foreign) && array_key_exists('foreign_keys_data', $this->existrel_foreign)
  177. ? $this->existrel_foreign['foreign_keys_data'] : array(),
  178. 'options_array' => $this->options_array,
  179. 'column_array' => $column_array,
  180. 'save_row' => array_values($columns),
  181. 'url_params' => $GLOBALS['url_params'],
  182. 'databases' => $GLOBALS['dblist']->databases,
  183. 'dbi' => $GLOBALS['dbi'],
  184. ])
  185. );
  186. if (Util::isForeignKeySupported($this->tbl_storage_engine)) {
  187. $this->response->addHTML(Index::getHtmlForDisplayIndexes());
  188. }
  189. $this->response->addHTML('</div>');
  190. }
  191. /**
  192. * Update for display field
  193. *
  194. * @return void
  195. */
  196. public function updateForDisplayField()
  197. {
  198. if ($this->upd_query->updateDisplayField(
  199. $_POST['display_field'], $this->cfgRelation
  200. )
  201. ) {
  202. $this->response->addHTML(
  203. Util::getMessage(
  204. __('Display column was successfully updated.'),
  205. '', 'success'
  206. )
  207. );
  208. }
  209. }
  210. /**
  211. * Update for FK
  212. *
  213. * @return void
  214. */
  215. public function updateForForeignKeysAction()
  216. {
  217. $multi_edit_columns_name = isset($_POST['foreign_key_fields_name'])
  218. ? $_POST['foreign_key_fields_name']
  219. : null;
  220. $preview_sql_data = '';
  221. $seen_error = false;
  222. // (for now, one index name only; we keep the definitions if the
  223. // foreign db is not the same)
  224. if (isset($_POST['destination_foreign_db'])
  225. && isset($_POST['destination_foreign_table'])
  226. && isset($_POST['destination_foreign_column'])) {
  227. list($html, $preview_sql_data, $display_query, $seen_error)
  228. = $this->upd_query->updateForeignKeys(
  229. $_POST['destination_foreign_db'],
  230. $multi_edit_columns_name, $_POST['destination_foreign_table'],
  231. $_POST['destination_foreign_column'], $this->options_array,
  232. $this->table,
  233. is_array($this->existrel_foreign) && array_key_exists('foreign_keys_data', $this->existrel_foreign)
  234. ? $this->existrel_foreign['foreign_keys_data']
  235. : []
  236. );
  237. $this->response->addHTML($html);
  238. }
  239. // If there is a request for SQL previewing.
  240. if (isset($_POST['preview_sql'])) {
  241. Core::previewSQL($preview_sql_data);
  242. }
  243. if (!empty($display_query) && !$seen_error) {
  244. $GLOBALS['display_query'] = $display_query;
  245. $this->response->addHTML(
  246. Util::getMessage(
  247. __('Your SQL query has been executed successfully.'),
  248. null, 'success'
  249. )
  250. );
  251. }
  252. }
  253. /**
  254. * Update for internal relation
  255. *
  256. * @return void
  257. */
  258. public function updateForInternalRelationAction()
  259. {
  260. $multi_edit_columns_name = isset($_POST['fields_name'])
  261. ? $_POST['fields_name']
  262. : null;
  263. if ($this->upd_query->updateInternalRelations(
  264. $multi_edit_columns_name,
  265. $_POST['destination_db'],
  266. $_POST['destination_table'],
  267. $_POST['destination_column'],
  268. $this->cfgRelation,
  269. isset($this->existrel) ? $this->existrel : null
  270. )
  271. ) {
  272. $this->response->addHTML(
  273. Util::getMessage(
  274. __('Internal relationships were successfully updated.'),
  275. '', 'success'
  276. )
  277. );
  278. }
  279. }
  280. /**
  281. * Send table columns for foreign table dropdown
  282. *
  283. * @return void
  284. *
  285. */
  286. public function getDropdownValueForTableAction()
  287. {
  288. $foreignTable = $_POST['foreignTable'];
  289. $table_obj = $this->dbi->getTable($_POST['foreignDb'], $foreignTable);
  290. // Since views do not have keys defined on them provide the full list of
  291. // columns
  292. if ($table_obj->isView()) {
  293. $columnList = $table_obj->getColumns(false, false);
  294. } else {
  295. $columnList = $table_obj->getIndexedColumns(false, false);
  296. }
  297. $columns = array();
  298. foreach ($columnList as $column) {
  299. $columns[] = htmlspecialchars($column);
  300. }
  301. if ($GLOBALS['cfg']['NaturalOrder']) {
  302. usort($columns, 'strnatcasecmp');
  303. }
  304. $this->response->addJSON('columns', $columns);
  305. // @todo should be: $server->db($db)->table($table)->primary()
  306. $primary = Index::getPrimary($foreignTable, $_POST['foreignDb']);
  307. if (false === $primary) {
  308. return;
  309. }
  310. $this->response->addJSON('primary', array_keys($primary->getColumns()));
  311. }
  312. /**
  313. * Send database selection values for dropdown
  314. *
  315. * @return void
  316. *
  317. */
  318. public function getDropdownValueForDbAction()
  319. {
  320. $tables = array();
  321. $foreign = isset($_POST['foreign']) && $_POST['foreign'] === 'true';
  322. if ($foreign) {
  323. $query = 'SHOW TABLE STATUS FROM '
  324. . Util::backquote($_POST['foreignDb']);
  325. $tables_rs = $this->dbi->query(
  326. $query,
  327. DatabaseInterface::CONNECT_USER,
  328. DatabaseInterface::QUERY_STORE
  329. );
  330. while ($row = $this->dbi->fetchArray($tables_rs)) {
  331. if (isset($row['Engine'])
  332. && mb_strtoupper($row['Engine']) == $this->tbl_storage_engine
  333. ) {
  334. $tables[] = htmlspecialchars($row['Name']);
  335. }
  336. }
  337. } else {
  338. $query = 'SHOW TABLES FROM '
  339. . Util::backquote($_POST['foreignDb']);
  340. $tables_rs = $this->dbi->query(
  341. $query,
  342. DatabaseInterface::CONNECT_USER,
  343. DatabaseInterface::QUERY_STORE
  344. );
  345. while ($row = $this->dbi->fetchArray($tables_rs)) {
  346. $tables[] = htmlspecialchars($row[0]);
  347. }
  348. }
  349. if ($GLOBALS['cfg']['NaturalOrder']) {
  350. usort($tables, 'strnatcasecmp');
  351. }
  352. $this->response->addJSON('tables', $tables);
  353. }
  354. }