| [ Index ] |
PHP Cross Reference of Joomla 2.5.4 DE |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * @package Joomla.Platform 4 * @subpackage Database 5 * 6 * @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved. 7 * @license GNU General Public License version 2 or later; see LICENSE 8 */ 9 10 defined('JPATH_PLATFORM') or die; 11 12 /** 13 * MySQL export driver. 14 * 15 * @package Joomla.Platform 16 * @subpackage Database 17 * @since 11.1 18 */ 19 class JDatabaseExporterMySQL 20 { 21 /** 22 * An array of cached data. 23 * 24 * @var array 25 * @since 11.1 26 */ 27 protected $cache = array(); 28 29 /** 30 * The database connector to use for exporting structure and/or data. 31 * 32 * @var JDatabaseMySQL 33 * @since 11.1 34 */ 35 protected $db = null; 36 37 /** 38 * An array input sources (table names). 39 * 40 * @var array 41 * @since 11.1 42 */ 43 protected $from = array(); 44 45 /** 46 * The type of output format (xml). 47 * 48 * @var string 49 * @since 11.1 50 */ 51 protected $asFormat = 'xml'; 52 53 /** 54 * An array of options for the exporter. 55 * 56 * @var JObject 57 * @since 11.1 58 */ 59 protected $options = null; 60 61 /** 62 * Constructor. 63 * 64 * Sets up the default options for the exporter. 65 * 66 * @since 11.1 67 */ 68 public function __construct() 69 { 70 $this->options = new JObject; 71 72 $this->cache = array('columns' => array(), 'keys' => array()); 73 74 // Set up the class defaults: 75 76 // Export with only structure 77 $this->withStructure(); 78 79 // Export as xml. 80 $this->asXml(); 81 82 // Default destination is a string using $output = (string) $exporter; 83 } 84 85 /** 86 * Magic function to exports the data to a string. 87 * 88 * @return string 89 * 90 * @since 11.1 91 * @throws Exception if an error is encountered. 92 */ 93 public function __toString() 94 { 95 // Check everything is ok to run first. 96 $this->check(); 97 98 $buffer = ''; 99 100 // Get the format. 101 switch ($this->asFormat) 102 { 103 case 'xml': 104 default: 105 $buffer = $this->buildXml(); 106 break; 107 } 108 109 return $buffer; 110 } 111 112 /** 113 * Set the output option for the exporter to XML format. 114 * 115 * @return JDatabaseExporterMySQL Method supports chaining. 116 * 117 * @since 11.1 118 */ 119 public function asXml() 120 { 121 $this->asFormat = 'xml'; 122 123 return $this; 124 } 125 126 /** 127 * Builds the XML data for the tables to export. 128 * 129 * @return string An XML string 130 * 131 * @since 11.1 132 * @throws Exception if an error occurs. 133 */ 134 protected function buildXml() 135 { 136 $buffer = array(); 137 138 $buffer[] = '<?xml version="1.0"?>'; 139 $buffer[] = '<mysqldump xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">'; 140 $buffer[] = ' <database name="">'; 141 142 $buffer = array_merge($buffer, $this->buildXmlStructure()); 143 144 $buffer[] = ' </database>'; 145 $buffer[] = '</mysqldump>'; 146 147 return implode("\n", $buffer); 148 } 149 150 /** 151 * Builds the XML structure to export. 152 * 153 * @return array An array of XML lines (strings). 154 * 155 * @since 11.1 156 * @throws Exception if an error occurs. 157 */ 158 protected function buildXmlStructure() 159 { 160 $buffer = array(); 161 162 foreach ($this->from as $table) 163 { 164 // Replace the magic prefix if found. 165 $table = $this->getGenericTableName($table); 166 167 // Get the details columns information. 168 $fields = $this->db->getTableColumns($table); 169 $keys = $this->db->getTableKeys($table); 170 171 $buffer[] = ' <table_structure name="' . $table . '">'; 172 173 foreach ($fields as $field) 174 { 175 $buffer[] = ' <field Field="' . $field->Field . '"' . ' Type="' . $field->Type . '"' . ' Null="' . $field->Null . '"' . ' Key="' . 176 $field->Key . '"' . (isset($field->Default) ? ' Default="' . $field->Default . '"' : '') . ' Extra="' . $field->Extra . '"' . 177 ' />'; 178 } 179 180 foreach ($keys as $key) 181 { 182 $buffer[] = ' <key Table="' . $table . '"' . ' Non_unique="' . $key->Non_unique . '"' . ' Key_name="' . $key->Key_name . '"' . 183 ' Seq_in_index="' . $key->Seq_in_index . '"' . ' Column_name="' . $key->Column_name . '"' . ' Collation="' . $key->Collation . '"' . 184 ' Null="' . $key->Null . '"' . ' Index_type="' . $key->Index_type . '"' . ' Comment="' . htmlspecialchars($key->Comment) . '"' . 185 ' />'; 186 } 187 188 $buffer[] = ' </table_structure>'; 189 } 190 191 return $buffer; 192 } 193 194 /** 195 * Checks if all data and options are in order prior to exporting. 196 * 197 * @return JDatabaseExporterMySQL Method supports chaining. 198 * 199 * @since 11.1 200 * 201 * @throws Exception if an error is encountered. 202 */ 203 public function check() 204 { 205 // Check if the db connector has been set. 206 if (!($this->db instanceof JDatabaseMySQL)) 207 { 208 throw new Exception('JPLATFORM_ERROR_DATABASE_CONNECTOR_WRONG_TYPE'); 209 } 210 211 // Check if the tables have been specified. 212 if (empty($this->from)) 213 { 214 throw new Exception('JPLATFORM_ERROR_NO_TABLES_SPECIFIED'); 215 } 216 217 return $this; 218 } 219 220 /** 221 * Get the generic name of the table, converting the database prefix to the wildcard string. 222 * 223 * @param string $table The name of the table. 224 * 225 * @return string The name of the table with the database prefix replaced with #__. 226 * 227 * @since 11.1 228 */ 229 protected function getGenericTableName($table) 230 { 231 // TODO Incorporate into parent class and use $this. 232 $prefix = $this->db->getPrefix(); 233 234 // Replace the magic prefix if found. 235 $table = preg_replace("|^$prefix|", '#__', $table); 236 237 return $table; 238 } 239 240 /** 241 * Specifies a list of table names to export. 242 * 243 * @param mixed $from The name of a single table, or an array of the table names to export. 244 * 245 * @return JDatabaseExporterMySQL Method supports chaining. 246 * 247 * @since 11.1 248 * @throws Exception if input is not a string or array. 249 */ 250 public function from($from) 251 { 252 if (is_string($from)) 253 { 254 $this->from = array($from); 255 } 256 elseif (is_array($from)) 257 { 258 $this->from = $from; 259 } 260 else 261 { 262 throw new Exception('JPLATFORM_ERROR_INPUT_REQUIRES_STRING_OR_ARRAY'); 263 } 264 265 return $this; 266 } 267 268 /** 269 * Sets the database connector to use for exporting structure and/or data from MySQL. 270 * 271 * @param JDatabaseMySQL $db The database connector. 272 * 273 * @return JDatabaseExporterMySQL Method supports chaining. 274 * 275 * @since 11.1 276 */ 277 public function setDbo(JDatabaseMySQL $db) 278 { 279 $this->db = $db; 280 281 return $this; 282 } 283 284 /** 285 * Sets an internal option to export the structure of the input table(s). 286 * 287 * @param boolean $setting True to export the structure, false to not. 288 * 289 * @return JDatabaseExporterMySQL Method supports chaining. 290 * 291 * @since 11.1 292 */ 293 public function withStructure($setting = true) 294 { 295 $this->options->set('with-structure', (boolean) $setting); 296 297 return $this; 298 } 299 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Tue Apr 3 11:40:28 2012 | Cross-referenced by PHPXref 0.7.1 |