2.11.1.2 release
[phpmyadmin/arisferyanto.git] / libraries / export / odt.php
blobc4a106ab0544855cb61ca55846d43183c7a34b56
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Set of functions used to build CSV dumps of tables
6 * @version $Id$
7 */
9 /**
12 if (isset($plugin_list)) {
13 $hide_structure = false;
14 if ($plugin_param['export_type'] == 'table' && !$plugin_param['single_table']) {
15 $hide_structure = true;
17 $plugin_list['odt'] = array(
18 'text' => 'strOpenDocumentText',
19 'extension' => 'odt',
20 'mime_type' => 'application/vnd.oasis.opendocument.text',
21 'force_file' => true,
22 'options' => array(), /* Filled later */
23 'options_text' => 'strOptions',
25 /* Structure options */
26 if (!$hide_structure) {
27 $plugin_list['odt']['options'][] =
28 array('type' => 'bgroup', 'name' => 'structure', 'text' => 'strStructure', 'force' => 'data');
29 if (!empty($GLOBALS['cfgRelation']['relation'])) {
30 $plugin_list['odt']['options'][] =
31 array('type' => 'bool', 'name' => 'relation', 'text' => 'strRelations');
33 if (!empty($GLOBALS['cfgRelation']['commwork']) || PMA_MYSQL_INT_VERSION >= 40100) {
34 $plugin_list['odt']['options'][] =
35 array('type' => 'bool', 'name' => 'comments', 'text' => 'strComments');
37 if (!empty($GLOBALS['cfgRelation']['mimework'])) {
38 $plugin_list['odt']['options'][] =
39 array('type' => 'bool', 'name' => 'mime', 'text' => 'strMIME_MIMEtype');
41 $plugin_list['odt']['options'][] =
42 array('type' => 'egroup');
44 /* Data */
45 $plugin_list['odt']['options'][] =
46 array('type' => 'bgroup', 'name' => 'data', 'text' => 'strData', 'force' => 'structure');
47 $plugin_list['odt']['options'][] =
48 array('type' => 'bool', 'name' => 'columns', 'text' => 'strPutColNames');
49 $plugin_list['odt']['options'][] =
50 array('type' => 'text', 'name' => 'null', 'text' => 'strReplaceNULLBy');
51 $plugin_list['odt']['options'][] =
52 array('type' => 'egroup');
53 } else {
55 $GLOBALS['odt_buffer'] = '';
56 require_once './libraries/opendocument.lib.php';
58 /**
59 * Outputs comment
61 * @param string Text of comment
63 * @return bool Whether it suceeded
65 function PMA_exportComment($text) {
66 return TRUE;
69 /**
70 * Outputs export footer
72 * @return bool Whether it suceeded
74 * @access public
76 function PMA_exportFooter() {
77 $GLOBALS['odt_buffer'] .= '</office:text>'
78 . '</office:body>'
79 . '</office:document-content>';
80 if (!PMA_exportOutputHandler(PMA_createOpenDocument('application/vnd.oasis.opendocument.text', $GLOBALS['odt_buffer']))) {
81 return FALSE;
83 return TRUE;
86 /**
87 * Outputs export header
89 * @return bool Whether it suceeded
91 * @access public
93 function PMA_exportHeader() {
94 $GLOBALS['odt_buffer'] .= '<?xml version="1.0" encoding="' . $GLOBALS['charset'] . '"?' . '>'
95 . '<office:document-content '. $GLOBALS['OpenDocumentNS'] . 'office:version="1.0">'
96 . '<office:body>'
97 . '<office:text>';
98 return TRUE;
102 * Outputs database header
104 * @param string Database name
106 * @return bool Whether it suceeded
108 * @access public
110 function PMA_exportDBHeader($db) {
111 $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>';
112 return TRUE;
116 * Outputs database footer
118 * @param string Database name
120 * @return bool Whether it suceeded
122 * @access public
124 function PMA_exportDBFooter($db) {
125 return TRUE;
129 * Outputs create database database
131 * @param string Database name
133 * @return bool Whether it suceeded
135 * @access public
137 function PMA_exportDBCreate($db) {
138 return TRUE;
142 * Outputs the content of a table in CSV format
144 * @param string the database name
145 * @param string the table name
146 * @param string the end of line sequence
147 * @param string the url to go back in case of error
148 * @param string SQL query for obtaining data
150 * @return bool Whether it suceeded
152 * @access public
154 function PMA_exportData($db, $table, $crlf, $error_url, $sql_query) {
155 global $what;
157 // Gets the data from the database
158 $result = PMA_DBI_query($sql_query, null, PMA_DBI_QUERY_UNBUFFERED);
159 $fields_cnt = PMA_DBI_num_fields($result);
160 $fields_meta = PMA_DBI_get_fields_meta($result);
161 $field_flags = array();
162 for ($j = 0; $j < $fields_cnt; $j++) {
163 $field_flags[$j] = PMA_DBI_field_flags($result, $j);
166 $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>';
167 $GLOBALS['odt_buffer'] .= '<table:table table:name="' . htmlspecialchars($table) . '_structure">';
168 $GLOBALS['odt_buffer'] .= '<table:table-column table:number-columns-repeated="' . $fields_cnt . '"/>';
170 // If required, get fields name at the first line
171 if (isset($GLOBALS[$what . '_columns'])) {
172 $GLOBALS['odt_buffer'] .= '<table:table-row>';
173 for ($i = 0; $i < $fields_cnt; $i++) {
174 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
175 . '<text:p>' . htmlspecialchars(stripslashes(PMA_DBI_field_name($result, $i))) . '</text:p>'
176 . '</table:table-cell>';
177 } // end for
178 $GLOBALS['odt_buffer'] .= '</table:table-row>';
179 } // end if
181 // Format the data
182 while ($row = PMA_DBI_fetch_row($result)) {
183 $GLOBALS['odt_buffer'] .= '<table:table-row>';
184 for ($j = 0; $j < $fields_cnt; $j++) {
185 if (!isset($row[$j]) || is_null($row[$j])) {
186 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
187 . '<text:p>' . htmlspecialchars($GLOBALS[$what . '_null']) . '</text:p>'
188 . '</table:table-cell>';
189 // ignore BLOB
190 } elseif (stristr($field_flags[$j], 'BINARY')
191 && $fields_meta[$j]->blob) {
192 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
193 . '<text:p></text:p>'
194 . '</table:table-cell>';
195 } elseif ($fields_meta[$j]->numeric && $fields_meta[$j]->type != 'timestamp' && ! $fields_meta[$j]->blob) {
196 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="float" office:value="' . $row[$j] . '" >'
197 . '<text:p>' . htmlspecialchars($row[$j]) . '</text:p>'
198 . '</table:table-cell>';
199 } else {
200 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
201 . '<text:p>' . htmlspecialchars($row[$j]) . '</text:p>'
202 . '</table:table-cell>';
204 } // end for
205 $GLOBALS['odt_buffer'] .= '</table:table-row>';
206 } // end while
207 PMA_DBI_free_result($result);
209 $GLOBALS['odt_buffer'] .= '</table:table>';
211 return TRUE;
215 * Returns $table's structure as Open Document Text
217 * @param string the database name
218 * @param string the table name
219 * @param string the end of line sequence
220 * @param string the url to go back in case of error
221 * @param boolean whether to include relation comments
222 * @param boolean whether to include column comments
223 * @param boolean whether to include mime comments
224 * @param string future feature: support view dependencies
226 * @return bool Whether it suceeded
228 * @access public
230 // @@@ $strTableStructure
231 function PMA_exportStructure($db, $table, $crlf, $error_url, $do_relation = false, $do_comments = false, $do_mime = false, $dates = false, $dummy)
233 global $cfgRelation;
235 /* Heading */
236 $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>';
239 * Get the unique keys in the table
241 $keys_query = 'SHOW KEYS FROM ' . PMA_backquote($table) . ' FROM '. PMA_backquote($db);
242 $keys_result = PMA_DBI_query($keys_query);
243 $unique_keys = array();
244 while ($key = PMA_DBI_fetch_assoc($keys_result)) {
245 if ($key['Non_unique'] == 0) {
246 $unique_keys[] = $key['Column_name'];
249 PMA_DBI_free_result($keys_result);
252 * Gets fields properties
254 PMA_DBI_select_db($db);
255 $local_query = 'SHOW FIELDS FROM ' . PMA_backquote($db) . '.' . PMA_backquote($table);
256 $result = PMA_DBI_query($local_query);
257 $fields_cnt = PMA_DBI_num_rows($result);
259 // Check if we can use Relations (Mike Beck)
260 if ($do_relation && !empty($cfgRelation['relation'])) {
261 // Find which tables are related with the current one and write it in
262 // an array
263 $res_rel = PMA_getForeigners($db, $table);
265 if ($res_rel && count($res_rel) > 0) {
266 $have_rel = TRUE;
267 } else {
268 $have_rel = FALSE;
270 } else {
271 $have_rel = FALSE;
272 } // end if
275 * Displays the table structure
277 $GLOBALS['odt_buffer'] .= '<table:table table:name="' . htmlspecialchars($table) . '_data">';
278 $columns_cnt = 4;
279 if ($do_relation && $have_rel) {
280 $columns_cnt++;
282 if ($do_comments && ($cfgRelation['commwork'] || PMA_MYSQL_INT_VERSION >= 40100)) {
283 $columns_cnt++;
285 if ($do_mime && $cfgRelation['mimework']) {
286 $columns_cnt++;
288 $GLOBALS['odt_buffer'] .= '<table:table-column table:number-columns-repeated="' . $columns_cnt . '"/>';
289 /* Header */
290 $GLOBALS['odt_buffer'] .= '<table:table-row>';
291 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
292 . '<text:p>' . htmlspecialchars($GLOBALS['strField']) . '</text:p>'
293 . '</table:table-cell>';
294 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
295 . '<text:p>' . htmlspecialchars($GLOBALS['strType']) . '</text:p>'
296 . '</table:table-cell>';
297 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
298 . '<text:p>' . htmlspecialchars($GLOBALS['strNull']) . '</text:p>'
299 . '</table:table-cell>';
300 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
301 . '<text:p>' . htmlspecialchars($GLOBALS['strDefault']) . '</text:p>'
302 . '</table:table-cell>';
303 if ($do_relation && $have_rel) {
304 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
305 . '<text:p>' . htmlspecialchars($GLOBALS['strLinksTo']) . '</text:p>'
306 . '</table:table-cell>';
308 if ($do_comments && ($cfgRelation['commwork'] || PMA_MYSQL_INT_VERSION >= 40100)) {
309 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
310 . '<text:p>' . htmlspecialchars($GLOBALS['strComments']) . '</text:p>'
311 . '</table:table-cell>';
312 $comments = PMA_getComments($db, $table);
314 if ($do_mime && $cfgRelation['mimework']) {
315 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
316 . '<text:p>' . htmlspecialchars($GLOBALS['strMIME_MIMEtype']) . '</text:p>'
317 . '</table:table-cell>';
318 $mime_map = PMA_getMIME($db, $table, true);
320 $GLOBALS['odt_buffer'] .= '</table:table-row>';
322 while ($row = PMA_DBI_fetch_assoc($result)) {
324 $GLOBALS['odt_buffer'] .= '<table:table-row>';
325 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
326 . '<text:p>' . htmlspecialchars($row['Field']) . '</text:p>'
327 . '</table:table-cell>';
328 // reformat mysql query output - staybyte - 9. June 2001
329 // loic1: set or enum types: slashes single quotes inside options
330 $field_name = $row['Field'];
331 $type = $row['Type'];
332 if (eregi('^(set|enum)\((.+)\)$', $type, $tmp)) {
333 $tmp[2] = substr(ereg_replace('([^,])\'\'', '\\1\\\'', ',' . $tmp[2]), 1);
334 $type = $tmp[1] . '(' . str_replace(',', ', ', $tmp[2]) . ')';
335 $type_nowrap = '';
337 $binary = 0;
338 $unsigned = 0;
339 $zerofill = 0;
340 } else {
341 $type_nowrap = ' nowrap="nowrap"';
342 $type = eregi_replace('BINARY', '', $type);
343 $type = eregi_replace('ZEROFILL', '', $type);
344 $type = eregi_replace('UNSIGNED', '', $type);
345 if (empty($type)) {
346 $type = '&nbsp;';
349 $binary = eregi('BINARY', $row['Type']);
350 $unsigned = eregi('UNSIGNED', $row['Type']);
351 $zerofill = eregi('ZEROFILL', $row['Type']);
353 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
354 . '<text:p>' . htmlspecialchars($type) . '</text:p>'
355 . '</table:table-cell>';
356 if (!isset($row['Default'])) {
357 if ($row['Null'] != '') {
358 $row['Default'] = 'NULL';
359 } else {
360 $row['Default'] = '';
362 } else {
363 $row['Default'] = $row['Default'];
365 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
366 . '<text:p>' . htmlspecialchars(($row['Null'] == '') ? $GLOBALS['strNo'] : $GLOBALS['strYes']) . '</text:p>'
367 . '</table:table-cell>';
368 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
369 . '<text:p>' . htmlspecialchars($row['Default']) . '</text:p>'
370 . '</table:table-cell>';
372 if ($do_relation && $have_rel) {
373 if (isset($res_rel[$field_name])) {
374 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
375 . '<text:p>' . htmlspecialchars($res_rel[$field_name]['foreign_table'] . ' (' . $res_rel[$field_name]['foreign_field'] . ')') . '</text:p>'
376 . '</table:table-cell>';
379 if ($do_comments && ($cfgRelation['commwork'] || PMA_MYSQL_INT_VERSION >= 40100)) {
380 if (isset($comments[$field_name])) {
381 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
382 . '<text:p>' . htmlspecialchars($comments[$field_name]) . '</text:p>'
383 . '</table:table-cell>';
384 } else {
385 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
386 . '<text:p></text:p>'
387 . '</table:table-cell>';
390 if ($do_mime && $cfgRelation['mimework']) {
391 if (isset($mime_map[$field_name])) {
392 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
393 . '<text:p>' . htmlspecialchars(str_replace('_', '/', $mime_map[$field_name]['mimetype'])) . '</text:p>'
394 . '</table:table-cell>';
395 } else {
396 $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
397 . '<text:p></text:p>'
398 . '</table:table-cell>';
401 $GLOBALS['odt_buffer'] .= '</table:table-row>';
402 } // end while
403 PMA_DBI_free_result($result);
405 $GLOBALS['odt_buffer'] .= '</table:table>';
406 return TRUE;
407 } // end of the 'PMA_exportStructure' function
409 } // end else