filters.backup_restore.inc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 backup settings for this filter.
  27. */
  28. function backup_settings_default() {
  29. backup_migrate_include('destinations');
  30. $out = array();
  31. foreach (backup_migrate_get_destinations('source') as $destination) {
  32. $out['destinations'][$destination->get_id()] = $destination->backup_settings_default();
  33. }
  34. return $out;
  35. }
  36. /**
  37. * Get the form for the settings for this filter.
  38. */
  39. function backup_settings_form_validate($form, &$form_state) {
  40. foreach ($this->_get_destination_types() as $destination) {
  41. $destination->backup_settings_form_validate($form, $form_state);
  42. }
  43. }
  44. /**
  45. * Submit the settings form. Any values returned will be saved.
  46. */
  47. function backup_settings_form_submit($form, &$form_state) {
  48. foreach ($this->_get_destination_types() as $destination) {
  49. $destination->backup_settings_form_submit($form, $form_state);
  50. }
  51. }
  52. /**
  53. * Get the default restore settings for this filter.
  54. */
  55. function restore_settings_default() {
  56. $out = array();
  57. foreach ($this->_get_destination_types() as $destination) {
  58. $out += $destination->restore_settings_default();
  59. }
  60. return $out;
  61. }
  62. /**
  63. * Get the form for the backup settings for this filter.
  64. */
  65. function backup_settings_form($settings) {
  66. backup_migrate_include('destinations');
  67. $out = array('destinations' => array(
  68. '#tree' => TRUE,
  69. ));
  70. foreach (backup_migrate_get_destinations('source') as $destination) {
  71. $destination_settings = (array)(@$settings['destinations'][$destination->get_id()]) + $settings;
  72. if ($form = $destination->backup_settings_form($destination_settings)) {
  73. $out['destinations'][$destination->get_id()] = array(
  74. '#type' => 'fieldset',
  75. '#title' => t('!name Backup Options', array('!name' => $destination->get('name'))),
  76. "#collapsible" => TRUE,
  77. "#collapsed" => TRUE,
  78. '#tree' => TRUE,
  79. '#parents' => array('filters', 'destinations', $destination->get_id()),
  80. ) + $form;
  81. }
  82. }
  83. return $out;
  84. }
  85. /**
  86. * Get the form for the restore settings for this filter.
  87. */
  88. function restore_settings_form($settings) {
  89. $form = array();
  90. foreach ($this->_get_destination_types() as $destination) {
  91. $destination->restore_settings_form($form, $settings);
  92. }
  93. return $form;
  94. }
  95. /**
  96. * Get the file types supported by this destination.
  97. */
  98. function file_types() {
  99. $types = array();
  100. foreach ($this->_get_destination_types() as $destination) {
  101. $types += $destination->file_types();
  102. }
  103. return $types;
  104. }
  105. /**
  106. * Backup the data from the source specified in the settings.
  107. */
  108. function backup($file, &$settings) {
  109. if ($source = $settings->get_source()) {
  110. if (!empty($settings->filters['destinations'][$source->get_id()])) {
  111. $settings->filters = (array)($settings->filters['destinations'][$source->get_id()]) + $settings->filters;
  112. }
  113. $file = $source->backup_to_file($file, $settings);
  114. return $file;
  115. }
  116. backup_migrate_backup_fail("Could not run backup because the source '%source' is missing.", array('%source' => $settings->source_id), $settings);
  117. return FALSE;
  118. }
  119. /**
  120. * Restore the data from to source specified in the settings.
  121. */
  122. function restore($file, &$settings) {
  123. if ($source = $settings->get_source()) {
  124. if (!empty($settings->filters['destinations'][$source->get_id()])) {
  125. $settings->filters = (array)($settings->filters['destinations'][$source->get_id()]) + $settings->filters;
  126. }
  127. $num = $source->restore_from_file($file, $settings);
  128. return $num ? $file : FALSE;
  129. }
  130. backup_migrate_restore_fail("Could not run restore because the source '%source' is missing.", array('%source' => $settings->source_id), $settings);
  131. return FALSE;
  132. }
  133. /**
  134. * Get a list of dummy destinations representing each of the available destination types.
  135. */
  136. function _get_destination_types() {
  137. backup_migrate_include('destinations');
  138. static $destinations = NULL;
  139. if (!is_array($destinations)) {
  140. $destinations = array();
  141. $types = backup_migrate_get_destination_types();
  142. // If no (valid) node type has been provided, display a node type overview.
  143. foreach ($types as $key => $type) {
  144. // Include the necessary file if specified by the type.
  145. if (!empty($type['file'])) {
  146. require_once './'. $type['file'];
  147. }
  148. $destinations[] = new $type['class'](array());
  149. }
  150. }
  151. return $destinations;
  152. }
  153. }