filters.utils.inc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <?php
  2. /**
  3. * @file
  4. * A filter to run some basic utility functions. Basically any useful option not big enough to justify it's own class.
  5. */
  6. /**
  7. * A filter to run some basic utility functions.
  8. *
  9. * @ingroup backup_migrate_filters
  10. */
  11. class backup_migrate_filter_utils extends backup_migrate_filter {
  12. public $op_weights = array('pre_backup' => -1000, 'post_backup' => 1000);
  13. public $saved_devel_query = NULL;
  14. /**
  15. * Get the default backup settings for this filter.
  16. */
  17. public function backup_settings_default() {
  18. return array(
  19. 'utils_disable_query_log' => TRUE,
  20. 'utils_site_offline' => FALSE,
  21. 'utils_description' => '',
  22. );
  23. }
  24. /**
  25. * Get the default restore settings for this filter.
  26. */
  27. public function restore_settings_default() {
  28. return array(
  29. 'utils_disable_query_log' => TRUE,
  30. 'utils_site_offline' => FALSE,
  31. );
  32. }
  33. /**
  34. * Get the form for the backup settings for this filter.
  35. */
  36. public function backup_settings_form($settings) {
  37. $form = array();
  38. if (module_exists('devel') && variable_get('dev_query', 0)) {
  39. $form['database']['utils_disable_query_log'] = array(
  40. '#type' => 'checkbox',
  41. '#title' => t('Disable query log'),
  42. '#default_value' => !empty($settings['utils_disable_query_log']) ? $settings['utils_disable_query_log'] : NULL,
  43. '#description' => t('Disable the devel module\'s query logging during the backup operation. It will be enabled again after backup is complete. This is very highly recommended.'),
  44. );
  45. }
  46. $form['advanced']['utils_site_offline'] = array(
  47. '#type' => 'checkbox',
  48. '#title' => t('Take site offline'),
  49. '#default_value' => !empty($settings['utils_site_offline']) ? $settings['utils_site_offline'] : NULL,
  50. '#description' => t('Take the site offline during backup and show a maintenance message. Site will be taken back online once the backup is complete.'),
  51. );
  52. $form['advanced']['utils_site_offline_message_wrapper'] = array(
  53. '#type' => 'backup_migrate_dependent',
  54. '#dependencies' => array(
  55. 'filters[utils_site_offline]' => TRUE,
  56. ),
  57. );
  58. $form['advanced']['utils_site_offline_message_wrapper']['utils_site_offline_message'] = array(
  59. '#type' => 'textarea',
  60. '#title' => t('Site off-line message'),
  61. '#default_value' => !empty($settings['utils_site_offline_message']) ? $settings['utils_site_offline_message'] : variable_get('site_offline_message', t('@site is currently under maintenance. We should be back shortly. Thank you for your patience.', array('@site' => variable_get('site_name', 'Drupal')))),
  62. '#description' => t('Message to show visitors when the site is in off-line mode.'),
  63. );
  64. $form['advanced']['utils_description'] = array(
  65. '#type' => 'textarea',
  66. '#title' => t('Add a note'),
  67. '#default_value' => !empty($settings['utils_description']) ? $settings['utils_description'] : NULL,
  68. '#description' => t('Add a short note to the backup file.'),
  69. );
  70. $form['advanced']['use_cli'] = array(
  71. "#type" => "checkbox",
  72. "#title" => t("Use cli commands"),
  73. "#default_value" => !empty($settings['use_cli']),
  74. "#description" => t("Use the command line tools (mysqldump, tar, gzip etc.) if available. This can be faster for large sites but will not work on all servers. EXPERIMENTAL"),
  75. );
  76. $form['advanced']['ignore_errors'] = array(
  77. "#type" => "checkbox",
  78. "#title" => t("Ignore errors"),
  79. "#default_value" => !empty($settings['ignore_errors']),
  80. "#description" => t("Will attempt to complete backup even if certain recoverable errors occur. This may make the backup files invalid. Enable this if you have unreadable files that you want to ignore during backup."),
  81. );
  82. return $form;
  83. }
  84. /**
  85. * Get the form for the restore settings for this filter.
  86. */
  87. public function restore_settings_form($settings) {
  88. $form = array();
  89. if (module_exists('devel') && variable_get('dev_query', 0)) {
  90. $form['advanced']['utils_disable_query_log'] = array(
  91. '#type' => 'checkbox',
  92. '#title' => t('Disable query log'),
  93. '#default_value' => @$settings['utils_disable_query_log'],
  94. '#description' => t('Disable the devel module\'s query logging during the restore operation. It will be enabled again after restore is complete. This is very highly recommended.'),
  95. );
  96. }
  97. $form['advanced']['utils_site_offline'] = array(
  98. '#type' => 'checkbox',
  99. '#title' => t('Take site offline'),
  100. '#default_value' => !empty($settings['utils_site_offline']) ? $settings['utils_site_offline'] : NULL,
  101. '#description' => t('Take the site offline during restore and show a maintenance message. Site will be taken back online once the restore is complete.'),
  102. );
  103. $form['advanced']['utils_site_offline_message_wrapper'] = array(
  104. '#type' => 'backup_migrate_dependent',
  105. '#dependencies' => array(
  106. 'filters[utils_site_offline]' => TRUE,
  107. ),
  108. );
  109. $form['advanced']['utils_site_offline_message_wrapper']['utils_site_offline_message'] = array(
  110. '#type' => 'textarea',
  111. '#title' => t('Site off-line message'),
  112. '#default_value' => !empty($settings['utils_site_offline_message']) ? $settings['utils_site_offline_message'] : variable_get('site_offline_message', t('@site is currently under maintenance. We should be back shortly. Thank you for your patience.', array('@site' => variable_get('site_name', 'Drupal')))),
  113. '#description' => t('Message to show visitors when the site is in off-line mode.'),
  114. );
  115. $form['advanced']['utils_drop_all_tables'] = array(
  116. '#type' => 'checkbox',
  117. '#title' => t('Drop all tables before import (MySQL only)'),
  118. '#default_value' => !empty($settings['utils_drop_all_tables']) ? $settings['utils_drop_all_tables'] : NULL,
  119. '#description' => t('Drop all existing database tables before restoring the backup. This option is currently available on MySQL servers only.'),
  120. );
  121. $form['advanced']['use_cli'] = array(
  122. "#type" => "checkbox",
  123. "#title" => t("Use cli commands"),
  124. "#default_value" => !empty($settings['use_cli']),
  125. "#description" => t("Use the command line tools (mysqldump, tar, gzip etc.) if available. This can be faster for large sites but will not work on all servers. EXPERIMENTAL"),
  126. );
  127. // $form['advanced']['ignore_errors'] = array(
  128. // "#type" => "checkbox",
  129. // "#title" => t("Ignore errors"),
  130. // "#default_value" => !empty($settings['ignore_errors']),
  131. // "#description" => t("Will attempt to complete restore even if certain recoverable errors occur. This may could corrupt your site."),
  132. // );
  133. return $form;
  134. }
  135. /**
  136. *
  137. */
  138. public function pre_backup($file, $settings) {
  139. $this->take_site_offline($settings);
  140. $this->disable_devel_query($settings);
  141. }
  142. /**
  143. *
  144. */
  145. public function post_backup($file, $settings) {
  146. $this->enable_devel_query($settings);
  147. $this->take_site_online($settings);
  148. if ($file) {
  149. $this->add_file_info($file, $settings);
  150. }
  151. }
  152. /**
  153. *
  154. */
  155. public function pre_restore($file, $settings) {
  156. $this->disable_devel_query($settings);
  157. $this->take_site_offline($settings);
  158. }
  159. /**
  160. *
  161. */
  162. public function post_restore($file, $settings) {
  163. $this->enable_devel_query($settings);
  164. $this->take_site_online($settings);
  165. }
  166. /**
  167. * Disable devel query logging if it's active and the user has chosen to do so.
  168. */
  169. public function disable_devel_query($settings) {
  170. $this->saved_devel_query = variable_get('dev_query', 0);
  171. if (module_exists('devel') && variable_get('dev_query', 0) && !empty($settings->filters['utils_disable_query_log'])) {
  172. variable_set('dev_query', 0);
  173. }
  174. }
  175. /**
  176. * Restore devel query to previous state.
  177. */
  178. public function enable_devel_query($settings) {
  179. if (module_exists('devel')) {
  180. variable_set('dev_query', $this->saved_devel_query);
  181. }
  182. }
  183. /**
  184. * Add the backup metadata to the file.
  185. */
  186. public function add_file_info($file, $settings) {
  187. $file->file_info['description'] = $settings->filters['utils_description'];
  188. $file->file_info['datestamp'] = time();
  189. $file->file_info['generator'] = 'Backup and Migrate (http://drupal.org/project/backup_migrate)';
  190. $file->file_info['generatorversion'] = BACKUP_MIGRATE_VERSION;
  191. $file->file_info['siteurl'] = url('', array('absolute' => TRUE));
  192. $file->file_info['sitename'] = variable_get('site_name', '');
  193. $file->file_info['drupalversion'] = VERSION;
  194. $file->calculate_filesize();
  195. $source = $settings->get('source');
  196. $file->file_info['bam_sourceid'] = $source->get('id');
  197. $file->file_info['bam_sourcetype'] = $source->get('subtype');
  198. $file->file_info['bam_sourcename'] = $source->get('name');
  199. // Add any additional info that has been added to the settings by other plugins.
  200. if (!empty($settings->file_info)) {
  201. $file->file_info += $settings->file_info;
  202. }
  203. }
  204. /**
  205. * Take the site offline if configured to do so.
  206. */
  207. public function take_site_offline($settings) {
  208. // If the site is already offline then don't do anything.
  209. if (!variable_get('maintenance_mode', 0)) {
  210. // Save the current state of the site in case a restore overwrites it.
  211. if (!empty($settings->filters['utils_site_offline'])) {
  212. $this->saved_site_offline = TRUE;
  213. $this->saved_site_offline_message = variable_get('maintenance_mode_message', NULL);
  214. if (!empty($settings->filters['utils_site_offline_message'])) {
  215. $this->saved_site_offline_message = variable_get('maintenance_mode_message', NULL);
  216. variable_set('maintenance_mode_message', $settings->filters['utils_site_offline_message']);
  217. }
  218. variable_set('maintenance_mode', 1);
  219. _backup_migrate_message('Site was taken offline.');
  220. }
  221. }
  222. }
  223. /**
  224. * Take the site online again after backup or restore.
  225. */
  226. public function take_site_online($settings) {
  227. // Take the site back off/online because the restored db may have changed that setting.
  228. if (variable_get('maintenance_mode', 0) && !empty($this->saved_site_offline)) {
  229. variable_set('maintenance_mode', 0);
  230. if ($settings->filters['utils_site_offline']) {
  231. if (!empty($this->saved_site_offline_message)) {
  232. variable_set('maintenance_mode_message', $this->saved_site_offline_message);
  233. }
  234. _backup_migrate_message('Site was taken online.');
  235. }
  236. }
  237. }
  238. }