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

« back to all changes in this revision

Viewing changes to libraries/export/php_array.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:
 
1
<?php
 
2
/**
 
3
 * Set of functions used to build dumps of tables as PHP Arrays
 
4
 *
 
5
 * @author  Geoffray Warnants <http://www.geoffray.be>
 
6
 * @version 0.2b (20090704)
 
7
 */
 
8
if (! defined('PHPMYADMIN')) {
 
9
    exit;
 
10
}
 
11
 
 
12
/**
 
13
 *
 
14
 */
 
15
if (isset($plugin_list)) {
 
16
    $plugin_list['php_array'] = array(
 
17
        'text'          => 'strPhpArray',
 
18
        'extension'     => 'php',
 
19
        'mime_type'     => 'text/plain',
 
20
        'options'       => array(
 
21
            array(
 
22
                'type' => 'hidden',
 
23
                'name' => 'data',
 
24
            ),
 
25
        ),
 
26
        'options_text'  => 'strOptions',
 
27
    );
 
28
} else {
 
29
 
 
30
/**
 
31
 * Set of functions used to build exports of tables
 
32
 */
 
33
 
 
34
/**
 
35
 * Outputs comment
 
36
 *
 
37
 * @param   string      Text of comment
 
38
 *
 
39
 * @return  bool        Whether it suceeded
 
40
 */
 
41
function PMA_exportComment($text)
 
42
{
 
43
    PMA_exportOutputHandler('// ' . $text . $GLOBALS['crlf']);
 
44
    return true;
 
45
}
 
46
 
 
47
/**
 
48
 * Outputs export footer
 
49
 *
 
50
 * @return  bool        Whether it suceeded
 
51
 *
 
52
 * @access  public
 
53
 */
 
54
function PMA_exportFooter()
 
55
{
 
56
    return true;
 
57
}
 
58
 
 
59
/**
 
60
 * Outputs export header
 
61
 *
 
62
 * @return  bool        Whether it suceeded
 
63
 *
 
64
 * @access  public
 
65
 */
 
66
function PMA_exportHeader()
 
67
{
 
68
    PMA_exportOutputHandler(
 
69
          '<?php' . $GLOBALS['crlf']
 
70
        . '/**' . $GLOBALS['crlf']
 
71
        . ' * Export to PHP Array plugin for PHPMyAdmin' . $GLOBALS['crlf']
 
72
        . ' * @author Geoffray Warnants' . $GLOBALS['crlf']
 
73
        . ' * @version 0.2b' . $GLOBALS['crlf']
 
74
        . ' */' . $GLOBALS['crlf'] . $GLOBALS['crlf']
 
75
    );
 
76
    return true;
 
77
}
 
78
 
 
79
/**
 
80
 * Outputs database header
 
81
 *
 
82
 * @param   string      Database name
 
83
 *
 
84
 * @return  bool        Whether it suceeded
 
85
 *
 
86
 * @access  public
 
87
 */
 
88
function PMA_exportDBHeader($db)
 
89
{
 
90
    PMA_exportOutputHandler('//' . $GLOBALS['crlf'] . '// Database "' . $db . '"' . $GLOBALS['crlf'] . '//' . $GLOBALS['crlf']);
 
91
    return true;
 
92
}
 
93
 
 
94
/**
 
95
 * Outputs database footer
 
96
 *
 
97
 * @param   string      Database name
 
98
 *
 
99
 * @return  bool        Whether it suceeded
 
100
 *
 
101
 * @access  public
 
102
 */
 
103
function PMA_exportDBFooter($db)
 
104
{
 
105
    return true;
 
106
}
 
107
 
 
108
/**
 
109
 * Outputs create database database
 
110
 *
 
111
 * @param   string      Database name
 
112
 *
 
113
 * @return  bool        Whether it suceeded
 
114
 *
 
115
 * @access  public
 
116
 */
 
117
function PMA_exportDBCreate($db)
 
118
{
 
119
    return true;
 
120
}
 
121
 
 
122
/**
 
123
 * Outputs the content of a table in YAML format
 
124
 *
 
125
 * @param   string      the database name
 
126
 * @param   string      the table name
 
127
 * @param   string      the end of line sequence
 
128
 * @param   string      the url to go back in case of error
 
129
 * @param   string      SQL query for obtaining data
 
130
 *
 
131
 * @return  bool        Whether it suceeded
 
132
 *
 
133
 * @access  public
 
134
 */
 
135
function PMA_exportData($db, $table, $crlf, $error_url, $sql_query)
 
136
{
 
137
    $result      = PMA_DBI_query($sql_query, null, PMA_DBI_QUERY_UNBUFFERED);
 
138
 
 
139
    $columns_cnt = PMA_DBI_num_fields($result);
 
140
    for ($i = 0; $i < $columns_cnt; $i++) {
 
141
        $columns[$i] = stripslashes(PMA_DBI_field_name($result, $i));
 
142
    }
 
143
    unset($i);
 
144
 
 
145
    $buffer = '';
 
146
    $record_cnt = 0;
 
147
    while ($record = PMA_DBI_fetch_row($result)) {
 
148
 
 
149
        $record_cnt++;
 
150
        
 
151
        // Output table name as comment if this is the first record of the table
 
152
        if ($record_cnt == 1) {
 
153
            $buffer .= $crlf . '// ' . $db . '.' . $table . $crlf;
 
154
            $buffer .= '$' . $table . ' = array(' . $crlf;
 
155
            $buffer .= '  array(';
 
156
        } else {
 
157
            $buffer .= ',' . $crlf . '  array(';
 
158
        }
 
159
 
 
160
 
 
161
        for ($i = 0; $i < $columns_cnt; $i++) {
 
162
 
 
163
            $isLastLine = ($i + 1 >= $columns_cnt);
 
164
 
 
165
            $column = $columns[$i];
 
166
 
 
167
            if (is_null($record[$i])) {
 
168
                $buffer .= "'" . $column . "'=>null" . (! $isLastLine ? ',' : '');
 
169
            } elseif (is_numeric($record[$i])) {
 
170
                $buffer .= "'" . $column . "'=>" . $record[$i] . (! $isLastLine ? ',' : '');
 
171
            } else {
 
172
                $buffer .= "'" . $column . "'=>'" . addslashes($record[$i]) . "'" . (! $isLastLine ? ',' : '');
 
173
            }
 
174
        }
 
175
 
 
176
        $buffer .= ')';
 
177
    }
 
178
    
 
179
    $buffer .= $crlf . ');' . $crlf;
 
180
    if (! PMA_exportOutputHandler($buffer)) {
 
181
        return FALSE;
 
182
    }
 
183
        
 
184
    PMA_DBI_free_result($result);
 
185
 
 
186
    return true;
 
187
}
 
188
 
 
189
}