Improve create-release script to handle QA branches higher than QA_4_8.
[phpmyadmin.git] / tbl_export.php
blob71172fa0a7f3766a7211f17872058bd346bc86e7
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Table export
6 * @package PhpMyAdmin
7 */
8 declare(strict_types=1);
10 use PhpMyAdmin\Config\PageSettings;
11 use PhpMyAdmin\DatabaseInterface;
12 use PhpMyAdmin\Di\Container;
13 use PhpMyAdmin\Display\Export;
14 use PhpMyAdmin\Relation;
15 use PhpMyAdmin\Response;
17 if (! defined('ROOT_PATH')) {
18 define('ROOT_PATH', __DIR__ . DIRECTORY_SEPARATOR);
21 global $db, $url_query;
23 require_once ROOT_PATH . 'libraries/common.inc.php';
25 $container = Container::getDefaultContainer();
26 $container->set(Response::class, Response::getInstance());
28 /** @var Response $response */
29 $response = $container->get(Response::class);
31 /** @var DatabaseInterface $dbi */
32 $dbi = $container->get(DatabaseInterface::class);
34 PageSettings::showGroup('Export');
36 $header = $response->getHeader();
37 $scripts = $header->getScripts();
38 $scripts->addFile('export.js');
40 // Get the relation settings
41 /** @var Relation $relation */
42 $relation = $containerBuilder->get('relation');
43 $cfgRelation = $relation->getRelationsParam();
45 $displayExport = new Export();
47 // handling export template actions
48 if (isset($_POST['templateAction']) && $cfgRelation['exporttemplateswork']) {
49 $displayExport->handleTemplateActions($cfgRelation);
50 exit;
53 /**
54 * Gets tables information and displays top links
56 require_once ROOT_PATH . 'libraries/tbl_common.inc.php';
57 $url_query .= '&amp;goto=tbl_export.php&amp;back=tbl_export.php';
59 // Dump of a table
61 $export_page_title = __('View dump (schema) of table');
63 // When we have some query, we need to remove LIMIT from that and possibly
64 // generate WHERE clause (if we are asked to export specific rows)
66 if (! empty($sql_query)) {
67 $parser = new PhpMyAdmin\SqlParser\Parser($sql_query);
69 if (! empty($parser->statements[0])
70 && ($parser->statements[0] instanceof PhpMyAdmin\SqlParser\Statements\SelectStatement)
71 ) {
72 // Finding aliases and removing them, but we keep track of them to be
73 // able to replace them in select expression too.
74 $aliases = [];
75 foreach ($parser->statements[0]->from as $from) {
76 if (! empty($from->table) && ! empty($from->alias)) {
77 $aliases[$from->alias] = $from->table;
78 // We remove the alias of the table because they are going to
79 // be replaced anyway.
80 $from->alias = null;
81 $from->expr = null; // Force rebuild.
85 // Rebuilding the SELECT and FROM clauses.
86 if (count($parser->statements[0]->from) > 0
87 && count($parser->statements[0]->union) === 0
88 ) {
89 $replaces = [
91 'FROM',
92 'FROM ' . PhpMyAdmin\SqlParser\Components\ExpressionArray::build(
93 $parser->statements[0]->from
99 // Checking if the WHERE clause has to be replaced.
100 if (! empty($where_clause) && is_array($where_clause)) {
101 $replaces[] = [
102 'WHERE',
103 'WHERE (' . implode(') OR (', $where_clause) . ')',
107 // Preparing to remove the LIMIT clause.
108 $replaces[] = [
109 'LIMIT',
113 // Replacing the clauses.
114 $sql_query = PhpMyAdmin\SqlParser\Utils\Query::replaceClauses(
115 $parser->statements[0],
116 $parser->list,
117 $replaces
120 // Removing the aliases by finding the alias followed by a dot.
121 $tokens = PhpMyAdmin\SqlParser\Lexer::getTokens($sql_query);
122 foreach ($aliases as $alias => $table) {
123 $tokens = PhpMyAdmin\SqlParser\Utils\Tokens::replaceTokens(
124 $tokens,
127 'value_str' => $alias,
130 'type' => PhpMyAdmin\SqlParser\Token::TYPE_OPERATOR,
131 'value_str' => '.',
135 new PhpMyAdmin\SqlParser\Token($table),
136 new PhpMyAdmin\SqlParser\Token('.', PhpMyAdmin\SqlParser\Token::TYPE_OPERATOR),
140 $sql_query = PhpMyAdmin\SqlParser\TokensList::build($tokens);
143 echo PhpMyAdmin\Util::getMessage(PhpMyAdmin\Message::success());
146 if (! isset($sql_query)) {
147 $sql_query = '';
149 if (! isset($num_tables)) {
150 $num_tables = 0;
152 if (! isset($unlim_num_rows)) {
153 $unlim_num_rows = 0;
155 if (! isset($multi_values)) {
156 $multi_values = '';
158 $response = Response::getInstance();
159 $response->addHTML(
160 $displayExport->getDisplay(
161 'table',
162 $db,
163 $table,
164 $sql_query,
165 $num_tables,
166 $unlim_num_rows,
167 $multi_values