Joomla Platform  13.1
Documentation des API du framework Joomla Platform
 Tout Classes Espaces de nommage Fichiers Fonctions Variables Pages
exporter.php
Aller à la documentation de ce fichier.
1 <?php
2 /**
3  * @package Joomla.Platform
4  * @subpackage Database
5  *
6  * @copyright Copyright (C) 2005 - 2013 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  * Joomla Platform Database Exporter Class
14  *
15  * @package Joomla.Platform
16  * @subpackage Database
17  * @since 12.1
18  */
19 abstract class JDatabaseExporter
20 {
21  /**
22  * The type of output format (xml).
23  *
24  * @var string
25  * @since 13.1
26  */
27  protected $asFormat = 'xml';
28 
29  /**
30  * An array of cached data.
31  *
32  * @var array
33  * @since 13.1
34  */
35  protected $cache = array();
36 
37  /**
38  * The database connector to use for exporting structure and/or data.
39  *
40  * @var JDatabaseDriver
41  * @since 13.1
42  */
43  protected $db = null;
44 
45  /**
46  * An array input sources (table names).
47  *
48  * @var array
49  * @since 13.1
50  */
51  protected $from = array();
52 
53  /**
54  * An array of options for the exporter.
55  *
56  * @var object
57  * @since 13.1
58  */
59  protected $options = null;
60 
61  /**
62  * Constructor.
63  *
64  * Sets up the default options for the exporter.
65  *
66  * @since 13.1
67  */
68  public function __construct()
69  {
70  $this->options = new stdClass;
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 13.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  // Get the format.
99  switch ($this->asFormat)
100  {
101  case 'xml':
102  default:
103  $buffer = $this->buildXml();
104  break;
105  }
106 
107  return $buffer;
108  }
109 
110  /**
111  * Set the output option for the exporter to XML format.
112  *
113  * @return DatabaseExporter Method supports chaining.
114  *
115  * @since 13.1
116  */
117  public function asXml()
118  {
119  $this->asFormat = 'xml';
120 
121  return $this;
122  }
123 
124  /**
125  * Builds the XML data for the tables to export.
126  *
127  * @return string An XML string
128  *
129  * @since 13.1
130  * @throws Exception if an error occurs.
131  */
132  abstract protected function buildXml();
133 
134  /**
135  * Builds the XML structure to export.
136  *
137  * @return array An array of XML lines (strings).
138  *
139  * @since 13.1
140  * @throws Exception if an error occurs.
141  */
142  abstract protected function buildXmlStructure();
143 
144  /**
145  * Checks if all data and options are in order prior to exporting.
146  *
147  * @return DatabaseDriver Method supports chaining.
148  *
149  * @since 13.1
150  * @throws Exception if an error is encountered.
151  */
152  abstract public function check();
153 
154  /**
155  * Specifies a list of table names to export.
156  *
157  * @param mixed $from The name of a single table, or an array of the table names to export.
158  *
159  * @return JDatabaseExporter Method supports chaining.
160  *
161  * @since 13.1
162  * @throws Exception if input is not a string or array.
163  */
164  public function from($from)
165  {
166  if (is_string($from))
167  {
168  $this->from = array($from);
169  }
170  elseif (is_array($from))
171  {
172  $this->from = $from;
173  }
174  else
175  {
176  throw new Exception('JPLATFORM_ERROR_INPUT_REQUIRES_STRING_OR_ARRAY');
177  }
178 
179  return $this;
180  }
181 
182  /**
183  * Get the generic name of the table, converting the database prefix to the wildcard string.
184  *
185  * @param string $table The name of the table.
186  *
187  * @return string The name of the table with the database prefix replaced with #__.
188  *
189  * @since 13.1
190  */
191  protected function getGenericTableName($table)
192  {
193  $prefix = $this->db->getPrefix();
194 
195  // Replace the magic prefix if found.
196  $table = preg_replace("|^$prefix|", '#__', $table);
197 
198  return $table;
199  }
200 
201  /**
202  * Sets the database connector to use for exporting structure and/or data from MySQL.
203  *
204  * @param JDatabaseDriver $db The database connector.
205  *
206  * @return JDatabaseExporter Method supports chaining.
207  *
208  * @since 13.1
209  */
210  public function setDbo(JDatabaseDriver $db)
211  {
212  $this->db = $db;
213 
214  return $this;
215  }
216 
217  /**
218  * Sets an internal option to export the structure of the input table(s).
219  *
220  * @param boolean $setting True to export the structure, false to not.
221  *
222  * @return JDatabaseExporter Method supports chaining.
223  *
224  * @since 13.1
225  */
226  public function withStructure($setting = true)
227  {
228  $this->options->withStructure = (boolean) $setting;
229 
230  return $this;
231  }
232 }