db_tracking.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Tracking configuration for database
  5. *
  6. * @package PhpMyAdmin
  7. */
  8. use PhpMyAdmin\Display\CreateTable;
  9. use PhpMyAdmin\Message;
  10. use PhpMyAdmin\Relation;
  11. use PhpMyAdmin\Response;
  12. use PhpMyAdmin\Tracker;
  13. use PhpMyAdmin\Tracking;
  14. use PhpMyAdmin\Util;
  15. /**
  16. * Run common work
  17. */
  18. require_once 'libraries/common.inc.php';
  19. //Get some js files needed for Ajax requests
  20. $response = Response::getInstance();
  21. $header = $response->getHeader();
  22. $scripts = $header->getScripts();
  23. $scripts->addFile('vendor/jquery/jquery.tablesorter.js');
  24. $scripts->addFile('db_tracking.js');
  25. /**
  26. * If we are not in an Ajax request, then do the common work and show the links etc.
  27. */
  28. require 'libraries/db_common.inc.php';
  29. $url_query .= '&amp;goto=tbl_tracking.php&amp;back=db_tracking.php';
  30. $url_params['goto'] = 'tbl_tracking.php';
  31. $url_params['back'] = 'db_tracking.php';
  32. // Get the database structure
  33. $sub_part = '_structure';
  34. list(
  35. $tables,
  36. $num_tables,
  37. $total_num_tables,
  38. $sub_part,
  39. $is_show_stats,
  40. $db_is_system_schema,
  41. $tooltip_truename,
  42. $tooltip_aliasname,
  43. $pos
  44. ) = Util::getDbInfo($db, isset($sub_part) ? $sub_part : '');
  45. if (isset($_POST['delete_tracking']) && isset($_POST['table'])) {
  46. Tracker::deleteTracking($GLOBALS['db'], $_POST['table']);
  47. Message::success(
  48. __('Tracking data deleted successfully.')
  49. )->display();
  50. } elseif (isset($_POST['submit_create_version'])) {
  51. Tracking::createTrackingForMultipleTables($_POST['selected']);
  52. Message::success(
  53. sprintf(
  54. __(
  55. 'Version %1$s was created for selected tables,'
  56. . ' tracking is active for them.'
  57. ),
  58. htmlspecialchars($_POST['version'])
  59. )
  60. )->display();
  61. } elseif (isset($_POST['submit_mult'])) {
  62. if (! empty($_POST['selected_tbl'])) {
  63. if ($_POST['submit_mult'] == 'delete_tracking') {
  64. foreach ($_POST['selected_tbl'] as $table) {
  65. Tracker::deleteTracking($GLOBALS['db'], $table);
  66. }
  67. Message::success(
  68. __('Tracking data deleted successfully.')
  69. )->display();
  70. } elseif ($_POST['submit_mult'] == 'track') {
  71. echo Tracking::getHtmlForDataDefinitionAndManipulationStatements(
  72. 'db_tracking.php' . $url_query,
  73. 0,
  74. $GLOBALS['db'],
  75. $_POST['selected_tbl']
  76. );
  77. exit;
  78. }
  79. } else {
  80. Message::notice(
  81. __('No tables selected.')
  82. )->display();
  83. }
  84. }
  85. // Get tracked data about the database
  86. $data = Tracker::getTrackedData($GLOBALS['db'], '', '1');
  87. // No tables present and no log exist
  88. if ($num_tables == 0 && count($data['ddlog']) == 0) {
  89. echo '<p>' , __('No tables found in database.') , '</p>' , "\n";
  90. if (empty($db_is_system_schema)) {
  91. echo CreateTable::getHtml($db);
  92. }
  93. exit;
  94. }
  95. // ---------------------------------------------------------------------------
  96. $relation = new Relation();
  97. $cfgRelation = $relation->getRelationsParam();
  98. // Prepare statement to get HEAD version
  99. $all_tables_query = ' SELECT table_name, MAX(version) as version FROM ' .
  100. Util::backquote($cfgRelation['db']) . '.' .
  101. Util::backquote($cfgRelation['tracking']) .
  102. ' WHERE db_name = \'' . $GLOBALS['dbi']->escapeString($GLOBALS['db']) .
  103. '\' ' .
  104. ' GROUP BY table_name' .
  105. ' ORDER BY table_name ASC';
  106. $all_tables_result = $relation->queryAsControlUser($all_tables_query);
  107. // If a HEAD version exists
  108. if (is_object($all_tables_result)
  109. && $GLOBALS['dbi']->numRows($all_tables_result) > 0
  110. ) {
  111. echo Tracking::getHtmlForTrackedTables(
  112. $GLOBALS['db'], $all_tables_result, $url_query, $pmaThemeImage,
  113. $text_dir, $cfgRelation
  114. );
  115. }
  116. $untracked_tables = Tracking::getUntrackedTables($GLOBALS['db']);
  117. // If untracked tables exist
  118. if (count($untracked_tables) > 0) {
  119. echo Tracking::getHtmlForUntrackedTables(
  120. $GLOBALS['db'], $untracked_tables, $url_query, $pmaThemeImage, $text_dir
  121. );
  122. }
  123. // If available print out database log
  124. if (count($data['ddlog']) > 0) {
  125. $log = '';
  126. foreach ($data['ddlog'] as $entry) {
  127. $log .= '# ' . $entry['date'] . ' ' . $entry['username'] . "\n"
  128. . $entry['statement'] . "\n";
  129. }
  130. echo Util::getMessage(__('Database Log'), $log);
  131. }