-<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');\r
-/**\r
- * Code Igniter\r
- *\r
- * An open source application development framework for PHP 4.3.2 or newer\r
- *\r
- * @package CodeIgniter\r
- * @author ExpressionEngine Dev Team\r
- * @copyright Copyright (c) 2008, EllisLab, Inc.\r
- * @license http://codeigniter.com/user_guide/license.html\r
- * @link http://codeigniter.com\r
- * @since Version 1.0\r
- * @filesource\r
- */\r
-\r
-// ------------------------------------------------------------------------\r
-\r
-/**\r
- * Database Utility Class\r
- *\r
- * @category Database\r
- * @author ExpressionEngine Dev Team\r
- * @link http://codeigniter.com/user_guide/database/\r
- */\r
-class CI_DB_utility extends CI_DB_forge {\r
-\r
- var $db;\r
- var $data_cache = array();\r
-\r
- /**\r
- * Constructor\r
- *\r
- * Grabs the CI super object instance so we can access it.\r
- *\r
- */ \r
- function CI_DB_utility()\r
- {\r
- // Assign the main database object to $this->db\r
- $CI =& get_instance();\r
- $this->db =& $CI->db;\r
- \r
- log_message('debug', "Database Utility Class Initialized");\r
- }\r
-\r
- // --------------------------------------------------------------------\r
-\r
- /**\r
- * List databases\r
- *\r
- * @access public\r
- * @return bool\r
- */\r
- function list_databases()\r
- { \r
- // Is there a cached result?\r
- if (isset($this->data_cache['db_names']))\r
- {\r
- return $this->data_cache['db_names'];\r
- }\r
- \r
- $query = $this->db->query($this->_list_databases());\r
- $dbs = array();\r
- if ($query->num_rows() > 0)\r
- {\r
- foreach ($query->result_array() as $row)\r
- {\r
- $dbs[] = current($row);\r
- }\r
- }\r
- \r
- $this->data_cache['db_names'] = $dbs;\r
- return $this->data_cache['db_names'];\r
- }\r
-\r
- // --------------------------------------------------------------------\r
-\r
- /**\r
- * Optimize Table\r
- *\r
- * @access public\r
- * @param string the table name\r
- * @return bool\r
- */\r
- function optimize_table($table_name)\r
- {\r
- $sql = $this->_optimize_table($table_name);\r
- \r
- if (is_bool($sql))\r
- {\r
- show_error('db_must_use_set');\r
- }\r
- \r
- $query = $this->db->query($sql);\r
- $res = $query->result_array();\r
- \r
- // Note: Due to a bug in current() that affects some versions\r
- // of PHP we can not pass function call directly into it\r
- return current($res);\r
- }\r
-\r
- // --------------------------------------------------------------------\r
-\r
- /**\r
- * Optimize Database\r
- *\r
- * @access public\r
- * @return array\r
- */\r
- function optimize_database()\r
- {\r
- $result = array();\r
- foreach ($this->db->list_tables() as $table_name)\r
- {\r
- $sql = $this->_optimize_table($table_name);\r
- \r
- if (is_bool($sql))\r
- {\r
- return $sql;\r
- }\r
- \r
- $query = $this->db->query($sql);\r
- \r
- // Build the result array...\r
- // Note: Due to a bug in current() that affects some versions\r
- // of PHP we can not pass function call directly into it\r
- $res = $query->result_array();\r
- $res = current($res);\r
- $key = str_replace($this->db->database.'.', '', current($res));\r
- $keys = array_keys($res);\r
- unset($res[$keys[0]]);\r
- \r
- $result[$key] = $res;\r
- }\r
-\r
- return $result;\r
- }\r
-\r
- // --------------------------------------------------------------------\r
-\r
- /**\r
- * Repair Table\r
- *\r
- * @access public\r
- * @param string the table name\r
- * @return bool\r
- */\r
- function repair_table($table_name)\r
- {\r
- $sql = $this->_repair_table($table_name);\r
- \r
- if (is_bool($sql))\r
- {\r
- return $sql;\r
- }\r
- \r
- $query = $this->db->query($sql);\r
- \r
- // Note: Due to a bug in current() that affects some versions\r
- // of PHP we can not pass function call directly into it\r
- $res = $query->result_array();\r
- return current($res);\r
- }\r
- \r
- // --------------------------------------------------------------------\r
-\r
- /**\r
- * Generate CSV from a query result object\r
- *\r
- * @access public\r
- * @param object The query result object\r
- * @param string The delimiter - comma by default\r
- * @param string The newline character - \n by default\r
- * @param string The enclosure - double quote by default\r
- * @return string\r
- */\r
- function csv_from_result($query, $delim = ",", $newline = "\n", $enclosure = '"')\r
- {\r
- if ( ! is_object($query) OR ! method_exists($query, 'field_names'))\r
- {\r
- show_error('You must submit a valid result object');\r
- } \r
- \r
- $out = '';\r
- \r
- // First generate the headings from the table column names\r
- foreach ($query->list_fields() as $name)\r
- {\r
- $out .= $enclosure.str_replace($enclosure, $enclosure.$enclosure, $name).$enclosure.$delim;\r
- }\r
- \r
- $out = rtrim($out);\r
- $out .= $newline;\r
- \r
- // Next blast through the result array and build out the rows\r
- foreach ($query->result_array() as $row)\r
- {\r
- foreach ($row as $item)\r
- {\r
- $out .= $enclosure.str_replace($enclosure, $enclosure.$enclosure, $item).$enclosure.$delim; \r
- }\r
- $out = rtrim($out);\r
- $out .= $newline;\r
- }\r
-\r
- return $out;\r
- }\r
- \r
- // --------------------------------------------------------------------\r
-\r
- /**\r
- * Generate XML data from a query result object\r
- *\r
- * @access public\r
- * @param object The query result object\r
- * @param array Any preferences\r
- * @return string\r
- */\r
- function xml_from_result($query, $params = array())\r
- {\r
- if ( ! is_object($query) OR ! method_exists($query, 'field_names'))\r
- {\r
- show_error('You must submit a valid result object');\r
- }\r
- \r
- // Set our default values\r
- foreach (array('root' => 'root', 'element' => 'element', 'newline' => "\n", 'tab' => "\t") as $key => $val)\r
- {\r
- if ( ! isset($params[$key]))\r
- {\r
- $params[$key] = $val;\r
- }\r
- }\r
- \r
- // Create variables for convenience\r
- extract($params);\r
- \r
- // Load the xml helper\r
- $CI =& get_instance();\r
- $CI->load->helper('xml');\r
-\r
- // Generate the result\r
- $xml = "<{$root}>".$newline;\r
- foreach ($query->result_array() as $row)\r
- {\r
- $xml .= $tab."<{$element}>".$newline;\r
- \r
- foreach ($row as $key => $val)\r
- {\r
- $xml .= $tab.$tab."<{$key}>".xml_convert($val)."</{$key}>".$newline;\r
- }\r
- $xml .= $tab."</{$element}>".$newline;\r
- }\r
- $xml .= "</$root>".$newline;\r
- \r
- return $xml;\r
- }\r
-\r
- // --------------------------------------------------------------------\r
-\r
- /**\r
- * Database Backup\r
- *\r
- * @access public\r
- * @return void\r
- */\r
- function backup($params = array())\r
- {\r
- // If the parameters have not been submitted as an\r
- // array then we know that it is simply the table\r
- // name, which is a valid short cut.\r
- if (is_string($params))\r
- {\r
- $params = array('tables' => $params);\r
- }\r
- \r
- // ------------------------------------------------------\r
- \r
- // Set up our default preferences\r
- $prefs = array(\r
- 'tables' => array(),\r
- 'ignore' => array(),\r
- 'filename' => '',\r
- 'format' => 'gzip', // gzip, zip, txt\r
- 'add_drop' => TRUE,\r
- 'add_insert' => TRUE,\r
- 'newline' => "\n"\r
- );\r
-\r
- // Did the user submit any preferences? If so set them....\r
- if (count($params) > 0)\r
- {\r
- foreach ($prefs as $key => $val)\r
- {\r
- if (isset($params[$key]))\r
- {\r
- $prefs[$key] = $params[$key];\r
- }\r
- }\r
- }\r
-\r
- // ------------------------------------------------------\r
-\r
- // Are we backing up a complete database or individual tables? \r
- // If no table names were submitted we'll fetch the entire table list\r
- if (count($prefs['tables']) == 0)\r
- {\r
- $prefs['tables'] = $this->db->list_tables();\r
- }\r
- \r
- // ------------------------------------------------------\r
-\r
- // Validate the format\r
- if ( ! in_array($prefs['format'], array('gzip', 'zip', 'txt'), TRUE))\r
- {\r
- $prefs['format'] = 'txt';\r
- }\r
-\r
- // ------------------------------------------------------\r
-\r
- // Is the encoder supported? If not, we'll either issue an\r
- // error or use plain text depending on the debug settings\r
- if (($prefs['format'] == 'gzip' AND ! @function_exists('gzencode'))\r
- OR ($prefs['format'] == 'zip' AND ! @function_exists('gzcompress')))\r
- {\r
- if ($this->db->db_debug)\r
- {\r
- return $this->db->display_error('db_unsuported_compression');\r
- }\r
- \r
- $prefs['format'] = 'txt';\r
- }\r
-\r
- // ------------------------------------------------------\r
-\r
- // Set the filename if not provided - Only needed with Zip files\r
- if ($prefs['filename'] == '' AND $prefs['format'] == 'zip')\r
- {\r
- $prefs['filename'] = (count($prefs['tables']) == 1) ? $prefs['tables'] : $this->db->database;\r
- $prefs['filename'] .= '_'.date('Y-m-d_H-i', time());\r
- }\r
-\r
- // ------------------------------------------------------\r
- \r
- // Was a Gzip file requested?\r
- if ($prefs['format'] == 'gzip')\r
- {\r
- return gzencode($this->_backup($prefs));\r
- }\r
-\r
- // ------------------------------------------------------\r
- \r
- // Was a text file requested?\r
- if ($prefs['format'] == 'txt')\r
- {\r
- return $this->_backup($prefs);\r
- }\r
-\r
- // ------------------------------------------------------\r
-\r
- // Was a Zip file requested? \r
- if ($prefs['format'] == 'zip')\r
- {\r
- // If they included the .zip file extension we'll remove it\r
- if (preg_match("|.+?\.zip$|", $prefs['filename']))\r
- {\r
- $prefs['filename'] = str_replace('.zip', '', $prefs['filename']);\r
- }\r
- \r
- // Tack on the ".sql" file extension if needed\r
- if ( ! preg_match("|.+?\.sql$|", $prefs['filename']))\r
- {\r
- $prefs['filename'] .= '.sql';\r
- }\r
-\r
- // Load the Zip class and output it\r
- \r
- $CI =& get_instance();\r
- $CI->load->library('zip');\r
- $CI->zip->add_data($prefs['filename'], $this->_backup($prefs)); \r
- return $CI->zip->get_zip();\r
- }\r
- \r
- }\r
-\r
-}\r
-\r
-\r
-/* End of file DB_utility.php */\r
+<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
+/**
+ * Code Igniter
+ *
+ * An open source application development framework for PHP 4.3.2 or newer
+ *
+ * @package CodeIgniter
+ * @author ExpressionEngine Dev Team
+ * @copyright Copyright (c) 2008 - 2009, EllisLab, Inc.
+ * @license http://codeigniter.com/user_guide/license.html
+ * @link http://codeigniter.com
+ * @since Version 1.0
+ * @filesource
+ */
+
+// ------------------------------------------------------------------------
+
+/**
+ * Database Utility Class
+ *
+ * @category Database
+ * @author ExpressionEngine Dev Team
+ * @link http://codeigniter.com/user_guide/database/
+ */
+class CI_DB_utility extends CI_DB_forge {
+
+ var $db;
+ var $data_cache = array();
+
+ /**
+ * Constructor
+ *
+ * Grabs the CI super object instance so we can access it.
+ *
+ */
+ function CI_DB_utility()
+ {
+ // Assign the main database object to $this->db
+ $CI =& get_instance();
+ $this->db =& $CI->db;
+
+ log_message('debug', "Database Utility Class Initialized");
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * List databases
+ *
+ * @access public
+ * @return bool
+ */
+ function list_databases()
+ {
+ // Is there a cached result?
+ if (isset($this->data_cache['db_names']))
+ {
+ return $this->data_cache['db_names'];
+ }
+
+ $query = $this->db->query($this->_list_databases());
+ $dbs = array();
+ if ($query->num_rows() > 0)
+ {
+ foreach ($query->result_array() as $row)
+ {
+ $dbs[] = current($row);
+ }
+ }
+
+ $this->data_cache['db_names'] = $dbs;
+ return $this->data_cache['db_names'];
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Optimize Table
+ *
+ * @access public
+ * @param string the table name
+ * @return bool
+ */
+ function optimize_table($table_name)
+ {
+ $sql = $this->_optimize_table($table_name);
+
+ if (is_bool($sql))
+ {
+ show_error('db_must_use_set');
+ }
+
+ $query = $this->db->query($sql);
+ $res = $query->result_array();
+
+ // Note: Due to a bug in current() that affects some versions
+ // of PHP we can not pass function call directly into it
+ return current($res);
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Optimize Database
+ *
+ * @access public
+ * @return array
+ */
+ function optimize_database()
+ {
+ $result = array();
+ foreach ($this->db->list_tables() as $table_name)
+ {
+ $sql = $this->_optimize_table($table_name);
+
+ if (is_bool($sql))
+ {
+ return $sql;
+ }
+
+ $query = $this->db->query($sql);
+
+ // Build the result array...
+ // Note: Due to a bug in current() that affects some versions
+ // of PHP we can not pass function call directly into it
+ $res = $query->result_array();
+ $res = current($res);
+ $key = str_replace($this->db->database.'.', '', current($res));
+ $keys = array_keys($res);
+ unset($res[$keys[0]]);
+
+ $result[$key] = $res;
+ }
+
+ return $result;
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Repair Table
+ *
+ * @access public
+ * @param string the table name
+ * @return bool
+ */
+ function repair_table($table_name)
+ {
+ $sql = $this->_repair_table($table_name);
+
+ if (is_bool($sql))
+ {
+ return $sql;
+ }
+
+ $query = $this->db->query($sql);
+
+ // Note: Due to a bug in current() that affects some versions
+ // of PHP we can not pass function call directly into it
+ $res = $query->result_array();
+ return current($res);
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Generate CSV from a query result object
+ *
+ * @access public
+ * @param object The query result object
+ * @param string The delimiter - comma by default
+ * @param string The newline character - \n by default
+ * @param string The enclosure - double quote by default
+ * @return string
+ */
+ function csv_from_result($query, $delim = ",", $newline = "\n", $enclosure = '"')
+ {
+ if ( ! is_object($query) OR ! method_exists($query, 'list_fields'))
+ {
+ show_error('You must submit a valid result object');
+ }
+
+ $out = '';
+
+ // First generate the headings from the table column names
+ foreach ($query->list_fields() as $name)
+ {
+ $out .= $enclosure.str_replace($enclosure, $enclosure.$enclosure, $name).$enclosure.$delim;
+ }
+
+ $out = rtrim($out);
+ $out .= $newline;
+
+ // Next blast through the result array and build out the rows
+ foreach ($query->result_array() as $row)
+ {
+ foreach ($row as $item)
+ {
+ $out .= $enclosure.str_replace($enclosure, $enclosure.$enclosure, $item).$enclosure.$delim;
+ }
+ $out = rtrim($out);
+ $out .= $newline;
+ }
+
+ return $out;
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Generate XML data from a query result object
+ *
+ * @access public
+ * @param object The query result object
+ * @param array Any preferences
+ * @return string
+ */
+ function xml_from_result($query, $params = array())
+ {
+ if ( ! is_object($query) OR ! method_exists($query, 'list_fields'))
+ {
+ show_error('You must submit a valid result object');
+ }
+
+ // Set our default values
+ foreach (array('root' => 'root', 'element' => 'element', 'newline' => "\n", 'tab' => "\t") as $key => $val)
+ {
+ if ( ! isset($params[$key]))
+ {
+ $params[$key] = $val;
+ }
+ }
+
+ // Create variables for convenience
+ extract($params);
+
+ // Load the xml helper
+ $CI =& get_instance();
+ $CI->load->helper('xml');
+
+ // Generate the result
+ $xml = "<{$root}>".$newline;
+ foreach ($query->result_array() as $row)
+ {
+ $xml .= $tab."<{$element}>".$newline;
+
+ foreach ($row as $key => $val)
+ {
+ $xml .= $tab.$tab."<{$key}>".xml_convert($val)."</{$key}>".$newline;
+ }
+ $xml .= $tab."</{$element}>".$newline;
+ }
+ $xml .= "</$root>".$newline;
+
+ return $xml;
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Database Backup
+ *
+ * @access public
+ * @return void
+ */
+ function backup($params = array())
+ {
+ // If the parameters have not been submitted as an
+ // array then we know that it is simply the table
+ // name, which is a valid short cut.
+ if (is_string($params))
+ {
+ $params = array('tables' => $params);
+ }
+
+ // ------------------------------------------------------
+
+ // Set up our default preferences
+ $prefs = array(
+ 'tables' => array(),
+ 'ignore' => array(),
+ 'filename' => '',
+ 'format' => 'gzip', // gzip, zip, txt
+ 'add_drop' => TRUE,
+ 'add_insert' => TRUE,
+ 'newline' => "\n"
+ );
+
+ // Did the user submit any preferences? If so set them....
+ if (count($params) > 0)
+ {
+ foreach ($prefs as $key => $val)
+ {
+ if (isset($params[$key]))
+ {
+ $prefs[$key] = $params[$key];
+ }
+ }
+ }
+
+ // ------------------------------------------------------
+
+ // Are we backing up a complete database or individual tables?
+ // If no table names were submitted we'll fetch the entire table list
+ if (count($prefs['tables']) == 0)
+ {
+ $prefs['tables'] = $this->db->list_tables();
+ }
+
+ // ------------------------------------------------------
+
+ // Validate the format
+ if ( ! in_array($prefs['format'], array('gzip', 'zip', 'txt'), TRUE))
+ {
+ $prefs['format'] = 'txt';
+ }
+
+ // ------------------------------------------------------
+
+ // Is the encoder supported? If not, we'll either issue an
+ // error or use plain text depending on the debug settings
+ if (($prefs['format'] == 'gzip' AND ! @function_exists('gzencode'))
+ OR ($prefs['format'] == 'zip' AND ! @function_exists('gzcompress')))
+ {
+ if ($this->db->db_debug)
+ {
+ return $this->db->display_error('db_unsuported_compression');
+ }
+
+ $prefs['format'] = 'txt';
+ }
+
+ // ------------------------------------------------------
+
+ // Set the filename if not provided - Only needed with Zip files
+ if ($prefs['filename'] == '' AND $prefs['format'] == 'zip')
+ {
+ $prefs['filename'] = (count($prefs['tables']) == 1) ? $prefs['tables'] : $this->db->database;
+ $prefs['filename'] .= '_'.date('Y-m-d_H-i', time());
+ }
+
+ // ------------------------------------------------------
+
+ // Was a Gzip file requested?
+ if ($prefs['format'] == 'gzip')
+ {
+ return gzencode($this->_backup($prefs));
+ }
+
+ // ------------------------------------------------------
+
+ // Was a text file requested?
+ if ($prefs['format'] == 'txt')
+ {
+ return $this->_backup($prefs);
+ }
+
+ // ------------------------------------------------------
+
+ // Was a Zip file requested?
+ if ($prefs['format'] == 'zip')
+ {
+ // If they included the .zip file extension we'll remove it
+ if (preg_match("|.+?\.zip$|", $prefs['filename']))
+ {
+ $prefs['filename'] = str_replace('.zip', '', $prefs['filename']);
+ }
+
+ // Tack on the ".sql" file extension if needed
+ if ( ! preg_match("|.+?\.sql$|", $prefs['filename']))
+ {
+ $prefs['filename'] .= '.sql';
+ }
+
+ // Load the Zip class and output it
+
+ $CI =& get_instance();
+ $CI->load->library('zip');
+ $CI->zip->add_data($prefs['filename'], $this->_backup($prefs));
+ return $CI->zip->get_zip();
+ }
+
+ }
+
+}
+
+
+/* End of file DB_utility.php */