Merge remote-tracking branch 'origin/master' into drizzle
[phpmyadmin.git] / libraries / export / odt.php
blob589f47b1e16639c60b932e6a8fb7532745fafaea
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
7 * @subpackage ODT
8 */
9 if (! defined('PHPMYADMIN')) {
10 exit;
13 /**
16 if (isset($plugin_list)) {
17 $hide_structure = false;
18 if ($plugin_param['export_type'] == 'table' && !$plugin_param['single_table']) {
19 $hide_structure = true;
21 $plugin_list['odt'] = array(
22 'text' => __('Open Document Text'),
23 'extension' => 'odt',
24 'mime_type' => 'application/vnd.oasis.opendocument.text',
25 'force_file' => true,
26 'options' => array(), /* Filled later */
27 'options_text' => __('Options'),
30 /* what to dump (structure/data/both) */
31 $plugin_list['odt']['options'][] =
32 array('type' => 'begin_group', 'text' => __('Dump table') , 'name' => 'general_opts');
33 $plugin_list['odt']['options'][] =
34 array('type' => 'radio', 'name' => 'structure_or_data', 'values' => array('structure' => __('structure'), 'data' => __('data'), 'structure_and_data' => __('structure and data')));
35 $plugin_list['odt']['options'][] = array('type' => 'end_group');
37 /* Structure options */
38 if (!$hide_structure) {
39 $plugin_list['odt']['options'][] =
40 array('type' => 'begin_group', 'name' => 'structure', 'text' => __('Object creation options'), 'force' => 'data');
41 if (!empty($GLOBALS['cfgRelation']['relation'])) {
42 $plugin_list['odt']['options'][] =
43 array('type' => 'bool', 'name' => 'relation', 'text' => __('Display foreign key relationships'));
45 $plugin_list['odt']['options'][] =
46 array('type' => 'bool', 'name' => 'comments', 'text' => __('Display comments'));
47 if (!empty($GLOBALS['cfgRelation']['mimework'])) {
48 $plugin_list['odt']['options'][] =
49 array('type' => 'bool', 'name' => 'mime', 'text' => __('Display MIME types'));
51 $plugin_list['odt']['options'][] =
52 array('type' => 'end_group');
54 /* Data */
55 $plugin_list['odt']['options'][] =
56 array('type' => 'begin_group', 'name' => 'data', 'text' => __('Data dump options'), 'force' => 'structure');
57 $plugin_list['odt']['options'][] =
58 array('type' => 'bool', 'name' => 'columns', 'text' => __('Put columns names in the first row'));
59 $plugin_list['odt']['options'][] =
60 array('type' => 'text', 'name' => 'null', 'text' => __('Replace NULL with:'));
61 $plugin_list['odt']['options'][] =
62 array('type' => 'end_group');
63 } else {
65 $GLOBALS['odt_buffer'] = '';
66 require_once './libraries/opendocument.lib.php';
68 /**
69 * Outputs export footer
71 * @return bool Whether it suceeded
73 * @access public
75 function PMA_exportFooter() {
76 $GLOBALS['odt_buffer'] .= '</office:text>'
77 . '</office:body>'
78 . '</office:document-content>';
79 if (!PMA_exportOutputHandler(PMA_createOpenDocument('application/vnd.oasis.opendocument.text', $GLOBALS['odt_buffer']))) {
80 return false;
82 return true;
85 /**
86 * Outputs export header
88 * @return bool Whether it suceeded
90 * @access public
92 function PMA_exportHeader() {
93 $GLOBALS['odt_buffer'] .= '<?xml version="1.0" encoding="utf-8"?' . '>'
94 . '<office:document-content '. $GLOBALS['OpenDocumentNS'] . 'office:version="1.0">'
95 . '<office:body>'
96 . '<office:text>';
97 return true;
101 * Outputs database header
103 * @param string $db Database name
104 * @return bool Whether it suceeded
106 * @access public
108 function PMA_exportDBHeader($db) {
109 $GLOBALS['odt_buffer'] .= '<text:h text:outline-level="1" text:style-name="Heading_1" text:is-list-header="true">' . htmlspecialchars(__('Database') . ' ' . $db) . '</text:h>';
110 return true;
114 * Outputs database footer
116 * @param string $db Database name
117 * @return bool Whether it suceeded
119 * @access public
121 function PMA_exportDBFooter($db) {
122 return true;
126 * Outputs CREATE DATABASE statement
128 * @param string $db Database name
129 * @return bool Whether it suceeded
131 * @access public
133 function PMA_exportDBCreate($db) {
134 return true;
138 * Outputs the content of a table in ODT format
140 * @param string $db database name
141 * @param string $table table name
142 * @param string $crlf the end of line sequence
143 * @param string $error_url the url to go back in case of error
144 * @param string $sql_query SQL query for obtaining data
145 * @return bool Whether it suceeded
147 * @access public
149 function PMA_exportData($db, $table, $crlf, $error_url, $sql_query) {
150 global $what;
152 // Gets the data from the database
153 $result = PMA_DBI_query($sql_query, null, PMA_DBI_QUERY_UNBUFFERED);
154 $fields_cnt = PMA_DBI_num_fields($result);
155 $fields_meta = PMA_DBI_get_fields_meta($result);
156 $field_flags = array();
157 for ($j = 0; $j < $fields_cnt; $j++) {
158 $field_flags[$j] = PMA_DBI_field_flags($result, $j);
161 $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>';
162 $GLOBALS['odt_buffer'] .= '<table:table table:name="' . htmlspecialchars($table) . '_structure">';
163 $GLOBALS['odt_buffer'] .= '<table:table-column table:number-columns-repeated="' . $fields_cnt . '"/>';
165 // If required, get fields name at the first line
166 if (isset($GLOBALS[$what . '_columns'])) {
167 $GLOBALS['odt_buffer'] .= '<table:table-row>';
168 for ($i = 0; $i < $fields_cnt; $i++) {
169 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
170 . '<text:p>' . htmlspecialchars(stripslashes(PMA_DBI_field_name($result, $i))) . '</text:p>'
171 . '</table:table-cell>';
172 } // end for
173 $GLOBALS['odt_buffer'] .= '</table:table-row>';
174 } // end if
176 // Format the data
177 while ($row = PMA_DBI_fetch_row($result)) {
178 $GLOBALS['odt_buffer'] .= '<table:table-row>';
179 for ($j = 0; $j < $fields_cnt; $j++) {
180 if (!isset($row[$j]) || is_null($row[$j])) {
181 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
182 . '<text:p>' . htmlspecialchars($GLOBALS[$what . '_null']) . '</text:p>'
183 . '</table:table-cell>';
184 // ignore BLOB
185 } elseif (stristr($field_flags[$j], 'BINARY')
186 && $fields_meta[$j]->blob) {
187 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
188 . '<text:p></text:p>'
189 . '</table:table-cell>';
190 } elseif ($fields_meta[$j]->numeric && $fields_meta[$j]->type != 'timestamp' && ! $fields_meta[$j]->blob) {
191 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="float" office:value="' . $row[$j] . '" >'
192 . '<text:p>' . htmlspecialchars($row[$j]) . '</text:p>'
193 . '</table:table-cell>';
194 } else {
195 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
196 . '<text:p>' . htmlspecialchars($row[$j]) . '</text:p>'
197 . '</table:table-cell>';
199 } // end for
200 $GLOBALS['odt_buffer'] .= '</table:table-row>';
201 } // end while
202 PMA_DBI_free_result($result);
204 $GLOBALS['odt_buffer'] .= '</table:table>';
206 return true;
210 * Outputs table's structure
212 * @param string $db database name
213 * @param string $table table name
214 * @param string $crlf the end of line sequence
215 * @param string $error_url the url to go back in case of error
216 * @param bool $do_relation whether to include relation comments
217 * @param bool $do_comments whether to include the pmadb-style column comments
218 * as comments in the structure; this is deprecated
219 * but the parameter is left here because export.php
220 * calls PMA_exportStructure() also for other export
221 * types which use this parameter
222 * @param bool $do_mime whether to include mime comments
223 * @param bool $dates whether to include creation/update/check dates
224 * @param string $export_mode 'create_table', 'triggers', 'create_view', 'stand_in'
225 * @param string $export_type 'server', 'database', 'table'
226 * @return bool Whether it suceeded
228 * @access public
230 function PMA_exportStructure($db, $table, $crlf, $error_url, $do_relation = false, $do_comments = false, $do_mime = false, $dates = false, $export_mode, $export_type)
232 global $cfgRelation;
234 /* Heading */
235 $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>';
238 * Get the unique keys in the table
240 $keys_query = PMA_DBI_get_table_indexes_sql($db, $table);
241 $keys_result = PMA_DBI_query($keys_query);
242 $unique_keys = array();
243 while ($key = PMA_DBI_fetch_assoc($keys_result)) {
244 if ($key['Non_unique'] == 0) {
245 $unique_keys[] = $key['Column_name'];
248 PMA_DBI_free_result($keys_result);
251 * Gets fields properties
253 PMA_DBI_select_db($db);
255 // Check if we can use Relations
256 if ($do_relation && !empty($cfgRelation['relation'])) {
257 // Find which tables are related with the current one and write it in
258 // an array
259 $res_rel = PMA_getForeigners($db, $table);
261 if ($res_rel && count($res_rel) > 0) {
262 $have_rel = true;
263 } else {
264 $have_rel = false;
266 } else {
267 $have_rel = false;
268 } // end if
271 * Displays the table structure
273 $GLOBALS['odt_buffer'] .= '<table:table table:name="' . htmlspecialchars($table) . '_data">';
274 $columns_cnt = 4;
275 if ($do_relation && $have_rel) {
276 $columns_cnt++;
278 if ($do_comments) {
279 $columns_cnt++;
281 if ($do_mime && $cfgRelation['mimework']) {
282 $columns_cnt++;
284 $GLOBALS['odt_buffer'] .= '<table:table-column table:number-columns-repeated="' . $columns_cnt . '"/>';
285 /* Header */
286 $GLOBALS['odt_buffer'] .= '<table:table-row>';
287 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
288 . '<text:p>' . htmlspecialchars(__('Column')) . '</text:p>'
289 . '</table:table-cell>';
290 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
291 . '<text:p>' . htmlspecialchars(__('Type')) . '</text:p>'
292 . '</table:table-cell>';
293 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
294 . '<text:p>' . htmlspecialchars(__('Null')) . '</text:p>'
295 . '</table:table-cell>';
296 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
297 . '<text:p>' . htmlspecialchars(__('Default')) . '</text:p>'
298 . '</table:table-cell>';
299 if ($do_relation && $have_rel) {
300 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
301 . '<text:p>' . htmlspecialchars(__('Links to')) . '</text:p>'
302 . '</table:table-cell>';
304 if ($do_comments) {
305 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
306 . '<text:p>' . htmlspecialchars(__('Comments')) . '</text:p>'
307 . '</table:table-cell>';
308 $comments = PMA_getComments($db, $table);
310 if ($do_mime && $cfgRelation['mimework']) {
311 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
312 . '<text:p>' . htmlspecialchars(__('MIME type')) . '</text:p>'
313 . '</table:table-cell>';
314 $mime_map = PMA_getMIME($db, $table, true);
316 $GLOBALS['odt_buffer'] .= '</table:table-row>';
318 $columns = PMA_DBI_get_columns($db, $table);
319 foreach ($columns as $column) {
321 $field_name = $column['Field'];
322 $GLOBALS['odt_buffer'] .= '<table:table-row>';
323 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
324 . '<text:p>' . htmlspecialchars($field_name) . '</text:p>'
325 . '</table:table-cell>';
326 // reformat mysql query output
327 // set or enum types: slashes single quotes inside options
328 $type = $column['Type'];
329 if (preg_match('/^(set|enum)\((.+)\)$/i', $type, $tmp)) {
330 $tmp[2] = substr(preg_replace('/([^,])\'\'/', '\\1\\\'', ',' . $tmp[2]), 1);
331 $type = $tmp[1] . '(' . str_replace(',', ', ', $tmp[2]) . ')';
332 $type_nowrap = '';
334 $binary = 0;
335 $unsigned = 0;
336 $zerofill = 0;
337 } else {
338 $type_nowrap = ' nowrap="nowrap"';
339 $type = preg_replace('/BINARY/i', '', $type);
340 $type = preg_replace('/ZEROFILL/i', '', $type);
341 $type = preg_replace('/UNSIGNED/i', '', $type);
342 if (empty($type)) {
343 $type = '&nbsp;';
346 $binary = preg_match('/BINARY/i', $column['Type']);
347 $unsigned = preg_match('/UNSIGNED/i', $column['Type']);
348 $zerofill = preg_match('/ZEROFILL/i', $column['Type']);
350 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
351 . '<text:p>' . htmlspecialchars($type) . '</text:p>'
352 . '</table:table-cell>';
353 if (!isset($column['Default'])) {
354 if ($column['Null'] != 'NO') {
355 $column['Default'] = 'NULL';
356 } else {
357 $column['Default'] = '';
359 } else {
360 $column['Default'] = $column['Default'];
362 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
363 . '<text:p>' . htmlspecialchars(($column['Null'] == '' || $column['Null'] == 'NO') ? __('No') : __('Yes')) . '</text:p>'
364 . '</table:table-cell>';
365 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
366 . '<text:p>' . htmlspecialchars($column['Default']) . '</text:p>'
367 . '</table:table-cell>';
369 if ($do_relation && $have_rel) {
370 if (isset($res_rel[$field_name])) {
371 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
372 . '<text:p>' . htmlspecialchars($res_rel[$field_name]['foreign_table'] . ' (' . $res_rel[$field_name]['foreign_field'] . ')') . '</text:p>'
373 . '</table:table-cell>';
376 if ($do_comments) {
377 if (isset($comments[$field_name])) {
378 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
379 . '<text:p>' . htmlspecialchars($comments[$field_name]) . '</text:p>'
380 . '</table:table-cell>';
381 } else {
382 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
383 . '<text:p></text:p>'
384 . '</table:table-cell>';
387 if ($do_mime && $cfgRelation['mimework']) {
388 if (isset($mime_map[$field_name])) {
389 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
390 . '<text:p>' . htmlspecialchars(str_replace('_', '/', $mime_map[$field_name]['mimetype'])) . '</text:p>'
391 . '</table:table-cell>';
392 } else {
393 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
394 . '<text:p></text:p>'
395 . '</table:table-cell>';
398 $GLOBALS['odt_buffer'] .= '</table:table-row>';
399 } // end while
401 $GLOBALS['odt_buffer'] .= '</table:table>';
402 return true;
403 } // end of the 'PMA_exportStructure' function
405 } // end else