Translated using Weblate (Chinese (China))
[phpmyadmin.git] / tbl_export.php
blobafbe4fb9f112e72b40e46c0c7f7fb04c858e2ea7
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Table export
6 * @package PhpMyAdmin
7 */
9 /**
12 require_once 'libraries/common.inc.php';
13 require_once 'libraries/config/page_settings.class.php';
14 require_once 'libraries/display_export.lib.php';
16 PMA_PageSettings::showGroup('Export');
18 $response = PMA_Response::getInstance();
19 $header = $response->getHeader();
20 $scripts = $header->getScripts();
21 $scripts->addFile('export.js');
23 // Get the relation settings
24 $cfgRelation = PMA_getRelationsParam();
26 // handling export template actions
27 if (isset($_REQUEST['templateAction']) && $cfgRelation['exporttemplateswork']) {
29 if (isset($_REQUEST['templateId'])) {
30 $templateId = $_REQUEST['templateId'];
31 $id = PMA_Util::sqlAddSlashes($templateId);
34 $templateTable = PMA_Util::backquote($cfgRelation['db']) . '.'
35 . PMA_Util::backquote($cfgRelation['export_templates']);
36 $user = PMA_Util::sqlAddSlashes($GLOBALS['cfg']['Server']['user']);
38 switch ($_REQUEST['templateAction']) {
39 case 'create':
40 $query = "INSERT INTO " . $templateTable . "("
41 . " `username`, `export_type`,"
42 . " `template_name`, `template_data`"
43 . ") VALUES ("
44 . "'" . $user . "', "
45 . "'" . PMA_Util::sqlAddSlashes($_REQUEST['exportType']) . "', "
46 . "'" . PMA_Util::sqlAddSlashes($_REQUEST['templateName']) . "', "
47 . "'" . PMA_Util::sqlAddSlashes($_REQUEST['templateData']) . "');";
48 break;
49 case 'load':
50 $query = "SELECT `template_data` FROM " . $templateTable
51 . " WHERE `id` = " . $id . " AND `username` = '" . $user . "'";
52 break;
53 case 'update':
54 $query = "UPDATE " . $templateTable . " SET `template_data` = "
55 . "'" . PMA_Util::sqlAddSlashes($_REQUEST['templateData']) . "'"
56 . " WHERE `id` = " . $id . " AND `username` = '" . $user . "'";
57 break;
58 case 'delete':
59 $query = "DELETE FROM " . $templateTable
60 . " WHERE `id` = " . $id . " AND `username` = '" . $user . "'";
61 break;
62 default:
63 break;
66 $result = PMA_queryAsControlUser($query, false);
68 $response = PMA_Response::getInstance();
69 if (! $result) {
70 $error = $GLOBALS['dbi']->getError($GLOBALS['controllink']);
71 $response->isSuccess(false);
72 $response->addJSON('message', $error);
73 exit;
76 $response->isSuccess(true);
77 if ('create' == $_REQUEST['templateAction']) {
78 $response->addJSON(
79 'data',
80 PMA_getOptionsForExportTemplates($_REQUEST['exportType'])
82 } elseif ('load' == $_REQUEST['templateAction']) {
83 $data = null;
84 while ($row = $GLOBALS['dbi']->fetchAssoc(
85 $result, $GLOBALS['controllink']
86 )) {
87 $data = $row['template_data'];
89 $response->addJSON('data', $data);
91 $GLOBALS['dbi']->freeResult($result);
92 exit;
95 /**
96 * Gets tables information and displays top links
98 require_once 'libraries/tbl_common.inc.php';
99 $url_query .= '&amp;goto=tbl_export.php&amp;back=tbl_export.php';
100 require_once 'libraries/tbl_info.inc.php';
102 // Dump of a table
104 $export_page_title = __('View dump (schema) of table');
106 // When we have some query, we need to remove LIMIT from that and possibly
107 // generate WHERE clause (if we are asked to export specific rows)
109 if (! empty($sql_query)) {
110 $parser = new SqlParser\Parser($sql_query);
112 if ((!empty($parser->statements[0]))
113 && ($parser->statements[0] instanceof SqlParser\Statements\SelectStatement)
116 // Finding aliases and removing them, but we keep track of them to be
117 // able to replace them in select expression too.
118 $aliases = array();
119 foreach ($parser->statements[0]->from as $from) {
120 if ((!empty($from->table)) && (!empty($from->alias))) {
121 $aliases[$from->alias] = $from->table;
122 // We remove the alias of the table because they are going to
123 // be replaced anyway.
124 $from->alias = null;
125 $from->expr = null; // Force rebuild.
129 // Replacing the aliases in select expressions.
130 foreach ($parser->statements[0]->expr as $expr) {
131 if ((!empty($expr->table)) && (!empty($aliases[$expr->table]))) {
132 // Changing the table to null (leave the MySQL to find the
133 // right table).
134 // This is possible because exporting selected rows is only
135 // allowed for queries involing a single table.
136 $expr->table = null;
137 $expr->expr = null; // Force rebuild.
141 // Rebuilding the SELECT and FROM clauses.
142 $replaces = array(
143 array(
144 'SELECT', 'SELECT ' . SqlParser\Components\ExpressionArray::build(
145 $parser->statements[0]->expr
148 array(
149 'FROM', 'FROM ' . SqlParser\Components\ExpressionArray::build(
150 $parser->statements[0]->from
155 // Checking if the WHERE clause has to be replaced.
156 if ((!empty($where_clause)) && (is_array($where_clause))) {
157 $replaces[] = array(
158 'WHERE', 'WHERE (' . implode(') OR (', $where_clause) . ')'
162 // Preparing to remove the LIMIT clause.
163 $replaces[] = array('LIMIT', '');
165 // Replacing the clauses.
166 $sql_query = SqlParser\Utils\Query::replaceClauses(
167 $parser->statements[0],
168 $parser->list,
169 $replaces
173 echo PMA_Util::getMessage(PMA_Message::success());
176 $export_type = 'table';
177 require_once 'libraries/display_export.inc.php';