sources.inc 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. * Implements 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. * Implements 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. public $db_table = "backup_migrate_sources";
  139. public $type_name = 'source';
  140. public $singular = 'source';
  141. public $plural = 'sources';
  142. public $title_plural = 'Sources';
  143. public $title_singular = 'Source';
  144. /**
  145. * This function is not supposed to be called.
  146. *
  147. * It is just here to help out the po extractor.
  148. */
  149. public function strings() {
  150. // Help the pot extractor find these strings.
  151. t('source');
  152. t('sources');
  153. t('Sources');
  154. t('Source');
  155. }
  156. /**
  157. * Get the available location types.
  158. */
  159. public function location_types() {
  160. return backup_migrate_get_source_subtypes();
  161. }
  162. }
  163. /**
  164. * A base class for creating sources.
  165. */
  166. class backup_migrate_source_remote extends backup_migrate_source {
  167. /**
  168. * The location is a URI so parse it and store the parts.
  169. */
  170. public function get_location() {
  171. return $this->url(FALSE);
  172. }
  173. /**
  174. * The location to display is the url without the password.
  175. */
  176. public function get_display_location() {
  177. return $this->url(TRUE);
  178. }
  179. /**
  180. * Returns the location with the password.
  181. */
  182. public function set_location($location) {
  183. $this->location = $location;
  184. $this->set_url($location);
  185. }
  186. /**
  187. * Source configuration callback.
  188. */
  189. public function edit_form() {
  190. $form = parent::edit_form();
  191. $form['scheme'] = array(
  192. "#type" => "textfield",
  193. "#title" => t("Scheme"),
  194. "#default_value" => @$this->dest_url['scheme'] ? $this->dest_url['scheme'] : '',
  195. "#required" => TRUE,
  196. "#weight" => 0,
  197. );
  198. $form['host'] = array(
  199. "#type" => "textfield",
  200. "#title" => t("Host"),
  201. "#default_value" => @$this->dest_url['host'] ? $this->dest_url['host'] : 'localhost',
  202. "#required" => TRUE,
  203. "#weight" => 10,
  204. );
  205. $form['path'] = array(
  206. "#type" => "textfield",
  207. "#title" => t("Path"),
  208. "#default_value" => @$this->dest_url['path'],
  209. "#required" => TRUE,
  210. "#weight" => 20,
  211. );
  212. $form['user'] = array(
  213. "#type" => "textfield",
  214. "#title" => t("Username"),
  215. "#default_value" => @$this->dest_url['user'],
  216. "#required" => TRUE,
  217. "#weight" => 30,
  218. );
  219. $form['pass'] = array(
  220. "#type" => "password",
  221. "#title" => t("Password"),
  222. "#default_value" => @$this->dest_url['pass'],
  223. '#description' => '',
  224. "#weight" => 40,
  225. );
  226. if (@$this->dest_url['pass']) {
  227. $form['old_password'] = array(
  228. "#type" => "value",
  229. "#value" => @$this->dest_url['pass'],
  230. );
  231. $form['pass']["#description"] .= t('You do not need to enter a password unless you wish to change the currently saved password.');
  232. }
  233. return $form;
  234. }
  235. /**
  236. * Submits the configuration form.
  237. *
  238. * Glue the url together and add the old password back if a new one was not
  239. * specified.
  240. */
  241. public function edit_form_submit($form, &$form_state) {
  242. $form_state['values']['pass'] = $form_state['values']['pass'] ? $form_state['values']['pass'] : $form_state['values']['old_password'];
  243. $form_state['values']['location'] = $this->glue_url($form_state['values'], FALSE);
  244. parent::edit_form_submit($form, $form_state);
  245. }
  246. }