backup_migrate.advanced_settings.inc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. /**
  3. * @file
  4. * Functionality required for the advanced settings form.
  5. */
  6. /**
  7. * The advanced settings form.
  8. */
  9. function backup_migrate_ui_advanced_settings_form($form, &$form_state) {
  10. $form['backup_migrate_advanced_settings_intro'] = array(
  11. '#markup' => t('These settings should be handled with care, it is recommended to leave them at their defaults unless there is a specific need to modify them.'),
  12. '#prefix' => '<p>',
  13. '#suffix' => '</p>',
  14. );
  15. $form['backup_migrate_memory_limit'] = array(
  16. '#type' => 'textfield',
  17. '#title' => t('Memory Limit'),
  18. '#default_value' => variable_get('backup_migrate_memory_limit', BACKUP_MIGRATE_MEMORY_LIMIT),
  19. '#description' => t('If backups fail due to an out-of-memory, try adjusting the memory limit using a php.ini style value, for example 512M for 512 megabytes or 1G for 1 gigabyte. Default: !default.', array('!default' => '<kbd>' . check_plain(BACKUP_MIGRATE_MEMORY_LIMIT) . '</kbd>')),
  20. '#required' => FALSE,
  21. '#element_validate' => array('backup_migrate_memory_limit_validate'),
  22. );
  23. $form['backup_migrate_backup_max_time'] = array(
  24. '#type' => 'textfield',
  25. '#title' => t('Time Limit'),
  26. '#default_value' => variable_get('backup_migrate_backup_max_time', BACKUP_MIGRATE_BACKUP_MAX_TIME),
  27. '#description' => t('If backups fail due to a PHP timeout error, especially an error saying "MySQL server has gone away", increase this setting. Before doing this, check to see what PHP\'s "max_execution_time" is set to, then set this field to a higher number, e.g. if max_execution_time is 180 (seconds) try setting Time Limit to 240 seconds (4 minutes), or 300 seconds (5 minutes). Default: !default.', array('!default' => '<kbd>' . check_plain(BACKUP_MIGRATE_BACKUP_MAX_TIME) . '</kbd>')),
  28. '#required' => FALSE,
  29. '#element_validate' => array('element_validate_integer_positive'),
  30. );
  31. $form['backup_migrate_verbose'] = array(
  32. '#type' => 'checkbox',
  33. '#title' => t('Verbose Output'),
  34. '#default_value' => variable_get('backup_migrate_verbose', BACKUP_MIGRATE_VERBOSE),
  35. '#description' => t('Enable this to make the module log additional messages to watchdog as the module performs certain actions. Default: !default.', array('!default' => (BACKUP_MIGRATE_VERBOSE ? 'on' : 'off'))),
  36. '#required' => FALSE,
  37. );
  38. $form['backup_migrate_disable_cron'] = array(
  39. '#type' => 'checkbox',
  40. '#title' => t('Disable Cron'),
  41. '#default_value' => variable_get('backup_migrate_disable_cron', BACKUP_MIGRATE_DISABLE_CRON),
  42. '#description' => t('It can be frustrating working from a production database backup on non-prod servers as schduled backups will automatically run via cron the same as they run on production. The custom cron tasks may be disabled using this checkbox. Note: this doesn\'t prevent people from manually running backups via the UI or from the Drush commands, so it is safe to enable on all site instances and then disable on production environments. Default: !default.', array('!default' => (BACKUP_MIGRATE_DISABLE_CRON ? 'on' : 'off'))),
  43. '#required' => FALSE,
  44. );
  45. $form['backup_migrate_data_rows_per_query'] = array(
  46. '#type' => 'textfield',
  47. '#title' => t('Rows Per Query'),
  48. '#default_value' => variable_get('backup_migrate_data_rows_per_query', BACKUP_MIGRATE_DATA_ROWS_PER_QUERY),
  49. '#description' => t('Controls how many records are loaded from the database at once. Defaults to "!default", i.e. !default rows. Note that setting this to a high number can cause problems when exporting large data sets, e.g. cache tables can have huge volumes of data per record.', array('!default' => '<kbd>' . check_plain(BACKUP_MIGRATE_DATA_ROWS_PER_QUERY) . '</kbd>')),
  50. '#required' => FALSE,
  51. '#element_validate' => array('element_validate_integer_positive'),
  52. );
  53. $form['backup_migrate_data_rows_per_line'] = array(
  54. '#type' => 'textfield',
  55. '#title' => t('Rows Per Line'),
  56. '#default_value' => variable_get('backup_migrate_data_rows_per_line', BACKUP_MIGRATE_DATA_ROWS_PER_LINE),
  57. '#description' => t('Controls how many records are included in a single INSERT statement. Defaults to "!default", i.e. !default records.', array('!default' => '<kbd>' . check_plain(BACKUP_MIGRATE_DATA_ROWS_PER_LINE) . '</kbd>')),
  58. '#required' => FALSE,
  59. '#element_validate' => array('element_validate_integer_positive'),
  60. );
  61. $form['backup_migrate_data_bytes_per_line'] = array(
  62. '#type' => 'textfield',
  63. '#title' => t('Data Bytes Per Line'),
  64. '#default_value' => variable_get('backup_migrate_data_bytes_per_line', BACKUP_MIGRATE_DATA_BYTES_PER_LINE),
  65. '#description' => t('Controls how much data will be inserted at once using a single INSERT statement. This works with the "backup_migrate_data_rows_per_line" variable to ensure that each INSERT statement doesn\'t end up being too large. Defaults to "!default", i.e. !default bytes.', array('!default' => '<kbd>' . check_plain(BACKUP_MIGRATE_DATA_BYTES_PER_LINE) . '</kbd>')),
  66. '#required' => FALSE,
  67. '#element_validate' => array('element_validate_integer_positive'),
  68. );
  69. $form['backup_migrate_max_email_size'] = array(
  70. '#type' => 'textfield',
  71. '#title' => t('Maximum E-mail Size'),
  72. '#default_value' => variable_get('backup_migrate_max_email_size', BACKUP_MIGRATE_MAX_EMAIL_SIZE),
  73. '#description' => t('In bytes. Limits the size of e-mails with attached back-ups that can be sent. Default: !default.', array('!default' => '<kbd>' . check_plain(BACKUP_MIGRATE_MAX_EMAIL_SIZE) . '</kbd>')),
  74. '#required' => FALSE,
  75. '#element_validate' => array('backup_migrate_unsigned_integer_validate'),
  76. );
  77. $form['backup_migrate_cleanup_temp_files'] = array(
  78. '#type' => 'checkbox',
  79. '#title' => t('Clean up Temporary Files'),
  80. '#default_value' => variable_get('backup_migrate_cleanup_temp_files', BACKUP_MIGRATE_CLEANUP_TEMP_FILES),
  81. '#description' => t('Should temporary files be deleted during the current back-up run? Default: !default.', array('!default' => (BACKUP_MIGRATE_CLEANUP_TEMP_FILES ? 'on' : 'off'))),
  82. '#required' => FALSE,
  83. );
  84. $form['backup_migrate_cleanup_time'] = array(
  85. '#type' => 'textfield',
  86. '#title' => t('Clean-up Time'),
  87. '#default_value' => variable_get('backup_migrate_cleanup_time', BACKUP_MIGRATE_CLEANUP_TIME),
  88. '#description' => t('Delete temporary files belonging to this modules that are older than the expire time. In seconds. 21600 seconds is 6 hours. Default: !default.', array('!default' => '<kbd>' . check_plain(BACKUP_MIGRATE_CLEANUP_TIME) . '</kbd>')),
  89. '#required' => FALSE,
  90. '#element_validate' => array('element_validate_integer'),
  91. );
  92. $form['backup_migrate_schedule_buffer'] = array(
  93. '#type' => 'textfield',
  94. '#title' => t('Schedule buffer'),
  95. '#default_value' => variable_get('backup_migrate_schedule_buffer', BACKUP_MIGRATE_SCHEDULE_BUFFER),
  96. '#description' => t('Add a small negative buffer (for example 1% of the entire period) to the time to account for slight difference in cron run length. Format: percentage of the period as a decimal fraction between 0 and 1. Default: !default.', array('!default' => '<kbd>' . check_plain(BACKUP_MIGRATE_SCHEDULE_BUFFER) . '</kbd>')),
  97. '#required' => FALSE,
  98. '#element_validate' => array('backup_migrate_fraction_validate'),
  99. );
  100. // Check for overrides and report them to the user.
  101. $keys = array_keys($form);
  102. $query = db_query('SELECT name, value FROM {variable} WHERE name IN (:keys)', array(':keys' => $keys));
  103. $result = $query->fetchAllKeyed();
  104. foreach ($form as $key => $field) {
  105. if (isset($result[$key])) {
  106. // Compare the value with what we have in the database.
  107. $db_value = unserialize($result[$key]);
  108. if ($field['#default_value'] != $db_value) {
  109. $form[$key]['#description'] .= ' <b>Overridden in settings.php: ' . check_plain($field['#default_value']) . '.</b>';
  110. }
  111. }
  112. }
  113. $form['actions'] = array('#type' => 'actions');
  114. $form['actions']['submit'] = array(
  115. '#type' => 'submit',
  116. '#value' => t('Save configuration'),
  117. );
  118. return $form;
  119. }
  120. /**
  121. * The advanced settings form.
  122. */
  123. function backup_migrate_ui_advanced_settings_form_submit($form, &$form_state) {
  124. $variables = array(
  125. 'backup_migrate_backup_max_time' => BACKUP_MIGRATE_BACKUP_MAX_TIME,
  126. 'backup_migrate_cleanup_temp_files' => BACKUP_MIGRATE_CLEANUP_TEMP_FILES,
  127. 'backup_migrate_cleanup_time' => BACKUP_MIGRATE_CLEANUP_TIME,
  128. 'backup_migrate_data_bytes_per_line' => BACKUP_MIGRATE_DATA_BYTES_PER_LINE,
  129. 'backup_migrate_data_rows_per_line' => BACKUP_MIGRATE_DATA_ROWS_PER_LINE,
  130. 'backup_migrate_data_rows_per_query' => BACKUP_MIGRATE_DATA_ROWS_PER_QUERY,
  131. 'backup_migrate_disable_cron' => BACKUP_MIGRATE_DISABLE_CRON,
  132. 'backup_migrate_max_email_size' => BACKUP_MIGRATE_MAX_EMAIL_SIZE,
  133. 'backup_migrate_memory_limit' => BACKUP_MIGRATE_MEMORY_LIMIT,
  134. 'backup_migrate_schedule_buffer' => BACKUP_MIGRATE_SCHEDULE_BUFFER,
  135. 'backup_migrate_verbose' => BACKUP_MIGRATE_VERBOSE,
  136. );
  137. foreach ($variables as $var_name => $var_default) {
  138. if (isset($form_state['values'][$var_name])) {
  139. // There's no need to save the variable if it's just the default.
  140. if ($form_state['values'][$var_name] == $var_default) {
  141. variable_del($var_name);
  142. }
  143. // The variable is not the default, so save it.
  144. else {
  145. variable_set($var_name, $form_state['values'][$var_name]);
  146. }
  147. }
  148. }
  149. }
  150. /**
  151. * Validate fields that require a PHP.ini-like memory_limit value.
  152. */
  153. function backup_migrate_memory_limit_validate($element, &$form_state, $form) {
  154. if (!_backup_migrate_is_valid_memory_limit($element['#value'])) {
  155. form_error($element, t('%name must be a valid memory limit setting.', array('%name' => $element['#title'])));
  156. }
  157. }
  158. /**
  159. * Validate fields that require zero or a positive integer.
  160. */
  161. function backup_migrate_unsigned_integer_validate($element, &$form_state, $form) {
  162. $value = $element['#value'];
  163. $test = preg_match('/^[0-9]+$/', $value);
  164. if ($test !== 1) {
  165. form_error($element, t('%name must be zero or a positive integer.', array('%name' => $element['#title'])));
  166. }
  167. }
  168. /**
  169. * Validate fields that require a decimal between 0 and 1.
  170. *
  171. * @todo Better name.
  172. */
  173. function backup_migrate_fraction_validate($element, &$form_state, $form) {
  174. $value = $element['#value'];
  175. $test = preg_match('/^(0|1|0?\.[0-9]+)$/', $value);
  176. if ($test !== 1) {
  177. form_error($element, t('%name must be a decimal between 0 and 1.', array('%name' => $element['#title'])));
  178. }
  179. }