filters.inc 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. <?php
  2. /**
  3. * @file
  4. * All of the filter handling code needed for Backup and Migrate.
  5. */
  6. /**
  7. * Get the available destination types.
  8. */
  9. function backup_migrate_get_filters($op = NULL) {
  10. static $filters = NULL;
  11. if ($filters === NULL) {
  12. $filters = array();
  13. $definitions = module_invoke_all('backup_migrate_filters');
  14. foreach ($definitions as $definition) {
  15. // Include the necessary file if specified by the filter.
  16. if (!empty($definition['file'])) {
  17. require_once './'. $definition['file'];
  18. }
  19. $filters[] = new $definition['class'];
  20. }
  21. }
  22. $sort = array();
  23. // Sort the filters based on the weight for the given operation.
  24. foreach ($filters as $filter) {
  25. $sort[] = $filter->weight($op);
  26. }
  27. array_multisort($sort, SORT_ASC, SORT_NUMERIC, $filters);
  28. return $filters;
  29. }
  30. /**
  31. * Implementation of hook_backup_migrate_filters().
  32. *
  33. * Get the built in Backup and Migrate filters.
  34. */
  35. function backup_migrate_backup_migrate_filters() {
  36. return array(
  37. 'backup_restore' => array(
  38. 'file' => drupal_get_path('module', 'backup_migrate') .'/includes/filters.backup_restore.inc',
  39. 'class' => 'backup_migrate_filter_backup_restore',
  40. ),
  41. 'compression' => array(
  42. 'file' => drupal_get_path('module', 'backup_migrate') .'/includes/filters.compression.inc',
  43. 'class' => 'backup_migrate_filter_compression',
  44. ),
  45. 'encryption' => array(
  46. 'file' => drupal_get_path('module', 'backup_migrate') .'/includes/filters.encryption.inc',
  47. 'class' => 'backup_migrate_filter_encryption',
  48. ),
  49. 'statusnotify' => array(
  50. 'file' => drupal_get_path('module', 'backup_migrate') .'/includes/filters.statusnotify.inc',
  51. 'class' => 'backup_migrate_filter_statusnotify',
  52. ),
  53. 'utils' => array(
  54. 'file' => drupal_get_path('module', 'backup_migrate') .'/includes/filters.utils.inc',
  55. 'class' => 'backup_migrate_filter_utils',
  56. ),
  57. );
  58. }
  59. /**
  60. * Invoke the given method on all of the available filters.
  61. */
  62. function backup_migrate_filters_invoke_all() {
  63. $args = func_get_args();
  64. $op = array_shift($args);
  65. $out = array();
  66. $filters = backup_migrate_get_filters($op);
  67. foreach ($filters as $filter) {
  68. if (method_exists($filter, $op)) {
  69. /* call_user_func_array() ignores the function signature, so we cannot
  70. * use it to pass references. (Call-time pass-by-reference is deprecated
  71. * in PHP5.3.) Work around it, since we have unknown function signatures.
  72. */
  73. switch (count($args)) {
  74. case 1:
  75. $ret = $filter->$op($args[0]);
  76. break;
  77. case 2:
  78. $ret = $filter->$op($args[0], $args[1]);
  79. break;
  80. default:
  81. // This assumes that no functions with more than 2 arguments expect a
  82. // reference as argument. If so, add another 'case block'.
  83. $ret = call_user_func_array(array($filter, $op), $args);
  84. }
  85. $out = array_merge_recursive($out, (array) $ret);
  86. }
  87. }
  88. return $out;
  89. }
  90. /**
  91. * Filter a backup file before sending it to the destination.
  92. */
  93. function backup_migrate_filters_backup($file, &$settings) {
  94. $filters = backup_migrate_get_filters('backup');
  95. foreach ($filters as $filter) {
  96. if ($file) {
  97. $file = $filter->backup($file, $settings);
  98. }
  99. }
  100. return $file;
  101. }
  102. /**
  103. * Filter a backup file before sending it to the destination.
  104. */
  105. function backup_migrate_filters_restore($file, &$settings) {
  106. $filters = backup_migrate_get_filters('restore');
  107. foreach ($filters as $filter) {
  108. if ($file) {
  109. $file = $filter->restore($file, $settings);
  110. }
  111. }
  112. return $file;
  113. }
  114. /**
  115. * Get the backup settings for all of the filters.
  116. */
  117. function backup_migrate_filters_settings_form($settings, $op) {
  118. $out = backup_migrate_filters_invoke_all($op .'_settings_form', $settings);
  119. $out = backup_migrate_filters_settings_form_set_parents($out);
  120. return $out;
  121. }
  122. /**
  123. * Add a form parent to the filter settings so that the filter values are saved in the right table.
  124. */
  125. function backup_migrate_filters_settings_form_set_parents($form) {
  126. foreach (element_children($form) as $key) {
  127. if (!isset($form[$key]['#parents'])) {
  128. $form[$key]['#parents'] = array('filters', $key);
  129. $form[$key] = backup_migrate_filters_settings_form_set_parents($form[$key]);
  130. }
  131. }
  132. return $form;
  133. }
  134. /**
  135. * Validate all the filters.
  136. */
  137. function backup_migrate_filters_settings_form_validate($op, $form, &$form_state) {
  138. //backup_migrate_filters_invoke_all($op .'_settings_form_validate', $form, $form_state);
  139. }
  140. /**
  141. * Submit all of the filters.
  142. */
  143. function backup_migrate_filters_settings_form_submit($op, $form, &$form_state) {
  144. //backup_migrate_filters_invoke_all($op .'_settings_form_submit', $form, $form_state);
  145. }
  146. /**
  147. * Get the default settings for the filters.
  148. */
  149. function backup_migrate_filters_settings_default($op) {
  150. return backup_migrate_filters_invoke_all($op .'_settings_default');
  151. }
  152. /**
  153. * Get the file types for all of the filters.
  154. */
  155. function backup_migrate_filters_file_types() {
  156. return backup_migrate_filters_invoke_all('file_types');
  157. }
  158. /**
  159. * A base class for basing filters on.
  160. */
  161. class backup_migrate_filter {
  162. var $weight = 0;
  163. var $op_weights = array();
  164. /**
  165. * Get the weight of the filter for the given op.
  166. */
  167. function weight($op = NULL) {
  168. if ($op && isset($this->op_weights[$op])) {
  169. return $this->op_weights[$op];
  170. }
  171. return $this->weight;
  172. }
  173. /**
  174. * Get the form for the settings for this filter.
  175. */
  176. function backup_settings_default() {
  177. return array();
  178. }
  179. /**
  180. * Get the form for the settings for this filter.
  181. */
  182. function backup_settings_form($settings) {
  183. return array();
  184. }
  185. /**
  186. * Get the form for the settings for this filter.
  187. */
  188. function backup_settings_form_validate($form, &$form_state) {
  189. }
  190. /**
  191. * Submit the settings form. Any values returned will be saved.
  192. */
  193. function backup_settings_form_submit($form, &$form_state) {
  194. return $form_state['values'];
  195. }
  196. /**
  197. * Get the form for the settings for this filter.
  198. */
  199. function restore_settings_default() {
  200. return array();
  201. }
  202. /**
  203. * Get the form for the settings for this filter.
  204. */
  205. function restore_settings_form($settings) {
  206. return array();
  207. }
  208. /**
  209. * Get the form for the settings for this filter.
  210. */
  211. function restore_settings_form_validate($form, &$form_state) {
  212. }
  213. /**
  214. * Submit the settings form. Any values returned will be saved.
  215. */
  216. function restore_settings_form_submit($form, &$form_state) {
  217. return $form_state['values'];
  218. }
  219. /**
  220. * Get a list of file types handled by this filter.
  221. */
  222. function file_types() {
  223. return array();
  224. }
  225. /**
  226. * Declare any default destinations for this filter.
  227. */
  228. function destinations() {
  229. return array();
  230. }
  231. /**
  232. * This function is called on a backup file after the backup has been completed.
  233. */
  234. function backup($file, &$settings) {
  235. return $file;
  236. }
  237. /**
  238. * This function is called on a backup file before importing it.
  239. */
  240. function restore($file, &$settings) {
  241. return $file;
  242. }
  243. /**
  244. * This function is called immediately prior to backup.
  245. */
  246. function pre_backup($source, $file, $settings) {
  247. }
  248. /**
  249. * This function is called immediately post backup.
  250. */
  251. function post_backup($source, $file, $settings) {
  252. }
  253. }