Translated using Weblate (Slovenian)
[phpmyadmin.git] / tbl_export.php
blob2db17e3284fe1946f41a8f27d0b447fe1d0b4b0f
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Table export
6 * @package PhpMyAdmin
7 */
8 use PMA\libraries\config\PageSettings;
9 use PMA\libraries\Response;
11 /**
14 require_once 'libraries/common.inc.php';
15 require_once 'libraries/display_export.lib.php';
16 require_once 'libraries/config/user_preferences.forms.php';
17 require_once 'libraries/config/page_settings.forms.php';
19 PageSettings::showGroup('Export');
21 $response = PMA\libraries\Response::getInstance();
22 $header = $response->getHeader();
23 $scripts = $header->getScripts();
24 $scripts->addFile('export.js');
26 // Get the relation settings
27 $cfgRelation = PMA_getRelationsParam();
29 // handling export template actions
30 if (isset($_REQUEST['templateAction']) && $cfgRelation['exporttemplateswork']) {
32 if (isset($_REQUEST['templateId'])) {
33 $templateId = $_REQUEST['templateId'];
34 $id = PMA\libraries\Util::sqlAddSlashes($templateId);
37 $templateTable = PMA\libraries\Util::backquote($cfgRelation['db']) . '.'
38 . PMA\libraries\Util::backquote($cfgRelation['export_templates']);
39 $user = PMA\libraries\Util::sqlAddSlashes($GLOBALS['cfg']['Server']['user']);
41 switch ($_REQUEST['templateAction']) {
42 case 'create':
43 $query = "INSERT INTO " . $templateTable . "("
44 . " `username`, `export_type`,"
45 . " `template_name`, `template_data`"
46 . ") VALUES ("
47 . "'" . $user . "', "
48 . "'" . PMA\libraries\Util::sqlAddSlashes($_REQUEST['exportType']) . "', "
49 . "'" . PMA\libraries\Util::sqlAddSlashes($_REQUEST['templateName']) . "', "
50 . "'" . PMA\libraries\Util::sqlAddSlashes($_REQUEST['templateData']) . "');";
51 break;
52 case 'load':
53 $query = "SELECT `template_data` FROM " . $templateTable
54 . " WHERE `id` = " . $id . " AND `username` = '" . $user . "'";
55 break;
56 case 'update':
57 $query = "UPDATE " . $templateTable . " SET `template_data` = "
58 . "'" . PMA\libraries\Util::sqlAddSlashes($_REQUEST['templateData']) . "'"
59 . " WHERE `id` = " . $id . " AND `username` = '" . $user . "'";
60 break;
61 case 'delete':
62 $query = "DELETE FROM " . $templateTable
63 . " WHERE `id` = " . $id . " AND `username` = '" . $user . "'";
64 break;
65 default:
66 break;
69 $result = PMA_queryAsControlUser($query, false);
71 $response = PMA\libraries\Response::getInstance();
72 if (! $result) {
73 $error = $GLOBALS['dbi']->getError($GLOBALS['controllink']);
74 $response->setRequestStatus(false);
75 $response->addJSON('message', $error);
76 exit;
79 $response->setRequestStatus(true);
80 if ('create' == $_REQUEST['templateAction']) {
81 $response->addJSON(
82 'data',
83 PMA_getOptionsForExportTemplates($_REQUEST['exportType'])
85 } elseif ('load' == $_REQUEST['templateAction']) {
86 $data = null;
87 while ($row = $GLOBALS['dbi']->fetchAssoc(
88 $result, $GLOBALS['controllink']
89 )) {
90 $data = $row['template_data'];
92 $response->addJSON('data', $data);
94 $GLOBALS['dbi']->freeResult($result);
95 exit;
98 /**
99 * Gets tables information and displays top links
101 require_once 'libraries/tbl_common.inc.php';
102 $url_query .= '&amp;goto=tbl_export.php&amp;back=tbl_export.php';
103 require_once 'libraries/tbl_info.inc.php';
105 // Dump of a table
107 $export_page_title = __('View dump (schema) of table');
109 // When we have some query, we need to remove LIMIT from that and possibly
110 // generate WHERE clause (if we are asked to export specific rows)
112 if (! empty($sql_query)) {
113 $parser = new SqlParser\Parser($sql_query);
115 if ((!empty($parser->statements[0]))
116 && ($parser->statements[0] instanceof SqlParser\Statements\SelectStatement)
119 // Finding aliases and removing them, but we keep track of them to be
120 // able to replace them in select expression too.
121 $aliases = array();
122 foreach ($parser->statements[0]->from as $from) {
123 if ((!empty($from->table)) && (!empty($from->alias))) {
124 $aliases[$from->alias] = $from->table;
125 // We remove the alias of the table because they are going to
126 // be replaced anyway.
127 $from->alias = null;
128 $from->expr = null; // Force rebuild.
132 // Rebuilding the SELECT and FROM clauses.
133 $replaces = array(
134 array(
135 'FROM', 'FROM ' . SqlParser\Components\ExpressionArray::build(
136 $parser->statements[0]->from
141 // Checking if the WHERE clause has to be replaced.
142 if ((!empty($where_clause)) && (is_array($where_clause))) {
143 $replaces[] = array(
144 'WHERE', 'WHERE (' . implode(') OR (', $where_clause) . ')'
148 // Preparing to remove the LIMIT clause.
149 $replaces[] = array('LIMIT', '');
151 // Replacing the clauses.
152 $sql_query = SqlParser\Utils\Query::replaceClauses(
153 $parser->statements[0],
154 $parser->list,
155 $replaces
158 // Removing the aliases by finding the alias followed by a dot.
159 $tokens = SqlParser\Lexer::getTokens($sql_query);
160 foreach ($aliases as $alias => $table) {
161 $tokens = SqlParser\Utils\Tokens::replaceTokens(
162 $tokens,
163 array(
164 array(
165 'value_str' => $alias,
167 array(
168 'type' => SqlParser\Token::TYPE_OPERATOR,
169 'value_str' => '.',
172 array()
175 $sql_query = SqlParser\TokensList::build($tokens);
178 echo PMA\libraries\Util::getMessage(PMA\libraries\Message::success());
181 require_once 'libraries/display_export.lib.php';
183 if (! isset($sql_query)) {
184 $sql_query = '';
186 if (! isset($num_tables)) {
187 $num_tables = 0;
189 if (! isset($unlim_num_rows)) {
190 $unlim_num_rows = 0;
192 if (! isset($multi_values)) {
193 $multi_values = '';
195 $response = Response::getInstance();
196 $response->addHTML(
197 PMA_getExportDisplay(
198 'table', $db, $table, $sql_query, $num_tables,
199 $unlim_num_rows, $multi_values