Translated using Weblate (Kazakh)
[phpmyadmin.git] / tbl_export.php
blob6fec76b3407fb3fe3a3ae45729d0c21f6a052268
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\Display\Export;
13 use PhpMyAdmin\Relation;
14 use PhpMyAdmin\Response;
16 if (! defined('ROOT_PATH')) {
17 define('ROOT_PATH', __DIR__ . DIRECTORY_SEPARATOR);
20 global $containerBuilder, $db, $url_query;
22 require_once ROOT_PATH . 'libraries/common.inc.php';
24 /** @var Response $response */
25 $response = $containerBuilder->get(Response::class);
27 /** @var DatabaseInterface $dbi */
28 $dbi = $containerBuilder->get(DatabaseInterface::class);
30 PageSettings::showGroup('Export');
32 $header = $response->getHeader();
33 $scripts = $header->getScripts();
34 $scripts->addFile('export.js');
36 // Get the relation settings
37 /** @var Relation $relation */
38 $relation = $containerBuilder->get('relation');
39 $cfgRelation = $relation->getRelationsParam();
41 /** @var Export $displayExport */
42 $displayExport = $containerBuilder->get('display_export');
44 // handling export template actions
45 if (isset($_POST['templateAction']) && $cfgRelation['exporttemplateswork']) {
46 $displayExport->handleTemplateActions($cfgRelation);
47 exit;
50 /**
51 * Gets tables information and displays top links
53 require_once ROOT_PATH . 'libraries/tbl_common.inc.php';
54 $url_query .= '&amp;goto=tbl_export.php&amp;back=tbl_export.php';
56 // Dump of a table
58 $export_page_title = __('View dump (schema) of table');
60 // When we have some query, we need to remove LIMIT from that and possibly
61 // generate WHERE clause (if we are asked to export specific rows)
63 if (! empty($sql_query)) {
64 $parser = new PhpMyAdmin\SqlParser\Parser($sql_query);
66 if (! empty($parser->statements[0])
67 && ($parser->statements[0] instanceof PhpMyAdmin\SqlParser\Statements\SelectStatement)
68 ) {
69 // Finding aliases and removing them, but we keep track of them to be
70 // able to replace them in select expression too.
71 $aliases = [];
72 foreach ($parser->statements[0]->from as $from) {
73 if (! empty($from->table) && ! empty($from->alias)) {
74 $aliases[$from->alias] = $from->table;
75 // We remove the alias of the table because they are going to
76 // be replaced anyway.
77 $from->alias = null;
78 $from->expr = null; // Force rebuild.
82 // Rebuilding the SELECT and FROM clauses.
83 if (count($parser->statements[0]->from) > 0
84 && count($parser->statements[0]->union) === 0
85 ) {
86 $replaces = [
88 'FROM',
89 'FROM ' . PhpMyAdmin\SqlParser\Components\ExpressionArray::build(
90 $parser->statements[0]->from
96 // Checking if the WHERE clause has to be replaced.
97 if (! empty($where_clause) && is_array($where_clause)) {
98 $replaces[] = [
99 'WHERE',
100 'WHERE (' . implode(') OR (', $where_clause) . ')',
104 // Preparing to remove the LIMIT clause.
105 $replaces[] = [
106 'LIMIT',
110 // Replacing the clauses.
111 $sql_query = PhpMyAdmin\SqlParser\Utils\Query::replaceClauses(
112 $parser->statements[0],
113 $parser->list,
114 $replaces
117 // Removing the aliases by finding the alias followed by a dot.
118 $tokens = PhpMyAdmin\SqlParser\Lexer::getTokens($sql_query);
119 foreach ($aliases as $alias => $table) {
120 $tokens = PhpMyAdmin\SqlParser\Utils\Tokens::replaceTokens(
121 $tokens,
124 'value_str' => $alias,
127 'type' => PhpMyAdmin\SqlParser\Token::TYPE_OPERATOR,
128 'value_str' => '.',
132 new PhpMyAdmin\SqlParser\Token($table),
133 new PhpMyAdmin\SqlParser\Token('.', PhpMyAdmin\SqlParser\Token::TYPE_OPERATOR),
137 $sql_query = PhpMyAdmin\SqlParser\TokensList::build($tokens);
140 echo PhpMyAdmin\Util::getMessage(PhpMyAdmin\Message::success());
143 if (! isset($sql_query)) {
144 $sql_query = '';
146 if (! isset($num_tables)) {
147 $num_tables = 0;
149 if (! isset($unlim_num_rows)) {
150 $unlim_num_rows = 0;
152 if (! isset($multi_values)) {
153 $multi_values = '';
155 $response = Response::getInstance();
156 $response->addHTML(
157 $displayExport->getDisplay(
158 'table',
159 $db,
160 $table,
161 $sql_query,
162 $num_tables,
163 $unlim_num_rows,
164 $multi_values