tbl_export.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Table export
  5. *
  6. * @package PhpMyAdmin
  7. */
  8. use PhpMyAdmin\Config\PageSettings;
  9. use PhpMyAdmin\Display\Export;
  10. use PhpMyAdmin\Relation;
  11. use PhpMyAdmin\Response;
  12. /**
  13. *
  14. */
  15. require_once 'libraries/common.inc.php';
  16. PageSettings::showGroup('Export');
  17. $response = Response::getInstance();
  18. $header = $response->getHeader();
  19. $scripts = $header->getScripts();
  20. $scripts->addFile('export.js');
  21. // Get the relation settings
  22. $relation = new Relation();
  23. $cfgRelation = $relation->getRelationsParam();
  24. $displayExport = new Export();
  25. // handling export template actions
  26. if (isset($_POST['templateAction']) && $cfgRelation['exporttemplateswork']) {
  27. $displayExport->handleTemplateActions($cfgRelation);
  28. exit;
  29. }
  30. /**
  31. * Gets tables information and displays top links
  32. */
  33. require_once 'libraries/tbl_common.inc.php';
  34. $url_query .= '&amp;goto=tbl_export.php&amp;back=tbl_export.php';
  35. // Dump of a table
  36. $export_page_title = __('View dump (schema) of table');
  37. // When we have some query, we need to remove LIMIT from that and possibly
  38. // generate WHERE clause (if we are asked to export specific rows)
  39. if (! empty($sql_query)) {
  40. $parser = new PhpMyAdmin\SqlParser\Parser($sql_query);
  41. if ((!empty($parser->statements[0]))
  42. && ($parser->statements[0] instanceof PhpMyAdmin\SqlParser\Statements\SelectStatement)
  43. ) {
  44. // Finding aliases and removing them, but we keep track of them to be
  45. // able to replace them in select expression too.
  46. $aliases = array();
  47. foreach ($parser->statements[0]->from as $from) {
  48. if ((!empty($from->table)) && (!empty($from->alias))) {
  49. $aliases[$from->alias] = $from->table;
  50. // We remove the alias of the table because they are going to
  51. // be replaced anyway.
  52. $from->alias = null;
  53. $from->expr = null; // Force rebuild.
  54. }
  55. }
  56. // Rebuilding the SELECT and FROM clauses.
  57. if (count($parser->statements[0]->from) > 0
  58. && count($parser->statements[0]->union) === 0
  59. ) {
  60. $replaces = array(
  61. array(
  62. 'FROM', 'FROM ' . PhpMyAdmin\SqlParser\Components\ExpressionArray::build(
  63. $parser->statements[0]->from
  64. ),
  65. ),
  66. );
  67. }
  68. // Checking if the WHERE clause has to be replaced.
  69. if ((!empty($where_clause)) && (is_array($where_clause))) {
  70. $replaces[] = array(
  71. 'WHERE', 'WHERE (' . implode(') OR (', $where_clause) . ')'
  72. );
  73. }
  74. // Preparing to remove the LIMIT clause.
  75. $replaces[] = array('LIMIT', '');
  76. // Replacing the clauses.
  77. $sql_query = PhpMyAdmin\SqlParser\Utils\Query::replaceClauses(
  78. $parser->statements[0],
  79. $parser->list,
  80. $replaces
  81. );
  82. // Removing the aliases by finding the alias followed by a dot.
  83. $tokens = PhpMyAdmin\SqlParser\Lexer::getTokens($sql_query);
  84. foreach ($aliases as $alias => $table) {
  85. $tokens = PhpMyAdmin\SqlParser\Utils\Tokens::replaceTokens(
  86. $tokens,
  87. array(
  88. array(
  89. 'value_str' => $alias,
  90. ),
  91. array(
  92. 'type' => PhpMyAdmin\SqlParser\Token::TYPE_OPERATOR,
  93. 'value_str' => '.',
  94. )
  95. ),
  96. array(
  97. new PhpMyAdmin\SqlParser\Token($table),
  98. new PhpMyAdmin\SqlParser\Token('.',PhpMyAdmin\SqlParser\Token::TYPE_OPERATOR)
  99. )
  100. );
  101. }
  102. $sql_query = PhpMyAdmin\SqlParser\TokensList::build($tokens);
  103. }
  104. echo PhpMyAdmin\Util::getMessage(PhpMyAdmin\Message::success());
  105. }
  106. if (! isset($sql_query)) {
  107. $sql_query = '';
  108. }
  109. if (! isset($num_tables)) {
  110. $num_tables = 0;
  111. }
  112. if (! isset($unlim_num_rows)) {
  113. $unlim_num_rows = 0;
  114. }
  115. if (! isset($multi_values)) {
  116. $multi_values = '';
  117. }
  118. $response = Response::getInstance();
  119. $response->addHTML(
  120. $displayExport->getDisplay(
  121. 'table', $db, $table, $sql_query, $num_tables,
  122. $unlim_num_rows, $multi_values
  123. )
  124. );