Merge remote-tracking branch 'origin/master'
[phpmyadmin.git] / tbl_export.php
blobe9adfdcbb00aefee859d7e7235f71a809737babc
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 = 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']) {
31 PMA_handleExportTemplateActions($cfgRelation);
32 exit;
35 /**
36 * Gets tables information and displays top links
38 require_once 'libraries/tbl_common.inc.php';
39 $url_query .= '&amp;goto=tbl_export.php&amp;back=tbl_export.php';
41 // Dump of a table
43 $export_page_title = __('View dump (schema) of table');
45 // When we have some query, we need to remove LIMIT from that and possibly
46 // generate WHERE clause (if we are asked to export specific rows)
48 if (! empty($sql_query)) {
49 $parser = new PhpMyAdmin\SqlParser\Parser($sql_query);
51 if ((!empty($parser->statements[0]))
52 && ($parser->statements[0] instanceof PhpMyAdmin\SqlParser\Statements\SelectStatement)
53 ) {
55 // Finding aliases and removing them, but we keep track of them to be
56 // able to replace them in select expression too.
57 $aliases = array();
58 foreach ($parser->statements[0]->from as $from) {
59 if ((!empty($from->table)) && (!empty($from->alias))) {
60 $aliases[$from->alias] = $from->table;
61 // We remove the alias of the table because they are going to
62 // be replaced anyway.
63 $from->alias = null;
64 $from->expr = null; // Force rebuild.
68 // Rebuilding the SELECT and FROM clauses.
69 if (count($parser->statements[0]->from) > 0
70 && count($parser->statements[0]->union) === 0
71 ) {
72 $replaces = array(
73 array(
74 'FROM', 'FROM ' . PhpMyAdmin\SqlParser\Components\ExpressionArray::build(
75 $parser->statements[0]->from
81 // Checking if the WHERE clause has to be replaced.
82 if ((!empty($where_clause)) && (is_array($where_clause))) {
83 $replaces[] = array(
84 'WHERE', 'WHERE (' . implode(') OR (', $where_clause) . ')'
88 // Preparing to remove the LIMIT clause.
89 $replaces[] = array('LIMIT', '');
91 // Replacing the clauses.
92 $sql_query = PhpMyAdmin\SqlParser\Utils\Query::replaceClauses(
93 $parser->statements[0],
94 $parser->list,
95 $replaces
98 // Removing the aliases by finding the alias followed by a dot.
99 $tokens = PhpMyAdmin\SqlParser\Lexer::getTokens($sql_query);
100 foreach ($aliases as $alias => $table) {
101 $tokens = PhpMyAdmin\SqlParser\Utils\Tokens::replaceTokens(
102 $tokens,
103 array(
104 array(
105 'value_str' => $alias,
107 array(
108 'type' => PhpMyAdmin\SqlParser\Token::TYPE_OPERATOR,
109 'value_str' => '.',
112 array(
113 new PhpMyAdmin\SqlParser\Token($table),
114 new PhpMyAdmin\SqlParser\Token('.',PhpMyAdmin\SqlParser\Token::TYPE_OPERATOR)
118 $sql_query = PhpMyAdmin\SqlParser\TokensList::build($tokens);
121 echo PMA\libraries\Util::getMessage(PMA\libraries\Message::success());
124 require_once 'libraries/display_export.lib.php';
126 if (! isset($sql_query)) {
127 $sql_query = '';
129 if (! isset($num_tables)) {
130 $num_tables = 0;
132 if (! isset($unlim_num_rows)) {
133 $unlim_num_rows = 0;
135 if (! isset($multi_values)) {
136 $multi_values = '';
138 $response = Response::getInstance();
139 $response->addHTML(
140 PMA_getExportDisplay(
141 'table', $db, $table, $sql_query, $num_tables,
142 $unlim_num_rows, $multi_values