Misleading comments
[phpmyadmin/ammaryasirr.git] / libraries / export / odt.php
blobb70388dd3cdd7da435f0a149d30917fc2afffd6a
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Set of functions used to build OpenDocument Text dumps of tables
6 * @package phpMyAdmin-Export-ODT
7 */
8 if (! defined('PHPMYADMIN')) {
9 exit;
12 /**
15 if (isset($plugin_list)) {
16 $hide_structure = false;
17 if ($plugin_param['export_type'] == 'table' && !$plugin_param['single_table']) {
18 $hide_structure = true;
20 $plugin_list['odt'] = array(
21 'text' => __('Open Document Text'),
22 'extension' => 'odt',
23 'mime_type' => 'application/vnd.oasis.opendocument.text',
24 'force_file' => true,
25 'options' => array(), /* Filled later */
26 'options_text' => __('Options'),
29 /* what to dump (structure/data/both) */
30 $plugin_list['odt']['options'][] =
31 array('type' => 'begin_group', 'text' => __('Dump table') , 'name' => 'general_opts');
32 $plugin_list['odt']['options'][] =
33 array('type' => 'radio', 'name' => 'structure_or_data', 'values' => array('structure' => __('structure'), 'data' => __('data'), 'structure_and_data' => __('structure and data')));
34 $plugin_list['odt']['options'][] = array('type' => 'end_group');
36 /* Structure options */
37 if (!$hide_structure) {
38 $plugin_list['odt']['options'][] =
39 array('type' => 'begin_group', 'name' => 'structure', 'text' => __('Object creation options'), 'force' => 'data');
40 if (!empty($GLOBALS['cfgRelation']['relation'])) {
41 $plugin_list['odt']['options'][] =
42 array('type' => 'bool', 'name' => 'relation', 'text' => __('Display foreign key relationships'));
44 $plugin_list['odt']['options'][] =
45 array('type' => 'bool', 'name' => 'comments', 'text' => __('Display comments'));
46 if (!empty($GLOBALS['cfgRelation']['mimework'])) {
47 $plugin_list['odt']['options'][] =
48 array('type' => 'bool', 'name' => 'mime', 'text' => __('Display MIME types'));
50 $plugin_list['odt']['options'][] =
51 array('type' => 'end_group');
53 /* Data */
54 $plugin_list['odt']['options'][] =
55 array('type' => 'begin_group', 'name' => 'data', 'text' => __('Data dump options'), 'force' => 'structure');
56 $plugin_list['odt']['options'][] =
57 array('type' => 'bool', 'name' => 'columns', 'text' => __('Put columns names in the first row'));
58 $plugin_list['odt']['options'][] =
59 array('type' => 'text', 'name' => 'null', 'text' => __('Replace NULL with:'));
60 $plugin_list['odt']['options'][] =
61 array('type' => 'end_group');
62 } else {
64 $GLOBALS['odt_buffer'] = '';
65 require_once './libraries/opendocument.lib.php';
67 /**
68 * Outputs comment
70 * @param string Text of comment
72 * @return bool Whether it suceeded
74 function PMA_exportComment($text) {
75 return TRUE;
78 /**
79 * Outputs export footer
81 * @return bool Whether it suceeded
83 * @access public
85 function PMA_exportFooter() {
86 $GLOBALS['odt_buffer'] .= '</office:text>'
87 . '</office:body>'
88 . '</office:document-content>';
89 if (!PMA_exportOutputHandler(PMA_createOpenDocument('application/vnd.oasis.opendocument.text', $GLOBALS['odt_buffer']))) {
90 return FALSE;
92 return TRUE;
95 /**
96 * Outputs export header
98 * @return bool Whether it suceeded
100 * @access public
102 function PMA_exportHeader() {
103 $GLOBALS['odt_buffer'] .= '<?xml version="1.0" encoding="' . $GLOBALS['charset'] . '"?' . '>'
104 . '<office:document-content '. $GLOBALS['OpenDocumentNS'] . 'office:version="1.0">'
105 . '<office:body>'
106 . '<office:text>';
107 return TRUE;
111 * Outputs database header
113 * @param string Database name
115 * @return bool Whether it suceeded
117 * @access public
119 function PMA_exportDBHeader($db) {
120 $GLOBALS['odt_buffer'] .= '<text:h text:outline-level="1" text:style-name="Heading_1" text:is-list-header="true">' . htmlspecialchars(__('Database') . ' ' . $db) . '</text:h>';
121 return TRUE;
125 * Outputs database footer
127 * @param string Database name
129 * @return bool Whether it suceeded
131 * @access public
133 function PMA_exportDBFooter($db) {
134 return TRUE;
138 * Outputs create database database
140 * @param string Database name
142 * @return bool Whether it suceeded
144 * @access public
146 function PMA_exportDBCreate($db) {
147 return TRUE;
151 * Outputs the content of a table in CSV format
153 * @param string the database name
154 * @param string the table name
155 * @param string the end of line sequence
156 * @param string the url to go back in case of error
157 * @param string SQL query for obtaining data
159 * @return bool Whether it suceeded
161 * @access public
163 function PMA_exportData($db, $table, $crlf, $error_url, $sql_query) {
164 global $what;
166 // Gets the data from the database
167 $result = PMA_DBI_query($sql_query, null, PMA_DBI_QUERY_UNBUFFERED);
168 $fields_cnt = PMA_DBI_num_fields($result);
169 $fields_meta = PMA_DBI_get_fields_meta($result);
170 $field_flags = array();
171 for ($j = 0; $j < $fields_cnt; $j++) {
172 $field_flags[$j] = PMA_DBI_field_flags($result, $j);
175 $GLOBALS['odt_buffer'] .= '<text:h text:outline-level="2" text:style-name="Heading_2" text:is-list-header="true">' . htmlspecialchars(__('Dumping data for table') . ' ' . $table) . '</text:h>';
176 $GLOBALS['odt_buffer'] .= '<table:table table:name="' . htmlspecialchars($table) . '_structure">';
177 $GLOBALS['odt_buffer'] .= '<table:table-column table:number-columns-repeated="' . $fields_cnt . '"/>';
179 // If required, get fields name at the first line
180 if (isset($GLOBALS[$what . '_columns'])) {
181 $GLOBALS['odt_buffer'] .= '<table:table-row>';
182 for ($i = 0; $i < $fields_cnt; $i++) {
183 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
184 . '<text:p>' . htmlspecialchars(stripslashes(PMA_DBI_field_name($result, $i))) . '</text:p>'
185 . '</table:table-cell>';
186 } // end for
187 $GLOBALS['odt_buffer'] .= '</table:table-row>';
188 } // end if
190 // Format the data
191 while ($row = PMA_DBI_fetch_row($result)) {
192 $GLOBALS['odt_buffer'] .= '<table:table-row>';
193 for ($j = 0; $j < $fields_cnt; $j++) {
194 if (!isset($row[$j]) || is_null($row[$j])) {
195 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
196 . '<text:p>' . htmlspecialchars($GLOBALS[$what . '_null']) . '</text:p>'
197 . '</table:table-cell>';
198 // ignore BLOB
199 } elseif (stristr($field_flags[$j], 'BINARY')
200 && $fields_meta[$j]->blob) {
201 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
202 . '<text:p></text:p>'
203 . '</table:table-cell>';
204 } elseif ($fields_meta[$j]->numeric && $fields_meta[$j]->type != 'timestamp' && ! $fields_meta[$j]->blob) {
205 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="float" office:value="' . $row[$j] . '" >'
206 . '<text:p>' . htmlspecialchars($row[$j]) . '</text:p>'
207 . '</table:table-cell>';
208 } else {
209 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
210 . '<text:p>' . htmlspecialchars($row[$j]) . '</text:p>'
211 . '</table:table-cell>';
213 } // end for
214 $GLOBALS['odt_buffer'] .= '</table:table-row>';
215 } // end while
216 PMA_DBI_free_result($result);
218 $GLOBALS['odt_buffer'] .= '</table:table>';
220 return TRUE;
224 * Returns $table's structure as Open Document Text
226 * @param string the database name
227 * @param string the table name
228 * @param string the end of line sequence
229 * @param string the url to go back in case of error
230 * @param boolean whether to include relation comments
231 * @param boolean whether to include column comments
232 * @param boolean whether to include mime comments
233 * @param string future feature: support view dependencies
235 * @return bool Whether it suceeded
237 * @access public
239 // @@@ Table structure
240 function PMA_exportStructure($db, $table, $crlf, $error_url, $do_relation = false, $do_comments = false, $do_mime = false, $dates = false, $dummy)
242 global $cfgRelation;
244 /* Heading */
245 $GLOBALS['odt_buffer'] .= '<text:h text:outline-level="2" text:style-name="Heading_2" text:is-list-header="true">' . htmlspecialchars(__('Table structure for table') . ' ' . $table) . '</text:h>';
248 * Get the unique keys in the table
250 $keys_query = 'SHOW KEYS FROM ' . PMA_backquote($table) . ' FROM '. PMA_backquote($db);
251 $keys_result = PMA_DBI_query($keys_query);
252 $unique_keys = array();
253 while ($key = PMA_DBI_fetch_assoc($keys_result)) {
254 if ($key['Non_unique'] == 0) {
255 $unique_keys[] = $key['Column_name'];
258 PMA_DBI_free_result($keys_result);
261 * Gets fields properties
263 PMA_DBI_select_db($db);
264 $local_query = 'SHOW FIELDS FROM ' . PMA_backquote($db) . '.' . PMA_backquote($table);
265 $result = PMA_DBI_query($local_query);
266 $fields_cnt = PMA_DBI_num_rows($result);
268 // Check if we can use Relations (Mike Beck)
269 if ($do_relation && !empty($cfgRelation['relation'])) {
270 // Find which tables are related with the current one and write it in
271 // an array
272 $res_rel = PMA_getForeigners($db, $table);
274 if ($res_rel && count($res_rel) > 0) {
275 $have_rel = TRUE;
276 } else {
277 $have_rel = FALSE;
279 } else {
280 $have_rel = FALSE;
281 } // end if
284 * Displays the table structure
286 $GLOBALS['odt_buffer'] .= '<table:table table:name="' . htmlspecialchars($table) . '_data">';
287 $columns_cnt = 4;
288 if ($do_relation && $have_rel) {
289 $columns_cnt++;
291 if ($do_comments) {
292 $columns_cnt++;
294 if ($do_mime && $cfgRelation['mimework']) {
295 $columns_cnt++;
297 $GLOBALS['odt_buffer'] .= '<table:table-column table:number-columns-repeated="' . $columns_cnt . '"/>';
298 /* Header */
299 $GLOBALS['odt_buffer'] .= '<table:table-row>';
300 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
301 . '<text:p>' . htmlspecialchars(__('Column')) . '</text:p>'
302 . '</table:table-cell>';
303 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
304 . '<text:p>' . htmlspecialchars(__('Type')) . '</text:p>'
305 . '</table:table-cell>';
306 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
307 . '<text:p>' . htmlspecialchars(__('Null')) . '</text:p>'
308 . '</table:table-cell>';
309 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
310 . '<text:p>' . htmlspecialchars(__('Default')) . '</text:p>'
311 . '</table:table-cell>';
312 if ($do_relation && $have_rel) {
313 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
314 . '<text:p>' . htmlspecialchars(__('Links to')) . '</text:p>'
315 . '</table:table-cell>';
317 if ($do_comments) {
318 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
319 . '<text:p>' . htmlspecialchars(__('Comments')) . '</text:p>'
320 . '</table:table-cell>';
321 $comments = PMA_getComments($db, $table);
323 if ($do_mime && $cfgRelation['mimework']) {
324 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
325 . '<text:p>' . htmlspecialchars(__('MIME type')) . '</text:p>'
326 . '</table:table-cell>';
327 $mime_map = PMA_getMIME($db, $table, true);
329 $GLOBALS['odt_buffer'] .= '</table:table-row>';
331 while ($row = PMA_DBI_fetch_assoc($result)) {
333 $GLOBALS['odt_buffer'] .= '<table:table-row>';
334 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
335 . '<text:p>' . htmlspecialchars($row['Field']) . '</text:p>'
336 . '</table:table-cell>';
337 // reformat mysql query output
338 // set or enum types: slashes single quotes inside options
339 $field_name = $row['Field'];
340 $type = $row['Type'];
341 if (preg_match('/^(set|enum)\((.+)\)$/i', $type, $tmp)) {
342 $tmp[2] = substr(preg_replace('/([^,])\'\'/', '\\1\\\'', ',' . $tmp[2]), 1);
343 $type = $tmp[1] . '(' . str_replace(',', ', ', $tmp[2]) . ')';
344 $type_nowrap = '';
346 $binary = 0;
347 $unsigned = 0;
348 $zerofill = 0;
349 } else {
350 $type_nowrap = ' nowrap="nowrap"';
351 $type = preg_replace('/BINARY/i', '', $type);
352 $type = preg_replace('/ZEROFILL/i', '', $type);
353 $type = preg_replace('/UNSIGNED/i', '', $type);
354 if (empty($type)) {
355 $type = '&nbsp;';
358 $binary = preg_match('/BINARY/i', $row['Type']);
359 $unsigned = preg_match('/UNSIGNED/i', $row['Type']);
360 $zerofill = preg_match('/ZEROFILL/i', $row['Type']);
362 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
363 . '<text:p>' . htmlspecialchars($type) . '</text:p>'
364 . '</table:table-cell>';
365 if (!isset($row['Default'])) {
366 if ($row['Null'] != 'NO') {
367 $row['Default'] = 'NULL';
368 } else {
369 $row['Default'] = '';
371 } else {
372 $row['Default'] = $row['Default'];
374 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
375 . '<text:p>' . htmlspecialchars(($row['Null'] == '' || $row['Null'] == 'NO') ? __('No') : __('Yes')) . '</text:p>'
376 . '</table:table-cell>';
377 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
378 . '<text:p>' . htmlspecialchars($row['Default']) . '</text:p>'
379 . '</table:table-cell>';
381 if ($do_relation && $have_rel) {
382 if (isset($res_rel[$field_name])) {
383 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
384 . '<text:p>' . htmlspecialchars($res_rel[$field_name]['foreign_table'] . ' (' . $res_rel[$field_name]['foreign_field'] . ')') . '</text:p>'
385 . '</table:table-cell>';
388 if ($do_comments) {
389 if (isset($comments[$field_name])) {
390 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
391 . '<text:p>' . htmlspecialchars($comments[$field_name]) . '</text:p>'
392 . '</table:table-cell>';
393 } else {
394 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
395 . '<text:p></text:p>'
396 . '</table:table-cell>';
399 if ($do_mime && $cfgRelation['mimework']) {
400 if (isset($mime_map[$field_name])) {
401 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
402 . '<text:p>' . htmlspecialchars(str_replace('_', '/', $mime_map[$field_name]['mimetype'])) . '</text:p>'
403 . '</table:table-cell>';
404 } else {
405 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
406 . '<text:p></text:p>'
407 . '</table:table-cell>';
410 $GLOBALS['odt_buffer'] .= '</table:table-row>';
411 } // end while
412 PMA_DBI_free_result($result);
414 $GLOBALS['odt_buffer'] .= '</table:table>';
415 return TRUE;
416 } // end of the 'PMA_exportStructure' function
418 } // end else