1 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
5 * An open source application development framework for PHP 4.3.2 or newer
8 * @author ExpressionEngine Dev Team
9 * @copyright Copyright (c) 2008 - 2009, EllisLab, Inc.
10 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
16 // ------------------------------------------------------------------------
19 * Database Utility Class
22 * @author ExpressionEngine Dev Team
23 * @link http://codeigniter.com/user_guide/database/
25 class CI_DB_utility extends CI_DB_forge {
28 var $data_cache = array();
33 * Grabs the CI super object instance so we can access it.
36 function CI_DB_utility()
38 // Assign the main database object to $this->db
39 $CI =& get_instance();
42 log_message('debug', "Database Utility Class Initialized");
45 // --------------------------------------------------------------------
53 function list_databases()
55 // Is there a cached result?
56 if (isset($this->data_cache['db_names']))
58 return $this->data_cache['db_names'];
61 $query = $this->db->query($this->_list_databases());
63 if ($query->num_rows() > 0)
65 foreach ($query->result_array() as $row)
67 $dbs[] = current($row);
71 $this->data_cache['db_names'] = $dbs;
72 return $this->data_cache['db_names'];
75 // --------------------------------------------------------------------
81 * @param string the table name
84 function optimize_table($table_name)
86 $sql = $this->_optimize_table($table_name);
90 show_error('db_must_use_set');
93 $query = $this->db->query($sql);
94 $res = $query->result_array();
96 // Note: Due to a bug in current() that affects some versions
97 // of PHP we can not pass function call directly into it
101 // --------------------------------------------------------------------
109 function optimize_database()
112 foreach ($this->db->list_tables() as $table_name)
114 $sql = $this->_optimize_table($table_name);
121 $query = $this->db->query($sql);
123 // Build the result array...
124 // Note: Due to a bug in current() that affects some versions
125 // of PHP we can not pass function call directly into it
126 $res = $query->result_array();
127 $res = current($res);
128 $key = str_replace($this->db->database.'.', '', current($res));
129 $keys = array_keys($res);
130 unset($res[$keys[0]]);
132 $result[$key] = $res;
138 // --------------------------------------------------------------------
144 * @param string the table name
147 function repair_table($table_name)
149 $sql = $this->_repair_table($table_name);
156 $query = $this->db->query($sql);
158 // Note: Due to a bug in current() that affects some versions
159 // of PHP we can not pass function call directly into it
160 $res = $query->result_array();
161 return current($res);
164 // --------------------------------------------------------------------
167 * Generate CSV from a query result object
170 * @param object The query result object
171 * @param string The delimiter - comma by default
172 * @param string The newline character - \n by default
173 * @param string The enclosure - double quote by default
176 function csv_from_result($query, $delim = ",", $newline = "\n", $enclosure = '"')
178 if ( ! is_object($query) OR ! method_exists($query, 'list_fields'))
180 show_error('You must submit a valid result object');
185 // First generate the headings from the table column names
186 foreach ($query->list_fields() as $name)
188 $out .= $enclosure.str_replace($enclosure, $enclosure.$enclosure, $name).$enclosure.$delim;
194 // Next blast through the result array and build out the rows
195 foreach ($query->result_array() as $row)
197 foreach ($row as $item)
199 $out .= $enclosure.str_replace($enclosure, $enclosure.$enclosure, $item).$enclosure.$delim;
208 // --------------------------------------------------------------------
211 * Generate XML data from a query result object
214 * @param object The query result object
215 * @param array Any preferences
218 function xml_from_result($query, $params = array())
220 if ( ! is_object($query) OR ! method_exists($query, 'list_fields'))
222 show_error('You must submit a valid result object');
225 // Set our default values
226 foreach (array('root' => 'root', 'element' => 'element', 'newline' => "\n", 'tab' => "\t") as $key => $val)
228 if ( ! isset($params[$key]))
230 $params[$key] = $val;
234 // Create variables for convenience
237 // Load the xml helper
238 $CI =& get_instance();
239 $CI->load->helper('xml');
241 // Generate the result
242 $xml = "<{$root}>".$newline;
243 foreach ($query->result_array() as $row)
245 $xml .= $tab."<{$element}>".$newline;
247 foreach ($row as $key => $val)
249 $xml .= $tab.$tab."<{$key}>".xml_convert($val)."</{$key}>".$newline;
251 $xml .= $tab."</{$element}>".$newline;
253 $xml .= "</$root>".$newline;
258 // --------------------------------------------------------------------
266 function backup($params = array())
268 // If the parameters have not been submitted as an
269 // array then we know that it is simply the table
270 // name, which is a valid short cut.
271 if (is_string($params))
273 $params = array('tables' => $params);
276 // ------------------------------------------------------
278 // Set up our default preferences
283 'format' => 'gzip', // gzip, zip, txt
285 'add_insert' => TRUE,
289 // Did the user submit any preferences? If so set them....
290 if (count($params) > 0)
292 foreach ($prefs as $key => $val)
294 if (isset($params[$key]))
296 $prefs[$key] = $params[$key];
301 // ------------------------------------------------------
303 // Are we backing up a complete database or individual tables?
304 // If no table names were submitted we'll fetch the entire table list
305 if (count($prefs['tables']) == 0)
307 $prefs['tables'] = $this->db->list_tables();
310 // ------------------------------------------------------
312 // Validate the format
313 if ( ! in_array($prefs['format'], array('gzip', 'zip', 'txt'), TRUE))
315 $prefs['format'] = 'txt';
318 // ------------------------------------------------------
320 // Is the encoder supported? If not, we'll either issue an
321 // error or use plain text depending on the debug settings
322 if (($prefs['format'] == 'gzip' AND ! @function_exists('gzencode'))
323 OR ($prefs['format'] == 'zip' AND ! @function_exists('gzcompress')))
325 if ($this->db->db_debug)
327 return $this->db->display_error('db_unsuported_compression');
330 $prefs['format'] = 'txt';
333 // ------------------------------------------------------
335 // Set the filename if not provided - Only needed with Zip files
336 if ($prefs['filename'] == '' AND $prefs['format'] == 'zip')
338 $prefs['filename'] = (count($prefs['tables']) == 1) ? $prefs['tables'] : $this->db->database;
339 $prefs['filename'] .= '_'.date('Y-m-d_H-i', time());
342 // ------------------------------------------------------
344 // Was a Gzip file requested?
345 if ($prefs['format'] == 'gzip')
347 return gzencode($this->_backup($prefs));
350 // ------------------------------------------------------
352 // Was a text file requested?
353 if ($prefs['format'] == 'txt')
355 return $this->_backup($prefs);
358 // ------------------------------------------------------
360 // Was a Zip file requested?
361 if ($prefs['format'] == 'zip')
363 // If they included the .zip file extension we'll remove it
364 if (preg_match("|.+?\.zip$|", $prefs['filename']))
366 $prefs['filename'] = str_replace('.zip', '', $prefs['filename']);
369 // Tack on the ".sql" file extension if needed
370 if ( ! preg_match("|.+?\.sql$|", $prefs['filename']))
372 $prefs['filename'] .= '.sql';
375 // Load the Zip class and output it
377 $CI =& get_instance();
378 $CI->load->library('zip');
379 $CI->zip->add_data($prefs['filename'], $this->_backup($prefs));
380 return $CI->zip->get_zip();
388 /* End of file DB_utility.php */
389 /* Location: ./system/database/DB_utility.php */