Subversion Repositories Applications.papyrus

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2005 Aurelien 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 4.3.2 or newer
6
 *
7
 * @package		CodeIgniter
8
 * @author		ExpressionEngine Dev Team
9
 * @copyright	Copyright (c) 2008, 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 CI_DB_utility()
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
	 * Optimize Table
79
	 *
80
	 * @access	public
81
	 * @param	string	the table name
82
	 * @return	bool
83
	 */
84
	function optimize_table($table_name)
85
	{
86
		$sql = $this->_optimize_table($table_name);
87
88
		if (is_bool($sql))
89
		{
90
				show_error('db_must_use_set');
91
		}
92
93
		$query = $this->db->query($sql);
94
		$res = $query->result_array();
95
96
		// Note: Due to a bug in current() that affects some versions
97
		// of PHP we can not pass function call directly into it
98
		return current($res);
99
	}
100
101
	// --------------------------------------------------------------------
102
103
	/**
104
	 * Optimize Database
105
	 *
106
	 * @access	public
107
	 * @return	array
108
	 */
109
	function optimize_database()
110
	{
111
		$result = array();
112
		foreach ($this->db->list_tables() as $table_name)
113
		{
114
			$sql = $this->_optimize_table($table_name);
115
116
			if (is_bool($sql))
117
			{
118
				return $sql;
119
			}
120
121
			$query = $this->db->query($sql);
122
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]]);
131
132
			$result[$key] = $res;
133
		}
134
135
		return $result;
136
	}
137
138
	// --------------------------------------------------------------------
139
140
	/**
141
	 * Repair Table
142
	 *
143
	 * @access	public
144
	 * @param	string	the table name
145
	 * @return	bool
146
	 */
147
	function repair_table($table_name)
148
	{
149
		$sql = $this->_repair_table($table_name);
150
151
		if (is_bool($sql))
152
		{
153
			return $sql;
154
		}
155
156
		$query = $this->db->query($sql);
157
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);
162
	}
163
164
	// --------------------------------------------------------------------
165
166
	/**
167
	 * Generate CSV from a query result object
168
	 *
169
	 * @access	public
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
174
	 * @return	string
175
	 */
176
	function csv_from_result($query, $delim = ",", $newline = "\n", $enclosure = '"')
177
	{
178
		if ( ! is_object($query) OR ! method_exists($query, 'field_names'))
179
		{
180
			show_error('You must submit a valid result object');
181
		}
182
183
		$out = '';
184
185
		// First generate the headings from the table column names
186
		foreach ($query->list_fields() as $name)
187
		{
188
			$out .= $enclosure.str_replace($enclosure, $enclosure.$enclosure, $name).$enclosure.$delim;
189
		}
190
191
		$out = rtrim($out);
192
		$out .= $newline;
193
194
		// Next blast through the result array and build out the rows
195
		foreach ($query->result_array() as $row)
196
		{
197
			foreach ($row as $item)
198
			{
199
				$out .= $enclosure.str_replace($enclosure, $enclosure.$enclosure, $item).$enclosure.$delim;
200
			}
201
			$out = rtrim($out);
202
			$out .= $newline;
203
		}
204
205
		return $out;
206
	}
207
208
	// --------------------------------------------------------------------
209
210
	/**
211
	 * Generate XML data from a query result object
212
	 *
213
	 * @access	public
214
	 * @param	object	The query result object
215
	 * @param	array	Any preferences
216
	 * @return	string
217
	 */
218
	function xml_from_result($query, $params = array())
