PageRenderTime 98ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/application/libraries/Engine/Db/Export.php

https://github.com/shopaholiccompany/shopaholic
PHP | 324 lines | 211 code | 71 blank | 42 comment | 16 complexity | be7d230b21208f6a27e85ea17da17548 MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-3.0, LGPL-2.1
  1. <?php
  2. /**
  3. * SocialEngine
  4. *
  5. * @category Engine
  6. * @package Engine_Db
  7. * @copyright Copyright 2006-2010 Webligo Developments
  8. * @license http://www.socialengine.net/license/
  9. * @version $Id: Export.php 7244 2010-09-01 01:49:53Z john $
  10. * @author John Boehr <j@webligo.com>
  11. */
  12. /**
  13. * @category Engine
  14. * @package Engine_Db
  15. * @copyright Copyright 2006-2010 Webligo Developments
  16. * @license http://www.socialengine.net/license/
  17. * @author John Boehr <j@webligo.com>
  18. */
  19. abstract class Engine_Db_Export
  20. {
  21. /**
  22. * @var Zend_Db_Adapter_Abstract
  23. */
  24. protected $_adapter;
  25. /**
  26. * @var array
  27. */
  28. protected $_params = array(
  29. 'dropTable' => true,
  30. 'fetchStructure' => true,
  31. 'fetchData' => true,
  32. 'fullInserts' => false,
  33. 'insertIgnore' => false,
  34. 'insertComplete' => true,
  35. 'insertExtended' => true,
  36. 'comments' => true,
  37. );
  38. protected $_tables;
  39. protected $_handle;
  40. protected $_mode;
  41. protected $_data;
  42. protected $_tableIndex;
  43. protected $_listeners;
  44. public static function factory(Zend_Db_Adapter_Abstract $adapter, $options = array())
  45. {
  46. list($prefix, $type) = explode('_Db_Adapter_', get_class($adapter));
  47. $class = 'Engine' . '_Db_Export_' . $type;
  48. Engine_Loader::loadClass($class);
  49. $instance = new $class($adapter, $options);
  50. if( !($instance instanceof Engine_Db_Export) ) {
  51. throw new Engine_Exception('Must be an instance of Engine_Db_Export');
  52. }
  53. return $instance;
  54. }
  55. public function __construct(Zend_Db_Adapter_Abstract $adapter, $options = array())
  56. {
  57. if( !$adapter->isConnected() ) {
  58. throw new Engine_Exception('Adapter not connected');
  59. }
  60. $this->_adapter = $adapter;
  61. if( is_array($options) ) {
  62. $this->setOptions($options);
  63. }
  64. }
  65. public function setOptions(array $options)
  66. {
  67. foreach( $options as $key => $value ) {
  68. $method = 'set' . ucfirst($key);
  69. if( method_exists($this, $method) ) {
  70. $this->$method($value);
  71. } else {
  72. $this->_params[$key] = $value;
  73. }
  74. }
  75. return $this;
  76. }
  77. public function setTables(array $tables)
  78. {
  79. $this->_tables = $tables;
  80. return $this;
  81. }
  82. public function getTables()
  83. {
  84. if( null === $this->_tables ) {
  85. $this->_tables = $this->_fetchTables();
  86. }
  87. return $this->_tables;
  88. }
  89. public function setParams(array $params)
  90. {
  91. $this->_params = array_merge((array)$this->_params, $params);
  92. return $this;
  93. }
  94. public function getParams()
  95. {
  96. return $this->_params;
  97. }
  98. public function setParam($key, $value)
  99. {
  100. $this->_params[$key] = $value;
  101. return $this;
  102. }
  103. public function getParam($key, $default = null)
  104. {
  105. if( !isset($this->_params[$key]) ) {
  106. return $default;
  107. }
  108. return $this->_params[$key];
  109. }
  110. /**
  111. * Get the adapter
  112. *
  113. * @return Zend_Db_Adapter_Abstract
  114. */
  115. public function getAdapter()
  116. {
  117. if( null === $this->_adapter ) {
  118. throw new Engine_Exception('No Db Adapter');
  119. }
  120. return $this->_adapter;
  121. }
  122. // Listeners
  123. public function getListerners()
  124. {
  125. return (array) $this->_listeners;
  126. }
  127. public function setListener(Engine_Observer_Interface $listener)
  128. {
  129. $this->_listeners[] = $listener;
  130. return $this;
  131. }
  132. public function setListeners(array $listeners)
  133. {
  134. $this->_listeners = array();
  135. foreach( $listeners as $listener ) {
  136. $this->_listeners[] = $listener;
  137. }
  138. return $this;
  139. }
  140. protected function _announce($event)
  141. {
  142. foreach( $this->getListerners() as $listener ) {
  143. $listener->notify($event);
  144. }
  145. return $this;
  146. }
  147. // Write
  148. public function write($file)
  149. {
  150. if( is_string($file) ) {
  151. if( !($handle = fopen($file, 'w')) ) {
  152. throw new Engine_Exception(sprintf('Unable to write to file: "%s"'), $file);
  153. }
  154. $this->_handle = $handle;
  155. } else if( is_resource($file) ) {
  156. $this->_handle = $handle = $file;
  157. } else {
  158. throw new Engine_Exception(sprintf('Unable to write to file, given: "%s"'), gettype($file));
  159. }
  160. $this->_mode = 'file';
  161. try {
  162. $this->_generate();
  163. } catch( Exception $e ) {
  164. fclose($handle);
  165. @unlink($file);
  166. $this->_mode = null;
  167. $this->_handle = null;
  168. throw $e;
  169. }
  170. fclose($handle);
  171. $this->_mode = null;
  172. $this->_handle = null;
  173. return $this;
  174. }
  175. public function toString()
  176. {
  177. $this->_mode = 'string';
  178. try {
  179. $this->_generate();
  180. } catch( Exception $e ) {
  181. $this->_data = null;
  182. $this->_mode = null;
  183. throw $e;
  184. }
  185. $data = $this->_data;
  186. $this->_data = null;
  187. $this->_mode = null;
  188. return $data;
  189. }
  190. final protected function _generate()
  191. {
  192. // Announce
  193. $this->_announce('onDatabaseExportStart');
  194. // Header
  195. $this->_write($this->_fetchHeader());
  196. // Tables
  197. $this->_tableIndex = 0;
  198. foreach( $this->getTables() as $table )
  199. {
  200. if( $this->getParam('fetchStructure', true) ) {
  201. // Table schema header
  202. $this->_write($this->_fetchTableSchemaHeader($table));
  203. // Table schema
  204. $this->_write($this->_fetchTableSchema($table));
  205. }
  206. if( $this->getParam('fetchData', true) ) {
  207. // Table data header
  208. $this->_write($this->_fetchTableDataHeader($table));
  209. // Table data
  210. $this->_write($this->_fetchTableData($table));
  211. }
  212. $this->_tableIndex++;
  213. // Announce
  214. $this->_announce('onDatabaseExportProgress');
  215. }
  216. // Footer
  217. $this->_write($this->_fetchFooter());
  218. // Announce
  219. $this->_announce('onDatabaseExportEnd');
  220. }
  221. final protected function _write($output)
  222. {
  223. if( is_string($output) ) {
  224. switch( $this->_mode ) {
  225. case 'file':
  226. fwrite($this->_handle, $output);
  227. break;
  228. case 'string':
  229. $this->_data .= $output;
  230. break;
  231. }
  232. }
  233. }
  234. // Progress
  235. public function getTableCount()
  236. {
  237. return count($this->getTables());
  238. }
  239. public function getTableIndex()
  240. {
  241. return $this->_tableIndex;
  242. }
  243. // Abstract
  244. abstract protected function _fetchHeader();
  245. abstract protected function _fetchFooter();
  246. abstract protected function _fetchComment($comment = '');
  247. abstract protected function _fetchTables();
  248. abstract protected function _fetchTableSchemaHeader($table);
  249. abstract protected function _fetchTableSchema($table);
  250. abstract protected function _fetchTableDataHeader($table);
  251. abstract protected function _fetchTableData($table);
  252. abstract protected function _queryRaw($sql);
  253. }