hosting_db_server.service.inc

Provide the hosting serivce classes for database integration.

Classes

Namesort descending Description
hostingService_db The base db service type class.
hostingService_db_mysql A MySQL specific db service implementation class.

File

db_server/hosting_db_server.service.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Provide the hosting serivce classes for database integration.
  5. */
  6. /**
  7. * The base db service type class.
  8. */
  9. class hostingService_db extends hostingService {
  10. public $service = 'db';
  11. }
  12. /**
  13. * A MySQL specific db service implementation class.
  14. */
  15. class hostingService_db_mysql extends hostingService_db {
  16. public $type = 'mysql';
  17. public $has_port = TRUE;
  18. function default_port() {
  19. return 3306;
  20. }
  21. function form(&$form) {
  22. parent::form($form);
  23. $form['db_user'] = array(
  24. '#type' => 'textfield',
  25. '#required' => !empty($this->available),
  26. '#title' => t('Username'),
  27. '#description' => t('The user that will be used to create new mysql users and databases for new sites.'),
  28. '#size' => 40,
  29. '#default_value' => isset($this->db_user) ? $this->db_user : NULL,
  30. '#maxlength' => 64,
  31. '#weight' => 5,
  32. );
  33. $passwd_description = '';
  34. if (isset($this->db_passwd)) {
  35. $passwd_description = t('<strong>You have already set a password for this database server.</strong><br />');
  36. }
  37. $form['db_passwd'] = array(
  38. '#type' => 'password_confirm',
  39. '#required' => FALSE,
  40. '#description' => $passwd_description . t('The password for the user that will be used to create new mysql users and databases for new sites'),
  41. '#size' => 30,
  42. '#weight' => 10,
  43. );
  44. }
  45. function insert() {
  46. parent::insert();
  47. $id = db_insert('hosting_db_server')
  48. ->fields(array(
  49. 'vid' => $this->server->vid,
  50. 'nid' => $this->server->nid,
  51. 'db_user' => $this->db_user,
  52. 'db_passwd' => $this->db_passwd,
  53. ))
  54. ->execute();
  55. }
  56. function update() {
  57. if (!empty($this->db_passwd)) {
  58. parent::update();
  59. }
  60. else {
  61. // only do the parent's update routine.
  62. parent::delete_revision();
  63. parent::insert();
  64. }
  65. }
  66. function delete_revision() {
  67. parent::delete_revision();
  68. db_delete('hosting_db_server')
  69. ->condition('vid', $this->server->vid)
  70. ->execute();
  71. }
  72. function delete() {
  73. parent::delete();
  74. db_delete('hosting_db_server')
  75. ->condition('nid', $this->server->nid)
  76. ->execute();
  77. }
  78. function load() {
  79. parent::load();
  80. $this->mergeData('SELECT db_user, db_passwd FROM {hosting_db_server} WHERE vid = :vid', array(':vid' => $this->server->vid));
  81. }
  82. function view(&$render) {
  83. parent::view($render);
  84. $render['db_user'] = array(
  85. '#type' => 'item',
  86. '#title' => t('Database user'),
  87. '#markup' => filter_xss($this->db_user),
  88. );
  89. }
  90. public function context_options($task_type, $ref_type, &$task) {
  91. parent::context_options($task_type, $ref_type, $task);
  92. // Provide context_options for verification and writing out to an alias
  93. $task->context_options['master_db'] = 'mysql' . '://' . urlencode($this->db_user) . ':' . urlencode($this->db_passwd) . '@' . $this->server->title;
  94. }
  95. public function context_import($context) {
  96. parent::context_import($context);
  97. $matches = array();
  98. preg_match("+^mysql://(.*):(.*)@.*$+", stripslashes($context->master_db), $matches);
  99. $this->db_user = urldecode($matches[1]);
  100. $this->db_passwd = urldecode($matches[2]);
  101. }
  102. }