~hexmode/+junk/main

« back to all changes in this revision

Viewing changes to install-files/apps/phpmyadmin2.10.1/libraries/export/odt.php

  • Committer: Mark A. Hershberger
  • Date: 2008-01-05 19:38:56 UTC
  • Revision ID: hershberger@spawn-xp-20080105193856-6rnzgwa4nehue3qj
initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/* $Id: odt.php 9805 2006-12-26 16:10:47Z lem9 $ */
 
3
// vim: expandtab sw=4 ts=4 sts=4:
 
4
 
 
5
/**
 
6
 * Set of functions used to build CSV dumps of tables
 
7
 */
 
8
 
 
9
if (isset($plugin_list)) {
 
10
    $hide_structure = false;
 
11
    if ($plugin_param['export_type'] == 'table' && !$plugin_param['single_table']) {
 
12
        $hide_structure = true;
 
13
    }
 
14
    $plugin_list['odt'] = array(
 
15
        'text' => 'strOpenDocumentText',
 
16
        'extension' => 'odt',
 
17
        'mime_type' => 'application/vnd.oasis.opendocument.text',
 
18
        'force_file' => true,
 
19
        'options' => array(), /* Filled later */
 
20
        'options_text' => 'strOpenDocumentTextOptions',
 
21
        );
 
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');
 
29
        }
 
30
        if (!empty($GLOBALS['cfgRelation']['commwork']) || PMA_MYSQL_INT_VERSION >= 40100) {
 
31
            $plugin_list['odt']['options'][] =
 
32
                array('type' => 'bool', 'name' => 'comments', 'text' => 'strComments');
 
33
        }
 
34
        if (!empty($GLOBALS['cfgRelation']['mimework'])) {
 
35
            $plugin_list['odt']['options'][] =
 
36
                array('type' => 'bool', 'name' => 'mime', 'text' => 'strMIME_MIMEtype');
 
37
        }
 
38
        $plugin_list['odt']['options'][] =
 
39
            array('type' => 'egroup');
 
40
    }
 
41
    /* Data */
 
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');
 
50
} else {
 
51
 
 
52
$GLOBALS['odt_buffer'] = '';
 
53
require_once('./libraries/opendocument.lib.php');
 
54
 
 
55
/**
 
56
 * Outputs comment
 
57
 *
 
58
 * @param   string      Text of comment
 
59
 *
 
60
 * @return  bool        Whether it suceeded
 
61
 */
 
62
function PMA_exportComment($text) {
 
63
    return TRUE;
 
64
}
 
65
 
 
66
/**
 
67
 * Outputs export footer
 
68
 *
 
69
 * @return  bool        Whether it suceeded
 
70
 *
 
71
 * @access  public
 
72
 */
 
73
function PMA_exportFooter() {
 
74
    $GLOBALS['odt_buffer'] .= '</office:text>'
 
75
        . '</office:body>'
 
76
        . '</office:document-content>';
 
77
    if (!PMA_exportOutputHandler(PMA_createOpenDocument('application/vnd.oasis.opendocument.text', $GLOBALS['odt_buffer']))) {
 
78
        return FALSE;
 
79
    }
 
80
    return TRUE;
 
81
}
 
82
 
 
83
/**
 
84
 * Outputs export header
 
85
 *
 
86
 * @return  bool        Whether it suceeded
 
87
 *
 
88
 * @access  public
 
89
 */
 
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">'
 
93
        . '<office:body>'
 
94
        . '<office:text>';
 
95
    return TRUE;
 
96
}
 
97
 
 
98
/**
 
99
 * Outputs database header
 
100
 *
 
101
 * @param   string      Database name
 
102
 *
 
103
 * @return  bool        Whether it suceeded
 
104
 *
 
105
 * @access  public
 
106
 */
 
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>';
 
109
    return TRUE;
 
110
}
 
111
 
 
112
/**
 
113
 * Outputs database footer
 
114
 *
 
115
 * @param   string      Database name
 
116
 *
 
117
 * @return  bool        Whether it suceeded
 
118
 *
 
119
 * @access  public
 
120
 */
 
121
function PMA_exportDBFooter($db) {
 
122
    return TRUE;
 
123
}
 
124
 
 
125
/**
 
126
 * Outputs create database database
 
127
 *
 
128
 * @param   string      Database name
 
129
 *
 
130
 * @return  bool        Whether it suceeded
 
131
 *
 
132
 * @access  public
 
133
 */
 
134
function PMA_exportDBCreate($db) {
 
135
    return TRUE;
 
136
}
 
137
 
 
138
/**
 
139
 * Outputs the content of a table in CSV format
 
140
 *
 
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
 
146
 *
 
147
 * @return  bool        Whether it suceeded
 
148
 *
 
149
 * @access  public
 
150
 */
 
151
function PMA_exportData($db, $table, $crlf, $error_url, $sql_query) {
 
152
    global $what;
 
153
 
 
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);
 
161
    }
 
162
 
 
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 . '"/>';
 
166
 
 
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>';
 
174
        } // end for
 
175
        $GLOBALS['odt_buffer'] .= '</table:table-row>';
 
