Welcome to mirror list, hosted at ThFree Co, Russian Federation.

xml.php « export « libraries - github.com/phpmyadmin/phpmyadmin.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: be005bfa7149af22341698ff310a2debf6d68d8b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
<?php
/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
 * Set of functions used to build XML dumps of tables
 *
 * @todo    
 * @version $Id$
 * @package phpMyAdmin-Export-XML
 */
if (! defined('PHPMYADMIN')) {
    exit;
}

if (strlen($GLOBALS['db'])) { /* Can't do server export */

if (isset($plugin_list)) {
    $plugin_list['xml'] = array(
        'text' => __('XML'),
        'extension' => 'xml',
        'mime_type' => 'text/xml',
        'options' => array(
            array('type' => 'hidden', 'name' => 'data'),
            ),
        'options_text' => __('Options')
        );
    
    /* Export structure */
    $plugin_list['xml']['options'][] =
        array('type' => 'bgroup', 'name' => 'export_struc', 'text' => __('Export Structure Schemas (recommended)'));
    $plugin_list['xml']['options'][] =
        array('type' => 'bool', 'name' => 'export_functions', 'text' => __('Export functions'));
    $plugin_list['xml']['options'][] =
        array('type' => 'bool', 'name' => 'export_procedures', 'text' => __('Export procedures'));
    $plugin_list['xml']['options'][] =
        array('type' => 'bool', 'name' => 'export_tables', 'text' => __('Export tables'));
    $plugin_list['xml']['options'][] =
        array('type' => 'bool', 'name' => 'export_triggers', 'text' => __('Export triggers'));
    $plugin_list['xml']['options'][] =
        array('type' => 'bool', 'name' => 'export_views', 'text' => __('Export views'));
    $plugin_list['xml']['options'][] =
        array('type' => 'egroup');
    
    /* Data */
    $plugin_list['xml']['options'][] =
        array('type' => 'bool', 'name' => 'export_contents', 'text' => __('Export contents'));
} else {

/**
 * Outputs comment
 *
 * @param   string      Text of comment
 *
 * @return  bool        Whether it suceeded
 */
function PMA_exportComment($text) {
    return PMA_exportOutputHandler('<!-- ' . $text . ' -->' . $GLOBALS['crlf']);
}

/**
 * Outputs export footer
 *
 * @return  bool        Whether it suceeded
 *
 * @access  public
 */
function PMA_exportFooter() {
    $foot = '</pma_xml_export>';
    
    return PMA_exportOutputHandler($foot);
}

/**
 * Outputs export header
 *
 * @return  bool        Whether it suceeded
 *
 * @access  public
 */
function PMA_exportHeader() {
    global $crlf;
    global $cfg;
    global $what;
    global $db;
    global $table;
    global $tables;
    
    $export_struct = isset($GLOBALS[$what . '_export_struc']) ? true : false;
    $export_data = isset($GLOBALS[$what . '_export_contents']) ? true : false;

    if ($GLOBALS['output_charset_conversion']) {
        $charset = $GLOBALS['charset_of_file'];
    } else {
        $charset = $GLOBALS['charset'];
    }

    $head  =  '<?xml version="1.0" encoding="' . $charset . '"?>' . $crlf
           .  '<!--' . $crlf
           .  '- phpMyAdmin XML Dump' . $crlf
           .  '- version ' . PMA_VERSION . $crlf
           .  '- http://www.phpmyadmin.net' . $crlf
           .  '-' . $crlf
           .  '- ' . __('Host') . ': ' . $cfg['Server']['host'];
    if (!empty($cfg['Server']['port'])) {
         $head .= ':' . $cfg['Server']['port'];
    }
    $head .= $crlf
           .  '- ' . __('Generation Time') . ': ' . PMA_localisedDate() . $crlf
           .  '- ' . __('Server version') . ': ' . substr(PMA_MYSQL_INT_VERSION, 0, 1) . '.' . (int) substr(PMA_MYSQL_INT_VERSION, 1, 2) . '.' . (int) substr(PMA_MYSQL_INT_VERSION, 3) . $crlf
           .  '- ' . __('PHP Version') . ': ' . phpversion() . $crlf
           .  '-->' . $crlf . $crlf;
    
    $head .= '<pma_xml_export version="1.0"' . (($export_struct) ? ' xmlns:pma="http://www.phpmyadmin.net/some_doc_url/"' : '') . '>' . $crlf;
    
    if ($export_struct) {
        $result = PMA_DBI_fetch_result('SELECT `DEFAULT_CHARACTER_SET_NAME`, `DEFAULT_COLLATION_NAME` FROM `information_schema`.`SCHEMATA` WHERE `SCHEMA_NAME` = \''.$db.'\' LIMIT 1');
        $db_collation = $result[0]['DEFAULT_COLLATION_NAME'];
        $db_charset = $result[0]['DEFAULT_CHARACTER_SET_NAME'];
        
        $head .= '    <!--' . $crlf;
        $head .= '    - Structure schemas' . $crlf;
        $head .= '    -->' . $crlf;
        $head .= '    <pma:structure_schemas>' . $crlf;
        $head .= '        <pma:database name="' . $db . '" collation="' . $db_collation . '" charset="' . $db_charset . '">' . $crlf;
        
        if (count($tables) == 0) {
            $tables[] = $table;
        }
        
        foreach ($tables as $table) {
            // Export tables and views
            $result = PMA_DBI_fetch_result('SHOW CREATE TABLE ' . PMA_backquote($db) . '.' . PMA_backquote($table), 0);
            $tbl =  $result[$table][1];
            
            $is_view = PMA_isView($db, $table);
            
            if ($is_view) {
                $type = 'view';
            } else {
                $type = 'table';
            }
            
            if ($is_view && ! isset($GLOBALS[$what . '_export_views'])) {
                continue;
            }
            
            if (! $is_view && ! isset($GLOBALS[$what . '_export_tables'])) {
                continue;
            }
            
            $head .= '            <pma:' . $type . ' name="' . $table . '">' . $crlf;
            
            $tbl = "                " . $tbl;
            $tbl = str_replace("\n", "\n                ", $tbl);
            
            $head .= $tbl . ';' . $crlf;
            $head .= '            </pma:' . $type . '>' . $crlf;
            
            if (isset($GLOBALS[$what . '_export_triggers']) && $GLOBALS[$what . '_export_triggers']) {
                // Export triggers
                $triggers = PMA_DBI_get_triggers($db, $table);
                if ($triggers) {
                    foreach ($triggers as $trigger) {
                        $code = $trigger['create'];
                        $head .= '            <pma:trigger name="' . $trigger['name'] . '">' . $crlf;
                        
                        // Do some formatting
                        $code = substr(rtrim($code), 0, -3);
                        $code = "                " . $code;
                        $code = str_replace("\n", "\n                ", $code);
                        
                        $head .= $code . $crlf;
                        $head .= '            </pma:trigger>' . $crlf;
                    }
                    
                    unset($trigger);
                    unset($triggers);
                }
            }
        }
        
        if (isset($GLOBALS[$what . '_export_functions']) && $GLOBALS[$what . '_export_functions']) {
            // Export functions
            $functions = PMA_DBI_get_procedures_or_functions($db, 'FUNCTION');
            if ($functions) {
                foreach ($functions as $function) {
                    $head .= '            <pma:function name="' . $function . '">' . $crlf;
                    
                    // Do some formatting
                    $sql = PMA_DBI_get_definition($db, 'FUNCTION', $function);
                    $sql = rtrim($sql);
                    $sql = "                " . $sql;
                    $sql = str_replace("\n", "\n                ", $sql);
                    
                    $head .= $sql . $crlf;
                    $head .= '            </pma:function>' . $crlf;
                }
                
                unset($create_func);
                unset($function);
                unset($functions);
            }
        }
        
        if (isset($GLOBALS[$what . '_export_procedures']) && $GLOBALS[$what . '_export_procedures']) {
            // Export procedures
            $procedures = PMA_DBI_get_procedures_or_functions($db, 'PROCEDURE');
            if ($procedures) {
                foreach ($procedures as $procedure) {
                    $head .= '            <pma:procedure name="' . $procedure . '">' . $crlf;
                    
                    // Do some formatting
                    $sql = PMA_DBI_get_definition($db, 'PROCEDURE', $procedure);
                    $sql = rtrim($sql);
                    $sql = "                " . $sql;
                    $sql = str_replace("\n", "\n                ", $sql);
                    
                    $head .= $sql . $crlf;
                    $head .= '            </pma:procedure>' . $crlf;
                }
                
                unset($create_proc);
                unset($procedure);
                unset($procedures);
            }
        }
        
        unset($result);
        
        $head .= '        </pma:database>' . $crlf;
        $head .= '    </pma:structure_schemas>' . $crlf;
        
        if ($export_data) {
            $head .= $crlf;
        }
    }
    
    return PMA_exportOutputHandler($head);
}

/**
 * Outputs database header
 *
 * @param   string      Database name
 *
 * @return  bool        Whether it suceeded
 *
 * @access  public
 */
function PMA_exportDBHeader($db) {
    global $crlf;
    global $what;
    
    if (isset($GLOBALS[$what . '_export_contents']) && $GLOBALS[$what . '_export_contents']) {
        $head = '    <!--' . $crlf
              . '    - ' . __('Database') . ': ' . (isset($GLOBALS['use_backquotes']) ? PMA_backquote($db) : '\'' . $db . '\''). $crlf
              . '    -->' . $crlf
              . '    <database name="' . $db . '">' . $crlf;
        
        return PMA_exportOutputHandler($head);
    }
    else
    {
        return TRUE;
    }
}

/**
 * Outputs database footer
 *
 * @param   string      Database name
 *
 * @return  bool        Whether it suceeded
 *
 * @access  public
 */
function PMA_exportDBFooter($db) {
    global $crlf;
    global $what;
    
    if (isset($GLOBALS[$what . '_export_contents']) && $GLOBALS[$what . '_export_contents']) {
        return PMA_exportOutputHandler('    </database>' . $crlf);
    }
    else
    {
        return TRUE;
    }
}

/**
 * Outputs create database database
 *
 * @param   string      Database name
 *
 * @return  bool        Whether it suceeded
 *
 * @access  public
 */
function PMA_exportDBCreate($db) {
    return TRUE;
}


/**
 * Outputs the content of a table
 *
 * @param   string      the database name
 * @param   string      the table name
 * @param   string      the end of line sequence
 * @param   string      the url to go back in case of error
 * @param   string      SQL query for obtaining data
 *
 * @return  bool        Whether it suceeded
 *
 * @access  public
 */
function PMA_exportData($db, $table, $crlf, $error_url, $sql_query) {
    global $what;
    
    if (isset($GLOBALS[$what . '_export_contents']) && $GLOBALS[$what . '_export_contents']) {
        $result      = PMA_DBI_query($sql_query, null, PMA_DBI_QUERY_UNBUFFERED);
        
        $columns_cnt = PMA_DBI_num_fields($result);
        for ($i = 0; $i < $columns_cnt; $i++) {
            $columns[$i] = stripslashes(str_replace(' ', '_', PMA_DBI_field_name($result, $i)));
        }
        unset($i);
        
        $buffer      = '        <!-- ' . __('Table') . ' ' . $table . ' -->' . $crlf;
        if (!PMA_exportOutputHandler($buffer)) {
            return FALSE;
        }
        
        while ($record = PMA_DBI_fetch_row($result)) {
            $buffer         = '        <table name="' . htmlspecialchars($table) . '">' . $crlf;
            for ($i = 0; $i < $columns_cnt; $i++) {
                // If a cell is NULL, still export it to preserve the XML structure
                if (!isset($record[$i]) || is_null($record[$i])) {
                    $record[$i] = 'NULL';
                }
                $buffer .= '            <column name="' . $columns[$i] . '">' . htmlspecialchars((string)$record[$i])
                        .  '</column>' . $crlf;
            }
            $buffer         .= '        </table>' . $crlf;
            
            if (!PMA_exportOutputHandler($buffer)) {
                return FALSE;
            }
        }
        PMA_DBI_free_result($result);
    }

    return TRUE;
} // end of the 'PMA_getTableXML()' function
}
}
?>