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