migrate_example_baseball.migrate.inc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /**
  3. * @file
  4. * A baseball game migration example.
  5. */
  6. /*
  7. * You must implement hook_migrate_api(), setting the API level to 2, for
  8. * your migration classes to be recognized by the Migrate module.
  9. */
  10. function migrate_example_baseball_migrate_api() {
  11. $api = array(
  12. 'api' => 2,
  13. );
  14. return $api;
  15. }
  16. /**
  17. * A dynamic migration that is reused for each source CSV file.
  18. */
  19. class GameBaseball extends DynamicMigration {
  20. public function __construct(array $arguments) {
  21. $this->arguments = $arguments;
  22. parent::__construct();
  23. $this->description = t('Import box scores from CSV file.');
  24. // Create a map object for tracking the relationships between source rows
  25. $this->map = new MigrateSQLMap($this->machineName,
  26. array(
  27. 'start_date' => array('type' => 'varchar',
  28. 'length' => 8,
  29. 'not null' => TRUE,
  30. 'description' => 'Start date',
  31. ),
  32. 'home_team' => array('type' => 'varchar',
  33. 'length' => 255,
  34. 'not null' => TRUE,
  35. 'description' => 'Home team',
  36. ),
  37. 'home_game_number' => array('type' => 'int',
  38. 'not null' => TRUE,
  39. 'description' => 'Home team game number',
  40. ),
  41. ),
  42. MigrateDestinationNode::getKeySchema()
  43. );
  44. // Create a MigrateSource object, which manages retrieving the input data.
  45. $this->source = new MigrateSourceCSV($arguments['source_file'], $this->csvcolumns(), array(), $this->fields());
  46. $this->destination = new MigrateDestinationNode('migrate_example_baseball');
  47. $this->addFieldMapping('title', 'title')
  48. ->description('See prepareRow().');
  49. $this->addFieldMapping('field_start_date', 'start_date');
  50. $this->addFieldMapping('field_park', 'park_id');
  51. $this->addFieldMapping('field_visiting_team', 'visiting_team');
  52. $this->addFieldMapping('field_home_team', 'home_team');
  53. $this->addFieldMapping('field_home_game_number', 'home_game_number');
  54. $this->addFieldMapping('field_home_score', 'home_score');
  55. $this->addFieldMapping('field_visiting_score', 'visiting_score');
  56. $this->addFieldMapping('field_outs', 'outs');
  57. $this->addFieldMapping('field_attendance', 'attendance');
  58. $this->addFieldMapping('field_duration', 'duration')
  59. ->defaultValue(NULL);
  60. $this->addFieldMapping('field_home_pitcher', 'home_pitcher');
  61. $this->addFieldMapping('field_visiting_pitcher', 'visiting_pitcher');
  62. $this->addFieldMapping('field_home_batters', 'home_batters')
  63. ->separator(',')
  64. ->description('See prepareRow().');
  65. $this->addFieldMapping('field_visiting_batters', 'visiting_batters')
  66. ->separator(',')
  67. ->description('See prepareRow().');
  68. for ($i=1; $i <= 9; $i++ ) {
  69. $this->addFieldMapping(NULL, "visiting_batter_$i")
  70. ->description('Not needed since we use the multi-value field: visiting_batters.');
  71. $this->addFieldMapping(NULL, "home_batter_$i")
  72. ->description('Not needed since we use the multi-value field: home_batters.');
  73. }
  74. }
  75. function csvcolumns() {
  76. // Note: Remember to subtract 1 from column number at http://www.retrosheet.org/gamelogs/glfields.txt
  77. $columns[0] = array('start_date', 'Date of game');
  78. $columns[3] = array('visiting_team', 'Visiting team');
  79. $columns[6] = array('home_team', 'Home team');
  80. $columns[8] = array('home_game_number', 'Home team game number');
  81. $columns[9] = array('home_score', 'Home score');
  82. $columns[10] = array('visiting_score', 'Visiting score');
  83. $columns[11] = array('outs', 'Length of game in outs');
  84. $columns[16] = array('park_id', 'Ballpark ID');
  85. $columns[17] = array('attendance', 'Attendance');
  86. $columns[18] = array('duration', 'Duration in minutes');
  87. for ($i=1; $i <= 9; $i++ ) {
  88. $columns[103+3*$i] = array("visiting_batter_$i", "Visiting batter $i");
  89. $columns[130+3*$i] = array("home_batter_$i", "Home batter $i");
  90. }
  91. $columns[102] = array('visiting_pitcher', 'Visiting starting pitcher');
  92. $columns[104] = array('home_pitcher', 'Home starting pitcher');
  93. return $columns;
  94. }
  95. function prepareRow($row) {
  96. // Collect all the batters into one multi-value field.
  97. for ($i=1; $i <= 9; $i++ ) {
  98. $key = "visiting_batter_$i";
  99. $visiting_batters[] = $row->$key;
  100. $key = "home_batter_$i";
  101. $home_batters[] = $row->$key;
  102. }
  103. $row->visiting_batters = implode(',', $visiting_batters);
  104. $row->home_batters = implode(',', $home_batters);
  105. $row->title = "$row->home_team vs. $row->visiting_team. " . gmdate('M d, Y', strtotime($row->start_date));
  106. }
  107. /**
  108. * Construct the machine name from the source file name.
  109. */
  110. protected function generateMachineName($class_name = NULL) {
  111. return drupal_strtolower(pathinfo($this->arguments['source_file'],
  112. PATHINFO_FILENAME));
  113. }
  114. function fields() {
  115. return array(
  116. 'title' => 'A composite field made during prepareRow().',
  117. 'home_batters' => 'An array of batters, populated during prepareRow().',
  118. 'visiting_batters' => 'An array of batters, populated during prepareRow().',
  119. );
  120. }
  121. }