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