3.2.5
[phpmyadmin/dkf.git] / libraries / export / odt.php
blobaeeb25a8925eda645a70ad357463bdda949ab152
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 * @version $Id$
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' => 'strOpenDocumentText',
23 'extension' => 'odt',
24 'mime_type' => 'application/vnd.oasis.opendocument.text',
25 'force_file' => true,
26 'options' => array(), /* Filled later */
27 'options_text' => 'strOptions',
29 /* Structure options */
30 if (!$hide_structure) {
31 $plugin_list['odt']['options'][] =
32 array('type' => 'bgroup', 'name' => 'structure', 'text' => 'strStructure', 'force' => 'data');
33 if (!empty($GLOBALS['cfgRelation']['relation'])) {
34 $plugin_list['odt']['options'][] =
35 array('type' => 'bool', 'name' => 'relation', 'text' => 'strRelations');
37 $plugin_list['odt']['options'][] =
38 array('type' => 'bool', 'name' => 'comments', 'text' => 'strComments');
39 if (!empty($GLOBALS['cfgRelation']['mimework'])) {
40 $plugin_list['odt']['options'][] =
41 array('type' => 'bool', 'name' => 'mime', 'text' => 'strMIME_MIMEtype');
43 $plugin_list['odt']['options'][] =
44 array('type' => 'egroup');
46 /* Data */
47 $plugin_list['odt']['options'][] =
48 array('type' => 'bgroup', 'name' => 'data', 'text' => 'strData', 'force' => 'structure');
49 $plugin_list['odt']['options'][] =
50 array('type' => 'bool', 'name' => 'columns', 'text' => 'strPutColNames');
51 $plugin_list['odt']['options'][] =
52 array('type' => 'text', 'name' => 'null', 'text' => 'strReplaceNULLBy');
53 $plugin_list['odt']['options'][] =
54 array('type' => 'egroup');
55 } else {
57 $GLOBALS['odt_buffer'] = '';
58 require_once './libraries/opendocument.lib.php';
60 /**
61 * Outputs comment
63 * @param string Text of comment
65 * @return bool Whether it suceeded
67 function PMA_exportComment($text) {
68 return TRUE;
71 /**
72 * Outputs export footer
74 * @return bool Whether it suceeded
76 * @access public
78 function PMA_exportFooter() {
79 $GLOBALS['odt_buffer'] .= '</office:text>'
80 . '</office:body>'
81 . '</office:document-content>';
82 if (!PMA_exportOutputHandler(PMA_createOpenDocument('application/vnd.oasis.opendocument.text', $GLOBALS['odt_buffer']))) {
83 return FALSE;
85 return TRUE;
88 /**
89 * Outputs export header
91 * @return bool Whether it suceeded
93 * @access public
95 function PMA_exportHeader() {
96 $GLOBALS['odt_buffer'] .= '<?xml version="1.0" encoding="' . $GLOBALS['charset'] . '"?' . '>'
97 . '<office:document-content '. $GLOBALS['OpenDocumentNS'] . 'office:version="1.0">'
98 . '<office:body>'
99 . '<office:text>';
100 return TRUE;
104 * Outputs database header
106 * @param string Database name
108 * @return bool Whether it suceeded
110 * @access public
112 function PMA_exportDBHeader($db) {
113 $GLOBALS['odt_buffer'] .= '<text:h text:outline-level="1" text:style-name="Heading_1" text:is-list-header="true">' . htmlspecialchars($GLOBALS['strDatabase'] . ' ' . $db) . '</text:h>';
114 return TRUE;
118 * Outputs database footer
120 * @param string Database name
122 * @return bool Whether it suceeded
124 * @access public
126 function PMA_exportDBFooter($db) {
127 return TRUE;
131 * Outputs create database database
133 * @param string Database name
135 * @return bool Whether it suceeded
137 * @access public
139 function PMA_exportDBCreate($db) {
140 return TRUE;
144 * Outputs the content of a table in CSV format
146 * @param string the database name
147 * @param string the table name
148 * @param string the end of line sequence
149 * @param string the url to go back in case of error
150 * @param string SQL query for obtaining data
152 * @return bool Whether it suceeded
154 * @access public
156 function PMA_exportData($db, $table, $crlf, $error_url, $sql_query) {
157 global $what;
159 // Gets the data from the database
160 $result = PMA_DBI_query($sql_query, null, PMA_DBI_QUERY_UNBUFFERED);
161 $fields_cnt = PMA_DBI_num_fields($result);
162 $fields_meta = PMA_DBI_get_fields_meta($result);
163 $field_flags = array();
164 for ($j = 0; $j < $fields_cnt; $j++) {
165 $field_flags[$j] = PMA_DBI_field_flags($result, $j);
168 $GLOBALS['odt_buffer'] .= '<text:h text:outline-level="2" text:style-name="Heading_2" text:is-list-header="true">' . htmlspecialchars($GLOBALS['strDumpingData'] . ' ' . $table) . '</text:h>';
169 $GLOBALS['odt_buffer'] .= '<table:table table:name="' . htmlspecialchars($table) . '_structure">';
170 $GLOBALS['odt_buffer'] .= '<table:table-column table:number-columns-repeated="' . $fields_cnt . '"/>';
172 // If required, get fields name at the first line
173 if (isset($GLOBALS[$what . '_columns'])) {
174 $GLOBALS['odt_buffer'] .= '<table:table-row>';
175 for ($i = 0; $i < $fields_cnt; $i++) {
176 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
177 . '<text:p>' . htmlspecialchars(stripslashes(PMA_DBI_field_name($result, $i))) . '</text:p>'
178 . '</table:table-cell>';
179 } // end for
180 $GLOBALS['odt_buffer'] .= '</table:table-row>';
181 } // end if
183 // Format the data
184 while ($row = PMA_DBI_fetch_row($result)) {
185 $GLOBALS['odt_buffer'] .= '<table:table-row>';
186 for ($j = 0; $j < $fields_cnt; $j++) {
187 if (!isset($row[$j]) || is_null($row[$j])) {
188 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
189 . '<text:p>' . htmlspecialchars($GLOBALS[$what . '_null']) . '</text:p>'
190 . '</table:table-cell>';
191 // ignore BLOB
192 } elseif (stristr($field_flags[$j], 'BINARY')
193 && $fields_meta[$j]->blob) {
194 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
195 . '<text:p></text:p>'
196 . '</table:table-cell>';
197 } elseif ($fields_meta[$j]->numeric && $fields_meta[$j]->type != 'timestamp' && ! $fields_meta[$j]->blob) {
198 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="float" office:value="' . $row[$j] . '" >'
199 . '<text:p>' . htmlspecialchars($row[$j]) . '</text:p>'
200 . '</table:table-cell>';
201 } else {
202 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
203 . '<text:p>' . htmlspecialchars($row[$j]) . '</text:p>'
204 . '</table:table-cell>';
206 } // end for
207 $GLOBALS['odt_buffer'] .= '</table:table-row>';
208 } // end while
209 PMA_DBI_free_result($result);
211 $GLOBALS['odt_buffer'] .= '</table:table>';
213 return TRUE;
217 * Returns $table's structure as Open Document Text
219 * @param string the database name
220 * @param string the table name
221 * @param string the end of line sequence
222 * @param string the url to go back in case of error
223 * @param boolean whether to include relation comments
224 * @param boolean whether to include column comments
225 * @param boolean whether to include mime comments
226 * @param string future feature: support view dependencies
228 * @return bool Whether it suceeded
230 * @access public
232 // @@@ $strTableStructure
233 function PMA_exportStructure($db, $table, $crlf, $error_url, $do_relation = false, $do_comments = false, $do_mime = false, $dates = false, $dummy)
235 global $cfgRelation;
237 /* Heading */
238 $GLOBALS['odt_buffer'] .= '<text:h text:outline-level="2" text:style-name="Heading_2" text:is-list-header="true">' . htmlspecialchars($GLOBALS['strTableStructure'] . ' ' . $table) . '</text:h>';
241 * Get the unique keys in the table
243 $keys_query = 'SHOW KEYS FROM ' . PMA_backquote($table) . ' FROM '. PMA_backquote($db);
244 $keys_result = PMA_DBI_query($keys_query);
245 $unique_keys = array();
246 while ($key = PMA_DBI_fetch_assoc($keys_result)) {
247 if ($key['Non_unique'] == 0) {
248 $unique_keys[] = $key['Column_name'];
251 PMA_DBI_free_result($keys_result);
254 * Gets fields properties
256 PMA_DBI_select_db($db);
257 $local_query = 'SHOW FIELDS FROM ' . PMA_backquote($db) . '.' . PMA_backquote($table);
258 $result = PMA_DBI_query($local_query);
259 $fields_cnt = PMA_DBI_num_rows($result);
261 // Check if we can use Relations (Mike Beck)
262 if ($do_relation && !empty($cfgRelation['relation'])) {
263 // Find which tables are related with the current one and write it in
264 // an array
265 $res_rel = PMA_getForeigners($db, $table);
267 if ($res_rel && count($res_rel) > 0) {
268 $have_rel = TRUE;
269 } else {
270 $have_rel = FALSE;
272 } else {
273 $have_rel = FALSE;
274 } // end if
277 * Displays the table structure
279 $GLOBALS['odt_buffer'] .= '<table:table table:name="' . htmlspecialchars($table) . '_data">';
280 $columns_cnt = 4;
281 if ($do_relation && $have_rel) {
282 $columns_cnt++;
284 if ($do_comments) {
285 $columns_cnt++;
287 if ($do_mime && $cfgRelation['mimework']) {
288 $columns_cnt++;
290 $GLOBALS['odt_buffer'] .= '<table:table-column table:number-columns-repeated="' . $columns_cnt . '"/>';
291 /* Header */
292 $GLOBALS['odt_buffer'] .= '<table:table-row>';
293 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
294 . '<text:p>' . htmlspecialchars($GLOBALS['strField']) . '</text:p>'
295 . '</table:table-cell>';
296 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
297 . '<text:p>' . htmlspecialchars($GLOBALS['strType']) . '</text:p>'
298 . '</table:table-cell>';
299 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
300 . '<text:p>' . htmlspecialchars($GLOBALS['strNull']) . '</text:p>'
301 . '</table:table-cell>';
302 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
303 . '<text:p>' . htmlspecialchars($GLOBALS['strDefault']) . '</text:p>'
304 . '</table:table-cell>';
305 if ($do_relation && $have_rel) {
306 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
307 . '<text:p>' . htmlspecialchars($GLOBALS['strLinksTo']) . '</text:p>'
308 . '</table:table-cell>';
310 if ($do_comments) {
311 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
312 . '<text:p>' . htmlspecialchars($GLOBALS['strComments']) . '</text:p>'
313 . '</table:table-cell>';
314 $comments = PMA_getComments($db, $table);
316 if ($do_mime && $cfgRelation['mimework']) {
317 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
318 . '<text:p>' . htmlspecialchars($GLOBALS['strMIME_MIMEtype']) . '</text:p>'
319 . '</table:table-cell>';
320 $mime_map = PMA_getMIME($db, $table, true);
322 $GLOBALS['odt_buffer'] .= '</table:table-row>';
324 while ($row = PMA_DBI_fetch_assoc($result)) {
326 $GLOBALS['odt_buffer'] .= '<table:table-row>';
327 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
328 . '<text:p>' . htmlspecialchars($row['Field']) . '</text:p>'
329 . '</table:table-cell>';
330 // reformat mysql query output - staybyte - 9. June 2001
331 // loic1: set or enum types: slashes single quotes inside options
332 $field_name = $row['Field'];
333 $type = $row['Type'];
334 if (preg_match('/^(set|enum)\((.+)\)$/i', $type, $tmp)) {
335 $tmp[2] = substr(preg_replace('/([^,])\'\'/', '\\1\\\'', ',' . $tmp[2]), 1);
336 $type = $tmp[1] . '(' . str_replace(',', ', ', $tmp[2]) . ')';
337 $type_nowrap = '';
339 $binary = 0;
340 $unsigned = 0;
341 $zerofill = 0;
342 } else {
343 $type_nowrap = ' nowrap="nowrap"';
344 $type = preg_replace('/BINARY/i', '', $type);
345 $type = preg_replace('/ZEROFILL/i', '', $type);
346 $type = preg_replace('/UNSIGNED/i', '', $type);
347 if (empty($type)) {
348 $type = '&nbsp;';
351 $binary = preg_match('/BINARY/i', $row['Type']);
352 $unsigned = preg_match('/UNSIGNED/i', $row['Type']);
353 $zerofill = preg_match('/ZEROFILL/i', $row['Type']);
355 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
356 . '<text:p>' . htmlspecialchars($type) . '</text:p>'
357 . '</table:table-cell>';
358 if (!isset($row['Default'])) {
359 if ($row['Null'] != 'NO') {
360 $row['Default'] = 'NULL';
361 } else {
362 $row['Default'] = '';
364 } else {
365 $row['Default'] = $row['Default'];
367 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
368 . '<text:p>' . htmlspecialchars(($row['Null'] == '' || $row['Null'] == 'NO') ? $GLOBALS['strNo'] : $GLOBALS['strYes']) . '</text:p>'
369 . '</table:table-cell>';
370 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
371 . '<text:p>' . htmlspecialchars($row['Default']) . '</text:p>'
372 . '</table:table-cell>';
374 if ($do_relation && $have_rel) {
375 if (isset($res_rel[$field_name])) {
376 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
377 . '<text:p>' . htmlspecialchars($res_rel[$field_name]['foreign_table'] . ' (' . $res_rel[$field_name]['foreign_field'] . ')') . '</text:p>'
378 . '</table:table-cell>';
381 if ($do_comments) {
382 if (isset($comments[$field_name])) {
383 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
384 . '<text:p>' . htmlspecialchars($comments[$field_name]) . '</text:p>'
385 . '</table:table-cell>';
386 } else {
387 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
388 . '<text:p></text:p>'
389 . '</table:table-cell>';
392 if ($do_mime && $cfgRelation['mimework']) {
393 if (isset($mime_map[$field_name])) {
394 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
395 . '<text:p>' . htmlspecialchars(str_replace('_', '/', $mime_map[$field_name]['mimetype'])) . '</text:p>'
396 . '</table:table-cell>';
397 } else {
398 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
399 . '<text:p></text:p>'
400 . '</table:table-cell>';
403 $GLOBALS['odt_buffer'] .= '</table:table-row>';
404 } // end while
405 PMA_DBI_free_result($result);
407 $GLOBALS['odt_buffer'] .= '</table:table>';
408 return TRUE;
409 } // end of the 'PMA_exportStructure' function
411 } // end else