/libraries/joomla/database/exporter.php

https://github.com/pjwiseman/joomla-cms · PHP · 230 lines · 69 code · 29 blank · 132 comment · 2 complexity · 4c4da71f77a004fe4670178ec0dcc2ad MD5 · raw file

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