filters.backup_restore.inc 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <?php
  2. /**
  3. * @file
  4. * This filter performs tha actual backup or restore operation.
  5. *
  6. * Not technically a filter per-se, but it does need to fit in the call chain.
  7. */
  8. /**
  9. * A filter backup or migrate the specified source.
  10. *
  11. * @ingroup backup_migrate_filters
  12. */
  13. class backup_migrate_filter_backup_restore extends backup_migrate_filter {
  14. public $op_weights = array('backup' => 0, 'restore' => 0);
  15. /**
  16. * Get the default destinations for this filter.
  17. */
  18. public function destinations() {
  19. $out = array();
  20. foreach ($this->_get_destination_types() as $destination) {
  21. if (method_exists($destination, 'destinations')) {
  22. $out += $destination->destinations();
  23. }
  24. }
  25. return $out;
  26. }
  27. /**
  28. * Get the default sources for this filter.
  29. */
  30. public function sources() {
  31. $out = array();
  32. foreach ($this->_get_source_types() as $type) {
  33. if (method_exists($type, 'sources')) {
  34. $out += $type->sources();
  35. }
  36. }
  37. return $out;
  38. }
  39. /**
  40. * Get the default backup settings for this filter.
  41. */
  42. public function backup_settings_default() {
  43. require_once dirname(__FILE__) . '/sources.inc';
  44. $out = array();
  45. foreach (backup_migrate_get_sources() as $source) {
  46. $out['sources'][$source->get_id()] = $source->backup_settings_default();
  47. }
  48. return $out;
  49. }
  50. /**
  51. * Get the form for the settings for this filter.
  52. */
  53. public function backup_settings_form_validate($form, &$form_state) {
  54. foreach ($this->_get_destination_types() as $destination) {
  55. $destination->backup_settings_form_validate($form, $form_state);
  56. }
  57. }
  58. /**
  59. * Submit the settings form. Any values returned will be saved.
  60. */
  61. public function backup_settings_form_submit($form, &$form_state) {
  62. foreach ($this->_get_destination_types() as $destination) {
  63. $destination->backup_settings_form_submit($form, $form_state);
  64. }
  65. }
  66. /**
  67. * Get the default restore settings for this filter.
  68. */
  69. public function restore_settings_default() {
  70. $out = array();
  71. foreach ($this->_get_destination_types() as $destination) {
  72. $out += $destination->restore_settings_default();
  73. }
  74. return $out;
  75. }
  76. /**
  77. * Get the form for the backup settings for this filter.
  78. */
  79. public function backup_settings_form($settings) {
  80. require_once dirname(__FILE__) . '/sources.inc';
  81. $out = array(
  82. 'sources' => array(
  83. '#tree' => TRUE,
  84. ),
  85. );
  86. foreach (backup_migrate_get_sources() as $source) {
  87. $source_settings = (array) (@$settings['sources'][$source->get_id()]) + $settings;
  88. if ($form = $source->backup_settings_form($source_settings)) {
  89. $out['sources'][$source->get_id()] = array(
  90. '#type' => 'fieldset',
  91. '#title' => t('!name Backup Options', array('!name' => $source->get('name'))),
  92. "#collapsible" => TRUE,
  93. "#collapsed" => TRUE,
  94. '#tree' => TRUE,
  95. '#parents' => array('filters', 'sources', $source->get_id()),
  96. ) + $form;
  97. }
  98. }
  99. return $out;
  100. }
  101. /**
  102. * Get the form for the restore settings for this filter.
  103. */
  104. public function restore_settings_form($settings) {
  105. $form = array();
  106. foreach ($this->_get_destination_types() as $destination) {
  107. $destination->restore_settings_form($form, $settings);
  108. }
  109. return $form;
  110. }
  111. /**
  112. * Get the before-backup form for the active sources and destinations.
  113. */
  114. public function before_action_form($op, $settings) {
  115. $form = array();
  116. $method = 'before_' . $op . '_form';
  117. if ($source = $settings->get_source()) {
  118. if (method_exists($source, $method)) {
  119. $form += $source->{$method}($settings);
  120. }
  121. }
  122. foreach ($settings->get_destinations() as $destination) {
  123. if (method_exists($destination, $method)) {
  124. $form += $destination->{$method}($settings);
  125. }
  126. }
  127. return $form;
  128. }
  129. /**
  130. * Get the before-backup form for the active sources and destinations.
  131. */
  132. public function before_action_form_validate($op, $settings, $form, $form_state) {
  133. $method = 'before_' . $op . '_form_validate';
  134. foreach ($settings->get_all_locations() as $location) {
  135. if (method_exists($location, $method)) {
  136. $location->{$method}($settings, $form, $form_state);
  137. }
  138. }
  139. }
  140. /**
  141. * Get the before-backup form for the active sources and destinations.
  142. */
  143. public function before_action_form_submit($op, $settings, $form, $form_state) {
  144. $method = 'before_' . $op . '_form_submit';
  145. foreach ($settings->get_all_locations() as $location) {
  146. if (method_exists($location, $method)) {
  147. $location->{$method}($settings, $form, $form_state);
  148. }
  149. }
  150. }
  151. /**
  152. * Get the file types supported by this destination.
  153. */
  154. public function file_types() {
  155. $types = array();
  156. foreach ($this->_get_destination_types() as $destination) {
  157. $types += $destination->file_types();
  158. }
  159. foreach ($this->_get_source_types() as $source) {
  160. $types += $source->file_types();
  161. }
  162. return $types;
  163. }
  164. /**
  165. * Backup the data from the source specified in the settings.
  166. */
  167. public function backup($file, $settings) {
  168. if ($source = $settings->get_source()) {
  169. if (!empty($settings->filters['sources'][$source->get_id()])) {
  170. $settings->filters = (array) ($settings->filters['sources'][$source->get_id()]) + $settings->filters;
  171. }
  172. $file = $source->backup_to_file($file, $settings);
  173. return $file;
  174. }
  175. backup_migrate_backup_fail("Could not run backup because the source '%source' is missing.", array('%source' => $settings->source_id), $settings);
  176. return FALSE;
  177. }
  178. /**
  179. * Restore the data from to source specified in the settings.
  180. */
  181. public function restore($file, $settings) {
  182. if ($source = $settings->get_source()) {
  183. if (!empty($settings->filters['sources'][$source->get_id()])) {
  184. $settings->filters = (array) ($settings->filters['sources'][$source->get_id()]) + $settings->filters;
  185. }
  186. $num = $source->restore_from_file($file, $settings);
  187. return $num ? $file : FALSE;
  188. }
  189. backup_migrate_restore_fail("Could not run restore because the source '%source' is missing.", array('%source' => $settings->source_id), $settings);
  190. return FALSE;
  191. }
  192. /**
  193. * Dummy destinations representing each of the available destination types.
  194. *
  195. * @return array
  196. * All of the available destination types.
  197. */
  198. public function _get_destination_types() {
  199. require_once dirname(__FILE__) . '/destinations.inc';
  200. $destinations = &drupal_static('backup_migrate_filter_backup_restore::_get_destination_types', NULL);
  201. if (!is_array($destinations)) {
  202. $destinations = array();
  203. $types = backup_migrate_get_destination_subtypes();
  204. // If no (valid) node type has been provided, display a node type overview.
  205. foreach ($types as $key => $type) {
  206. // Include the necessary file if specified by the type.
  207. if (!empty($type['file'])) {
  208. require_once './' . $type['file'];
  209. }
  210. $destinations[] = new $type['class'](array());
  211. }
  212. }
  213. return $destinations;
  214. }
  215. /**
  216. * Dummy destinations representing each of the available source types.
  217. *
  218. * @return array
  219. * All of the available source types.
  220. */
  221. public function _get_source_types() {
  222. require_once dirname(__FILE__) . '/sources.inc';
  223. $sources = &drupal_static('backup_migrate_filter_backup_restore::_get_source_types', NULL);
  224. if (!is_array($sources)) {
  225. $sources = array();
  226. $types = backup_migrate_get_source_subtypes();
  227. // If no (valid) node type has been provided, display a node type
  228. // overview.
  229. foreach ($types as $key => $type) {
  230. // Include the necessary file if specified by the type.
  231. if (!empty($type['file'])) {
  232. require_once './' . $type['file'];
  233. }
  234. $sources[] = new $type['class'](array());
  235. }
  236. }
  237. return $sources;
  238. }
  239. }