group.inc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. /**
  3. * @file
  4. * Definition for a migration group.
  5. */
  6. class MigrateGroup {
  7. /**
  8. * The machine name of the group - used to identify it in drush commands.
  9. *
  10. * @var string
  11. */
  12. protected $name;
  13. public function getName() {
  14. return $this->name;
  15. }
  16. /**
  17. * The user-visible title of the group.
  18. *
  19. * @var string
  20. */
  21. protected $title;
  22. public function getTitle() {
  23. return $this->title;
  24. }
  25. /**
  26. * Domain-specific arguments for the group (to be applied to all migrations
  27. * in the group).
  28. *
  29. * @var array
  30. */
  31. protected $arguments = array();
  32. public function getArguments() {
  33. return $this->arguments;
  34. }
  35. /**
  36. * List of groups this group is dependent on.
  37. *
  38. * @var array
  39. */
  40. protected $dependencies = array();
  41. public function getDependencies() {
  42. return $this->dependencies;
  43. }
  44. /**
  45. * The central list of all known groups, keyed by group name.
  46. *
  47. * @var array
  48. */
  49. static protected $groupList = array();
  50. static public function groups() {
  51. $groups = array();
  52. $dependent_groups = array();
  53. $dependencies_list = array();
  54. $required_groups = array();
  55. foreach (self::$groupList as $name => $group) {
  56. $dependencies = $group->getDependencies();
  57. $dependencies_list[$name] = $dependencies;
  58. if (count($dependencies) > 0) {
  59. // Set groups with dependencies aside for reordering
  60. $dependent_groups[$name] = $group;
  61. $required_groups += $dependencies;
  62. }
  63. else {
  64. // No dependencies, just add
  65. $groups[$name] = $group;
  66. }
  67. }
  68. $ordered_groups = migrate_order_dependencies($dependencies_list);
  69. foreach ($ordered_groups as $name) {
  70. if (!isset($groups[$name])) {
  71. $groups[$name] = $dependent_groups[$name];
  72. }
  73. }
  74. return $groups;
  75. }
  76. /**
  77. * Basic constructor.
  78. *
  79. * @param string $name
  80. * Group name.
  81. *
  82. * @param array $dependencies
  83. * List of dependent groups.
  84. */
  85. public function __construct($name, $dependencies = array(), $title = '', $arguments = array()) {
  86. $this->name = $name;
  87. $this->title = $title;
  88. $this->arguments = $arguments;
  89. $this->dependencies = $dependencies;
  90. }
  91. /**
  92. * Retrieve (creating if necessary) an instance of the named group.
  93. *
  94. * @param string $name
  95. * Group name.
  96. *
  97. * @param array $dependencies
  98. * List of dependent groups.
  99. */
  100. static public function getInstance($name, $dependencies = array()) {
  101. if (empty(self::$groupList[$name])) {
  102. $row = db_select('migrate_group', 'mg')
  103. ->fields('mg')
  104. ->condition('name', $name)
  105. ->execute()
  106. ->fetchObject();
  107. if ($row) {
  108. $arguments = unserialize($row->arguments);
  109. $arguments = MigrationBase::decryptArguments($arguments);
  110. if (empty($dependencies) && isset($arguments['dependencies'])) {
  111. $dependencies = $arguments['dependencies'];
  112. }
  113. self::$groupList[$name] = new MigrateGroup($name, $dependencies,
  114. $row->title, $arguments);
  115. }
  116. else {
  117. self::register($name);
  118. self::$groupList[$name] = new MigrateGroup($name, $dependencies);
  119. }
  120. }
  121. return self::$groupList[$name];
  122. }
  123. /**
  124. * Register a new migration group in the migrate_group table.
  125. *
  126. * @param string $name
  127. * The machine name (unique identifier) for the group.
  128. *
  129. * @param string $title
  130. * A user-visible title for the group. Defaults to the machine name.
  131. *
  132. * @param array $arguments
  133. * An array of group arguments - generally data that applies to all
  134. * migrations in the group.
  135. */
  136. static public function register($name, $title = NULL, array $arguments = array()) {
  137. if (!$title) {
  138. $title = $name;
  139. }
  140. $arguments = MigrationBase::encryptArguments($arguments);
  141. // Register the migration if it's not already there; if it is,
  142. // update the class and arguments in case they've changed.
  143. db_merge('migrate_group')
  144. ->key(array('name' => $name))
  145. ->fields(array(
  146. 'title' => $title,
  147. 'arguments' => serialize($arguments),
  148. ))
  149. ->execute();
  150. }
  151. /**
  152. * Deregister a migration group - remove it from the database, and also
  153. * remove migrations attached to it.
  154. *
  155. * @param string $name
  156. * (Machine) name of the group to deregister.
  157. */
  158. static public function deregister($name) {
  159. $result = db_select('migrate_status', 'ms')
  160. ->fields('ms', array('machine_name'))
  161. ->condition('group_name', $name)
  162. ->execute();
  163. foreach ($result as $row) {
  164. Migration::deregisterMigration($row->machine_name);
  165. }
  166. db_delete('migrate_group')
  167. ->condition('name', $name)
  168. ->execute();
  169. }
  170. /**
  171. * Remove any groups which no longer contain any migrations.
  172. */
  173. static public function deleteOrphans() {
  174. $query = db_select('migrate_group', 'mg');
  175. $query->addField('mg', 'name');
  176. $query->leftJoin('migrate_status', 'ms', 'mg.name=ms.group_name');
  177. $query->isNull('ms.machine_name');
  178. $result = $query->execute();
  179. foreach ($result as $row) {
  180. db_delete('migrate_group')
  181. ->condition('name', $row->name)
  182. ->execute();
  183. }
  184. }
  185. }