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