-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdomain.migrate.inc
333 lines (306 loc) · 10.7 KB
/
domain.migrate.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
<?php
/**
* @file
* Support for domains in core Drupal objects
*/
/**
* Migration class for importing domains into Drupal.
*
* In your migration class, you may map existing data directly to a Drupal
* domain record. The fields are as follows:
*
* - subdomain -- the domain URL (e.g. example.com), not including a protocol
* or a path.
* - machine_name (optional) -- an optional machine name for the domain record.
* - sitename -- the human-readable name of the domain.
* - scheme (optional)-- the URL scheme for the domain, either http or https.
* - valid (optional) -- binary indicator that the domain is active or not.
* - weight (optional) -- signed integer indicating the sort order of the domain.
* - is_default (optional) -- binary indicator that this is the default domain.
*/
class MigrateDestinationDomain extends MigrateDestination {
static public function getKeySchema() {
return array(
'machine_name' => array(
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
'description' => 'Primary Key for domain entries.',
),
);
}
public function __construct() {
parent::__construct();
}
public function __toString() {
$output = t('Domain');
return $output;
}
/**
* Returns a list of fields available to be mapped for domains.
*
* @param Migration $migration
* Optionally, the migration containing this destination.
* @return array
* Keys: machine names of the fields (to be passed to addFieldMapping)
* Values: Human-friendly descriptions of the fields.
*/
public function fields($migration = NULL) {
$fields = array(
'machine_name' => t('An optional machine name for the domain record. Will be generated is necessary.'),
'subdomain' => t('The domain URL (e.g. example.com), not including a protocol or a path. Required.'),
'sitename' => t('The human-readable name of the domain. Required.'),
'scheme' => t('The URL scheme for the domain, either http or https. Optional.'),
'valid' => t('Binary indicator that the domain is active or not. Optional.'),
'weight' => t('Signed integer indicating the sort order of the domain. Optional.'),
'is_default' => t('Binary indicator that this is the default domain. Optional.'),
);
return $fields;
}
/**
* Import a single row.
*
* @param $domain
* Domain object to build. Prefilled with any fields mapped in the Migration.
* @param $row
* Raw source data object - passed through to prepare/complete handlers.
* @return array
* Array of key fields of the object that was saved if
* successful. FALSE on failure.
*/
public function import(stdClass $domain, stdClass $row) {
// Set up optional fields.
if (empty($domain->machine_name)) {
$domain->machine_name = domain_machine_name($domain->subdomain);
}
if (!isset($domain->valid)) {
$domain->valid = 1;
}
if (empty($domain->scheme)) {
$domain->scheme = 'http';
}
if (!isset($domain->weight)) {
$domain->weight = count(domain_domains()) + 1;
}
if (!isset($domain->is_default)) {
$domain->is_default = 0;
}
// Invoke migration prepare handlers.
$this->prepare($domain, $row);
// Domains are handled as arrays, so clone the object to an array.
$domain = clone $domain;
$domain = (array) $domain;
// Check to see if this is a new domain.
$update = FALSE;
if ($data = domain_machine_name_load($domain['machine_name'])) {
$domain['domain_id'] = $data['domain_id'];
$update = TRUE;
}
// domain_save() provides no return callback, so we can't really test this
// without running a domain_load() check.
migrate_instrument_start('domain_save');
domain_save($domain);
migrate_instrument_stop('domain_save');
// Return the new id or FALSE on failure.
if ($data = domain_machine_name_load($domain['machine_name'])) {
// Increment the count if the save succeeded.
if ($update) {
$this->numUpdated++;
}
else {
$this->numCreated++;
}
// Return the primary key to the mapping table.
$return = array($data['machine_name']);
}
else {
$return = FALSE;
}
// Invoke migration complete handlers.
$domain = (object) $data;
$this->complete($domain, $row);
return $return;
}
/**
* Implementation of MigrateDestination::prepare().
*/
public function prepare($domain, stdClass $row) {
// We do nothing here but allow child classes to act.
$migration = Migration::currentMigration();
$domain->migrate = array(
'machineName' => $migration->getMachineName(),
);
// Call any general handlers.
migrate_handler_invoke_all('domain', 'prepare', $domain, $row);
// Then call any prepare handler for this specific Migration.
if (method_exists($migration, 'prepare')) {
$migration->prepare($domain, $row);
}
}
public function complete($domain, stdClass $row) {
// We do nothing here but allow child classes to act.
$migration = Migration::currentMigration();
$domain->migrate = array(
'machineName' => $migration->getMachineName(),
);
// Call any general handlers.
migrate_handler_invoke_all('domain', 'complete', $domain, $row);
// Then call any complete handler for this specific Migration.
if (method_exists($migration, 'complete')) {
$migration->complete($domain, $row);
}
}
/**
* Delete a single domain.
*
* @param $id
* Array of fields representing the key (in this case, just domain_name).
*/
public function rollback(array $id) {
$domain_name = reset($id);
migrate_instrument_start('domain_delete');
$this->prepareRollback($domain_name);
if ($domain = domain_machine_name_load($domain_name)) {
// We cannot delete the primary domain.
$default = domain_default_id();
if ($domain['domain_id'] != $default) {
domain_delete($domain);
}
else {
backdrop_set_message(t('The default domain may not be rolled back.'), 'status', FALSE);
}
}
$this->completeRollback($domain_name);
migrate_instrument_stop('domain_delete');
}
/**
* Give handlers a shot at cleaning up before a domain has been rolled back.
*
* @param $domain_name
* The machine_name ID of the domain about to be deleted.
*/
public function prepareRollback($domain_name) {
// We do nothing here but allow child classes to act.
$migration = Migration::currentMigration();
// Call any general handlers.
migrate_handler_invoke_all('domain', 'prepareRollback', $domain_name);
// Then call any complete handler for this specific Migration.
if (method_exists($migration, 'prepareRollback')) {
$migration->prepareRollback($domain_name);
}
}
/**
* Give handlers a shot at cleaning up after a domain has been rolled back.
*
* @param $domain_name
* ID of the domain which has been deleted.
*/
public function completeRollback($domain_name) {
// We do nothing here but allow child classes to act.
$migration = Migration::currentMigration();
// Call any general handlers.
migrate_handler_invoke_all('domain', 'completeRollback', $domain_name);
// Then call any complete handler for this specific Migration.
if (method_exists($migration, 'completeRollback')) {
$migration->completeRollback($domain_name);
}
}
}
/**
* Migration class for importing Drupal nodes.
*
* In your migration class, there are two field mappings you may set.
*
* - domain_site is a binary flag indicating that a node is assigned to
* "all affiliates" in your installation.
* - domains is a domain_id string or array of domain ids (either numeric or
* machine_name, and you may mix the two) that indicate the domain(s) the
* content should be published to.
*
* Note that failure to set these values explicitly will mean they are set
* automatically. The domain_site value is configurable per content type. The
* domains array will be populated with your default domain, if it exists.
*/
class DomainNodeHandler extends MigrateDestinationHandler {
public function __construct() {
$this->registerTypes(array('node'));
}
public function fields() {
$fields = array(
'domain_site' => t('Domain: all affiliates'),
'domains' => t('Domain: assigned domains'),
);
return $fields;
}
public function prepare($node, stdClass $row) {
$domains = array();
// If not set, be sure that we aren't updating an existing record.
if (!empty($node->nid) && empty($node->domains)) {
$nodes[$node->nid] = $node;
domain_node_load($nodes, array());
}
// If set, ensure domains exist.
if (isset($node->domains)) {
$node->domains = (array) $node->domains;
foreach ($node->domains as $id) {
if ($domain = domain_load($id)) {
$domains[$domain['domain_id']] = $domain['domain_id'];
}
if ($domain = domain_machine_name_load($id)) {
$domains[$domain['domain_id']] = $domain['domain_id'];
}
}
}
// If empty, assign to the default domain. Note that domain_site can be
// set automatically if not added by the migration.
if (empty($domains) && $default = domain_default_id()) {
$domains[$default] = $default;
}
$node->domains = $domains;
}
}
/**
* Migration class for supporting Drupal user accounts.
*
* In our migration class, you may add a domain_user field mapping. This field
* may contain a domain_id string or array of domain ids (either numeric or
* machine_name, and you may mix the two).
*
* If you fail to set domain_user, the user will be assigned to the default
* domain, if it exists.
*/
class DomainUserHandler extends MigrateDestinationHandler {
public function __construct() {
$this->registerTypes(array('user'));
}
public function fields() {
$fields = array(
'domain_user' => t('Domain: assigned domains'),
);
return $fields;
}
public function prepare($user, stdClass $row) {
// If set, ensure domains exist.
$domains = array();
if (isset($user->uid)) {
domain_user_set($user);
}
if (isset($user->domain_user)) {
$user->domain_user = (array) $user->domain_user;
foreach ($user->domain_user as $id) {
if ($domain = domain_load($id)) {
$domains[$domain['domain_id']] = $domain['domain_id'];
}
if ($domain = domain_machine_name_load($id)) {
$domains[$domain['domain_id']] = $domain['domain_id'];
}
}
}
// If empty, assign to the default domain.
if (empty($domains) && $default = domain_default_id()) {
$domains[$default] = $default;
}
$user->domain_user = $domains;
}
}