map.inc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. /**
  3. * @file
  4. * Defines the framework for map and message handling.
  5. */
  6. /**
  7. * We implement the Iterator interface to support iteration over the map table
  8. * for the purpose of rollback.
  9. */
  10. abstract class MigrateMap implements Iterator {
  11. /**
  12. * Codes reflecting the current status of a map row.
  13. */
  14. const STATUS_IMPORTED = 0;
  15. const STATUS_NEEDS_UPDATE = 1;
  16. const STATUS_IGNORED = 2;
  17. const STATUS_FAILED = 3;
  18. /**
  19. * Codes reflecting how to handle the destination item on rollback.
  20. *
  21. */
  22. const ROLLBACK_DELETE = 0;
  23. const ROLLBACK_PRESERVE = 1;
  24. /**
  25. * Arrays of key fields for the source and destination. Array keys are the
  26. * field names - array values are specific to the concrete map class.
  27. *
  28. * @var array
  29. */
  30. protected $sourceKey, $destinationKey;
  31. abstract public function getSourceKey();
  32. abstract public function getDestinationKey();
  33. /**
  34. * Mapping from field names to the map/message table key names (e.g.,
  35. * from input_field to sourceid1, or from nid to destid1)
  36. *
  37. * @var array
  38. */
  39. protected $sourceKeyMap, $destinationKeyMap;
  40. /**
  41. * Get the source key map.
  42. */
  43. public function getSourceKeyMap() {
  44. return $this->sourceKeyMap;
  45. }
  46. /**
  47. * Boolean determining whether to track last_imported times in map tables
  48. *
  49. * @var boolean
  50. */
  51. protected $trackLastImported = FALSE;
  52. public function getTrackLastImported() {
  53. return $this->trackLastImported;
  54. }
  55. public function setTrackLastImported($trackLastImported) {
  56. if (is_bool($trackLastImported)) {
  57. $this->trackLastImported = $trackLastImported;
  58. }
  59. }
  60. /**
  61. * Save a mapping from the key values in the source row to the destination
  62. * keys.
  63. *
  64. * @param $source_row
  65. * @param $dest_ids
  66. * @param $status
  67. * @param $rollback_action
  68. * @param $hash
  69. */
  70. abstract public function saveIDMapping(stdClass $source_row, array $dest_ids,
  71. $status = MigrateMap::STATUS_IMPORTED,
  72. $rollback_action = MigrateMap::ROLLBACK_DELETE, $hash = NULL);
  73. /**
  74. * Record a message related to a source record
  75. *
  76. * @param array $source_key
  77. * Source ID of the record in error
  78. * @param string $message
  79. * The message to record.
  80. * @param int $level
  81. * Optional message severity (defaults to MESSAGE_ERROR).
  82. */
  83. abstract public function saveMessage($source_key, $message, $level = MigrationBase::MESSAGE_ERROR);
  84. /**
  85. * Prepare to run a full update - mark all previously-imported content as
  86. * ready to be re-imported.
  87. */
  88. abstract public function prepareUpdate();
  89. /**
  90. * Report the number of processed items in the map
  91. */
  92. abstract public function processedCount();
  93. /**
  94. * Report the number of imported items in the map
  95. */
  96. abstract public function importedCount();
  97. /**
  98. * Report the number of items that failed to import
  99. */
  100. abstract public function errorCount();
  101. /**
  102. * Report the number of messages
  103. */
  104. abstract public function messageCount();
  105. /**
  106. * Delete the map and message entries for a given source record
  107. *
  108. * @param array $source_key
  109. */
  110. abstract public function delete(array $source_key, $messages_only = FALSE);
  111. /**
  112. * Delete the map and message entries for a given destination record
  113. *
  114. * @param array $destination_key
  115. */
  116. abstract public function deleteDestination(array $destination_key);
  117. /**
  118. * Delete the map and message entries for a set of given source records.
  119. *
  120. * @param array $source_keys
  121. */
  122. abstract public function deleteBulk(array $source_keys);
  123. /**
  124. * Clear all messages from the map.
  125. */
  126. abstract public function clearMessages();
  127. /**
  128. * Retrieve map data for a given source or destination item
  129. */
  130. abstract public function getRowBySource(array $source_id);
  131. abstract public function getRowByDestination(array $destination_id);
  132. /**
  133. * Retrieve an array of map rows marked as needing update.
  134. */
  135. abstract public function getRowsNeedingUpdate($count);
  136. /**
  137. * Given a (possibly multi-field) destination key, return the (possibly
  138. * multi-field) source key mapped to it.
  139. *
  140. * @param array $destination_id
  141. * Array of destination key values.
  142. *
  143. * @return array
  144. * Array of source key values, or NULL on failure.
  145. */
  146. abstract public function lookupSourceID(array $destination_id);
  147. /**
  148. * Given a (possibly multi-field) source key, return the (possibly
  149. * multi-field) destination key it is mapped to.
  150. *
  151. * @param array $source_id
  152. * Array of source key values.
  153. *
  154. * @return array
  155. * Array of destination key values, or NULL on failure.
  156. */
  157. abstract public function lookupDestinationID(array $source_id);
  158. /**
  159. * Remove any persistent storage used by this map (e.g., map and message
  160. * tables)
  161. */
  162. abstract public function destroy();
  163. }