Translated using Weblate (Portuguese)
[phpmyadmin.git] / tbl_export.php
blobc815b99986a91310116282f50855a2e8036fab0c
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\Display\Export;
12 use PhpMyAdmin\Relation;
13 use PhpMyAdmin\Response;
15 if (! defined('ROOT_PATH')) {
16 define('ROOT_PATH', __DIR__ . DIRECTORY_SEPARATOR);
19 /**
22 require_once ROOT_PATH . 'libraries/common.inc.php';
24 PageSettings::showGroup('Export');
26 $response = Response::getInstance();
27 $header = $response->getHeader();
28 $scripts = $header->getScripts();
29 $scripts->addFile('export.js');
31 // Get the relation settings
32 $relation = new Relation($GLOBALS['dbi']);
33 $cfgRelation = $relation->getRelationsParam();
35 $displayExport = new Export();
37 // handling export template actions
38 if (isset($_POST['templateAction']) && $cfgRelation['exporttemplateswork']) {
39 $displayExport->handleTemplateActions($cfgRelation);
40 exit;
43 /**
44 * Gets tables information and displays top links
46 require_once ROOT_PATH . 'libraries/tbl_common.inc.php';
47 $url_query .= '&amp;goto=tbl_export.php&amp;back=tbl_export.php';
49 // Dump of a table
51 $export_page_title = __('View dump (schema) of table');
53 // When we have some query, we need to remove LIMIT from that and possibly
54 // generate WHERE clause (if we are asked to export specific rows)
56 if (! empty($sql_query)) {
57 $parser = new PhpMyAdmin\SqlParser\Parser($sql_query);
59 if (! empty($parser->statements[0])
60 && ($parser->statements[0] instanceof PhpMyAdmin\SqlParser\Statements\SelectStatement)
61 ) {
62 // Finding aliases and removing them, but we keep track of them to be
63 // able to replace them in select expression too.
64 $aliases = [];
65 foreach ($parser->statements[0]->from as $from) {
66 if (! empty($from->table) && ! empty($from->alias)) {
67 $aliases[$from->alias] = $from->table;
68 // We remove the alias of the table because they are going to
69 // be replaced anyway.
70 $from->alias = null;
71 $from->expr = null; // Force rebuild.
75 // Rebuilding the SELECT and FROM clauses.
76 if (count($parser->statements[0]->from) > 0
77 && count($parser->statements[0]->union) === 0
78 ) {
79 $replaces = [
81 'FROM',
82 'FROM ' . PhpMyAdmin\SqlParser\Components\ExpressionArray::build(
83 $parser->statements[0]->from
89 // Checking if the WHERE clause has to be replaced.
90 if (! empty($where_clause) && is_array($where_clause)) {
91 $replaces[] = [
92 'WHERE',
93 'WHERE (' . implode(') OR (', $where_clause) . ')',
97 // Preparing to remove the LIMIT clause.
98 $replaces[] = [
99 'LIMIT',
103 // Replacing the clauses.
104 $sql_query = PhpMyAdmin\SqlParser\Utils\Query::replaceClauses(
105 $parser->statements[0],
106 $parser->list,
107 $replaces
110 // Removing the aliases by finding the alias followed by a dot.
111 $tokens = PhpMyAdmin\SqlParser\Lexer::getTokens($sql_query);
112 foreach ($aliases as $alias => $table) {
113 $tokens = PhpMyAdmin\SqlParser\Utils\Tokens::replaceTokens(
114 $tokens,
117 'value_str' => $alias,
120 'type' => PhpMyAdmin\SqlParser\Token::TYPE_OPERATOR,
121 'value_str' => '.',
125 new PhpMyAdmin\SqlParser\Token($table),
126 new PhpMyAdmin\SqlParser\Token('.', PhpMyAdmin\SqlParser\Token::TYPE_OPERATOR),
130 $sql_query = PhpMyAdmin\SqlParser\TokensList::build($tokens);
133 echo PhpMyAdmin\Util::getMessage(PhpMyAdmin\Message::success());
136 if (! isset($sql_query)) {
137 $sql_query = '';
139 if (! isset($num_tables)) {
140 $num_tables = 0;
142 if (! isset($unlim_num_rows)) {
143 $unlim_num_rows = 0;
145 if (! isset($multi_values)) {
146 $multi_values = '';
148 $response = Response::getInstance();
149 $response->addHTML(
150 $displayExport->getDisplay(
151 'table',
152 $db,
153 $table,
154 $sql_query,
155 $num_tables,
156 $unlim_num_rows,
157 $multi_values