~ubuntu-branches/ubuntu/lucid/phpmyadmin/lucid

« back to all changes in this revision

Viewing changes to libraries/export/xml.php

  • Committer: Bazaar Package Importer
  • Author(s): Michal Čihař
  • Date: 2010-03-08 15:25:00 UTC
  • mfrom: (1.2.8 upstream)
  • Revision ID: james.westby@ubuntu.com-20100308152500-6e8hmuqc5co39de5
Tags: 4:3.3.0-1
* New upstream version.
* Rediff debian/patches.
* Fix permissions on mediawiki export extension.

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
/**
4
4
 * Set of functions used to build XML dumps of tables
5
5
 *
6
 
 * @version $Id: xml.php 12378 2009-04-19 12:24:45Z lem9 $
 
6
 * @todo    
 
7
 * @version $Id: xml.php 13190 2009-12-29 17:58:07Z lem9 $
7
8
 * @package phpMyAdmin-Export-XML
8
9
 */
9
10
if (! defined('PHPMYADMIN')) {
10
11
    exit;
11
12
}
12
13
 
13
 
/**
14
 
 *
15
 
 */
16
14
if (strlen($GLOBALS['db'])) { /* Can't do server export */
17
15
 
18
16
if (isset($plugin_list)) {
23
21
        'options' => array(
24
22
            array('type' => 'hidden', 'name' => 'data'),
25
23
            ),
26
 
        'options_text' => 'strOptions',
 
24
        'options_text' => 'strOptions'
27
25
        );
 
26
    
 
27
    /* Export structure */
 
28
    $plugin_list['xml']['options'][] =
 
29
        array('type' => 'bgroup', 'name' => 'export_struc', 'text' => 'strXMLExportStructs');
 
30
    $plugin_list['xml']['options'][] =
 
31
        array('type' => 'bool', 'name' => 'export_functions', 'text' => 'strXMLExportFunctions');
 
32
    $plugin_list['xml']['options'][] =
 
33
        array('type' => 'bool', 'name' => 'export_procedures', 'text' => 'strXMLExportProcedures');
 
34
    $plugin_list['xml']['options'][] =
 
35
        array('type' => 'bool', 'name' => 'export_tables', 'text' => 'strXMLExportTables');
 
36
    $plugin_list['xml']['options'][] =
 
37
        array('type' => 'bool', 'name' => 'export_triggers', 'text' => 'strXMLExportTriggers');
 
38
    $plugin_list['xml']['options'][] =
 
39
        array('type' => 'bool', 'name' => 'export_views', 'text' => 'strXMLExportViews');
 
40
    $plugin_list['xml']['options'][] =
 
41
        array('type' => 'egroup');
 
42
    
 
43
    /* Data */
 
44
    $plugin_list['xml']['options'][] =
 
45
        array('type' => 'bool', 'name' => 'export_contents', 'text' => 'strXMLExportContents');
28
46
} else {
29
47
 
30
48
/**
46
64
 * @access  public
47
65
 */
48
66
function PMA_exportFooter() {
49
 
    return TRUE;
 
67
    $foot = '</pma_xml_export>';
 
68
    
 
69
    return PMA_exportOutputHandler($foot);
50
70
}
51
71
 
52
72
/**
59
79
function PMA_exportHeader() {
60
80
    global $crlf;
61
81
    global $cfg;
 
82
    global $what;
 
83
    global $db;
 
84
    global $table;
 
85
    global $tables;
 
86
    
 
87
    $export_struct = isset($GLOBALS[$what . '_export_struc']) ? true : false;
 
88
    $export_data = isset($GLOBALS[$what . '_export_contents']) ? true : false;
62
89
 
63
90
    if ($GLOBALS['output_charset_conversion']) {
64
91
        $charset = $GLOBALS['charset_of_file'];
66
93
        $charset = $GLOBALS['charset'];
67
94
    }
68
95
 
69
 
    $head  =  '<?xml version="1.0" encoding="' . $charset . '" ?>' . $crlf
 
96
    $head  =  '<?xml version="1.0" encoding="' . $charset . '"?>' . $crlf
70
97
           .  '<!--' . $crlf
71
 
           .  '-' . $crlf
72
98
           .  '- phpMyAdmin XML Dump' . $crlf
73
99
           .  '- version ' . PMA_VERSION . $crlf
74
100
           .  '- http://www.phpmyadmin.net' . $crlf
82
108
           .  '- ' . $GLOBALS['strServerVersion'] . ': ' . substr(PMA_MYSQL_INT_VERSION, 0, 1) . '.' . (int) substr(PMA_MYSQL_INT_VERSION, 1, 2) . '.' . (int) substr(PMA_MYSQL_INT_VERSION, 3) . $crlf
83
109
           .  '- ' . $GLOBALS['strPHPVersion'] . ': ' . phpversion() . $crlf
84
110
           .  '-->' . $crlf . $crlf;
 
111
    
 
112
    $head .= '<pma_xml_export version="1.0"' . (($export_struct) ? ' xmlns:pma="http://www.phpmyadmin.net/some_doc_url/"' : '') . '>' . $crlf;
 
113
    
 
114
    if ($export_struct) {
 
115
        $result = PMA_DBI_fetch_result('SELECT `DEFAULT_CHARACTER_SET_NAME`, `DEFAULT_COLLATION_NAME` FROM `information_schema`.`SCHEMATA` WHERE `SCHEMA_NAME` = \''.$db.'\' LIMIT 1');
 
116
        $db_collation = $result[0]['DEFAULT_COLLATION_NAME'];
 
117
        $db_charset = $result[0]['DEFAULT_CHARACTER_SET_NAME'];
 
118
        
 
119
        $head .= '    <!--' . $crlf;
 
120
        $head .= '    - Structure schemas' . $crlf;
 
121
        $head .= '    -->' . $crlf;
 
122
        $head .= '    <pma:structure_schemas>' . $crlf;
 
123
        $head .= '        <pma:database name="' . $db . '" collation="' . $db_collation . '" charset="' . $db_charset . '">' . $crlf;
 
124
        
 
125
        if (count($tables) == 0) {
 
126
            $tables[] = $table;
 
127
        }
 
128
        
 
129
        foreach ($tables as $table) {
 
130
            // Export tables and views
 
131
            $result = PMA_DBI_fetch_result('SHOW CREATE TABLE ' . PMA_backquote($db) . '.' . PMA_backquote($table), 0);
 
132
            $tbl =  $result[$table][1];
 
133
            
 
134
            $is_view = PMA_isView($db, $table);
 
135
            
 
136
            if ($is_view) {
 
137
                $type = 'view';
 
138
            } else {
 
139
                $type = 'table';
 
140
            }
 
141
            
 
142
            if ($is_view && ! isset($GLOBALS[$what . '_export_views'])) {
 
143
                continue;
 
144
            }
 
145
            
 
146
            if (! $is_view && ! isset($GLOBALS[$what . '_export_tables'])) {
 
147
                continue;
 
148
            }
 
149
            
 
150
            $head .= '            <pma:' . $type . ' name="' . $table . '">' . $crlf;
 
151
            
 
152
            $tbl = "                " . $tbl;
 
153
            $tbl = str_replace("\n", "\n                ", $tbl);
 
154
            
 
155
            $head .= $tbl . ';' . $crlf;
 
156
            $head .= '            </pma:' . $type . '>' . $crlf;
 
157
            
 
158
            if (isset($GLOBALS[$what . '_export_triggers']) && $GLOBALS[$what . '_export_triggers']) {
 
159
                // Export triggers
 
160
                $triggers = PMA_DBI_get_triggers($db, $table);
 
161
                if ($triggers) {
 
162
                    foreach ($triggers as $trigger) {
 
163
                        $code = $trigger['create'];
 
164
                        $head .= '            <pma:trigger name="' . $trigger['name'] . '">' . $crlf;
 
165
                        
 
166
                        // Do some formatting
 
167
                        $code = substr(rtrim($code), 0, -3);
 
168
                        $code = "                " . $code;
 
169
                        $code = str_replace("\n", "\n                ", $code);
 
170
                        
 
171
                        $head .= $code . $crlf;
 
172
                        $head .= '            </pma:trigger>' . $crlf;
 
173
                    }
 
174
                    
 
175
                    unset($trigger);
 
176
                    unset($triggers);
 
177
                }
 
178
            }
 
179
        }
 
180
        
 
181
        if (isset($GLOBALS[$what . '_export_functions']) && $GLOBALS[$what . '_export_functions']) {
 
182
            // Export functions
 
183
            $functions = PMA_DBI_get_procedures_or_functions($db, 'FUNCTION');
 
184
            if ($functions) {
 
185
                foreach ($functions as $function) {
 
186
                    $head .= '            <pma:function name="' . $function . '">' . $crlf;
 
187
                    
 
188
                    // Do some formatting
 
189
                    $sql = PMA_DBI_get_definition($db, 'FUNCTION', $function);
 
190
                    $sql = rtrim($sql);
 
191
                    $sql = "                " . $sql;
 
192
                    $sql = str_replace("\n", "\n                ", $sql);
 
193
                    
 
194
                    $head .= $sql . $crlf;
 
195
                    $head .= '            </pma:function>' . $crlf;
 
196
                }
 
197
                
 
198
                unset($create_func);
 
199
                unset($function);
 
200
                unset($functions);
 
201
            }
 
202
        }
 
203
        
 
204
        if (isset($GLOBALS[$what . '_export_procedures']) && $GLOBALS[$what . '_export_procedures']) {
 
205
            // Export procedures
 
206
            $procedures = PMA_DBI_get_procedures_or_functions($db, 'PROCEDURE');
 
207
            if ($procedures) {
 
208
                foreach ($procedures as $procedure) {
 
209
                    $head .= '            <pma:procedure name="' . $procedure . '">' . $crlf;
 
210
                    
 
211
                    // Do some formatting
 
212
                    $sql = PMA_DBI_get_definition($db, 'PROCEDURE', $procedure);
 
213
                    $sql = rtrim($sql);
 
214
                    $sql = "                " . $sql;
 
215
                    $sql = str_replace("\n", "\n                ", $sql);
 
216
                    
 
217
                    $head .= $sql . $crlf;
 
218
                    $head .= '            </pma:procedure>' . $crlf;
 
219
                }
 
220
                
 
221
                unset($create_proc);
 
222
                unset($procedure);
 
223
                unset($procedures);
 
224
            }
 
225
        }
 
226
        
 
227
        unset($result);
 
228
        
 
229
        $head .= '        </pma:database>' . $crlf;
 
230
        $head .= '    </pma:structure_schemas>' . $crlf;
 
231
        
 
232
        if ($export_data) {
 
233
            $head .= $crlf;
 
234
        }
 
235
    }
 
236
    
85
237
    return PMA_exportOutputHandler($head);
86
238
}
87
239
 
96
248
 */
97
249
function PMA_exportDBHeader($db) {
98
250
    global $crlf;
99
 
    $db = str_replace(' ', '_', $db);
100
 
    $head = '<!--' . $crlf
101
 
          . '- ' . $GLOBALS['strDatabase'] . ': ' . (isset($GLOBALS['use_backquotes']) ? PMA_backquote($db) : '\'' . $db . '\''). $crlf
102
 
          . '-->' . $crlf
103
 
          . '<' . $db . '>' . $crlf;
104
 
    return PMA_exportOutputHandler($head);
 
251
    global $what;
 
252
    
 
253
    if (isset($GLOBALS[$what . '_export_contents']) && $GLOBALS[$what . '_export_contents']) {
 
254
        $head = '    <!--' . $crlf
 
255
              . '    - ' . $GLOBALS['strDatabase'] . ': ' . (isset($GLOBALS['use_backquotes']) ? PMA_backquote($db) : '\'' . $db . '\''). $crlf
 
256
              . '    -->' . $crlf
 
257
              . '    <database name="' . $db . '">' . $crlf;
 
258
        
 
259
        return PMA_exportOutputHandler($head);
 
260
    }
 
261
    else
 
262
    {
 
263
        return TRUE;
 
264
    }
105
265
}
106
266
 
107
267
/**
115
275
 */
116
276
function PMA_exportDBFooter($db) {
117
277
    global $crlf;
118
 
    $db = str_replace(' ', '_', $db);
119
 
    return PMA_exportOutputHandler('</' . $db . '>' . $crlf);
 
278
    global $what;
 
279
    
 
280
    if (isset($GLOBALS[$what . '_export_contents']) && $GLOBALS[$what . '_export_contents']) {
 
281
        return PMA_exportOutputHandler('    </database>' . $crlf);
 
282
    }
 
283
    else
 
284
    {
 
285
        return TRUE;
 
286
    }
120
287
}
121
288
 
122
289
/**
147
314
 * @access  public
148
315
 */
149
316
function PMA_exportData($db, $table, $crlf, $error_url, $sql_query) {
150
 
    $db = str_replace(' ', '_', $db);
151
 
    $table = str_replace(' ', '_', $table);
152
 
    $result      = PMA_DBI_query($sql_query, null, PMA_DBI_QUERY_UNBUFFERED);
153
 
 
154
 
    $columns_cnt = PMA_DBI_num_fields($result);
155
 
    for ($i = 0; $i < $columns_cnt; $i++) {
156
 
        $columns[$i] = stripslashes(str_replace(' ', '_', PMA_DBI_field_name($result, $i)));
157
 
    }
158
 
    unset($i);
159
 
 
160
 
    $buffer      = '  <!-- ' . $GLOBALS['strTable'] . ' ' . $table . ' -->' . $crlf;
161
 
    if (!PMA_exportOutputHandler($buffer)) {
162
 
        return FALSE;
163
 
    }
164
 
 
165
 
    while ($record = PMA_DBI_fetch_row($result)) {
166
 
        $buffer         = '    <' . $table . '>' . $crlf;
 
317
    global $what;
 
318
    
 
319
    if (isset($GLOBALS[$what . '_export_contents']) && $GLOBALS[$what . '_export_contents']) {
 
320
        $result      = PMA_DBI_query($sql_query, null, PMA_DBI_QUERY_UNBUFFERED);
 
321
        
 
322
        $columns_cnt = PMA_DBI_num_fields($result);
167
323
        for ($i = 0; $i < $columns_cnt; $i++) {
168
 
            if (isset($record[$i]) && !is_null($record[$i])) {
169
 
                $buffer .= '        <' . $columns[$i] . '>' . htmlspecialchars($record[$i])
170
 
                        .  '</' . $columns[$i] . '>' . $crlf;
171
 
            }
 
324
            $columns[$i] = stripslashes(str_replace(' ', '_', PMA_DBI_field_name($result, $i)));
172
325
        }
173
 
        $buffer         .= '    </' . $table . '>' . $crlf;
174
 
 
 
326
        unset($i);
 
327
        
 
328
        $buffer      = '        <!-- ' . $GLOBALS['strTable'] . ' ' . $table . ' -->' . $crlf;
175
329
        if (!PMA_exportOutputHandler($buffer)) {
176
330
            return FALSE;
177
331
        }
 
332
        
 
333
        while ($record = PMA_DBI_fetch_row($result)) {
 
334
            $buffer         = '        <table name="' . htmlspecialchars($table) . '">' . $crlf;
 
335
            for ($i = 0; $i < $columns_cnt; $i++) {
 
336
                // If a cell is NULL, still export it to preserve the XML structure
 
337
                if (!isset($record[$i]) || is_null($record[$i])) {
 
338
                    $record[$i] = 'NULL';
 
339
                }
 
340
                $buffer .= '            <column name="' . $columns[$i] . '">' . htmlspecialchars(utf8_encode((string)$record[$i]))
 
341
                        .  '</column>' . $crlf;
 
342
            }
 
343
            $buffer         .= '        </table>' . $crlf;
 
344
            
 
345
            if (!PMA_exportOutputHandler($buffer)) {
 
346
                return FALSE;
 
347
            }
 
348
        }
 
349
        PMA_DBI_free_result($result);
178
350
    }
179
 
    PMA_DBI_free_result($result);
180
351
 
181
352
    return TRUE;
182
353
} // end of the 'PMA_getTableXML()' function