2
/* $Id: odt.php 9805 2006-12-26 16:10:47Z lem9 $ */
3
// vim: expandtab sw=4 ts=4 sts=4:
6
* Set of functions used to build CSV dumps of tables
9
if (isset($plugin_list)) {
10
$hide_structure = false;
11
if ($plugin_param['export_type'] == 'table' && !$plugin_param['single_table']) {
12
$hide_structure = true;
14
$plugin_list['odt'] = array(
15
'text' => 'strOpenDocumentText',
17
'mime_type' => 'application/vnd.oasis.opendocument.text',
19
'options' => array(), /* Filled later */
20
'options_text' => 'strOpenDocumentTextOptions',
22
/* Structure options */
23
if (!$hide_structure) {
24
$plugin_list['odt']['options'][] =
25
array('type' => 'bgroup', 'name' => 'structure', 'text' => 'strStructure', 'force' => 'data');
26
if (!empty($GLOBALS['cfgRelation']['relation'])) {
27
$plugin_list['odt']['options'][] =
28
array('type' => 'bool', 'name' => 'relation', 'text' => 'strRelations');
30
if (!empty($GLOBALS['cfgRelation']['commwork']) || PMA_MYSQL_INT_VERSION >= 40100) {
31
$plugin_list['odt']['options'][] =
32
array('type' => 'bool', 'name' => 'comments', 'text' => 'strComments');
34
if (!empty($GLOBALS['cfgRelation']['mimework'])) {
35
$plugin_list['odt']['options'][] =
36
array('type' => 'bool', 'name' => 'mime', 'text' => 'strMIME_MIMEtype');
38
$plugin_list['odt']['options'][] =
39
array('type' => 'egroup');
42
$plugin_list['odt']['options'][] =
43
array('type' => 'bgroup', 'name' => 'data', 'text' => 'strData', 'force' => 'structure');
44
$plugin_list['odt']['options'][] =
45
array('type' => 'bool', 'name' => 'columns', 'text' => 'strPutColNames');
46
$plugin_list['odt']['options'][] =
47
array('type' => 'text', 'name' => 'null', 'text' => 'strReplaceNULLBy');
48
$plugin_list['odt']['options'][] =
49
array('type' => 'egroup');
52
$GLOBALS['odt_buffer'] = '';
53
require_once('./libraries/opendocument.lib.php');
58
* @param string Text of comment
60
* @return bool Whether it suceeded
62
function PMA_exportComment($text) {
67
* Outputs export footer
69
* @return bool Whether it suceeded
73
function PMA_exportFooter() {
74
$GLOBALS['odt_buffer'] .= '</office:text>'
76
. '</office:document-content>';
77
if (!PMA_exportOutputHandler(PMA_createOpenDocument('application/vnd.oasis.opendocument.text', $GLOBALS['odt_buffer']))) {
84
* Outputs export header
86
* @return bool Whether it suceeded
90
function PMA_exportHeader() {
91
$GLOBALS['odt_buffer'] .= '<?xml version="1.0" encoding="' . $GLOBALS['charset'] . '"?' . '>'
92
. '<office:document-content '. $GLOBALS['OpenDocumentNS'] . 'office:version="1.0">'
99
* Outputs database header
101
* @param string Database name
103
* @return bool Whether it suceeded
107
function PMA_exportDBHeader($db) {
108
$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>';
113
* Outputs database footer
115
* @param string Database name
117
* @return bool Whether it suceeded
121
function PMA_exportDBFooter($db) {
126
* Outputs create database database
128
* @param string Database name
130
* @return bool Whether it suceeded
134
function PMA_exportDBCreate($db) {
139
* Outputs the content of a table in CSV format
141
* @param string the database name
142
* @param string the table name
143
* @param string the end of line sequence
144
* @param string the url to go back in case of error
145
* @param string SQL query for obtaining data
147
* @return bool Whether it suceeded
151
function PMA_exportData($db, $table, $crlf, $error_url, $sql_query) {
154
// Gets the data from the database
155
$result = PMA_DBI_query($sql_query, null, PMA_DBI_QUERY_UNBUFFERED);
156
$fields_cnt = PMA_DBI_num_fields($result);
157
$fields_meta = PMA_DBI_get_fields_meta($result);
158
$field_flags = array();
159
for ($j = 0; $j < $fields_cnt; $j++) {
160
$field_flags[$j] = PMA_DBI_field_flags($result, $j);
163
$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>';
164
$GLOBALS['odt_buffer'] .= '<table:table table:name="' . htmlspecialchars($table) . '_structure">';
165
$GLOBALS['odt_buffer'] .= '<table:table-column table:number-columns-repeated="' . $fields_cnt . '"/>';
167
// If required, get fields name at the first line
168
if (isset($GLOBALS[$what . '_columns'])) {
169
$GLOBALS['odt_buffer'] .= '<table:table-row>';
170
for ($i = 0; $i < $fields_cnt; $i++) {
171
$GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
172
. '<text:p>' . htmlspecialchars(stripslashes(PMA_DBI_field_name($result, $i))) . '</text:p>'
173
. '</table:table-cell>';
175
$GLOBALS['odt_buffer'] .= '</table:table-row>';
179
while ($row = PMA_DBI_fetch_row($result)) {
180
$GLOBALS['odt_buffer'] .= '<table:table-row>';
181
for ($j = 0; $j < $fields_cnt; $j++) {
182
if (!isset($row[$j]) || is_null($row[$j])) {
183
$GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
184
. '<text:p>' . htmlspecialchars($GLOBALS[$what . '_null']) . '</text:p>'
185
. '</table:table-cell>';
186
// ignore binary field
187
// Note: with mysqli, under MySQL 4.1.3, we get the flag
188
// "binary" for those field types (I don't know why)
189
} elseif (stristr($field_flags[$j], 'BINARY')
190
&& isset($GLOBALS['sql_hex_for_binary'])
191
&& $fields_meta[$j]->type != 'datetime'
192
&& $fields_meta[$j]->type != 'date'
193
&& $fields_meta[$j]->type != 'time'
194
&& $fields_meta[$j]->type != 'timestamp'
196
$GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
197
. '<text:p></text:p>'
198
. '</table:table-cell>';
199
} elseif ($fields_meta[$j]->numeric && $fields_meta[$j]->type != 'timestamp' && ! $fields_meta[$j]->blob) {
200
$GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="float" office:value="' . $row[$j] . '" >'
201
. '<text:p>' . htmlspecialchars($row[$j]) . '</text:p>'
202
. '</table:table-cell>';
204
$GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
205
. '<text:p>' . htmlspecialchars($row[$j]) . '</text:p>'
206
. '</table:table-cell>';
209
$GLOBALS['odt_buffer'] .= '</table:table-row>';
211
PMA_DBI_free_result($result);
213
$GLOBALS['odt_buffer'] .= '</table:table>';
219
* Returns $table's structure as Open Document Text
221
* @param string the database name
222
* @param string the table name
223
* @param string the end of line sequence
224
* @param string the url to go back in case of error
225
* @param boolean whether to include relation comments
226
* @param boolean whether to include column comments
227
* @param boolean whether to include mime comments
228
* @param string future feature: support view dependencies
230
* @return bool Whether it suceeded
234
// @@@ $strTableStructure
235
function PMA_exportStructure($db, $table, $crlf, $error_url, $do_relation = false, $do_comments = false, $do_mime = false, $dates = false, $dummy)
240
$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>';
243
* Get the unique keys in the table
245
$keys_query = 'SHOW KEYS FROM ' . PMA_backquote($table) . ' FROM '. PMA_backquote($db);
246
$keys_result = PMA_DBI_query($keys_query);
247
$unique_keys = array();
248
while ($key = PMA_DBI_fetch_assoc($keys_result)) {
249
if ($key['Non_unique'] == 0) {
250
$unique_keys[] = $key['Column_name'];
253
PMA_DBI_free_result($keys_result);
256
* Gets fields properties
258
PMA_DBI_select_db($db);
259
$local_query = 'SHOW FIELDS FROM ' . PMA_backquote($db) . '.' . PMA_backquote($table);
260
$result = PMA_DBI_query($local_query);
261
$fields_cnt = PMA_DBI_num_rows($result);
263
// Check if we can use Relations (Mike Beck)
264
if ($do_relation && !empty($cfgRelation['relation'])) {
265
// Find which tables are related with the current one and write it in
267
$res_rel = PMA_getForeigners($db, $table);
269
if ($res_rel && count($res_rel) > 0) {
279
* Displays the table structure
281
$GLOBALS['odt_buffer'] .= '<table:table table:name="' . htmlspecialchars($table) . '_data">';
283
if ($do_relation && $have_rel) {
286
if ($do_comments && ($cfgRelation['commwork'] || PMA_MYSQL_INT_VERSION >= 40100)) {
289
if ($do_mime && $cfgRelation['mimework']) {
292
$GLOBALS['odt_buffer'] .= '<table:table-column table:number-columns-repeated="' . $columns_cnt . '"/>';
294
$GLOBALS['odt_buffer'] .= '<table:table-row>';
295
$GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
296
. '<text:p>' . htmlspecialchars($GLOBALS['strField']) . '</text:p>'
297
. '</table:table-cell>';
298
$GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
299
. '<text:p>' . htmlspecialchars($GLOBALS['strType']) . '</text:p>'
300
. '</table:table-cell>';
301
$GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
302
. '<text:p>' . htmlspecialchars($GLOBALS['strNull']) . '</text:p>'
303
. '</table:table-cell>';
304
$GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
305
. '<text:p>' . htmlspecialchars($GLOBALS['strDefault']) . '</text:p>'
306
. '</table:table-cell>';
307
if ($do_relation && $have_rel) {
308
$GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
309
. '<text:p>' . htmlspecialchars($GLOBALS['strLinksTo']) . '</text:p>'
310
. '</table:table-cell>';
312
if ($do_comments && ($cfgRelation['commwork'] || PMA_MYSQL_INT_VERSION >= 40100)) {
313
$GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
314
. '<text:p>' . htmlspecialchars($GLOBALS['strComments']) . '</text:p>'
315
. '</table:table-cell>';
316
$comments = PMA_getComments($db, $table);
318
if ($do_mime && $cfgRelation['mimework']) {
319
$GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
320
. '<text:p>' . htmlspecialchars($GLOBALS['strMIME_MIMEtype']) . '</text:p>'
321
. '</table:table-cell>';
322
$mime_map = PMA_getMIME($db, $table, true);
324
$GLOBALS['odt_buffer'] .= '</table:table-row>';
326
while ($row = PMA_DBI_fetch_assoc($result)) {
328
$GLOBALS['odt_buffer'] .= '<table:table-row>';
329
$GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
330
. '<text:p>' . htmlspecialchars($row['Field']) . '</text:p>'
331
. '</table:table-cell>';
332
// reformat mysql query output - staybyte - 9. June 2001
333
// loic1: set or enum types: slashes single quotes inside options
334
$field_name = $row['Field'];
335
$type = $row['Type'];
336
if (eregi('^(set|enum)\((.+)\)$', $type, $tmp)) {
337
$tmp[2] = substr(ereg_replace('([^,])\'\'', '\\1\\\'', ',' . $tmp[2]), 1);
338
$type = $tmp[1] . '(' . str_replace(',', ', ', $tmp[2]) . ')';
345
$type_nowrap = ' nowrap="nowrap"';
346
$type = eregi_replace('BINARY', '', $type);
347
$type = eregi_replace('ZEROFILL', '', $type);
348
$type = eregi_replace('UNSIGNED', '', $type);
353
$binary = eregi('BINARY', $row['Type']);
354
$unsigned = eregi('UNSIGNED', $row['Type']);
355
$zerofill = eregi('ZEROFILL', $row['Type']);
357
$GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
358
. '<text:p>' . htmlspecialchars($type) . '</text:p>'
359
. '</table:table-cell>';
360
if (!isset($row['Default'])) {
361
if ($row['Null'] != '') {
362
$row['Default'] = 'NULL';
364
$row['Default'] = '';
367
$row['Default'] = $row['Default'];
369
$GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
370
. '<text:p>' . htmlspecialchars(($row['Null'] == '') ? $GLOBALS['strNo'] : $GLOBALS['strYes']) . '</text:p>'
371
. '</table:table-cell>';
372
$GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
373
. '<text:p>' . htmlspecialchars($row['Default']) . '</text:p>'
374
. '</table:table-cell>';
376
if ($do_relation && $have_rel) {
377
if (isset($res_rel[$field_name])) {
378
$GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
379
. '<text:p>' . htmlspecialchars($res_rel[$field_name]['foreign_table'] . ' (' . $res_rel[$field_name]['foreign_field'] . ')') . '</text:p>'
380
. '</table:table-cell>';
383
if ($do_comments && ($cfgRelation['commwork'] || PMA_MYSQL_INT_VERSION >= 40100)) {
384
if (isset($comments[$field_name])) {
385
$GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
386
. '<text:p>' . htmlspecialchars($comments[$field_name]) . '</text:p>'
387
. '</table:table-cell>';
389
$GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
390
. '<text:p></text:p>'
391
. '</table:table-cell>';
394
if ($do_mime && $cfgRelation['mimework']) {
395
if (isset($mime_map[$field_name])) {
396
$GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
397
. '<text:p>' . htmlspecialchars(str_replace('_', '/', $mime_map[$field_name]['mimetype'])) . '</text:p>'
398
. '</table:table-cell>';
400
$GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
401
. '<text:p></text:p>'
402
. '</table:table-cell>';
405
$GLOBALS['odt_buffer'] .= '</table:table-row>';
407
PMA_DBI_free_result($result);
409
$GLOBALS['odt_buffer'] .= '</table:table>';
411
} // end of the 'PMA_exportStructure' function