176
    } // end if
 
177
 
 
178
    // Format the data
 
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'
 
195
                   ) {
 
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>';
 
203
            } else {
 
204
                $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
 
205
                    . '<text:p>' . htmlspecialchars($row[$j]) . '</text:p>'
 
206
                    . '</table:table-cell>';
 
207
            }
 
208
        } // end for
 
209
        $GLOBALS['odt_buffer'] .= '</table:table-row>';
 
210
    } // end while
 
211
    PMA_DBI_free_result($result);
 
212
 
 
213
    $GLOBALS['odt_buffer'] .= '</table:table>';
 
214
 
 
215
    return TRUE;
 
216
}
 
217
 
 
218
/**
 
219
 * Returns $table's structure as Open Document Text
 
220
 *
 
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 
 
229
 *
 
230
 * @return  bool     Whether it suceeded
 
231
 *
 
232
 * @access  public
 
233
 */
 
234
 // @@@ $strTableStructure
 
235
function PMA_exportStructure($db, $table, $crlf, $error_url, $do_relation = false, $do_comments = false, $do_mime = false, $dates = false, $dummy)
 
236
{
 
237
    global $cfgRelation;
 
238
 
 
239
    /* Heading */
 
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>';
 
241
 
 
242
    /**
 
243
     * Get the unique keys in the table
 
244
     */
 
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'];
 
251
        }
 
252
    }
 
253
    PMA_DBI_free_result($keys_result);
 
254
 
 
255
    /**
 
256
     * Gets fields properties
 
257
     */
 
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);
 
262
 
 
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
 
266
        // an array
 
267
        $res_rel = PMA_getForeigners($db, $table);
 
268
 
 
269
        if ($res_rel && count($res_rel) > 0) {
 
270
            $have_rel = TRUE;
 
271
        } else {
 
272
            $have_rel = FALSE;
 
273
        }
 
274
    } else {
 
275
           $have_rel = FALSE;
 
276
    } // end if
 
277
 
 
278
    /**
 
279
     * Displays the table structure
 
280
     */
 
281
    $GLOBALS['odt_buffer'] .= '<table:table table:name="' . htmlspecialchars($table) . '_data">';
 
282
    $columns_cnt = 4;
 
283
    if ($do_relation && $have_rel) {
 
284
        $columns_cnt++;
 
285
    }
 
286
    if ($do_comments && ($cfgRelation['commwork'] || PMA_MYSQL_INT_VERSION >= 40100)) {
 
287
        $columns_cnt++;
 
288
    }
 
289
    if ($do_mime && $cfgRelation['mimework']) {
 
290
        $columns_cnt++;
 
291
    }
 
292
    $GLOBALS['odt_buffer'] .= '<table:table-column table:number-columns-repeated="' . $columns_cnt . '"/>';
 
293
    /* Header */
 
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>';
 
311
    }
 
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);
 
317
    }
 
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);
 
323
    }
 
324
    $GLOBALS['odt_buffer'] .= '</table:table-row>';
 
325
 
 
326
    while ($row = PMA_DBI_fetch_assoc($result)) {
 
327
 
 
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]) . ')';
 
339
            $type_nowrap  = '';
 
340
 
 
341
            $binary       = 0;
 
342
            $unsigned     = 0;
 
343
            $zerofill     = 0;
 
344
        } else {
 
345
            $type_nowrap  = ' nowrap="nowrap"';
 
346
            $type         = eregi_replace('BINARY', '', $type);
 
347
            $type         = eregi_replace('ZEROFILL', '', $type);
 
348
            $type         = eregi_replace('UNSIGNED', '', $type);
 
349
            if (empty($type)) {
 
350
                $type     = '&nbsp;';
 
351
            }
 
352
 
 
353
            $binary       = eregi('BINARY', $row['Type']);
 
354
            $unsigned     = eregi('UNSIGNED', $row['Type']);
 
355
            $zerofill     = eregi('ZEROFILL', $row['Type']);
 
356
        }
 
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';
 
363
            } else {
 
364
                $row['Default'] = '';
 
365
            }
 
366
        } else {
 
367
            $row['Default'] = $row['Default'];
 
368
        }
 
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>';
 
375
 
 
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>';
 
381
            }
 
382
        }
 
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>';
 
388
            } else {
 
389
                $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
 
390
                    . '<text:p></text:p>'
 
391
                    . '</table:table-cell>';
 
392
            }
 
393
        }
 
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>';
 
399
            } else {
 
400
                $GLOBALS['odt_buffer'] .= '<table:table-cell office:value-type="string">'
 
401
                    . '<text:p></text:p>'
 
402
                    . '</table:table-cell>';
 
403
            }
 
404
        }
 
405
        $GLOBALS['odt_buffer'] .= '</table:table-row>';
 
406
    } // end while
 
407
    PMA_DBI_free_result($result);
 
408
 
 
409
    $GLOBALS['odt_buffer'] .= '</table:table>';
 
410
    return TRUE;
 
411
} // end of the 'PMA_exportStructure' function
 
412
 
 
413
} // end else
 
414
?>