Projet

Général

Profil

Paste
Télécharger (5,82 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / votingapi / votingapi.migrate.inc @ f419e8de

1 f419e8de Assos Assos
<?php
2
3
/**
4
 * @file
5
 * Migration support for voting api.
6
 */
7
class MigrateDestinationVotingApiVote extends MigrateDestination {
8
9
  protected $importedIds = array();
10
11
  /**
12
   * Threshold of count of id's after which votes will be recalculated.
13
   *
14
   * @var int
15
   */
16
  protected $idRecalculationThreshold = 1000;
17
18
  static public function getKeySchema() {
19
    return array(
20
      'vote_id' => array(
21
        'type' => 'int',
22
        'unsigned' => TRUE,
23
        'not null' => TRUE,
24
        'description' => 'Vote ID',
25
      ),
26
    );
27
  }
28
29
  /**
30
   * {@inheritdoc}
31
   */
32
  public function __toString() {
33
    return t('Voting API Vote');
34
  }
35
36
  /**
37
   * {@inheritdoc}
38
   */
39
  public function fields() {
40
    return array(
41
      'vote_id' => 'Vote ID',
42
      'entity_type' => "Entity Type (defaults to 'node')",
43
      'entity_id' => 'Entity ID',
44
      'value' => 'Numeric vote value',
45
      'value_type' => "Value type (percent/points, defaults to 'percent')",
46
      'tag' => "Tag (defaults to 'vote')",
47
      'uid' => 'User ID',
48
      'timestamp' => 'Timestamp',
49
      'vote_source' => 'Vote Source IP Address',
50
    );
51
  }
52
53
  /**
54
   * {@inheritdoc}
55
   */
56
  public function import(stdClass $votingapi_vote, stdClass $row) {
57
    $updating = FALSE;
58
    $vote = array();
59
    if (isset($row->migrate_map_destid1)) {
60
      // We're updating an existing vote - start from the previous data.
61
      $votes = votingapi_select_votes(array('vote_id' => $row->migrate_map_destid1));
62
      if (count($votes)) {
63
        $vote = reset($votes);
64
        $updating = TRUE;
65
      }
66
    }
67
68
    if (!$updating) {
69
      unset($votingapi_vote->vote_id);
70
    }
71
72
    $this->prepare($votingapi_vote, $row);
73
74
    // Votes have to be assigned to an entity.
75
    if (empty($votingapi_vote->entity_id)) {
76
      watchdog('VotingAPI Import', 'Skipped import due to empty entity_id for vote import: @serial', array('@serial' => json_encode($row)), WATCHDOG_ERROR);
77
      return FALSE;
78
    }
79
80
    migrate_instrument_start('votingapi_vote_save');
81
    // Just converting $entity to an array doesn't work...
82
    $vote = array();
83
    foreach ((array) $votingapi_vote as $field => $value) {
84
      $vote[$field] = $value;
85
    }
86
    votingapi_add_votes($vote);
87
    migrate_instrument_stop('votingapi_vote_save');
88
89
    if (isset($vote[0]['vote_id'])) {
90
      $votingapi_vote->vote_id = $vote[0]['vote_id'];
91
      $this->complete($votingapi_vote, $row);
92
93
      // Store the ID in the hash as it is faster than getting uniques later.
94
      $this->importedIds[$votingapi_vote->entity_type][$votingapi_vote->entity_id] = TRUE;
95
96
      // If we hit the threshold of entity id's we imported, we should
97
      // recalculate to avoid excessive memory usage (and clear out the array).
98
      if (count($this->importedIds[$votingapi_vote->entity_type]) >= $this->idRecalculationThreshold) {
99
        $this->recalculateResults($votingapi_vote->entity_type, array_keys($this->importedIds[$votingapi_vote->entity_type]));
100
        $this->importedIds[$votingapi_vote->entity_type] = array();
101
      }
102
103
      if ($updating) {
104
        $this->numUpdated++;
105
      }
106
      else {
107
        $this->numCreated++;
108
      }
109
      return array($votingapi_vote->vote_id);
110
    }
111
    else {
112
      return FALSE;
113
    }
114
  }
115
116
  /**
117
   * Recalculate votes for a list of id's.
118
   *
119
   * @param string $entity_type
120
   *   The entity type of the id's
121
   * @param array $ids
122
   *   List of the ID's for which votes are to be recalculated.
123
   */
124
  protected function recalculateResults($entity_type, array $ids) {
125
    foreach ($ids as $entity_id) {
126
      votingapi_recalculate_results($entity_type, $entity_id, TRUE);
127
    }
128
  }
129
130
  /**
131
   * We're done with importing votes, recalculate the results.
132
   */
133
  public function postImport() {
134
    foreach ($this->importedIds as $entity_type => $entity_ids) {
135
      $this->recalculateResults($entity_type, array_keys($entity_ids));
136
    }
137
  }
138
139
  /**
140
   * Delete the provided votes and recalculate the results.
141
   *
142
   * @param array $ids
143
   *   ID's to be deleted.
144
   */
145
  public function bulkRollback(array $ids) {
146
    migrate_instrument_start(__METHOD__);
147
148
    foreach ($ids as $id) {
149
      $votes[]['vote_id'] = $id;
150
    }
151
    votingapi_delete_votes($votes);
152
153
    migrate_instrument_stop(__METHOD__);
154
  }
155
156
  public function prepare($vote, stdClass $source_row) {
157
    $migration = Migration::currentMigration();
158
    if (method_exists($migration, 'prepare')) {
159
      $vote->migrate = array(
160
        'machineName' => $migration->getMachineName(),
161
      );
162
      $migration->prepare($vote, $source_row);
163
    }
164
  }
165
166
  public function complete($entity, stdClass $source_row) {
167
    // Call any complete handler for this specific Migration.
168
    $migration = Migration::currentMigration();
169
    if (method_exists($migration, 'complete')) {
170
      try {
171
        $migration->complete($entity, $source_row);
172
      } catch (Exception $e) {
173
        // If we catch any errors here, save the messages without letting
174
        // the exception prevent the saving of the question.
175
        $migration->saveMessage($e->getMessage());
176
      }
177
    }
178
  }
179
180
  /**
181
   * Give handlers a shot at cleaning up before a question has been rolled back.
182
   *
183
   * @param $entity_id
184
   *  ID of the entity about to be deleted..
185
   */
186
  public function prepareRollback($entity_id) {
187
    // Call any prepare handler for this specific Migration.
188
    $migration = Migration::currentMigration();
189
    if (method_exists($migration, 'prepareRollback')) {
190
      $migration->prepareRollback($entity_id);
191
    }
192
  }
193
194
  /**
195
   * Give handlers a shot at cleaning up after an entity has been rolled back.
196
   *
197
   * @param $entity_id
198
   *  ID of the entity which has been deleted.
199
   */
200
  public function completeRollback($entity_id) {
201
    // Call any complete handler for this specific Migration.
202
    $migration = Migration::currentMigration();
203
    if (method_exists($migration, 'completeRollback')) {
204
      $migration->completeRollback($entity_id);
205
    }
206
  }
207
208
}