RelationController.php 12 KB

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