2 /* vim: set expandtab sw=4 ts=4 sts=4: */
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']) {
40 $query = "INSERT INTO " . $templateTable . "("
41 . " `username`, `export_type`,"
42 . " `template_name`, `template_data`"
45 . "'" . PMA_Util
::sqlAddSlashes($_REQUEST['exportType']) . "', "
46 . "'" . PMA_Util
::sqlAddSlashes($_REQUEST['templateName']) . "', "
47 . "'" . PMA_Util
::sqlAddSlashes($_REQUEST['templateData']) . "');";
50 $query = "SELECT `template_data` FROM " . $templateTable
51 . " WHERE `id` = " . $id . " AND `username` = '" . $user . "'";
54 $query = "UPDATE " . $templateTable . " SET `template_data` = "
55 . "'" . PMA_Util
::sqlAddSlashes($_REQUEST['templateData']) . "'"
56 . " WHERE `id` = " . $id . " AND `username` = '" . $user . "'";
59 $query = "DELETE FROM " . $templateTable
60 . " WHERE `id` = " . $id . " AND `username` = '" . $user . "'";
66 $result = PMA_queryAsControlUser($query, false);
68 $response = PMA_Response
::getInstance();
70 $error = $GLOBALS['dbi']->getError($GLOBALS['controllink']);
71 $response->isSuccess(false);
72 $response->addJSON('message', $error);
76 $response->isSuccess(true);
77 if ('create' == $_REQUEST['templateAction']) {
80 PMA_getOptionsForExportTemplates($_REQUEST['exportType'])
82 } elseif ('load' == $_REQUEST['templateAction']) {
84 while ($row = $GLOBALS['dbi']->fetchAssoc(
85 $result, $GLOBALS['controllink']
87 $data = $row['template_data'];
89 $response->addJSON('data', $data);
91 $GLOBALS['dbi']->freeResult($result);
96 * Gets tables information and displays top links
98 require_once 'libraries/tbl_common.inc.php';
99 $url_query .= '&goto=tbl_export.php&back=tbl_export.php';
100 require_once 'libraries/tbl_info.inc.php';
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.
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.
125 $from->expr
= null; // Force rebuild.
129 // Rebuilding the SELECT and FROM clauses.
132 'FROM', 'FROM ' . SqlParser\Components\ExpressionArray
::build(
133 $parser->statements
[0]->from
138 // Checking if the WHERE clause has to be replaced.
139 if ((!empty($where_clause)) && (is_array($where_clause))) {
141 'WHERE', 'WHERE (' . implode(') OR (', $where_clause) . ')'
145 // Preparing to remove the LIMIT clause.
146 $replaces[] = array('LIMIT', '');
148 // Replacing the clauses.
149 $sql_query = SqlParser\Utils\Query
::replaceClauses(
150 $parser->statements
[0],
155 // Removing the aliases by finding the alias followed by a dot.
156 $tokens = SqlParser\Lexer
::getTokens($sql_query);
157 foreach ($aliases as $alias => $table) {
158 $tokens = SqlParser\Utils\Tokens
::replaceTokens(
162 'value_str' => $alias,
165 'type' => SqlParser\Token
::TYPE_OPERATOR
,
170 new SqlParser\
Token($table),
171 new SqlParser\
Token('.',SqlParser\Token
::TYPE_OPERATOR
)
175 $sql_query = SqlParser\TokensList
::build($tokens);
178 echo PMA_Util
::getMessage(PMA_Message
::success());
181 $export_type = 'table';
182 require_once 'libraries/display_export.inc.php';