Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2 | /** |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 3 | * CodeIgniter |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 4 | * |
Phil Sturgeon | 07c1ac8 | 2012-03-09 17:03:37 +0000 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.2.4 or newer |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 7 | * NOTICE OF LICENSE |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 8 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 9 | * Licensed under the Open Software License version 3.0 |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 10 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 11 | * This source file is subject to the Open Software License (OSL 3.0) that is |
| 12 | * bundled with this package in the files license.txt / license.rst. It is |
| 13 | * also available through the world wide web at this URL: |
| 14 | * http://opensource.org/licenses/OSL-3.0 |
| 15 | * If you did not receive a copy of the license and are unable to obtain it |
| 16 | * through the world wide web, please send an email to |
| 17 | * licensing@ellislab.com so we can send you a copy immediately. |
| 18 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 19 | * @package CodeIgniter |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 20 | * @author EllisLab Dev Team |
Greg Aker | 0defe5d | 2012-01-01 18:46:41 -0600 | [diff] [blame] | 21 | * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/) |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 22 | * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 23 | * @link http://codeigniter.com |
| 24 | * @since Version 1.0 |
| 25 | * @filesource |
| 26 | */ |
| 27 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 28 | /** |
| 29 | * Database Utility Class |
| 30 | * |
| 31 | * @category Database |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 32 | * @author EllisLab Dev Team |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 33 | * @link http://codeigniter.com/user_guide/database/ |
| 34 | */ |
Timothy Warren | 833d504 | 2012-03-19 16:12:03 -0400 | [diff] [blame] | 35 | abstract class CI_DB_utility extends CI_DB_forge { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 36 | |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 37 | public $db; |
| 38 | public $data_cache = array(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 39 | |
Andrey Andreev | b457a40 | 2012-04-09 16:11:56 +0300 | [diff] [blame^] | 40 | // Platform specific SQL strings |
| 41 | // Just setting those defaults to FALSE as they are mostly MySQL-specific |
| 42 | protected $_optimize_table = FALSE; |
| 43 | protected $_repair_table = FALSE; |
| 44 | |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 45 | public function __construct() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 46 | { |
| 47 | // Assign the main database object to $this->db |
| 48 | $CI =& get_instance(); |
| 49 | $this->db =& $CI->db; |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 50 | log_message('debug', 'Database Utility Class Initialized'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | // -------------------------------------------------------------------- |
| 54 | |
| 55 | /** |
| 56 | * List databases |
| 57 | * |
Andrey Andreev | b457a40 | 2012-04-09 16:11:56 +0300 | [diff] [blame^] | 58 | * @return array |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 59 | */ |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 60 | public function list_databases() |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 61 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 62 | // Is there a cached result? |
| 63 | if (isset($this->data_cache['db_names'])) |
| 64 | { |
| 65 | return $this->data_cache['db_names']; |
| 66 | } |
Andrey Andreev | b457a40 | 2012-04-09 16:11:56 +0300 | [diff] [blame^] | 67 | elseif ($this->_list_databases === FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 68 | { |
Andrey Andreev | b457a40 | 2012-04-09 16:11:56 +0300 | [diff] [blame^] | 69 | return ($this->db->db_debug) ? $this->db->display_error('db_unsuported_feature') : FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 70 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 71 | |
Andrey Andreev | b457a40 | 2012-04-09 16:11:56 +0300 | [diff] [blame^] | 72 | $this->data_cache['db_names'] = array(); |
| 73 | |
| 74 | $query = $this->db->query($this->_list_databases); |
| 75 | if ($query === FALSE) |
| 76 | { |
| 77 | return $this->data_cache['db_names']; |
| 78 | } |
| 79 | |
| 80 | for ($i = 0, $c = count($query); $i < $c; $i++) |
| 81 | { |
| 82 | $this->data_cache['db_names'] = current($query[$i]); |
| 83 | } |
| 84 | |
| 85 | return $this->data_cache['db_names']; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | // -------------------------------------------------------------------- |
| 89 | |
| 90 | /** |
Derek Allard | e7f0325 | 2010-02-04 16:47:01 +0000 | [diff] [blame] | 91 | * Determine if a particular database exists |
| 92 | * |
Derek Allard | e7f0325 | 2010-02-04 16:47:01 +0000 | [diff] [blame] | 93 | * @param string |
Andrey Andreev | b457a40 | 2012-04-09 16:11:56 +0300 | [diff] [blame^] | 94 | * @return bool |
Derek Allard | e7f0325 | 2010-02-04 16:47:01 +0000 | [diff] [blame] | 95 | */ |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 96 | public function database_exists($database_name) |
Derek Allard | 6239623 | 2010-02-04 17:45:47 +0000 | [diff] [blame] | 97 | { |
Andrey Andreev | b457a40 | 2012-04-09 16:11:56 +0300 | [diff] [blame^] | 98 | return in_array($database_name, $this->list_databases()); |
Derek Allard | e7f0325 | 2010-02-04 16:47:01 +0000 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | // -------------------------------------------------------------------- |
| 102 | |
| 103 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 104 | * Optimize Table |
| 105 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 106 | * @param string the table name |
Andrey Andreev | b457a40 | 2012-04-09 16:11:56 +0300 | [diff] [blame^] | 107 | * @return mixed |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 108 | */ |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 109 | public function optimize_table($table_name) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 110 | { |
Andrey Andreev | b457a40 | 2012-04-09 16:11:56 +0300 | [diff] [blame^] | 111 | if ($this->_optimize_table === FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 112 | { |
Andrey Andreev | b457a40 | 2012-04-09 16:11:56 +0300 | [diff] [blame^] | 113 | return ($this->db->db_debug) ? $this->db->display_error('db_unsuported_feature') : FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 114 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 115 | |
Andrey Andreev | b457a40 | 2012-04-09 16:11:56 +0300 | [diff] [blame^] | 116 | $query = $this->db->query(sprintf($this->_optimize_table, $this->db->escape_identifiers($table_name))); |
| 117 | if ($query !== FALSE) |
| 118 | { |
| 119 | $query = $query->result_array(); |
| 120 | return current($res); |
| 121 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 122 | |
Andrey Andreev | b457a40 | 2012-04-09 16:11:56 +0300 | [diff] [blame^] | 123 | return FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | // -------------------------------------------------------------------- |
| 127 | |
| 128 | /** |
| 129 | * Optimize Database |
| 130 | * |
Andrey Andreev | b457a40 | 2012-04-09 16:11:56 +0300 | [diff] [blame^] | 131 | * @return mixed |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 132 | */ |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 133 | public function optimize_database() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 134 | { |
Andrey Andreev | b457a40 | 2012-04-09 16:11:56 +0300 | [diff] [blame^] | 135 | if ($this->_optimize_table === FALSE) |
| 136 | { |
| 137 | return ($this->db->db_debug) ? $this->db->display_error('db_unsuported_feature') : FALSE; |
| 138 | } |
| 139 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 140 | $result = array(); |
| 141 | foreach ($this->db->list_tables() as $table_name) |
| 142 | { |
Andrey Andreev | b457a40 | 2012-04-09 16:11:56 +0300 | [diff] [blame^] | 143 | $res = $this->db->query(sprintf($this->_optimize_table, $this->db->escape_identifiers($table_name))); |
| 144 | if (is_bool($res)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 145 | { |
Andrey Andreev | b457a40 | 2012-04-09 16:11:56 +0300 | [diff] [blame^] | 146 | return $res; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 147 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 148 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 149 | // Build the result array... |
Andrey Andreev | b457a40 | 2012-04-09 16:11:56 +0300 | [diff] [blame^] | 150 | $res = $res->result_array(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 151 | $res = current($res); |
| 152 | $key = str_replace($this->db->database.'.', '', current($res)); |
| 153 | $keys = array_keys($res); |
| 154 | unset($res[$keys[0]]); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 155 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 156 | $result[$key] = $res; |
| 157 | } |
| 158 | |
| 159 | return $result; |
| 160 | } |
| 161 | |
| 162 | // -------------------------------------------------------------------- |
| 163 | |
| 164 | /** |
| 165 | * Repair Table |
| 166 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 167 | * @param string the table name |
Andrey Andreev | b457a40 | 2012-04-09 16:11:56 +0300 | [diff] [blame^] | 168 | * @return mixed |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 169 | */ |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 170 | public function repair_table($table_name) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 171 | { |
Andrey Andreev | b457a40 | 2012-04-09 16:11:56 +0300 | [diff] [blame^] | 172 | if ($this->_repair_table === FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 173 | { |
Andrey Andreev | b457a40 | 2012-04-09 16:11:56 +0300 | [diff] [blame^] | 174 | return ($this->db->db_debug) ? $this->db->display_error('db_unsuported_feature') : FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 175 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 176 | |
Andrey Andreev | b457a40 | 2012-04-09 16:11:56 +0300 | [diff] [blame^] | 177 | $query = $this->db->query(sprintf($this->_repair_table, $this->db->escape_identifiers($table_name))); |
| 178 | if (is_bool($query)) |
| 179 | { |
| 180 | return $query; |
| 181 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 182 | |
Andrey Andreev | b457a40 | 2012-04-09 16:11:56 +0300 | [diff] [blame^] | 183 | $query = $query->result_array(); |
| 184 | return current($query); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 185 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 186 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 187 | // -------------------------------------------------------------------- |
| 188 | |
| 189 | /** |
| 190 | * Generate CSV from a query result object |
| 191 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 192 | * @param object The query result object |
| 193 | * @param string The delimiter - comma by default |
| 194 | * @param string The newline character - \n by default |
| 195 | * @param string The enclosure - double quote by default |
| 196 | * @return string |
| 197 | */ |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 198 | public function csv_from_result($query, $delim = ',', $newline = "\n", $enclosure = '"') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 199 | { |
Derek Jones | 4787413 | 2009-02-10 17:32:15 +0000 | [diff] [blame] | 200 | if ( ! is_object($query) OR ! method_exists($query, 'list_fields')) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 201 | { |
| 202 | show_error('You must submit a valid result object'); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 203 | } |
| 204 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 205 | $out = ''; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 206 | // First generate the headings from the table column names |
| 207 | foreach ($query->list_fields() as $name) |
| 208 | { |
| 209 | $out .= $enclosure.str_replace($enclosure, $enclosure.$enclosure, $name).$enclosure.$delim; |
| 210 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 211 | |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 212 | $out = rtrim($out).$newline; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 213 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 214 | // Next blast through the result array and build out the rows |
| 215 | foreach ($query->result_array() as $row) |
| 216 | { |
| 217 | foreach ($row as $item) |
| 218 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 219 | $out .= $enclosure.str_replace($enclosure, $enclosure.$enclosure, $item).$enclosure.$delim; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 220 | } |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 221 | $out = rtrim($out).$newline; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | return $out; |
| 225 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 226 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 227 | // -------------------------------------------------------------------- |
| 228 | |
| 229 | /** |
| 230 | * Generate XML data from a query result object |
| 231 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 232 | * @param object The query result object |
| 233 | * @param array Any preferences |
| 234 | * @return string |
| 235 | */ |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 236 | public function xml_from_result($query, $params = array()) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 237 | { |
Pascal Kriete | 69b1fcc | 2009-08-24 16:42:52 +0000 | [diff] [blame] | 238 | if ( ! is_object($query) OR ! method_exists($query, 'list_fields')) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 239 | { |
| 240 | show_error('You must submit a valid result object'); |
| 241 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 242 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 243 | // Set our default values |
| 244 | foreach (array('root' => 'root', 'element' => 'element', 'newline' => "\n", 'tab' => "\t") as $key => $val) |
| 245 | { |
| 246 | if ( ! isset($params[$key])) |
| 247 | { |
| 248 | $params[$key] = $val; |
| 249 | } |
| 250 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 251 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 252 | // Create variables for convenience |
| 253 | extract($params); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 254 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 255 | // Load the xml helper |
| 256 | $CI =& get_instance(); |
| 257 | $CI->load->helper('xml'); |
| 258 | |
| 259 | // Generate the result |
Andrey Andreev | b457a40 | 2012-04-09 16:11:56 +0300 | [diff] [blame^] | 260 | $xml = '<'.$root.'>'.$newline; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 261 | foreach ($query->result_array() as $row) |
| 262 | { |
Andrey Andreev | b457a40 | 2012-04-09 16:11:56 +0300 | [diff] [blame^] | 263 | $xml .= $tab.'<'.$element.'>'.$newline; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 264 | foreach ($row as $key => $val) |
| 265 | { |
Andrey Andreev | b457a40 | 2012-04-09 16:11:56 +0300 | [diff] [blame^] | 266 | $xml .= $tab.$tab.'<'.$key.'>'.xml_convert($val).'</'.$key.'>'.$newline; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 267 | } |
Andrey Andreev | b457a40 | 2012-04-09 16:11:56 +0300 | [diff] [blame^] | 268 | $xml .= $tab.'</'.$element.'>'.$newline; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 269 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 270 | |
Andrey Andreev | b457a40 | 2012-04-09 16:11:56 +0300 | [diff] [blame^] | 271 | return $xml.'</'.$root.'>'.$newline; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | // -------------------------------------------------------------------- |
| 275 | |
| 276 | /** |
| 277 | * Database Backup |
| 278 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 279 | * @return void |
| 280 | */ |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 281 | public function backup($params = array()) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 282 | { |
| 283 | // If the parameters have not been submitted as an |
| 284 | // array then we know that it is simply the table |
| 285 | // name, which is a valid short cut. |
| 286 | if (is_string($params)) |
| 287 | { |
| 288 | $params = array('tables' => $params); |
| 289 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 290 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 291 | // ------------------------------------------------------ |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 292 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 293 | // Set up our default preferences |
| 294 | $prefs = array( |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 295 | 'tables' => array(), |
| 296 | 'ignore' => array(), |
| 297 | 'filename' => '', |
| 298 | 'format' => 'gzip', // gzip, zip, txt |
| 299 | 'add_drop' => TRUE, |
| 300 | 'add_insert' => TRUE, |
| 301 | 'newline' => "\n" |
| 302 | ); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 303 | |
| 304 | // Did the user submit any preferences? If so set them.... |
| 305 | if (count($params) > 0) |
| 306 | { |
| 307 | foreach ($prefs as $key => $val) |
| 308 | { |
| 309 | if (isset($params[$key])) |
| 310 | { |
| 311 | $prefs[$key] = $params[$key]; |
| 312 | } |
| 313 | } |
| 314 | } |
| 315 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 316 | // Are we backing up a complete database or individual tables? |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 317 | // If no table names were submitted we'll fetch the entire table list |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 318 | if (count($prefs['tables']) === 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 319 | { |
| 320 | $prefs['tables'] = $this->db->list_tables(); |
| 321 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 322 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 323 | // Validate the format |
| 324 | if ( ! in_array($prefs['format'], array('gzip', 'zip', 'txt'), TRUE)) |
| 325 | { |
| 326 | $prefs['format'] = 'txt'; |
| 327 | } |
| 328 | |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 329 | // Is the encoder supported? If not, we'll either issue an |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 330 | // error or use plain text depending on the debug settings |
Andrey Andreev | b457a40 | 2012-04-09 16:11:56 +0300 | [diff] [blame^] | 331 | if (($prefs['format'] === 'gzip' && ! @function_exists('gzencode')) |
| 332 | OR ($prefs['format'] === 'zip' && ! @function_exists('gzcompress'))) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 333 | { |
| 334 | if ($this->db->db_debug) |
| 335 | { |
| 336 | return $this->db->display_error('db_unsuported_compression'); |
| 337 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 338 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 339 | $prefs['format'] = 'txt'; |
| 340 | } |
| 341 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 342 | // Was a Zip file requested? |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 343 | if ($prefs['format'] === 'zip') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 344 | { |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 345 | // Set the filename if not provided (only needed with Zip files) |
| 346 | if ($prefs['filename'] == '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 347 | { |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 348 | $prefs['filename'] = (count($prefs['tables']) === 1 ? $prefs['tables'] : $this->db->database) |
| 349 | .date('Y-m-d_H-i', time()).'.sql'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 350 | } |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 351 | else |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 352 | { |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 353 | // If they included the .zip file extension we'll remove it |
| 354 | if (preg_match('|.+?\.zip$|', $prefs['filename'])) |
| 355 | { |
| 356 | $prefs['filename'] = str_replace('.zip', '', $prefs['filename']); |
| 357 | } |
| 358 | |
| 359 | // Tack on the ".sql" file extension if needed |
| 360 | if ( ! preg_match('|.+?\.sql$|', $prefs['filename'])) |
| 361 | { |
| 362 | $prefs['filename'] .= '.sql'; |
| 363 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | // Load the Zip class and output it |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 367 | $CI =& get_instance(); |
| 368 | $CI->load->library('zip'); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 369 | $CI->zip->add_data($prefs['filename'], $this->_backup($prefs)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 370 | return $CI->zip->get_zip(); |
| 371 | } |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 372 | elseif ($prefs['format'] == 'txt') // Was a text file requested? |
| 373 | { |
| 374 | return $this->_backup($prefs); |
| 375 | } |
| 376 | elseif ($prefs['format'] === 'gzip') // Was a Gzip file requested? |
| 377 | { |
| 378 | return gzencode($this->_backup($prefs)); |
| 379 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 380 | |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 381 | return; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | } |
| 385 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 386 | /* End of file DB_utility.php */ |
Timothy Warren | 215890b | 2012-03-20 09:38:16 -0400 | [diff] [blame] | 387 | /* Location: ./system/database/DB_utility.php */ |