]>
Commit | Line | Data |
---|---|---|
59c06b17 CS |
1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
2 | /** | |
3 | * Code Igniter | |
4 | * | |
5 | * An open source application development framework for PHP 5.1.6 or newer | |
6 | * | |
7 | * @package CodeIgniter | |
8 | * @author ExpressionEngine Dev Team | |
9 | * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. | |
10 | * @license http://codeigniter.com/user_guide/license.html | |
11 | * @link http://codeigniter.com | |
12 | * @since Version 1.0 | |
13 | * @filesource | |
14 | */ | |
15 | ||
16 | // ------------------------------------------------------------------------ | |
17 | ||
18 | /** | |
19 | * Database Utility Class | |
20 | * | |
21 | * @category Database | |
22 | * @author ExpressionEngine Dev Team | |
23 | * @link http://codeigniter.com/user_guide/database/ | |
24 | */ | |
25 | class CI_DB_utility extends CI_DB_forge { | |
26 | ||
27 | var $db; | |
28 | var $data_cache = array(); | |
29 | ||
30 | /** | |
31 | * Constructor | |
32 | * | |
33 | * Grabs the CI super object instance so we can access it. | |
34 | * | |
35 | */ | |
36 | function __construct() | |
37 | { | |
38 | // Assign the main database object to $this->db | |
39 | $CI =& get_instance(); | |
40 | $this->db =& $CI->db; | |
41 | ||
42 | log_message('debug', "Database Utility Class Initialized"); | |
43 | } | |
44 | ||
45 | // -------------------------------------------------------------------- | |
46 | ||
47 | /** | |
48 | * List databases | |
49 | * | |
50 | * @access public | |
51 | * @return bool | |
52 | */ | |
53 | function list_databases() | |
54 | { | |
55 | // Is there a cached result? | |
56 | if (isset($this->data_cache['db_names'])) | |
57 | { | |
58 | return $this->data_cache['db_names']; | |
59 | } | |
60 | ||
61 | $query = $this->db->query($this->_list_databases()); | |
62 | $dbs = array(); | |
63 | if ($query->num_rows() > 0) | |
64 | { | |
65 | foreach ($query->result_array() as $row) | |
66 | { | |
67 | $dbs[] = current($row); | |
68 | } | |
69 | } | |
70 | ||
71 | $this->data_cache['db_names'] = $dbs; | |
72 | return $this->data_cache['db_names']; | |
73 | } | |
74 | ||
75 | // -------------------------------------------------------------------- | |
76 | ||
77 | /** | |
78 | * Determine if a particular database exists | |
79 | * | |
80 | * @access public | |
81 | * @param string | |
82 | * @return boolean | |
83 | */ | |
84 | function database_exists($database_name) | |
85 | { | |
86 | // Some databases won't have access to the list_databases() function, so | |
87 | // this is intended to allow them to override with their own functions as | |
88 | // defined in $driver_utility.php | |
89 | if (method_exists($this, '_database_exists')) | |
90 | { | |
91 | return $this->_database_exists($database_name); | |
92 | } | |
93 | else | |
94 | { | |
95 | return ( ! in_array($database_name, $this->list_databases())) ? FALSE : TRUE; | |
96 | } | |
97 | } | |
98 | ||
99 | ||
100 | // -------------------------------------------------------------------- | |
101 | ||
102 | /** | |
103 | * Optimize Table | |
104 | * | |
105 | * @access public | |
106 | * @param string the table name | |
107 | * @return bool | |
108 | */ | |
109 | function optimize_table($table_name) | |
110 | { | |
111 | $sql = $this->_optimize_table($table_name); | |
112 | ||
113 | if (is_bool($sql)) | |
114 | { | |
115 | show_error('db_must_use_set'); | |
116 | } | |
117 | ||
118 | $query = $this->db->query($sql); | |
119 | $res = $query->result_array(); | |
120 | ||
121 | // Note: Due to a bug in current() that affects some versions | |
122 | // of PHP we can not pass function call directly into it | |
123 | return current($res); | |
124 | } | |
125 | ||
126 | // -------------------------------------------------------------------- | |
127 | ||
128 | /** | |
129 | * Optimize Database | |
130 | * | |
131 | * @access public | |
132 | * @return array | |
133 | */ | |
134 | function optimize_database() | |
135 | { | |
136 | $result = array(); | |
137 | foreach ($this->db->list_tables() as $table_name) | |
138 | { | |
139 | $sql = $this->_optimize_table($table_name); | |
140 | ||
141 | if (is_bool($sql)) | |
142 | { | |
143 | return $sql; | |
144 | } | |
145 | ||
146 | $query = $this->db->query($sql); | |
147 | ||
148 | // Build the result array... | |
149 | // Note: Due to a bug in current() that affects some versions | |
150 | // of PHP we can not pass function call directly into it | |
151 | $res = $query->result_array(); | |
152 | $res = current($res); | |
153 | $key = str_replace($this->db->database.'.', '', current($res)); | |
154 | $keys = array_keys($res); | |
155 | unset($res[$keys[0]]); | |
156 | ||
157 | $result[$key] = $res; | |
158 | } | |
159 | ||
160 | return $result; | |
161 | } | |
162 | ||
163 | // -------------------------------------------------------------------- | |
164 | ||
165 | /** | |
166 | * Repair Table | |
167 | * | |
168 | * @access public | |
169 | * @param string the table name | |
170 | * @return bool | |
171 | */ | |
172 | function repair_table($table_name) | |
173 | { | |
174 | $sql = $this->_repair_table($table_name); | |
175 | ||
176 | if (is_bool($sql)) | |
177 | { | |
178 | return $sql; | |
179 | } | |
180 | ||
181 | $query = $this->db->query($sql); | |
182 | ||
183 | // Note: Due to a bug in current() that affects some versions | |
184 | // of PHP we can not pass function call directly into it | |
185 | $res = $query->result_array(); | |
186 | return current($res); | |
187 | } | |
188 | ||
189 | // -------------------------------------------------------------------- | |
190 | ||
191 | /** | |
192 | * Generate CSV from a query result object | |
193 | * | |
194 | * @access public | |
195 | * @param object The query result object | |
196 | * @param string The delimiter - comma by default | |
197 | * @param string The newline character - \n by default | |
198 | * @param string The enclosure - double quote by default | |
199 | * @return string | |
200 | */ | |
201 | function csv_from_result($query, $delim = ",", $newline = "\n", $enclosure = '"') | |
202 | { | |
203 | if ( ! is_object($query) OR ! method_exists($query, 'list_fields')) | |
204 | { | |
205 | show_error('You must submit a valid result object'); | |
206 | } | |
207 | ||
208 | $out = ''; | |
209 | ||
210 | // First generate the headings from the table column names | |
211 | foreach ($query->list_fields() as $name) | |
212 | { | |
213 | $out .= $enclosure.str_replace($enclosure, $enclosure.$enclosure, $name).$enclosure.$delim; | |
214 | } | |
215 | ||
216 | $out = rtrim($out); | |
217 | $out .= $newline; | |
218 | ||
219 | // Next blast through the result array and build out the rows | |
220 | foreach ($query->result_array() as $row) | |
221 | { | |
222 | foreach ($row as $item) | |
223 | { | |
224 | $out .= $enclosure.str_replace($enclosure, $enclosure.$enclosure, $item).$enclosure.$delim; | |
225 | } | |
226 | $out = rtrim($out); | |
227 | $out .= $newline; | |
228 | } | |
229 | ||
230 | return $out; | |
231 | } | |
232 | ||
233 | // -------------------------------------------------------------------- | |
234 | ||
235 | /** | |
236 | * Generate XML data from a query result object | |
237 | * | |
238 | * @access public | |
239 | * @param object The query result object | |
240 | * @param array Any preferences | |
241 | * @return string | |
242 | */ | |
243 | function xml_from_result($query, $params = array()) | |
244 | { | |
245 | if ( ! is_object($query) OR ! method_exists($query, 'list_fields')) | |
246 | { | |
247 | show_error('You must submit a valid result object'); | |
248 | } | |
249 | ||
250 | // Set our default values | |
251 | foreach (array('root' => 'root', 'element' => 'element', 'newline' => "\n", 'tab' => "\t") as $key => $val) | |
252 | { | |
253 | if ( ! isset($params[$key])) | |
254 | { | |
255 | $params[$key] = $val; | |
256 | } | |
257 | } | |
258 | ||
259 | // Create variables for convenience | |
260 | extract($params); | |
261 | ||
262 | // Load the xml helper | |
263 | $CI =& get_instance(); | |
264 | $CI->load->helper('xml'); | |
265 | ||
266 | // Generate the result | |
267 | $xml = "<{$root}>".$newline; | |
268 | foreach ($query->result_array() as $row) | |
269 | { | |
270 | $xml .= $tab."<{$element}>".$newline; | |
271 | ||
272 | foreach ($row as $key => $val) | |
273 | { | |
274 | $xml .= $tab.$tab."<{$key}>".xml_convert($val)."</{$key}>".$newline; | |
275 | } | |
276 | $xml .= $tab."</{$element}>".$newline; | |
277 | } | |
278 | $xml .= "</$root>".$newline; | |
279 | ||
280 | return $xml; | |
281 | } | |
282 | ||
283 | // -------------------------------------------------------------------- | |
284 | ||
285 | /** | |
286 | * Database Backup | |
287 | * | |
288 | * @access public | |
289 | * @return void | |
290 | */ | |
291 | function backup($params = array()) | |
292 | { | |
293 | // If the parameters have not been submitted as an | |
294 | // array then we know that it is simply the table | |
295 | // name, which is a valid short cut. | |
296 | if (is_string($params)) | |
297 | { | |
298 | $params = array('tables' => $params); | |
299 | } | |
300 | ||
301 | // ------------------------------------------------------ | |
302 | ||
303 | // Set up our default preferences | |
304 | $prefs = array( | |
305 | 'tables' => array(), | |
306 | 'ignore' => array(), | |
307 | 'filename' => '', | |
308 | 'format' => 'gzip', // gzip, zip, txt | |
309 | 'add_drop' => TRUE, | |
310 | 'add_insert' => TRUE, | |
311 | 'newline' => "\n" | |
312 | ); | |
313 | ||
314 | // Did the user submit any preferences? If so set them.... | |
315 | if (count($params) > 0) | |
316 | { | |
317 | foreach ($prefs as $key => $val) | |
318 | { | |
319 | if (isset($params[$key])) | |
320 | { | |
321 | $prefs[$key] = $params[$key]; | |
322 | } | |
323 | } | |
324 | } | |
325 | ||
326 | // ------------------------------------------------------ | |
327 | ||
328 | // Are we backing up a complete database or individual tables? | |
329 | // If no table names were submitted we'll fetch the entire table list | |
330 | if (count($prefs['tables']) == 0) | |
331 | { | |
332 | $prefs['tables'] = $this->db->list_tables(); | |
333 | } | |
334 | ||
335 | // ------------------------------------------------------ | |
336 | ||
337 | // Validate the format | |
338 | if ( ! in_array($prefs['format'], array('gzip', 'zip', 'txt'), TRUE)) | |
339 | { | |
340 | $prefs['format'] = 'txt'; | |
341 | } | |
342 | ||
343 | // ------------------------------------------------------ | |
344 | ||
345 | // Is the encoder supported? If not, we'll either issue an | |
346 | // error or use plain text depending on the debug settings | |
347 | if (($prefs['format'] == 'gzip' AND ! @function_exists('gzencode')) | |
348 | OR ($prefs['format'] == 'zip' AND ! @function_exists('gzcompress'))) | |
349 | { | |
350 | if ($this->db->db_debug) | |
351 | { | |
352 | return $this->db->display_error('db_unsuported_compression'); | |
353 | } | |
354 | ||
355 | $prefs['format'] = 'txt'; | |
356 | } | |
357 | ||
358 | // ------------------------------------------------------ | |
359 | ||
360 | // Set the filename if not provided - Only needed with Zip files | |
361 | if ($prefs['filename'] == '' AND $prefs['format'] == 'zip') | |
362 | { | |
363 | $prefs['filename'] = (count($prefs['tables']) == 1) ? $prefs['tables'] : $this->db->database; | |
364 | $prefs['filename'] .= '_'.date('Y-m-d_H-i', time()); | |
365 | } | |
366 | ||
367 | // ------------------------------------------------------ | |
368 | ||
369 | // Was a Gzip file requested? | |
370 | if ($prefs['format'] == 'gzip') | |
371 | { | |
372 | return gzencode($this->_backup($prefs)); | |
373 | } | |
374 | ||
375 | // ------------------------------------------------------ | |
376 | ||
377 | // Was a text file requested? | |
378 | if ($prefs['format'] == 'txt') | |
379 | { | |
380 | return $this->_backup($prefs); | |
381 | } | |
382 | ||
383 | // ------------------------------------------------------ | |
384 | ||
385 | // Was a Zip file requested? | |
386 | if ($prefs['format'] == 'zip') | |
387 | { | |
388 | // If they included the .zip file extension we'll remove it | |
389 | if (preg_match("|.+?\.zip$|", $prefs['filename'])) | |
390 | { | |
391 | $prefs['filename'] = str_replace('.zip', '', $prefs['filename']); | |
392 | } | |
393 | ||
394 | // Tack on the ".sql" file extension if needed | |
395 | if ( ! preg_match("|.+?\.sql$|", $prefs['filename'])) | |
396 | { | |
397 | $prefs['filename'] .= '.sql'; | |
398 | } | |
399 | ||
400 | // Load the Zip class and output it | |
401 | ||
402 | $CI =& get_instance(); | |
403 | $CI->load->library('zip'); | |
404 | $CI->zip->add_data($prefs['filename'], $this->_backup($prefs)); | |
405 | return $CI->zip->get_zip(); | |
406 | } | |
407 | ||
408 | } | |
409 | ||
410 | } | |
411 | ||
412 | ||
413 | /* End of file DB_utility.php */ | |
414 | /* Location: ./system/database/DB_utility.php */ |