Fixed: Not selecting a datalabel used to issue a notice(undefined offset)
[phpmyadmin/ammaryasirr.git] / libraries / export / odt.php
bloba73bb8576fdd873281b99a5bd5e4be8562faf834
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">' . __('Database') . ' ' . htmlspecialchars($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">' . __('Dumping data for table') . ' ' . htmlspecialchars($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">' . __('Table structure for table') . ' ' . htmlspecialchars($table) . '</text:h>';
238 * Get the unique keys in the table
240 $keys_query = 'SHOW KEYS FROM ' . PMA_backquote($table) . ' FROM '. PMA_backquote($db);
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>' . __('Column') . '</text:p>'
289 . '</table:table-cell>';
290 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
291 . '<text:p>' . __('Type') . '</text:p>'
292 . '</table:table-cell>';
293 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
294 . '<text:p>' . __('Null') . '</text:p>'
295 . '</table:table-cell>';
296 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
297 . '<text:p>' . __('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>' . __('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>' . __('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>' . __('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) {
320 $field_name = $column['Field'];
321 $GLOBALS['odt_buffer'] .= '<table:table-row>';
322 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
323 . '<text:p>' . htmlspecialchars($field_name) . '</text:p>'
324 . '</table:table-cell>';
326 $extracted_fieldspec = PMA_extractFieldSpec($column['Type']);
327 $type = htmlspecialchars($extracted_fieldspec['print_type']);
328 if (empty($type)) {
329 $type = '&nbsp;';
332 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
333 . '<text:p>' . htmlspecialchars($type) . '</text:p>'
334 . '</table:table-cell>';
335 if (!isset($column['Default'])) {
336 if ($column['Null'] != 'NO') {
337 $column['Default'] = 'NULL';
338 } else {
339 $column['Default'] = '';
341 } else {
342 $column['Default'] = $column['Default'];
344 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
345 . '<text:p>' . (($column['Null'] == '' || $column['Null'] == 'NO') ? __('No') : __('Yes')) . '</text:p>'
346 . '</table:table-cell>';
347 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
348 . '<text:p>' . htmlspecialchars($column['Default']) . '</text:p>'
349 . '</table:table-cell>';
351 if ($do_relation && $have_rel) {
352 if (isset($res_rel[$field_name])) {
353 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
354 . '<text:p>' . htmlspecialchars($res_rel[$field_name]['foreign_table'] . ' (' . $res_rel[$field_name]['foreign_field'] . ')') . '</text:p>'
355 . '</table:table-cell>';
358 if ($do_comments) {
359 if (isset($comments[$field_name])) {
360 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
361 . '<text:p>' . htmlspecialchars($comments[$field_name]) . '</text:p>'
362 . '</table:table-cell>';
363 } else {
364 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
365 . '<text:p></text:p>'
366 . '</table:table-cell>';
369 if ($do_mime && $cfgRelation['mimework']) {
370 if (isset($mime_map[$field_name])) {
371 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
372 . '<text:p>' . htmlspecialchars(str_replace('_', '/', $mime_map[$field_name]['mimetype'])) . '</text:p>'
373 . '</table:table-cell>';
374 } else {
375 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
376 . '<text:p></text:p>'
377 . '</table:table-cell>';
380 $GLOBALS['odt_buffer'] .= '</table:table-row>';
381 } // end while
383 $GLOBALS['odt_buffer'] .= '</table:table>';
384 return true;
385 } // end of the 'PMA_exportStructure' function
387 } // end else