Ticket was moved to bugs
[phpmyadmin.git] / tbl_export.php
bloba8a1178936887d7b4d6c075d2c2b0be690d0effe
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';
14 $response = PMA_Response::getInstance();
15 $header = $response->getHeader();
16 $scripts = $header->getScripts();
17 $scripts->addFile('export.js');
19 /**
20 * Gets tables informations and displays top links
22 require_once 'libraries/tbl_common.inc.php';
23 $url_query .= '&amp;goto=tbl_export.php&amp;back=tbl_export.php';
24 require_once 'libraries/tbl_info.inc.php';
26 // Dump of a table
28 $export_page_title = __('View dump (schema) of table');
30 // When we have some query, we need to remove LIMIT from that and possibly
31 // generate WHERE clause (if we are asked to export specific rows)
33 if (! empty($sql_query)) {
34 // Parse query so we can work with tokens
35 $parsed_sql = PMA_SQP_parse($sql_query);
36 $analyzed_sql = PMA_SQP_analyze($parsed_sql);
38 // Need to generate WHERE clause?
39 if (isset($where_clause)) {
41 // If a table alias is used, get rid of it since
42 // where clauses are on real table name
43 if ($analyzed_sql[0]['table_ref'][0]['table_alias']) {
44 // Exporting seleted rows is only allowed for queries involving
45 // a single table. So we can safely assume that there is only one
46 // table in 'table_ref' array.
47 $temp_sql_array = preg_split('/\bfrom\b/i', $sql_query);
48 $sql_query = $temp_sql_array[0] . 'FROM ';
49 if (! empty($analyzed_sql[0]['table_ref'][0]['db'])) {
50 $sql_query .= PMA_Util::backquote(
51 $analyzed_sql[0]['table_ref'][0]['db']
53 $sql_query .= '.';
55 $sql_query .= PMA_Util::backquote(
56 $analyzed_sql[0]['table_ref'][0]['table_name']
59 unset($temp_sql_array);
61 // Regular expressions which can appear in sql query,
62 // before the sql segment which remains as it is.
63 $regex_array = array(
64 '/\bwhere\b/i', '/\bgroup by\b/i', '/\bhaving\b/i', '/\border by\b/i'
67 $first_occurring_regex = PMA_Util::getFirstOccurringRegularExpression(
68 $regex_array, $sql_query
70 unset($regex_array);
72 // The part "SELECT `id`, `name` FROM `customers`"
73 // is not modified by the next code segment, when exporting
74 // the result set from a query such as
75 // "SELECT `id`, `name` FROM `customers` WHERE id NOT IN
76 // ( SELECT id FROM companies WHERE name LIKE '%u%')"
77 if (! is_null($first_occurring_regex)) {
78 $temp_sql_array = preg_split($first_occurring_regex, $sql_query);
79 $sql_query = $temp_sql_array[0];
81 unset($first_occurring_regex, $temp_sql_array);
83 // Append the where clause using the primary key of each row
84 if (is_array($where_clause) && (count($where_clause) > 0)) {
85 $sql_query .= ' WHERE (' . implode(') OR (', $where_clause) . ')';
88 if (!empty($analyzed_sql[0]['group_by_clause'])) {
89 $sql_query .= ' GROUP BY ' . $analyzed_sql[0]['group_by_clause'];
91 if (!empty($analyzed_sql[0]['having_clause'])) {
92 $sql_query .= ' HAVING ' . $analyzed_sql[0]['having_clause'];
94 if (!empty($analyzed_sql[0]['order_by_clause'])) {
95 $sql_query .= ' ORDER BY ' . $analyzed_sql[0]['order_by_clause'];
97 } else {
98 // Just crop LIMIT clause
99 $sql_query = $analyzed_sql[0]['section_before_limit']
100 . $analyzed_sql[0]['section_after_limit'];
102 echo PMA_Util::getMessage(PMA_Message::success());
105 $export_type = 'table';
106 require_once 'libraries/display_export.inc.php';