sources.inc 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <?php
  2. /**
  3. * @file
  4. * All of the source handling code needed for Backup and Migrate.
  5. */
  6. backup_migrate_include('crud', 'locations', 'destinations');
  7. /**
  8. * Get all the available backup sources.
  9. */
  10. function backup_migrate_get_sources() {
  11. return backup_migrate_crud_get_items('source');
  12. }
  13. /**
  14. * Get the available source types.
  15. */
  16. function backup_migrate_get_source_subtypes() {
  17. return backup_migrate_crud_subtypes('source');
  18. }
  19. /**
  20. * Get the destination of the given id.
  21. */
  22. function backup_migrate_get_source($id) {
  23. $sources = backup_migrate_get_sources();
  24. return empty($sources[$id]) ? NULL : $sources[$id];
  25. }
  26. /**
  27. * Create a source object of the given type with the given params.
  28. */
  29. function backup_migrate_create_source($subtype, $params = array()) {
  30. $params['subtype'] = $subtype;
  31. return backup_migrate_crud_create_item('source', $params);
  32. }
  33. /**
  34. * Implementation of hook_backup_migrate_source_subtypes().
  35. *
  36. * Get the built in Backup and Migrate source types.
  37. */
  38. function backup_migrate_backup_migrate_source_subtypes() {
  39. $out = array();
  40. $out += array(
  41. 'db' => array(
  42. 'type_name' => t('Database'),
  43. 'description' => t('Import the backup directly into another database. Database sources can also be used as a source to backup from.'),
  44. 'file' => drupal_get_path('module', 'backup_migrate') .'/includes/sources.db.inc',
  45. 'class' => 'backup_migrate_source_db',
  46. 'can_create' => FALSE,
  47. ),
  48. 'mysql' => array(
  49. 'type_name' => t('MySQL Database'),
  50. 'description' => t('Import the backup directly into another MySQL database. Database sources can also be used as a source to backup from.'),
  51. 'file' => drupal_get_path('module', 'backup_migrate') .'/includes/sources.db.mysql.inc',
  52. 'class' => 'backup_migrate_source_db_mysql',
  53. 'can_create' => TRUE,
  54. ),
  55. 'filesource' => array(
  56. 'description' => t('A files directory which can be backed up from.'),
  57. 'file' => drupal_get_path('module', 'backup_migrate') .'/includes/sources.filesource.inc',
  58. 'class' => 'backup_migrate_destination_filesource',
  59. 'type_name' => t('File Directory'),
  60. 'can_create' => TRUE,
  61. ),
  62. 'archive' => array(
  63. 'description' => t('Create an archive of your entire site.'),
  64. 'file' => drupal_get_path('module', 'backup_migrate') .'/includes/sources.archivesource.inc',
  65. 'class' => 'backup_migrate_files_destination_archivesource',
  66. 'type_name' => t('Site Archive'),
  67. 'can_create' => FALSE,
  68. ),
  69. );
  70. return $out;
  71. }
  72. /**
  73. * Implementation of hook_backup_migrate_sources().
  74. *
  75. * Get the built in backup sources and those in the db.
  76. */
  77. function backup_migrate_backup_migrate_sources() {
  78. $out = array();
  79. // Expose the configured databases as sources.
  80. backup_migrate_include('filters');
  81. $out += backup_migrate_filters_invoke_all('sources');
  82. return $out;
  83. }
  84. /**
  85. * Get the source options as a form element.
  86. */
  87. function _backup_migrate_get_source_form($source_id = 'db') {
  88. backup_migrate_include('destinations');
  89. $form = array();
  90. $sources = _backup_migrate_get_source_pulldown($source_id);
  91. if (count($sources['#options']) > 1) {
  92. $form['source'] = array(
  93. "#type" => "fieldset",
  94. "#title" => t("Backup Source"),
  95. "#collapsible" => TRUE,
  96. "#collapsed" => FALSE,
  97. "#tree" => FALSE,
  98. );
  99. $sources['#description'] = t("Choose the database to backup. Any database destinations you have created and any databases specified in your settings.php can be backed up.");
  100. $form['source']['source_id'] = $sources;
  101. }
  102. else {
  103. $form = array();
  104. $form['source']['source_id'] = array(
  105. "#type" => "value",
  106. "#value" => $source_id,
  107. );
  108. }
  109. return $form;
  110. }
  111. /**
  112. * Get pulldown to select existing source options.
  113. */
  114. function _backup_migrate_get_source_pulldown($source_id = NULL) {
  115. $sources = _backup_migrate_get_source_form_item_options();
  116. $form = array(
  117. "#type" => "select",
  118. "#title" => t("Backup Source"),
  119. "#options" => _backup_migrate_get_source_form_item_options(),
  120. "#default_value" => $source_id,
  121. );
  122. return $form;
  123. }
  124. /**
  125. * Get the location options as an options array for a form item.
  126. */
  127. function _backup_migrate_get_source_form_item_options() {
  128. $out = array();
  129. foreach (backup_migrate_get_sources() as $key => $location) {
  130. $out[$key] = $location->get_name();
  131. }
  132. return $out;
  133. }
  134. /**
  135. * A base class for creating sources.
  136. */
  137. class backup_migrate_source extends backup_migrate_location {
  138. var $db_table = "backup_migrate_sources";
  139. var $type_name = 'source';
  140. var $singular = 'source';
  141. var $plural = 'sources';
  142. var $title_plural = 'Sources';
  143. var $title_singular = 'Source';
  144. /**
  145. * This function is not supposed to be called. It is just here to help the po extractor out.
  146. */
  147. function strings() {
  148. // Help the pot extractor find these strings.
  149. t('source');
  150. t('sources');
  151. t('Sources');
  152. t('Source');
  153. }
  154. /**
  155. * Get the available location types.
  156. */
  157. function location_types() {
  158. return backup_migrate_get_source_subtypes();
  159. }
  160. }
  161. /**
  162. * A base class for creating sources.
  163. */
  164. class backup_migrate_source_remote extends backup_migrate_source {
  165. /**
  166. * The location is a URI so parse it and store the parts.
  167. */
  168. function get_location() {
  169. return $this->url(FALSE);
  170. }
  171. /**
  172. * The location to display is the url without the password.
  173. */
  174. function get_display_location() {
  175. return $this->url(TRUE);
  176. }
  177. /**
  178. * Return the location with the password.
  179. */
  180. function set_location($location) {
  181. $this->location = $location;
  182. $this->set_url($location);
  183. }
  184. /**
  185. * source configuration callback.
  186. */
  187. function edit_form() {
  188. $form = parent::edit_form();
  189. $form['scheme'] = array(
  190. "#type" => "textfield",
  191. "#title" => t("Scheme"),
  192. "#default_value" => @$this->dest_url['scheme'] ? $this->dest_url['scheme'] : '',
  193. "#required" => TRUE,
  194. "#weight" => 0,
  195. );
  196. $form['host'] = array(
  197. "#type" => "textfield",
  198. "#title" => t("Host"),
  199. "#default_value" => @$this->dest_url['host'] ? $this->dest_url['host'] : 'localhost',
  200. "#required" => TRUE,
  201. "#weight" => 10,
  202. );
  203. $form['path'] = array(
  204. "#type" => "textfield",
  205. "#title" => t("Path"),
  206. "#default_value" => @$this->dest_url['path'],
  207. "#required" => TRUE,
  208. "#weight" => 20,
  209. );
  210. $form['user'] = array(
  211. "#type" => "textfield",
  212. "#title" => t("Username"),
  213. "#default_value" => @$this->dest_url['user'],
  214. "#required" => TRUE,
  215. "#weight" => 30,
  216. );
  217. $form['pass'] = array(
  218. "#type" => "password",
  219. "#title" => t("Password"),
  220. "#default_value" => @$this->dest_url['pass'],
  221. '#description' => '',
  222. "#weight" => 40,
  223. );
  224. if (@$this->dest_url['pass']) {
  225. $form['old_password'] = array(
  226. "#type" => "value",
  227. "#value" => @$this->dest_url['pass'],
  228. );
  229. $form['pass']["#description"] .= t(' You do not need to enter a password unless you wish to change the currently saved password.');
  230. }
  231. return $form;
  232. }
  233. /**
  234. * Submit the configuration form. Glue the url together and add the old password back if a new one was not specified.
  235. */
  236. function edit_form_submit($form, &$form_state) {
  237. $form_state['values']['pass'] = $form_state['values']['pass'] ? $form_state['values']['pass'] : $form_state['values']['old_password'];
  238. $form_state['values']['location'] = $this->glue_url($form_state['values'], FALSE);
  239. parent::edit_form_submit($form, $form_state);
  240. }
  241. }