primary_key is really a WHERE clause (that works also on tables where no PK is defined)
[phpmyadmin/madhuracj.git] / libraries / export / php_array.php
blob17d5f4c286b2be903378e3da330305ab2ec471d8
1 <?php
2 /**
3 * Set of functions used to build dumps of tables as PHP Arrays
5 * @author Geoffray Warnants <http://www.geoffray.be>
6 * @version 0.2b (20090704)
7 */
8 if (! defined('PHPMYADMIN')) {
9 exit;
12 /**
15 if (isset($plugin_list)) {
16 $plugin_list['php_array'] = array(
17 'text' => 'strPhpArray',
18 'extension' => 'php',
19 'mime_type' => 'text/plain',
20 'options' => array(
21 array(
22 'type' => 'hidden',
23 'name' => 'data',
26 'options_text' => 'strOptions',
28 } else {
30 /**
31 * Set of functions used to build exports of tables
34 /**
35 * Outputs comment
37 * @param string Text of comment
39 * @return bool Whether it suceeded
41 function PMA_exportComment($text)
43 PMA_exportOutputHandler('// ' . $text . $GLOBALS['crlf']);
44 return true;
47 /**
48 * Outputs export footer
50 * @return bool Whether it suceeded
52 * @access public
54 function PMA_exportFooter()
56 return true;
59 /**
60 * Outputs export header
62 * @return bool Whether it suceeded
64 * @access public
66 function PMA_exportHeader()
68 PMA_exportOutputHandler(
69 '<?php' . $GLOBALS['crlf']
70 . '/**' . $GLOBALS['crlf']
71 . ' * Export to PHP Array plugin for PHPMyAdmin' . $GLOBALS['crlf']
72 . ' * @author Geoffray Warnants' . $GLOBALS['crlf']
73 . ' * @version 0.2b' . $GLOBALS['crlf']
74 . ' */' . $GLOBALS['crlf'] . $GLOBALS['crlf']
76 return true;
79 /**
80 * Outputs database header
82 * @param string Database name
84 * @return bool Whether it suceeded
86 * @access public
88 function PMA_exportDBHeader($db)
90 PMA_exportOutputHandler('//' . $GLOBALS['crlf'] . '// Database "' . $db . '"' . $GLOBALS['crlf'] . '//' . $GLOBALS['crlf']);
91 return true;
94 /**
95 * Outputs database footer
97 * @param string Database name
99 * @return bool Whether it suceeded
101 * @access public
103 function PMA_exportDBFooter($db)
105 return true;
109 * Outputs create database database
111 * @param string Database name
113 * @return bool Whether it suceeded
115 * @access public
117 function PMA_exportDBCreate($db)
119 return true;
123 * Outputs the content of a table in YAML format
125 * @param string the database name
126 * @param string the table name
127 * @param string the end of line sequence
128 * @param string the url to go back in case of error
129 * @param string SQL query for obtaining data
131 * @return bool Whether it suceeded
133 * @access public
135 function PMA_exportData($db, $table, $crlf, $error_url, $sql_query)
137 $result = PMA_DBI_query($sql_query, null, PMA_DBI_QUERY_UNBUFFERED);
139 $columns_cnt = PMA_DBI_num_fields($result);
140 for ($i = 0; $i < $columns_cnt; $i++) {
141 $columns[$i] = stripslashes(PMA_DBI_field_name($result, $i));
143 unset($i);
145 $buffer = '';
146 $record_cnt = 0;
147 while ($record = PMA_DBI_fetch_row($result)) {
149 $record_cnt++;
151 // Output table name as comment if this is the first record of the table
152 if ($record_cnt == 1) {
153 $buffer .= $crlf . '// ' . $db . '.' . $table . $crlf;
154 $buffer .= '$' . $table . ' = array(' . $crlf;
155 $buffer .= ' array(';
156 } else {
157 $buffer .= ',' . $crlf . ' array(';
161 for ($i = 0; $i < $columns_cnt; $i++) {
163 $isLastLine = ($i + 1 >= $columns_cnt);
165 $column = $columns[$i];
167 if (is_null($record[$i])) {
168 $buffer .= "'" . $column . "'=>null" . (! $isLastLine ? ',' : '');
169 } elseif (is_numeric($record[$i])) {
170 $buffer .= "'" . $column . "'=>" . $record[$i] . (! $isLastLine ? ',' : '');
171 } else {
172 $buffer .= "'" . $column . "'=>'" . addslashes($record[$i]) . "'" . (! $isLastLine ? ',' : '');
176 $buffer .= ')';
179 $buffer .= $crlf . ');' . $crlf;
180 if (! PMA_exportOutputHandler($buffer)) {
181 return FALSE;
184 PMA_DBI_free_result($result);
186 return true;