filters.inc 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. <?php
  2. /**
  3. * @file
  4. * All of the filter handling code needed for Backup and Migrate.
  5. */
  6. /**
  7. * Gets the available destination types.
  8. */
  9. function backup_migrate_get_filters($op = NULL) {
  10. $filters = &drupal_static('backup_migrate_get_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. * Implements 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. * Invokes 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. * Filters a backup file before sending it to the destination.
  92. */
  93. function backup_migrate_filters_backup($file, &$settings) {
  94. backup_migrate_filters_invoke_all('pre_backup', $file, $settings);
  95. $filters = backup_migrate_get_filters('backup');
  96. foreach ($filters as $filter) {
  97. if ($file) {
  98. $file = $filter->backup($file, $settings);
  99. }
  100. }
  101. backup_migrate_filters_invoke_all('post_backup', $file, $settings);
  102. return $file;
  103. }
  104. /**
  105. * Filters a backup file before sending it to the destination.
  106. */
  107. function backup_migrate_filters_restore($file, &$settings) {
  108. backup_migrate_filters_invoke_all('pre_restore', $file, $settings);
  109. $filters = backup_migrate_get_filters('restore');
  110. foreach ($filters as $filter) {
  111. if ($file) {
  112. $file = $filter->restore($file, $settings);
  113. }
  114. }
  115. backup_migrate_filters_invoke_all('post_restore', $file, $settings);
  116. return $file;
  117. }
  118. /**
  119. * Gets the backup settings for all of the filters.
  120. */
  121. function backup_migrate_filters_settings_form($settings, $op) {
  122. $out = backup_migrate_filters_invoke_all($op . '_settings_form', $settings);
  123. $out = backup_migrate_filters_settings_form_set_parents($out);
  124. return $out;
  125. }
  126. /**
  127. * Adds form parent to filter settings so the values are saved in correct table.
  128. */
  129. function backup_migrate_filters_settings_form_set_parents($form) {
  130. foreach (element_children($form) as $key) {
  131. if (!isset($form[$key]['#parents'])) {
  132. $form[$key]['#parents'] = array('filters', $key);
  133. $form[$key] = backup_migrate_filters_settings_form_set_parents($form[$key]);
  134. }
  135. }
  136. return $form;
  137. }
  138. /**
  139. * Validates all the filters.
  140. */
  141. function backup_migrate_filters_settings_form_validate($op, $form, &$form_state) {
  142. backup_migrate_filters_invoke_all($op . '_settings_form_validate', $form, $form_state);
  143. }
  144. /**
  145. * Submits all of the filters.
  146. */
  147. function backup_migrate_filters_settings_form_submit($op, $form, &$form_state) {
  148. backup_migrate_filters_invoke_all($op . '_settings_form_submit', $form, $form_state);
  149. }
  150. /**
  151. * Gets the default settings for the filters.
  152. */
  153. function backup_migrate_filters_settings_default($op) {
  154. return backup_migrate_filters_invoke_all($op . '_settings_default');
  155. }
  156. /**
  157. * Get the backup settings for all of the filters.
  158. */
  159. function backup_migrate_filters_before_action_form($settings, $op) {
  160. $out = array();
  161. $out += backup_migrate_filters_invoke_all('before_action_form', $op, $settings);
  162. $out += backup_migrate_filters_invoke_all('before_' . $op . '_form', $settings);
  163. return $out;
  164. }
  165. /**
  166. * Get the backup settings for all of the filters.
  167. */
  168. function backup_migrate_filters_before_action_form_validate($settings, $op, $form, &$form_state) {
  169. backup_migrate_filters_invoke_all('before_action_form_validate', $op, $settings, $form, $form_state);
  170. backup_migrate_filters_invoke_all('before_' . $op . '_form_validate', $settings, $form, $form_state);
  171. }
  172. /**
  173. * Get the backup settings for all of the filters.
  174. */
  175. function backup_migrate_filters_before_action_form_submit($settings, $op, $form, &$form_state) {
  176. backup_migrate_filters_invoke_all('before_action_form_submit', $op, $settings, $form, $form_state);
  177. backup_migrate_filters_invoke_all('before_' . $op . '_form_submit', $settings, $form, $form_state);
  178. }
  179. /**
  180. * Get the file types for all of the filters.
  181. */
  182. function backup_migrate_filters_file_types() {
  183. return backup_migrate_filters_invoke_all('file_types');
  184. }
  185. /**
  186. * A base class for basing filters on.
  187. */
  188. class backup_migrate_filter {
  189. public $weight = 0;
  190. public $op_weights = array();
  191. /**
  192. * Get the weight of the filter for the given op.
  193. */
  194. public function weight($op = NULL) {
  195. if ($op && isset($this->op_weights[$op])) {
  196. return $this->op_weights[$op];
  197. }
  198. return $this->weight;
  199. }
  200. /**
  201. * Get the form for the settings for this filter.
  202. */
  203. public function backup_settings_default() {
  204. return array();
  205. }
  206. /**
  207. * Get the form for the settings for this filter.
  208. */
  209. public function backup_settings_form($settings) {
  210. return array();
  211. }
  212. /**
  213. * Get the form for the settings for this filter.
  214. */
  215. public function backup_settings_form_validate($form, &$form_state) {
  216. }
  217. /**
  218. * Submit the settings form. Any values returned will be saved.
  219. */
  220. public function backup_settings_form_submit($form, &$form_state) {
  221. }
  222. /**
  223. * Get the form for the settings for this filter.
  224. */
  225. public function restore_settings_default() {
  226. return array();
  227. }
  228. /**
  229. * Get the form for the settings for this filter.
  230. */
  231. public function restore_settings_form($settings) {
  232. return array();
  233. }
  234. /**
  235. * Get the form for the settings for this filter.
  236. */
  237. public function restore_settings_form_validate($form, &$form_state) {
  238. }
  239. /**
  240. * Submit the settings form. Any values returned will be saved.
  241. */
  242. public function restore_settings_form_submit($form, &$form_state) {
  243. return $form_state['values'];
  244. }
  245. /**
  246. * Get a list of file types handled by this filter.
  247. */
  248. public function file_types() {
  249. return array();
  250. }
  251. /**
  252. * Declare any default destinations for this filter.
  253. */
  254. public function destinations() {
  255. return array();
  256. }
  257. /**
  258. * Called on a backup file after the backup has been completed.
  259. */
  260. public function backup($file, $settings) {
  261. return $file;
  262. }
  263. /**
  264. * This function is called immediately prior to backup.
  265. */
  266. public function pre_backup($file, $settings) {
  267. }
  268. /**
  269. * This function is called immediately post backup.
  270. */
  271. public function post_backup($file, $settings) {
  272. }
  273. /**
  274. * This function is called on a backup file before importing it.
  275. */
  276. public function restore($file, $settings) {
  277. return $file;
  278. }
  279. /**
  280. * This function is called immediately prior to restore.
  281. */
  282. public function pre_restore($file, $settings) {
  283. }
  284. /**
  285. * This function is called immediately post restore.
  286. */
  287. public function post_restore($file, $settings) {
  288. }
  289. }