variable.inc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. /**
  3. * @file
  4. * Support for variable destinations.
  5. */
  6. /**
  7. * Destination class implementing migration into {variable}.
  8. */
  9. class MigrateDestinationVariable extends MigrateDestination {
  10. static public function getKeySchema() {
  11. return array(
  12. 'name' => array(
  13. 'description' => 'The name of the variable.',
  14. 'type' => 'varchar',
  15. 'length' => 128,
  16. 'not null' => TRUE,
  17. 'default' => '',
  18. ),
  19. );
  20. }
  21. public function __construct() {
  22. parent::__construct();
  23. }
  24. public function __toString() {
  25. $output = t('Variable');
  26. return $output;
  27. }
  28. /**
  29. * Returns a list of fields available to be mapped for variables.
  30. *
  31. * @param Migration $migration
  32. * Optionally, the migration containing this destination.
  33. * @return array
  34. * Keys: machine names of the fields (to be passed to addFieldMapping)
  35. * Values: Human-friendly descriptions of the fields.
  36. */
  37. public function fields($migration = NULL) {
  38. $fields = array(
  39. 'name' => t('The name of the variable.'),
  40. 'value' => t('The value of the variable.'),
  41. );
  42. return $fields;
  43. }
  44. /**
  45. * Import a single row.
  46. *
  47. * @param $variable
  48. * Variable object to build. Prefilled with any fields mapped in the Migration.
  49. * @param $row
  50. * Raw source data object - passed through to prepare/complete handlers.
  51. * @return array
  52. * Array of key fields of the object that was saved if
  53. * successful. FALSE on failure.
  54. */
  55. public function import(stdClass $variable, stdClass $row) {
  56. // Invoke migration prepare handlers
  57. $this->prepare($variable, $row);
  58. // Check to see if this is a new variable.
  59. $update = FALSE;
  60. // We cannot just check against NULL because a variable might actually be
  61. // set to NULL. Attempt to use a unique variable default value that nothing
  62. // else would use.
  63. $default = 'migrate:' . REQUEST_TIME . ':' . drupal_random_key();
  64. if (variable_get($variable->name, $default) !== $default) {
  65. $update = TRUE;
  66. }
  67. // variable_set() provides no return callback, so we can't really test this
  68. // without running a variable_get() check.
  69. migrate_instrument_start('variable_set');
  70. variable_set($variable->name, $variable->value);
  71. migrate_instrument_stop('variable_set');
  72. // Return the new id or FALSE on failure.
  73. if (variable_get($variable->name, $default) === $variable->value) {
  74. // Increment the count if the save succeeded.
  75. if ($update) {
  76. $this->numUpdated++;
  77. }
  78. else {
  79. $this->numCreated++;
  80. }
  81. // Return the primary key to the mapping table.
  82. $return = array($variable->name);
  83. }
  84. else {
  85. $return = FALSE;
  86. }
  87. // Invoke migration complete handlers.
  88. $this->complete($variable, $row);
  89. return $return;
  90. }
  91. /**
  92. * Implementation of MigrateDestination::prepare().
  93. */
  94. public function prepare($variable, stdClass $row) {
  95. // We do nothing here but allow child classes to act.
  96. $migration = Migration::currentMigration();
  97. $variable->migrate = array(
  98. 'machineName' => $migration->getMachineName(),
  99. );
  100. // Call any general handlers.
  101. migrate_handler_invoke_all('variable', 'prepare', $variable, $row);
  102. // Then call any prepare handler for this specific Migration.
  103. if (method_exists($migration, 'prepare')) {
  104. $migration->prepare($variable, $row);
  105. }
  106. }
  107. /**
  108. * Implementation of MigrateDestination::complete().
  109. */
  110. public function complete($variable, stdClass $row) {
  111. // We do nothing here but allow child classes to act.
  112. $migration = Migration::currentMigration();
  113. $variable->migrate = array(
  114. 'machineName' => $migration->getMachineName(),
  115. );
  116. // Call any general handlers.
  117. migrate_handler_invoke_all('variable', 'complete', $variable, $row);
  118. // Then call any complete handler for this specific Migration.
  119. if (method_exists($migration, 'complete')) {
  120. $migration->complete($variable, $row);
  121. }
  122. }
  123. /**
  124. * Delete a single variable.
  125. *
  126. * @param $id
  127. * Array of fields representing the key (in this case, just variable name).
  128. */
  129. public function rollback(array $id) {
  130. $name = reset($id);
  131. migrate_instrument_start('variable_delete');
  132. $this->prepareRollback($name);
  133. variable_del($name);
  134. $this->completeRollback($name);
  135. migrate_instrument_stop('variable_delete');
  136. }
  137. /**
  138. * Give handlers a shot at cleaning up before a variable has been rolled back.
  139. *
  140. * @param $name
  141. * The name of the variable about to be deleted.
  142. */
  143. public function prepareRollback($name) {
  144. // We do nothing here but allow child classes to act.
  145. $migration = Migration::currentMigration();
  146. // Call any general handlers.
  147. migrate_handler_invoke_all('variable', 'prepareRollback', $name);
  148. // Then call any complete handler for this specific Migration.
  149. if (method_exists($migration, 'prepareRollback')) {
  150. $migration->prepareRollback($name);
  151. }
  152. }
  153. /**
  154. * Give handlers a shot at cleaning up after a variable has been rolled back.
  155. *
  156. * @param $name
  157. * The name of the variable which has been deleted.
  158. */
  159. public function completeRollback($name) {
  160. // We do nothing here but allow child classes to act.
  161. $migration = Migration::currentMigration();
  162. // Call any general handlers.
  163. migrate_handler_invoke_all('variable', 'completeRollback', $name);
  164. // Then call any complete handler for this specific Migration.
  165. if (method_exists($migration, 'completeRollback')) {
  166. $migration->completeRollback($name);
  167. }
  168. }
  169. }