filters.backup_restore.inc 7.4 KB

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