2 /* vim: set expandtab sw=4 ts=4 sts=4: */
8 use PMA\libraries\config\PageSettings
;
9 use PMA\libraries\Response
;
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']) {
43 $query = "INSERT INTO " . $templateTable . "("
44 . " `username`, `export_type`,"
45 . " `template_name`, `template_data`"
48 . "'" . PMA\libraries\Util
::sqlAddSlashes($_REQUEST['exportType']) . "', "
49 . "'" . PMA\libraries\Util
::sqlAddSlashes($_REQUEST['templateName']) . "', "
50 . "'" . PMA\libraries\Util
::sqlAddSlashes($_REQUEST['templateData']) . "');";
53 $query = "SELECT `template_data` FROM " . $templateTable
54 . " WHERE `id` = " . $id . " AND `username` = '" . $user . "'";
57 $query = "UPDATE " . $templateTable . " SET `template_data` = "
58 . "'" . PMA\libraries\Util
::sqlAddSlashes($_REQUEST['templateData']) . "'"
59 . " WHERE `id` = " . $id . " AND `username` = '" . $user . "'";
62 $query = "DELETE FROM " . $templateTable
63 . " WHERE `id` = " . $id . " AND `username` = '" . $user . "'";
69 $result = PMA_queryAsControlUser($query, false);
71 $response = PMA\libraries\Response
::getInstance();
73 $error = $GLOBALS['dbi']->getError($GLOBALS['controllink']);
74 $response->setRequestStatus(false);
75 $response->addJSON('message', $error);
79 $response->setRequestStatus(true);
80 if ('create' == $_REQUEST['templateAction']) {
83 PMA_getOptionsForExportTemplates($_REQUEST['exportType'])
85 } elseif ('load' == $_REQUEST['templateAction']) {
87 while ($row = $GLOBALS['dbi']->fetchAssoc(
88 $result, $GLOBALS['controllink']
90 $data = $row['template_data'];
92 $response->addJSON('data', $data);
94 $GLOBALS['dbi']->freeResult($result);
99 * Gets tables information and displays top links
101 require_once 'libraries/tbl_common.inc.php';
102 $url_query .= '&goto=tbl_export.php&back=tbl_export.php';
103 require_once 'libraries/tbl_info.inc.php';
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.
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.
128 $from->expr
= null; // Force rebuild.
132 // Rebuilding the SELECT and FROM clauses.
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))) {
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],
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(
165 'value_str' => $alias,
168 'type' => SqlParser\Token
::TYPE_OPERATOR
,
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)) {
186 if (! isset($num_tables)) {
189 if (! isset($unlim_num_rows)) {
192 if (! isset($multi_values)) {
195 $response = Response
::getInstance();
197 PMA_getExportDisplay(
198 'table', $db, $table, $sql_query, $num_tables,
199 $unlim_num_rows, $multi_values