219
	{
220
		if ( ! is_object($query) OR ! method_exists($query, 'field_names'))
221
		{
222
			show_error('You must submit a valid result object');
223
		}
224
225
		// Set our default values
226
		foreach (array('root' => 'root', 'element' => 'element', 'newline' => "\n", 'tab' => "\t") as $key => $val)
227
		{
228
			if ( ! isset($params[$key]))
229
			{
230
				$params[$key] = $val;
231
			}
232
		}
233
234
		// Create variables for convenience
235
		extract($params);
236
237
		// Load the xml helper
238
		$CI =& get_instance();
239
		$CI->load->helper('xml');
240
241
		// Generate the result
242
		$xml = "<{$root}>".$newline;
243
		foreach ($query->result_array() as $row)
244
		{
245
			$xml .= $tab."<{$element}>".$newline;
246
247
			foreach ($row as $key => $val)
248
			{
249
				$xml .= $tab.$tab."<{$key}>".xml_convert($val)."</{$key}>".$newline;
250
			}
251
			$xml .= $tab."</{$element}>".$newline;
252
		}
253
		$xml .= "</$root>".$newline;
254
255
		return $xml;
256
	}
257
258
	// --------------------------------------------------------------------
259
260
	/**
261
	 * Database Backup
262
	 *
263
	 * @access	public
264
	 * @return	void
265
	 */
266
	function backup($params = array())
267
	{
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))
272
		{
273
			$params = array('tables' => $params);
274
		}
275
276
		// ------------------------------------------------------
277
278
		// Set up our default preferences
279
		$prefs = array(
280
							'tables'		=> array(),
281
							'ignore'		=> array(),
282
							'filename'		=> '',
283
							'format'		=> 'gzip', // gzip, zip, txt
284
							'add_drop'		=> TRUE,
285
							'add_insert'	=> TRUE,
286
							'newline'		=> "\n"
287
						);
288
289
		// Did the user submit any preferences? If so set them....
290
		if (count($params) > 0)
291
		{
292
			foreach ($prefs as $key => $val)
293
			{
294
				if (isset($params[$key]))
295
				{
296
					$prefs[$key] = $params[$key];
297
				}
298
			}
299
		}
300
301
		// ------------------------------------------------------
302
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)
306
		{
307
			$prefs['tables'] = $this->db->list_tables();
308
		}
309
310
		// ------------------------------------------------------
311
312
		// Validate the format
313
		if ( ! in_array($prefs['format'], array('gzip', 'zip', 'txt'), TRUE))
314
		{
315
			$prefs['format'] = 'txt';
316
		}
317
318
		// ------------------------------------------------------
319
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')))
324
		{
325
			if ($this->db->db_debug)
326
			{
327
				return $this->db->display_error('db_unsuported_compression');
328
			}
329
330
			$prefs['format'] = 'txt';
331
		}
332
333
		// ------------------------------------------------------
334
335
		// Set the filename if not provided - Only needed with Zip files
336
		if ($prefs['filename'] == '' AND $prefs['format'] == 'zip')
337
		{
338
			$prefs['filename'] = (count($prefs['tables']) == 1) ? $prefs['tables'] : $this->db->database;
339
			$prefs['filename'] .= '_'.date('Y-m-d_H-i', time());
340
		}
341
342
		// ------------------------------------------------------
343
344
		// Was a Gzip file requested?
345
		if ($prefs['format'] == 'gzip')
346
		{
347
			return gzencode($this->_backup($prefs));
348
		}
349
350
		// ------------------------------------------------------
351
352
		// Was a text file requested?
353
		if ($prefs['format'] == 'txt')
354
		{
355
			return $this->_backup($prefs);
356
		}
357
358
		// ------------------------------------------------------
359
360
		// Was a Zip file requested?
361
		if ($prefs['format'] == 'zip')
362
		{
363
			// If they included the .zip file extension we'll remove it
364
			if (preg_match("|.+?\.zip$|", $prefs['filename']))
365
			{
366
				$prefs['filename'] = str_replace('.zip', '', $prefs['filename']);
367
			}
368
369
			// Tack on the ".sql" file extension if needed
370
			if ( ! preg_match("|.+?\.sql$|", $prefs['filename']))
371
			{
372
				$prefs['filename'] .= '.sql';
373
			}
374
375
			// Load the Zip class and output it
376
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();
381
		}
382
383
	}
384
385
}
386
387
388
/* End of file DB_utility.php */
389
/* Location: ./system/database/DB_utility.php */